我目前正在使用 FCFS 和循环算法模拟进程调度程序。
首先,我想让输入的解析尽可能简单......
我有一些结构来保存特定信息。该程序的工作原理如下:
my_project FCFS in.file
OR
my_project RR 2 in.file
in.file 如下所示:
./Job1.txt
./Job2.txt
./Job3.txt
./Job4.txt
所以我想处理这个输入文件并订购作业。
文本文件如下所示。
10
1fi
if i < 3 i=i+1 goto 8
3sdkfj
4ksdkk
5kdkfk
6kdkjf
7dkjkfd
if k < 2 k=k+1 goto 2
9dkkf
10dku
if j < 2 j=j+1 goto 2
除了第一行(表示该作业的开始时间)和以 if 开头的行之外,所有行均无意义。即 if i < 3 i = i+1 goto 4 表示只要 i 小于 3 就跳转到第 4 行。
所以基本上现在我想通过上面的命令行解析输入文件,并按开始时间(第一行)对作业进行排序。我真的很想尽可能高效地完成这一步。到目前为止,我已经编写了以下代码:
/* I/O Files */
static char *inputFile;
static FILE *input;
/*Scheduled jobs indexed by PID*/
struct job list[20];
/* the next job to schedule */
static struct job *job_next = NULL;
/* Time */
time clock;
/*Initialises job list* /
static void initialise_list(void) {
for(int i = 0; i < sizeof(job_list); i++) {
job_list[i].params.pid = -1;
}
}
/** 从输入文件中读取并解析输入 */ 静态无效 parse_input(void) {
char buffer[BUFSIZ];
unsigned int jobs;
struct job *current;
jobs = 0;
initialise_list();
/** Read input file **/
while( fgets(buffer, sizeof(buffer), input)) {
time start, finish;
pid job;
//if(buffer[0] == '#') {
// continue;
//}
sscanf(buffer, "Job%d%ld", &job, &start);
if(start < 0) {
fprintf(stderr, "Job start time must be greater than or equal to 0, found %ld.\n", start);
exit(EXIT_FAILURE);
}
if(finish <= 0) {
fprintf(stderr, "Job finish time must be greater than 0, found %ld. \n", arrival);
exit(EXIT_FAILURE);
}
current = &list[job];
current->parameters.pid = job;
current->parameters.start = start;
jobs++;
}
int main(int argc, char **argv) {
/* Open input and output files */
for(int i = 0; i < argc; i++) {
if(strcmp(argv[i], "in.file") {
inputFile = argv[i];
input = fopen(inputFile,"r");
}
}
if(!inputFile) {
exit(EXIT_FAILURE);
}
parse_input();
fclose;
return EXIT_SUCCESS;
}
到目前为止我使用的结构。
/**
* Simulation of a process scheduler
*/
#ifndef SCHEDULER_H_
#define SCHEDULER_H_
#include <stddef.h>
/* types */
/** units of time */
typedef long time;
/** process identifier */
typedef int pid;
/** Information about a job of interest to the task scheduler */
struct job_data {
/* pid of this process */
pid pid;
/* time process starts */
time start;
/* time needed to finish */
time finish;
/* time spent processing so far */
time scheduled;
/* size of the process */
size_t size;
};
struct job {
/* Various parameters used by the scheduler */
struct job_data parameters;
/* next job to be scheduled */
struct job *next;
};
最后,我希望能够按照开始时间的顺序对作业进行排序,以便它们准备好由特定算法进行调度。
因此,我需要有关如何传递输入文件的帮助。文件读取作业并获取开始时间和顺序,然后通过启动“滴答”时间(即文本文件的第一行)来获取开始时间和顺序。
任何帮助都会很棒!!
最佳答案
我不知道你的问题是什么(你似乎没有问任何问题就发布了你正在做的事情的描述)。因此,我的答案不能成为答案,所以我只是以“希望有用”的方式闲聊。
在真实系统中;不同的任务在不同的时间开始运行,在不同的时间阻塞以等待不同的事情(并且当它们正在等待的事情发生时被解除阻塞),最终它们终止/退出。
除此之外还有一些高级功能;例如显式任务优先级控制(例如“nice()”)、分组任务、CPU 时间配额等。我不确定您是否愿意担心这些事情。
您不需要实现 BASIC 的最小子集(带有变量、循环等)来实现其中任何一个,并且尝试这样做只会增加毫无意义的复杂性。每个文件可以是一个简单的线性列表。例如:
123 ;Starting time
r22 ;Task runs for 22 ticks
s23 ;Task blocks due to "sleep()" for 23 seconds
r4 ;Task runs for 4 ticks
f4 ;Task blocks waiting for "4 units" of file IO (how quickly it unblocks depends on file system load)
r32 ;Task runs for 32 ticks
n8 ;Task blocks until it receives 8 packets from network
r22 ;Task runs for 22 ticks
;Task terminates
您可以随时向其中添加命令。例如,从“r”命令开始,然后添加“由于 sleep 而阻塞”等。最终您可以添加诸如“任务生成新任务”、“任务等待子进程退出”和“任务发送一些东西(通过管道、套接字等)来解锁另一个任务”,等等。
您会注意到(在我的示例中)所有命令都采用“操作码,立即”形式。这是故意的 - 它使解析变得简单(获取字符,获取整数,将它们添加到数组或链接列表中)。
关于c - 进程调度器模拟解析输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12758287/