C语言 为什么会出现此错误?“信号:非法指令(核心转储)”

vbopmzt1  于 2022-12-03  发布在  其他
关注(0)|答案(1)|浏览(208)

我正在编写一个模拟操作系统的作业调度程序的程序。调度程序将读入一个作业列表及其相关信息,并输出每个作业的完成顺序沿着完成时间和其他相关统计信息。我正在使用replit编译这段c代码,并得到(核心转储)错误,我不知道为什么或这到底是什么意思。我是相对新的c代码,所以任何帮助是感激。

#include <stdio.h>
#include <stdbool.h>

void program1(int *id, int *arrival, int *cpuBurst);
int *program2(int *id, int *arrival, int *cpuBurst, int *enter);
int program3(int *id, int *arrival, int  *cpuBurst, int *enter);

int main() {

   int id[7] = {
      100,
      101,
      102,
      103,
      104,
      105,
      106
   };
   int arrival[7] = {
      0,
      6,
      8,
      12,
      19,
      30,
      35
   };
   int cpuBurst[7] = {
      10,
      10,
      4,
      20,
      15,
      5,
      10
   };

   int enter[7] = {
      0,
      0,
      0,
      0,
      0,
      0,
      0
   };

   program1(id, arrival, cpuBurst);
   int *ptr = program2(id, arrival, cpuBurst, enter);
   int loop = program3(id, arrival, cpuBurst, enter);

   return 0;
}

/************************** PROGRAM 1 ****************************/
void program1(int *id, int *arrival, int *cpuBurst) {
   int completion[7];
   int enter[7];
   float att;
   float art;
   int time = 0;
   int loop = 1;
   while (loop > 0) {
      printf("FIRST COME FIRST SERVE\n");
      for (int i = 0; i < 7; i++) {
         if (arrival[i] <= time) {
            enter[i] = time;
            while (cpuBurst[i] > 0) {
               cpuBurst[i] = cpuBurst[i] - 1;
               time = time + 1;
            }
         }
         if (cpuBurst[i] == 0) {
            completion[i] = time;

            printf("%s %d %s %d\n", "Id: ", id[i], " Completion: ", completion[i]);
         } else {
            loop = loop + 1;
         }
      }
      loop = loop - 1;
   }
   for (int j = 0; j < 7; j++) {
      att = att + completion[j] - arrival[j];
   }
   att = att / 7;
   printf("Average turnaround time: %f\n", att);
   for (int r = 0; r < 7; r++) {
      art = art + enter[r] - arrival[r];
   }
   art = art / 7;
   printf("Average response time: %f\n", art);
}

/************************** PROGRAM 2 ************************************/
int *program2(int *id, int *arrival, int *cpuBurst, int *enter) {
   int completion[7];
   float att;
   float art;
   int time = 0;
   int temp;
   int timeCheck(int hey, int yuh[], int sup[]);
   int go(int yuh, int oof);

   printf("SHORTEST JOB FRIST\n");
   for (int i = 0; i < 7; i++) {
      temp = timeCheck(time, arrival, cpuBurst);
      enter[temp] = time;
      while (cpuBurst[temp] > 0) {
         cpuBurst[temp] = cpuBurst[temp] - 1;
         time = time + 1;
      }
      cpuBurst[temp] = 20000;
      completion[temp] = time;
      printf("%s %d %s %d\n", "Id: ", id[temp], " Completion: ", completion[temp]);
   }
   for (int j = 0; j < 7; j++) {
      att = att + completion[j] - arrival[j];
   }
   att = att / 7;
   printf("Average turnaround time: %f\n", att);
   for (int r = 0; r < 7; r++) {
      art = art + enter[r] - arrival[r];
   }
   art = art / 7;
   printf("Average response time: %f\n", art);
}

int go(int time, int arrival) {
   if (time >= arrival) {
      return 1;
   } else return 0;
}

int timeCheck(int time, int ar[], int burst[]) {
   int *ptr;
   *ptr = 0;
   for (int i = 0; i < 7; i++) {
      if (ar[i] <= time) {
         if (burst[i] < burst[ * ptr]) {
            *ptr = i;
         }
      }
   }
   return *ptr;
}

/************************************ PROGRAM 3 *********************************/
int program3(int *id, int *arrival, int *cpuBurst, int *enter) {
   float att;
   float art;
   int time = 0;
   int completion[7];
   int hey = 0;
   int checker = 1;
   int check(int yo[]);

   printf("ROUND ROBIN\n");
   while (checker > 0) {
      if (7 == hey) {
         hey = 0;
      }
      for (int t = 0; t < 10; t++) {
         if (cpuBurst[hey] == 0) {
            break;
         }
         if (enter[hey] == 0) {
            enter[hey] = time;
         }
         cpuBurst[hey] -= 1;
         if (cpuBurst[hey] > 0) {
            time = time + 1;
         }
      }
      if (cpuBurst[hey] == 0) {
         completion[hey] = time;
         printf("%s %d %s %d\n", "Id: ", id[hey], " Completion: ", completion[hey]);
      }
      hey = hey + 1;
      checker = check(cpuBurst);
   }
   for (int j = 0; j < 7; j++) {
      att = att + completion[j] - arrival[j];
   }
   att = att / 7;
   printf("Average turnaround time: %f\n", att);
   for (int r = 0; r < 7; r++) {
      art = art + enter[r] - arrival[r];
   }
   art = art / 7;
   printf("Average response time: %f\n", art);
}
int check(int burst[]) {
   int loop = 0;
   for (int i = 0; i < 7; i++) {
      if (burst[i] == 0) {
         continue;
      } else loop++;
   }
   return loop;
}

输出:

 make -s
 ./main
FIRST COME FIRST SERVE
Id:  100  Completion:  10
Id:  101  Completion:  20
Id:  102  Completion:  24
Id:  103  Completion:  44
Id:  104  Completion:  59
Id:  105  Completion:  64
Id:  106  Completion:  74
Average turnaround time: nan
Average response time: nan
SHORTEST JOB FRIST
signal: illegal instruction (core dumped)
jjhzyzn0

jjhzyzn01#

至少在这个功能

int timeCheck(int time, int ar[], int burst[]) {
   int *ptr;
   *ptr = 0;
   //...

您正在取消引用一个未初始化的指针,该指针具有导致未定义行为的不确定值。
在函数中不需要使用int *类型的指针来代替int类型的对象。
注意,例如,函数program2program3不返回任何内容,尽管它们的返回类型不是void
或者在函数program2中声明了函数go

int go(int yuh, int oof);

但是它不在函数program2中使用。

相关问题