fork(3) download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define MAX_PROCESS 5 // 最多5个进程
  5.  
  6. // 定义进程控制块PCB,新增周转时间相关字段
  7. typedef struct PCB {
  8. char name[10]; // 进程名
  9. int priority; // 优先级
  10. int need_time; // 需要运行时间
  11. int run_time; // 已运行时间
  12. char state; // 状态:R-运行,W-就绪,F-完成
  13. int finish_time; // 完成时刻(新增,用于计算周转时间)
  14. int turn_time; // 周转时间(新增)
  15. float wturn_time; // 带权周转时间(新增)
  16. } PCB;
  17.  
  18. PCB pcb[MAX_PROCESS]; // 进程数组
  19. int process_num = 0; // 实际进程数
  20. int current_time = 0; // 全局时间,记录当前时刻(新增)
  21.  
  22. // 初始化进程(和你截图里的输入数据完全一致)
  23. void initProcess() {
  24. // 输入数据:aa 0 5,bb 0 8,cc 1 10
  25. strcpy(pcb[0].name, "aa");
  26. pcb[0].priority = 0;
  27. pcb[0].need_time = 5;
  28. pcb[0].run_time = 0;
  29. pcb[0].state = 'W';
  30.  
  31. strcpy(pcb[1].name, "bb");
  32. pcb[1].priority = 0;
  33. pcb[1].need_time = 8;
  34. pcb[1].run_time = 0;
  35. pcb[1].state = 'W';
  36.  
  37. strcpy(pcb[2].name, "cc");
  38. pcb[2].priority = 1;
  39. pcb[2].need_time = 10;
  40. pcb[2].run_time = 0;
  41. pcb[2].state = 'W';
  42.  
  43. process_num = 3;
  44. current_time = 0; // 初始时刻为0
  45. }
  46.  
  47. // 查找当前优先级最高的就绪进程
  48. int findMaxPriority() {
  49. int max = -1;
  50. int index = -1;
  51. for (int i = 0; i < process_num; i++) {
  52. if (pcb[i].state == 'W' && pcb[i].priority > max) {
  53. max = pcb[i].priority;
  54. index = i;
  55. }
  56. }
  57. return index;
  58. }
  59.  
  60. // 显示所有进程信息
  61. void showProcess() {
  62. printf("\n当前进程状态:\n");
  63. printf("进程名\t优先级\t需运行时间\t已运行时间\t状态\n");
  64. for (int i = 0; i < process_num; i++) {
  65. printf("%s\t%d\t%d\t\t%d\t\t%c\n",
  66. pcb[i].name, pcb[i].priority,
  67. pcb[i].need_time, pcb[i].run_time, pcb[i].state);
  68. }
  69. }
  70.  
  71. // 输出最终的周转时间统计(新增)
  72. void showTurnaroundInfo() {
  73. float total_turn = 0;
  74. float total_wturn = 0;
  75. printf("\n===== 进程调度完成,周转时间统计 =====\n");
  76. printf("进程名\t完成时刻\t周转时间\t带权周转时间\n");
  77. for (int i = 0; i < process_num; i++) {
  78. // 计算周转时间(假设所有进程同时到达,到达时刻为0)
  79. pcb[i].turn_time = pcb[i].finish_time - 0;
  80. pcb[i].wturn_time = (float)pcb[i].turn_time / pcb[i].need_time;
  81. total_turn += pcb[i].turn_time;
  82. total_wturn += pcb[i].wturn_time;
  83. printf("%s\t%d\t\t%d\t\t%.2f\n",
  84. pcb[i].name, pcb[i].finish_time,
  85. pcb[i].turn_time, pcb[i].wturn_time);
  86. }
  87. printf("平均周转时间:%.2f\n", total_turn / process_num);
  88. printf("平均带权周转时间:%.2f\n", total_wturn / process_num);
  89. }
  90.  
  91. // 进程调度主函数
  92. void scheduleProcess() {
  93. int finish_count = 0; // 完成进程数
  94. int current; // 当前运行进程下标
  95.  
  96. printf("===== 最高优先数优先调度算法开始 =====\n");
  97. showProcess();
  98.  
  99. while (finish_count < process_num) {
  100. current = findMaxPriority();
  101. if (current == -1) break; // 无就绪进程
  102.  
  103. // 选中进程运行
  104. pcb[current].state = 'R';
  105. printf("\n运行进程:%s\n", pcb[current].name);
  106. pcb[current].run_time++; // 运行时间+1
  107. current_time++; // 全局时间+1(新增)
  108.  
  109. // 运行完成
  110. if (pcb[current].run_time == pcb[current].need_time) {
  111. pcb[current].state = 'F';
  112. pcb[current].finish_time = current_time; // 记录完成时刻(新增)
  113. finish_count++;
  114. printf("进程 %s 运行完毕!\n", pcb[current].name);
  115. } else {
  116. // 动态优先级:运行一次优先级-1
  117. pcb[current].priority--;
  118. pcb[current].state = 'W';
  119. }
  120. showProcess();
  121. }
  122. printf("\n===== 所有进程调度完成 =====\n");
  123. showTurnaroundInfo(); // 输出周转时间统计(新增)
  124. }
  125.  
  126. int main() {
  127. initProcess(); // 初始化并输入进程
  128. scheduleProcess();// 调度进程
  129. return 0;
  130. }
Success #stdin #stdout 0s 5292KB
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