我正在编写先到先服务调度算法。我相信我有正确的算法,并按正确的顺序和时间处理作业列表。我遇到的唯一问题是它是如何输出到我的输出文件的。除了列表中的第一个作业外,其他所有作业名称都正在输出。
以下是我的标题文件: fcfs.h
#ifndef FUNCTION_H
#define FUNCTION_H
//---------------------------------------------------------------------------
// STRUCTURE THAT HOLDS CPU INFORMATION |
//--------------------------------------------------------------------------
// Struct that simulates the cpu running, cpu1 being the cpu currently running
// tells what the clock pulse is, its current job, and whether or not it is
// occupied
struct cpu
{
int clock_pulse;
struct processes* job;
bool occupied;
}cpu1 = {0, NULL, false};
//---------------------------------------------------------------------------
// FUNCTIONS FOR CPU |
//--------------------------------------------------------------------------
void increment_clock_pulse(); // increments clock_pulse by 1
bool is_cpu_occupied(); // returns true if cpu is occupied, false otherwise
void check_arrivals(); // changes cpu1 state and waiting queue
//---------------------------------------------------------------------------
// STRUCTURE THAT HOLDS INFORMATION FOR PROCESS QUEUE |
//--------------------------------------------------------------------------
// Structure that holds individual nodes for each process in the waiting
// queue. Holds process name, arrival time, service time, priority level
struct processes
{
char name[10];
int arrival_time;
int service_time;
int priority_level;
struct processes *next;
};
struct processes *head = NULL; // instance of processes that points to beginning
struct processes *rear = NULL; // instance of processes that points to end
//---------------------------------------------------------------------------
// BASIC FUNCTIONS FOR LINKED LIST QUEUE |
//--------------------------------------------------------------------------
void enqueue(char *n, int a, int s, int p); // places process at the back of the queue
void dequeue(); // removes the front of the queue
void print_list(); // prints out the list of processes
bool is_empty(); // returns true if empty, false if not
//---------------------------------------------------------------------------
// FUNCTION FOR READING FILE INTO QUEUE |
//--------------------------------------------------------------------------
void fill_array(char *file); // fills in the queue from file input
void output(); // outputs the jobs into output.txt, creates it if it doesn't exist
#endif
以下是我的实施文件: fcfs.c
#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "stdlib.h"
#include "fcfs.h"
//---------------------------------------------------------------------------
// FUNCTIONS FOR CPU |
//--------------------------------------------------------------------------
// increments the clock pulse by one
void increment_clock_pulse()
{
cpu1.clock_pulse++;
}
// returns true if cpu is occupied, false if not
bool is_cpu_occupied()
{
return cpu1.occupied;
}
// checks the queue if there are jobs ready to be serviced
void check_arrivals()
{
// if job is ready to be serviced and if there is not already a job in the CPU
if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}
}
//---------------------------------------------------------------------------
// BASIC FUNCTIONS FOR LINKED LIST QUEUE |
//--------------------------------------------------------------------------
// Funtcion the takes in a string and 3 integers for its input
// and inserts the data into the queue
void enqueue(char *n, int a, int s, int p)
{
struct processes *temp = (struct processes*)malloc(sizeof(struct processes));
strcpy(temp->name, n);
temp->arrival_time = a;
temp->service_time = s;
temp->priority_level = p;
temp->next = NULL;
if(head == NULL && rear == NULL){
head = rear = temp;
return;
}
rear->next = temp;
rear = temp;
}
// Function that dequeues the first item in the queue and then moves the
// queue forward
void dequeue()
{
struct processes* temp = head;
if(head == NULL) {
printf("Queue is Empty\n");
return;
}
if(head == rear) {
head = rear = NULL;
}
else {
head = head->next;
}
free(temp);
}
// Function that prints out the current queue
void print_list()
{
struct processes *ptr = head;
printf("\n[ ");
while(ptr != NULL){
printf("(%s %d %d %d) ",ptr->name, ptr->arrival_time,
ptr->service_time, ptr->priority_level);
ptr = ptr->next;
}
printf(" ]");
}
// Returns true if the queue is empty, false if it is not
bool is_empty()
{
return head == NULL;
}
//---------------------------------------------------------------------------
// FUNCTION FOR READING FILE INTO QUEUE |
//--------------------------------------------------------------------------
// Function that fills in the queue, takes in an argument that is the
// name of the file that contains the processes
void fill_array(char *file)
{
FILE *fp; // File pointer
fp = fopen(file, "r"); // opens the file to read processes
// checks to see whether or not fopen() is successful
if (fp == NULL)
{
printf("Error while opening file");
exit(1);
}
// reads in data until End of File
int a, s, p;
char n[10];
while(feof(fp)==0)
{
fscanf(fp, "%s %d %d %d", n, &a, &s, &p);
enqueue(n, a, s, p);
}
fclose(fp);
}
void output()
{
FILE *fp;
fp = fopen("output.txt", "a");
fprintf(fp, "%s %d %d \n", cpu1.job->name, (cpu1.clock_pulse - cpu1.job->arrival_time), cpu1.clock_pulse);
fclose(fp);
}
我的驱动程序文件: main.c
#include "stdbool.h"
#include "string.h"
#include "stdio.h"
#include "fcfs.c"
int main()
{
char file[20]; // buffer for file name
printf("Please enter your file name: "); // prompts user for file name
scanf("%s", file);
fill_array(file); // fills array from file
// Beginning of the FCFS Algorithm
while(!is_empty())
{
if(cpu1.occupied) // If CPU is busy
{
if(cpu1.job->service_time == 0) // If current job in CPU is done CPU changes to not busy
{
output(); // outputs job to file when job is finished
cpu1.occupied = false;
}
}
check_arrivals(); // checks for arrivals in the waiting queue
if(cpu1.occupied) // If the CPU is occupied job is not done
{
cpu1.job->service_time--; // decrement service time of current job
}
increment_clock_pulse(); // increment the clock pulse
}
return 0;
}
这是我的输入文件: processes.txt
A0 6 15 4
A1 9 40 6
A2 9 12 9
A3 12 15 4
A4 30 11 2
A5 45 70 1
A6 70 23 9
A7 75 23 5
A8 75 18 7
A9 90 5 6
输出: output.txt
¢ 15 21
A1 52 61
A2 64 73
A3 76 88
A4 69 99
A5 124 169
A6 122 192
A7 140 215
A8 158 233
A9 148 238
每次运行它时,我会为写入的第一个进程获取不同的字符串,在本例中为A0。
我的想法是,我将它们从队列中传递给cpu错误,并且它给了我乱七八糟的垃圾。
我到处搜索过,无法在任何地方找到答案。请帮忙!
答案 0 :(得分:1)
这里有一个很大的问题,鉴于症状很可能是相关的:
// checks the queue if there are jobs ready to be serviced
void check_arrivals()
{
// if job is ready to be serviced and if there is not already a job in the CPU
if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}
}
存储head
的值,然后调用删除dequeue()
内存的head
。
cpu1.job
现在指向未分配的内存=&gt;未定义的行为
尽管如此,程序“几乎”运行良好,所以这只是内存所有权问题。
我建议更改您的cpu
数据结构,以便能够保存作业的内存,如下所示(job
上没有指针):
struct cpu
{
int clock_pulse;
bool occupied;
struct processes job;
}cpu1 = {0, false, {"",0,0,0,NULL} };
然后更改此代码(以及job->
现在job.
的所有代码)
if(head->arrival_time <= cpu1.clock_pulse && !cpu1.occupied)
{
cpu1.occupied = true; // changes the CPU to occupied
cpu1.job = *head; // gives the CPU the next job
dequeue(); // dispatches the previous job from the queue
}
所以head
在被解除分配之前被复制。在这种情况下,你可以保证内存安全。