为我的进程调度模拟器程序逐行读取C中的文件时出现分段故障(核心转储)错误



我有一个程序,它从文本文件中获取有关进程的信息,然后使用所需的CPU调度算法来调度进程并相应地打印它们。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/time.h>
#include<time.h>
//Structure to store the information about a process
typedef struct{
int pid;
int arrival;
int burst;
}Processes;
//Array of structure
typedef struct
{
Processes *array;
size_t used;
size_t size;
} Array;
enum ALGORITHM_TYPE{FCFS, RR, SRTF};                        //enum for creating the algorithm types
int procNum = 0;                                            //Variable to keep track of number of processes
Array proc, fin;

//Function to determine the number of processes
void procNumFunction(char *fileName){
FILE *fp;                                               //File pointer
char *buffer, c;
fp = fopen(fileName, "r");
if(fp == NULL)
exit(1);
// Extract characters from file and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))
if (c == 'n') // Increment count if this character is newline
procNum++;
printf("nTotal %d tasks are read from "%s"nn", procNum, fileName);
}
//function to dynamically fill the array
void initArray(Array *a, size_t initialSize)
{
// Allocate initial space
a->array = (Processes *)calloc(initialSize, sizeof(Processes));
a->used = 0;                                            // no elements used
a->size = initialSize;                                  // available nr of elements
}
// Add element to array
void insertArray(Array *a, Processes element)
{
if (a->used == a->size)
{
a->size *= 2;
a->array = (Processes *)realloc(a->array, a->size * sizeof(Processes));
}
a->array[a->used].pid=element.pid;
a->array[a->used].arrival=element.arrival;
a->array[a->used].burst=element.burst;
a->used++;
}
//Functino to free array
void freeArray(Array *a)
{
free(a->array);
a->array = NULL;
a->used = 0;
a->size = 0;
}
//Function to read from the file
void readFile(char *fileName){
FILE *fp = NULL;                                               //File pointer
char *buffer = NULL, *pid = NULL, *arr = NULL, *bur = NULL,*p = NULL;
Processes temp;
int count = 0;
fp = fopen(fileName, "r");
if(fp == NULL)
exit(1);
//Loop to read the file line by line
while(fgets(buffer, sizeof(buffer), fp)){
/*        copyToken(pid, buffer, sizeof(pid), "t");
copyToken(arr, buffer, sizeof(arr), "t");
copyToken(bur, buffer, sizeof(bur), "n");*/
while (*p) {
if ( isdigit(*p) || ( (*p=='-'||*p=='+') && isdigit(*(p+1)) )) {
// Found a number
long val = strtol(p, &p, 10); // Read number
if(count == 0){
temp.pid = val;
count++;
}
else if(count == 1){
temp.arrival = val;
count++;
}
else if(count == 3){
temp.burst = val;
count++;
}
} else {
p++;
}
}
//Insert element into array
insertArray(&proc, temp);
count = 0;
}
fclose(fp);
}
/*void copyToken(char * dest, char *source, int len, char const *delim){
char *token = strtok(source, delim);
if(token != NULL){
dest[0] = '';
strncat(dest, token, len-1);
}
}*/
void fcfs(char *fileName){
int finCount = 0, sysTime = 0;
//Calling this function to determine the number of processes
procNumFunction(fileName);
//Initializing the array
initArray(&proc, procNum);
initArray(&fin, procNum);
//Reading the file
readFile(fileName);
while(finCount <= procNum){
printf("<system timet%d> process %d is runningn", sysTime, proc.array[finCount].pid);
sysTime++;
if(proc.array[finCount].burst != 0)
proc.array[finCount].burst--;
else{
printf("<system timet%d> process %d is finished...n", sysTime, proc.array[finCount].pid);
finCount++;
}
}
if(finCount > procNum){
printf("<system timet%d> All processes finishedn", sysTime);
printf("==================================================================================n");
}
}
void srtf(char * fileName){

}
//The main function of the program
int main(int argc, char *argv[]){
//Conditions to check if the arguments have been given correctly or not
if (argc < 2)
printf("Error. File name missing !!!");
else if(argc == 2)
printf("Error. Scheduling type missing !!!");
else{
if(!(strcmp(argv[2],"FCFS"))){
printf("Scheduling algorithm: %s", argv[2]);
fcfs(argv[1]);                                  //call FCFS
}
else if(!(strcmp(argv[2], "RR"))){
printf("Scheduling algorithm: %sn", argv[2]);
if(argc != 4)
printf("Error. Time quantum not given !!!");
else{
//check if valid input
}
}
else if(!(strcmp(argv[2], "SRTF"))){
printf("Scheduling algorithm: %s", argv[2]);
srtf(argv[1]);                                  //call SRTF
}
}
}

我的代码目前还不完整,但它可以编译并运行。我只是想运行这个程序,看看我的程序是否至少在FCFS算法上正常工作。

我用这个名为input1.txt 的文件测试了这个程序

1 0 10
2 0 9
3 3 5
4 7 4
5 10 6
6 10 7

这里,第一个数字是进程的PID,第二个数字是该进程的到达时间,第三个数字是这个进程的突发时间。总共有6个过程。

我的问题是,当我运行程序时,它会以以下输出运行:

$ ./a.out input1.txt FCFS
Scheduling algorithm: FCFS
Total 5 tasks are read from "input1.txt"
Segmentation fault (core dumped)

我在这里不明白的是,我的am我遇到了一个分段错误,在运行eclipse调试器后,程序在readFile函数中的while(fgets(buffer, sizeof(buffer), fp)){行停止。有人能告诉我我做错了什么吗?因为我认为我逐行读取文件的实现是正确的?

提前谢谢。

您的buffer是一个指向任何位置的指针(永远没有为字符串分配空间!(。它的大小是指针的大小(4或8字节(,而不是放置任何东西的空间。将缓冲区设为一个数组,例如100字节(临时存储!(,即char buffer[100]

  1. procNumFunction()中打开文件,但没有在fclose()中打开,然后在readFile()中再次尝试打开。

  2. procNum=0;initArray(&proc, procNum);使calloc(0, sizeof(Processes))
    我不确定调用calloc(0(会发生什么,但malloc(0(在这里是错误的。(有些地方使用malloc(0(,但这里不好(

void*malloc(size_t-size(;

malloc((分配大小字节,并返回一个指向已分配记忆力内存未清除如果大小为0,则malloc((返回NULL,或以后可以成功的唯一指针值传递给free((。

  1. initArray之后,使用的a->为0,a->大小为0,然后为insertArray()
a->size *= 2;
a->array = (Processes *)realloc(a->array, a->size * sizeof(Processes));

realloc(p, 0)电话,我觉得很危险。

在函数readFile()中,指针p初始化为NULL

然后执行以下语句:

while (*p) {

由于p包含NULL,程序将因seg错误事件而崩溃。

最新更新