나는 First Come First Served Scheduling Algorithm을 쓰고있다. 나는 올바른 알고리즘을 가지고 있고 프로세스 목록에 올바른 순서와 시간으로 작업을 처리하고 있다고 생각한다. 내가 가진 문제는 출력 파일에 출력되는 방식뿐입니다. 다른 모든 작업 이름은 목록의 첫 번째 작업을 제외하고 바로 출력됩니다. 여기 C로 파일에 출력 할 때 임의의 문자 스트링
내 헤더 파일입니다 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;
}
나는에 대한 문자의 다른 문자열을 얻을
내가 그것을 실행할 때마다 ¢ 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
경우 output.txt :
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
그리고 출력 :
이 내 입력 파일입니다 첫 번째 프로세스는이 경우 A0으로 작성됩니다.
내 생각에 내가 큐에서 CPU로 잘못 전달하고있어 쓰레기가 엉망이되었다.
전 검색을했으며 어디에서나 답을 찾을 수 없습니다. 도와주세요!
시도해 보았습니다. 두 가지 일이 일어났습니다. A0는 파일에 쓰여지지 않았고, 그 다음에는 프로그램이 끝나기 전에 추락했습니다. 문제는 그것을 우선적으로 큐에서 빼는 것입니다. 일단 큐를 빼내려고하는 마지막 작업에 도달하면 어떻게됩니까? 빠른 응답을 주셔서 감사합니다, 내가 그것을 고칠 수있는 방법에 대한 아이디어를 줬어. 내가하면 다시보고 할게. –
다른 접근법을 제안합니다. 당신 일이 거의 효과가 있었기 때문에 일해야합니다. –