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

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

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

  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. void program1(int *id, int *arrival, int *cpuBurst);
  4. int *program2(int *id, int *arrival, int *cpuBurst, int *enter);
  5. int program3(int *id, int *arrival, int *cpuBurst, int *enter);
  6. int main() {
  7. int id[7] = {
  8. 100,
  9. 101,
  10. 102,
  11. 103,
  12. 104,
  13. 105,
  14. 106
  15. };
  16. int arrival[7] = {
  17. 0,
  18. 6,
  19. 8,
  20. 12,
  21. 19,
  22. 30,
  23. 35
  24. };
  25. int cpuBurst[7] = {
  26. 10,
  27. 10,
  28. 4,
  29. 20,
  30. 15,
  31. 5,
  32. 10
  33. };
  34. int enter[7] = {
  35. 0,
  36. 0,
  37. 0,
  38. 0,
  39. 0,
  40. 0,
  41. 0
  42. };
  43. program1(id, arrival, cpuBurst);
  44. int *ptr = program2(id, arrival, cpuBurst, enter);
  45. int loop = program3(id, arrival, cpuBurst, enter);
  46. return 0;
  47. }
  48. /************************** PROGRAM 1 ****************************/
  49. void program1(int *id, int *arrival, int *cpuBurst) {
  50. int completion[7];
  51. int enter[7];
  52. float att;
  53. float art;
  54. int time = 0;
  55. int loop = 1;
  56. while (loop > 0) {
  57. printf("FIRST COME FIRST SERVE\n");
  58. for (int i = 0; i < 7; i++) {
  59. if (arrival[i] <= time) {
  60. enter[i] = time;
  61. while (cpuBurst[i] > 0) {
  62. cpuBurst[i] = cpuBurst[i] - 1;
  63. time = time + 1;
  64. }
  65. }
  66. if (cpuBurst[i] == 0) {
  67. completion[i] = time;
  68. printf("%s %d %s %d\n", "Id: ", id[i], " Completion: ", completion[i]);
  69. } else {
  70. loop = loop + 1;
  71. }
  72. }
  73. loop = loop - 1;
  74. }
  75. for (int j = 0; j < 7; j++) {
  76. att = att + completion[j] - arrival[j];
  77. }
  78. att = att / 7;
  79. printf("Average turnaround time: %f\n", att);
  80. for (int r = 0; r < 7; r++) {
  81. art = art + enter[r] - arrival[r];
  82. }
  83. art = art / 7;
  84. printf("Average response time: %f\n", art);
  85. }
  86. /************************** PROGRAM 2 ************************************/
  87. int *program2(int *id, int *arrival, int *cpuBurst, int *enter) {
  88. int completion[7];
  89. float att;
  90. float art;
  91. int time = 0;
  92. int temp;
  93. int timeCheck(int hey, int yuh[], int sup[]);
  94. int go(int yuh, int oof);
  95. printf("SHORTEST JOB FRIST\n");
  96. for (int i = 0; i < 7; i++) {
  97. temp = timeCheck(time, arrival, cpuBurst);
  98. enter[temp] = time;
  99. while (cpuBurst[temp] > 0) {
  100. cpuBurst[temp] = cpuBurst[temp] - 1;
  101. time = time + 1;
  102. }
  103. cpuBurst[temp] = 20000;
  104. completion[temp] = time;
  105. printf("%s %d %s %d\n", "Id: ", id[temp], " Completion: ", completion[temp]);
  106. }
  107. for (int j = 0; j < 7; j++) {
  108. att = att + completion[j] - arrival[j];
  109. }
  110. att = att / 7;
  111. printf("Average turnaround time: %f\n", att);
  112. for (int r = 0; r < 7; r++) {
  113. art = art + enter[r] - arrival[r];
  114. }
  115. art = art / 7;
  116. printf("Average response time: %f\n", art);
  117. }
  118. int go(int time, int arrival) {
  119. if (time >= arrival) {
  120. return 1;
  121. } else return 0;
  122. }
  123. int timeCheck(int time, int ar[], int burst[]) {
  124. int *ptr;
  125. *ptr = 0;
  126. for (int i = 0; i < 7; i++) {
  127. if (ar[i] <= time) {
  128. if (burst[i] < burst[ * ptr]) {
  129. *ptr = i;
  130. }
  131. }
  132. }
  133. return *ptr;
  134. }
  135. /************************************ PROGRAM 3 *********************************/
  136. int program3(int *id, int *arrival, int *cpuBurst, int *enter) {
  137. float att;
  138. float art;
  139. int time = 0;
  140. int completion[7];
  141. int hey = 0;
  142. int checker = 1;
  143. int check(int yo[]);
  144. printf("ROUND ROBIN\n");
  145. while (checker > 0) {
  146. if (7 == hey) {
  147. hey = 0;
  148. }
  149. for (int t = 0; t < 10; t++) {
  150. if (cpuBurst[hey] == 0) {
  151. break;
  152. }
  153. if (enter[hey] == 0) {
  154. enter[hey] = time;
  155. }
  156. cpuBurst[hey] -= 1;
  157. if (cpuBurst[hey] > 0) {
  158. time = time + 1;
  159. }
  160. }
  161. if (cpuBurst[hey] == 0) {
  162. completion[hey] = time;
  163. printf("%s %d %s %d\n", "Id: ", id[hey], " Completion: ", completion[hey]);
  164. }
  165. hey = hey + 1;
  166. checker = check(cpuBurst);
  167. }
  168. for (int j = 0; j < 7; j++) {
  169. att = att + completion[j] - arrival[j];
  170. }
  171. att = att / 7;
  172. printf("Average turnaround time: %f\n", att);
  173. for (int r = 0; r < 7; r++) {
  174. art = art + enter[r] - arrival[r];
  175. }
  176. art = art / 7;
  177. printf("Average response time: %f\n", art);
  178. }
  179. int check(int burst[]) {
  180. int loop = 0;
  181. for (int i = 0; i < 7; i++) {
  182. if (burst[i] == 0) {
  183. continue;
  184. } else loop++;
  185. }
  186. return loop;
  187. }

输出:

  1. make -s
  2. ./main
  3. FIRST COME FIRST SERVE
  4. Id: 100 Completion: 10
  5. Id: 101 Completion: 20
  6. Id: 102 Completion: 24
  7. Id: 103 Completion: 44
  8. Id: 104 Completion: 59
  9. Id: 105 Completion: 64
  10. Id: 106 Completion: 74
  11. Average turnaround time: nan
  12. Average response time: nan
  13. SHORTEST JOB FRIST
  14. signal: illegal instruction (core dumped)
jjhzyzn0

jjhzyzn01#

至少在这个功能

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

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

  1. int go(int yuh, int oof);

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

相关问题