所以我只是想知道有关进行调度程序模拟的一些技巧。
到目前为止,我只想在命令行输入一个文件,即/.scheduler in.file
in.file 包含以下信息:
./Job1.txt
./Job2.txt
./Job3.txt
./Job4.txt
每个作业 .txt 文件都有随机的代码行。只有第一行是重要的。第一行是开始“滴答”时间。
工作A:
10
1fi
3sdkfj
4ksdkk
5kdkfk
6kdkjf
7dkjkfd
9dkkf
10dku
目前,我只想获取 in.file 并按照“作业”文件到达时间的顺序(即第一行)对“作业”文件进行排序。
到目前为止我的代码:
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "projscheduler.h"
/* I/O Files */
//static char *inputFile;
char * in;
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(list)/sizeof(list[0]); i++) {
list[i].parameters.pid = -1;
}
}
/** Read and parse input from input file */
static void parse_input(void)
{
char buffer[BUFSIZ];
//int jobs;
initialise_list();
while( fgets(buffer, sizeof(buffer), input) )
{
pid j_pid;
sscanf(buffer, "./%d.txt", &j_pid);
}
}
int main(int argc, char **argv)
{
if ( (input = fopen(in, "r")) == NULL ) {
fprintf(stderr, "cannot open %s\n", argv[1]);
}
parse_input();
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 */
char job_name[BUFSIZ];
struct job_data parameters;
/* next job to be scheduled */
//struct job *next;
};
最佳答案
我不确定您到底遇到了什么问题,但我可以在代码中看到以下错误:
在
initialise_list()
函数中,for
循环将迭代太多次并超出数组范围:for(int i = 0; i < sizeof(list); i++) {
as sizeof(list)
将返回数组占用的字节数,即20 * sizeof(struct job)
。更改为:
for(int i = 0; i < sizeof(list) / sizeof(list[0]); i++) {
这是
fopen()
尝试中缺少的左括号(意味着发布的代码无法编译):if( (input = fopen(inputfile, "r") == NULL )
应该是:
if( (input = fopen(inputfile, "r")) == NULL )
proj
是一个初始化的char*
,这很可能会导致段错误:char *proj; sscanf(buffer, "./%s.txt", proj);
关于c - 在c中处理输入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12781954/