2012-10-08 29 views
-2

所以我只是想知道关于做一个调度程序模拟的几个技巧。处理输入文件在c

到目前为止,我只是想输入在命令行即/.scheduler in.file

的in.file拥有以下信息的文件:

./Job1.txt 
./Job2.txt 
./Job3.txt 
./Job4.txt 

每个工作.txt文件有随机的代码行。只有第一行是重要的。第一行开始“打勾”时间。

工作答:

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; 


}; 
+0

或许这是应该继续下去代码审查。 –

+0

我的不好!对不起,我只是审查它,因为我havnt睡觉!我已经取得了进展... –

回答

2

我不确定什么你有确切,但我可​​以在代码中看到以下错误的问题:

  • initialise_list()函数for循环将迭代太多tim ES和超越的界限的数组:

    for(int i = 0; i < sizeof(list); i++) { 
    

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); 
    
+0

我的问题是,我不知道如何打开文件'file.in'并从作业文本文件中收集与可执行文件相同的文件夹中的信息。 –

+0

感谢堆现在我明白了第一个循环创建问题......现在剩下的部分: –

+0

我用一点帮助来更新我的代码......让我们知道你在想什么!欢呼传奇 –

1
#include <errno.h> 
#include <limits.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <unistd.h> 
#include <ctype.h> 


#include "projscheduler.h" 



/* I/O Files */ 
//static char *inputFile; 
char * in; 
static FILE *input; 
static FILE *cur; 
/*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; 
    } 
} 

/*Order Jobs*/ 
/*=static void order_jobs(void) 
{ 
    for(int i=0; i < sizeof(list)/sizeof(list[0]); i++) 
    { 


} 
*/ 

/** Read and parse input from input file */ 
static void parse_input(void) 
{ 
    char buffer[BUFSIZ]; 
    char lines[BUFSIZ]; 
    int jobs = 0; 
    struct job *current; 


    initialise_list(); 



    while(fgets(buffer, sizeof(buffer), input)) 
    { 


     time start; 
     char buf[20]; 
     sscanf(buffer,"./%s/", buf); 
     cur = fopen(buf, "r"); 





     fgets(lines, sizeof(lines), cur); 
     sscanf(lines,"%ld", &start); 


     current = &list[jobs]; 

     current->job_id = buf; 
     current->parameters.start = start; 

     jobs++; 

    } 

    for (int i = 0; i < jobs; i++) 
    { 
     printf("%s starts at %ld\n", list[i].job_id, list[i].parameters.start); 
    } 

} 


int main(int argc, char **argv) 
{ 
    in = argv[1]; 
    if ((input = fopen(in, "r")) == NULL) { 
     fprintf(stderr, "cannot open %s\n", argv[1]); 
    } 

    parse_input(); 



    return EXIT_SUCCESS; 
} 

上面,我成功地读取in.file和提取每个地址表示文本的第一行文件。

上面下面的头相关的守则:

/** 
* 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_id; 
    struct job_data parameters; 
    /* next job to be scheduled */ 
    //struct job *next; 


}; 
+0

有一些缺陷,因为我有封闭的文件等... –