fork(2) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_PROCESS 5
  5.  
  6. typedef struct PCB {
  7. char name[10];
  8. int priority; // 优先级(动态变化)
  9. int need_time; // 需要运行时间(固定)
  10. int run_time; // 已运行时间(动态变化)
  11. char state; // 'W'就绪 / 'R'运行 / 'F'完成
  12. int finish_time; // 完成时刻
  13. int turn_time; // 周转时间
  14. float wturn_time; // 带权周转时间
  15. } PCB;
  16.  
  17. PCB pcb[MAX_PROCESS];
  18. int process_num = 0;
  19. int current_time = 0; // 全局时间轴
  20.  
  21. // 1. 初始化进程(你截图里的输入:aa 0 5, bb 0 8, cc 1 10)
  22. void initProcess() {
  23. // 进程aa
  24. strcpy(pcb[0].name, "aa");
  25. pcb[0].priority = 0;
  26. pcb[0].need_time = 5;
  27. pcb[0].run_time = 0;
  28. pcb[0].state = 'W';
  29. // 进程bb
  30. strcpy(pcb[1].name, "bb");
  31. pcb[1].priority = 0;
  32. pcb[1].need_time = 8;
  33. pcb[1].run_time = 0;
  34. pcb[1].state = 'W';
  35. // 进程cc
  36. strcpy(pcb[2].name, "cc");
  37. pcb[2].priority = 1;
  38. pcb[2].need_time = 10;
  39. pcb[2].run_time = 0;
  40. pcb[2].state = 'W';
  41.  
  42. process_num = 3;
  43. current_time = 0;
  44. }
  45.  
  46. // 2. 查找当前优先级最高的就绪进程
  47. int findMaxPriority() {
  48. int max_prio = -1;
  49. int idx = -1;
  50. for (int i = 0; i < process_num; i++) {
  51. if (pcb[i].state == 'W' && pcb[i].priority > max_prio) {
  52. max_prio = pcb[i].priority;
  53. idx = i;
  54. }
  55. }
  56. return idx;
  57. }
  58.  
  59. // 3. 显示当前所有进程状态
  60. void showProcess() {
  61. printf("\n当前进程状态:\n");
  62. printf("进程名\t优先级\t需运行时间\t已运行时间\t状态\n");
  63. for (int i = 0; i < process_num; i++) {
  64. printf("%s\t%d\t%d\t\t%d\t\t%c\n",
  65. pcb[i].name, pcb[i].priority,
  66. pcb[i].need_time, pcb[i].run_time, pcb[i].state);
  67. }
  68. }
  69.  
  70. // 4. 调度主逻辑
  71. void schedule() {
  72. int finished = 0;
  73. int current;
  74.  
  75. printf("===== 最高优先数优先调度开始 =====\n");
  76. showProcess();
  77.  
  78. while (finished < process_num) {
  79. current = findMaxPriority();
  80. if (current == -1) break; // 理论上不会发生
  81.  
  82. // 进程开始运行
  83. pcb[current].state = 'R';
  84. printf("\n运行进程:%s\n", pcb[current].name);
  85.  
  86. // 运行1个时间片
  87. pcb[current].run_time++;
  88. current_time++;
  89.  
  90. // 判断是否运行完成
  91. if (pcb[current].run_time == pcb[current].need_time) {
  92. pcb[current].state = 'F';
  93. pcb[current].finish_time = current_time; // ✅ 关键:只有完成时才赋值
  94. finished++;
  95. printf("进程 %s 运行完毕!完成时刻:%d\n", pcb[current].name, current_time);
  96. } else {
  97. // 未完成:优先级减1,回到就绪队列
  98. pcb[current].priority--;
  99. pcb[current].state = 'W';
  100. }
  101.  
  102. showProcess();
  103. }
  104. printf("\n===== 所有进程调度完成 =====\n");
  105. }
  106.  
  107. // 5. 计算并输出周转时间
  108. void calcTurnaround() {
  109. float total_turn = 0;
  110. float total_wturn = 0;
  111.  
  112. printf("\n===== 周转时间统计 =====\n");
  113. printf("进程名\t完成时刻\t周转时间\t带权周转时间\n");
  114. for (int i = 0; i < process_num; i++) {
  115. // 所有进程同时到达,到达时刻为0
  116. pcb[i].turn_time = pcb[i].finish_time - 0;
  117. pcb[i].wturn_time = (float)pcb[i].turn_time / pcb[i].need_time;
  118.  
  119. total_turn += pcb[i].turn_time;
  120. total_wturn += pcb[i].wturn_time;
  121.  
  122. printf("%s\t%d\t\t%d\t\t%.2f\n",
  123. pcb[i].name, pcb[i].finish_time,
  124. pcb[i].turn_time, pcb[i].wturn_time);
  125. }
  126.  
  127. printf("平均周转时间:%.2f\n", total_turn / process_num);
  128. printf("平均带权周转时间:%.2f\n", total_wturn / process_num);
  129. }
  130.  
  131. int main() {
  132. initProcess();
  133. schedule();
  134. calcTurnaround();
  135. return 0;
  136. }
Success #stdin #stdout 0.01s 5316KB
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		0.00
bb	0		0		0.00
cc	0		0		0.00
平均周转时间:0.00
平均带权周转时间:0.00