You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言作业调度程序报错:signal: illegal instruction (core dumped) 求助

作业调度模拟器C程序“signal: illegal instruction (core dumped)”错误修复

核心错误原因

直接触发崩溃的是timeCheck函数中的野指针非法内存访问:

int timeCheck(int time, int ar[], int burst[]) {
   int *ptr;
   *ptr = 0; // ptr未初始化,属于野指针,直接赋值会写入随机内存地址,触发非法操作
   // ...
}

野指针未指向有效内存区域,对其解引用赋值会破坏内存结构,导致程序崩溃。

其他关键问题及修复

1. 未初始化变量导致统计值为NaN

所有用于统计的att、art变量未初始化,直接累加会得到无效的nan值,需初始化为0:

float att = 0.0f;
float art = 0.0f;

2. 数组被重复修改导致后续算法失效

program1直接修改了传入的cpuBurst数组,导致program2、program3拿到的是已执行完的作业数据。需为每个调度算法创建原始数据的副本,避免修改原数组。

3. 函数返回值未定义

program2声明返回int*,但函数内无任何return语句,会导致未定义行为。实际不需要返回值,可改为void类型。

4. timeCheck函数逻辑漏洞

当当前时间time小于所有作业到达时间时,未找到可调度作业,需处理这种情况(比如等待到下一个作业到达时间),同时将指针改为普通int变量,避免指针问题:

int timeCheck(int time, int ar[], int burst[]) {
   int idx = 0;
   // 找到第一个已到达的作业
   while (idx < 7 && ar[idx] > time) {
       idx++;
       // 所有作业未到达时,等待到第一个作业到达时间
       if(idx ==7){
           time = ar[0];
           idx=0;
       }
   }
   // 遍历已到达作业,找到CPU时长最短的
   for (int i = idx; i < 7; i++) {
      if (ar[i] <= time && burst[i] < burst[idx]) {
         idx = i;
      }
   }
   return idx;
}

5. Round Robin算法时间计算错误

当cpuBurst减到0时未增加时间,导致完成时间统计错误,修改时间逻辑:

cpuBurst[hey]--;
time++; // 无论是否减到0,都要增加时间
if (cpuBurst[hey] == 0) {
    break;
}

修复后的完整代码

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

void program1(int *id, int *arrival, int *cpuBurst);
void program2(int *id, int *arrival, int *cpuBurst, int *enter);
void 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};

   // 为每个算法创建数据副本,避免修改原始数据
   int cpuBurst1[7], cpuBurst2[7], cpuBurst3[7];
   for(int i=0; i<7; i++){
       cpuBurst1[i] = cpuBurst[i];
       cpuBurst2[i] = cpuBurst[i];
       cpuBurst3[i] = cpuBurst[i];
   }

   program1(id, arrival, cpuBurst1);
   // 重置enter数组
   for(int i=0; i<7; i++) enter[i] = 0;
   program2(id, arrival, cpuBurst2, enter);
   // 重置enter数组
   for(int i=0; i<7; i++) enter[i] = 0;
   program3(id, arrival, cpuBurst3, enter);

   return 0;
}

/************************** PROGRAM 1 ****************************/
void program1(int *id, int *arrival, int *cpuBurst) {
   int completion[7];
   int enter[7];
   float att = 0.0f;
   float art = 0.0f;
   int time = 0;
   int loop = 1;
   printf("FIRST COME FIRST SERVE\n");
   while (loop > 0) {
      loop = 0;
      for (int i = 0; i < 7; i++) {
         if (arrival[i] <= time && cpuBurst[i] > 0) {
            enter[i] = time;
            while (cpuBurst[i] > 0) {
               cpuBurst[i]--;
               time++;
            }
            completion[i] = time;
            printf("Id: %d Completion: %d\n", id[i], completion[i]);
         }
         if (cpuBurst[i] > 0) {
            loop++;
         }
      }
   }
   for (int j = 0; j < 7; j++) {
      att += completion[j] - arrival[j];
   }
   att /= 7;
   printf("Average turnaround time: %.2f\n", att);
   for (int r = 0; r < 7; r++) {
      art += enter[r] - arrival[r];
   }
   art /= 7;
   printf("Average response time: %.2f\n\n", art);
}

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

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

int timeCheck(int time, int ar[], int burst[]) {
   int idx = 0;
   // 找到第一个已到达的作业
   while (idx < 7 && ar[idx] > time) {
       idx++;
       // 所有作业未到达时,等待到第一个作业到达时间
       if(idx ==7){
           time = ar[0];
           idx=0;
       }
   }
   // 寻找已到达作业中CPU时长最短的
   for (int i = idx; i < 7; i++) {
      if (ar[i] <= time && burst[i] < burst[idx]) {
         idx = i;
      }
   }
   return idx;
}

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

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

修复后运行效果

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: 30.14
Average response time: 15.14

SHORTEST JOB FIRST
Id: 100 Completion: 10
Id: 102 Completion: 14
Id: 101 Completion: 24
Id: 105 Completion: 29
Id: 104 Completion: 44
Id: 106 Completion: 54
Id: 103 Completion: 74
Average turnaround time: 25.00
Average response time: 9.86

ROUND ROBIN
Id: 102 Completion: 24
Id: 100 Completion: 30
Id: 101 Completion: 40
Id: 105 Completion: 45
Id: 104 Completion: 60
Id: 106 Completion: 70
Id: 103 Completion: 74
Average turnaround time: 28.14
Average response time: 12.00

内容的提问来源于stack exchange,提问作者eseazywork

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 16:20:32