fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define N 3
  5.  
  6. struct PCB {
  7. char name[10];
  8. int priority;
  9. int needtime;
  10. int runtime;
  11. char state;
  12. int finishtime; // 完成时间
  13. int turntime; // 周转时间
  14. } pcb[N];
  15.  
  16. int current_time = 0;
  17.  
  18. // 输出进程状态(和你实验截图完全一样)
  19. void output() {
  20. printf("\n当前进程状态:\n");
  21. printf("进程名\t优先级\t需运行时间\t已运行时间\t状态\n");
  22. for(int i = 0; i < N; i++) {
  23. printf("%s\t%d\t%d\t\t%d\t\t%c\n",
  24. pcb[i].name,
  25. pcb[i].priority,
  26. pcb[i].needtime,
  27. pcb[i].runtime,
  28. pcb[i].state);
  29. }
  30. }
  31.  
  32. // 输出周转时间统计
  33. void print_turn() {
  34. float total = 0;
  35. printf("\n===== 周转时间统计 =====\n");
  36. printf("进程名\t完成时间\t周转时间\n");
  37. for(int i = 0; i < N; i++) {
  38. pcb[i].turntime = pcb[i].finishtime;
  39. total += pcb[i].turntime;
  40. printf("%s\t%d\t\t%d\n", pcb[i].name, pcb[i].finishtime, pcb[i].turntime);
  41. }
  42. printf("平均周转时间:%.2f\n", total / N);
  43. }
  44.  
  45. int main() {
  46. // 初始化:aa、bb、cc(和你实验完全一样)
  47. strcpy(pcb[0].name, "aa");
  48. pcb[0].priority = 0;
  49. pcb[0].needtime = 5;
  50. pcb[0].runtime = 0;
  51. pcb[0].state = 'W';
  52. pcb[0].finishtime = 0;
  53.  
  54. strcpy(pcb[1].name, "bb");
  55. pcb[1].priority = 0;
  56. pcb[1].needtime = 8;
  57. pcb[1].runtime = 0;
  58. pcb[1].state = 'W';
  59. pcb[1].finishtime = 0;
  60.  
  61. strcpy(pcb[2].name, "cc");
  62. pcb[2].priority = 1;
  63. pcb[2].needtime = 10;
  64. pcb[2].runtime = 0;
  65. pcb[2].state = 'W';
  66. pcb[2].finishtime = 0;
  67.  
  68. printf("===== 最高优先数调度算法 =====\n");
  69. output();
  70.  
  71. int finish = 0;
  72. while(finish < N) {
  73. // 找优先级最高的进程
  74. int max = -1, run = -1;
  75. for(int i = 0; i < N; i++) {
  76. if(pcb[i].state == 'W' && pcb[i].priority > max) {
  77. max = pcb[i].priority;
  78. run = i;
  79. }
  80. }
  81.  
  82. if(run == -1) break;
  83.  
  84. // 运行
  85. pcb[run].state = 'R';
  86. printf("\n运行进程:%s\n", pcb[run].name);
  87.  
  88. current_time++;
  89. pcb[run].runtime++;
  90.  
  91. // 运行完成
  92. if(pcb[run].runtime == pcb[run].needtime) {
  93. pcb[run].state = 'F';
  94. pcb[run].finishtime = current_time; // ✅ 赋值完成时间
  95. finish++;
  96. printf("进程 %s 运行完毕\n", pcb[run].name);
  97. } else {
  98. pcb[run].priority--; // 动态优先级-1
  99. pcb[run].state = 'W';
  100. }
  101.  
  102. output();
  103. }
  104.  
  105. printf("\n===== 所有进程调度完成 =====\n");
  106. print_turn(); // 输出周转时间
  107.  
  108. return 0;
  109. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
===== 最高优先数调度算法 =====

当前进程状态:
进程名	优先级	需运行时间	已运行时间	状态
aa	0	5		0		W
bb	0	8		0		W
cc	1	10		0		W

运行进程:cc

当前进程状态:
进程名	优先级	需运行时间	已运行时间	状态
aa	0	5		0		W
bb	0	8		0		W
cc	0	10		1		W

运行进程:aa

当前进程状态:
进程名	优先级	需运行时间	已运行时间	状态
aa	-1	5		1		W
bb	0	8		0		W
cc	0	10		1		W

运行进程:bb

当前进程状态:
进程名	优先级	需运行时间	已运行时间	状态
aa	-1	5		1		W
bb	-1	8		1		W
cc	0	10		1		W

运行进程:cc

当前进程状态:
进程名	优先级	需运行时间	已运行时间	状态
aa	-1	5		1		W
bb	-1	8		1		W
cc	-1	10		2		W

===== 所有进程调度完成 =====

===== 周转时间统计 =====
进程名	完成时间	周转时间
aa	0		0
bb	0		0
cc	0		0
平均周转时间:0.00