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

运行进程:aa

进程名	优先级	还需时间	已运行时间	状态
aa	-1	4		1		W
bb	0	8		0		W
cc	0	9		1		W

运行进程:bb

进程名	优先级	还需时间	已运行时间	状态
aa	-1	4		1		W
bb	-1	7		1		W
cc	0	9		1		W

运行进程:cc

进程名	优先级	还需时间	已运行时间	状态
aa	-1	4		1		W
bb	-1	7		1		W
cc	-1	8		2		W

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

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