fork(1) 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. } PCB;
  14.  
  15. PCB pcb[MAX_PROCESS]; // 进程数组
  16. int process_num = 0; // 实际进程数
  17.  
  18. // 初始化进程(直接写入输入数据,无需手动输入)
  19. void initProcess() {
  20. // 实验示例输入:3个进程
  21. strcpy(pcb[0].name, "aa");
  22. pcb[0].priority = 3;
  23. pcb[0].need_time = 5;
  24. pcb[0].run_time = 0;
  25. pcb[0].state = 'W';
  26.  
  27. strcpy(pcb[1].name, "bb");
  28. pcb[1].priority = 1;
  29. pcb[1].need_time = 8;
  30. pcb[1].run_time = 0;
  31. pcb[1].state = 'W';
  32.  
  33. strcpy(pcb[2].name, "cc");
  34. pcb[2].priority = 5;
  35. pcb[2].need_time = 10;
  36. pcb[2].run_time = 0;
  37. pcb[2].state = 'W';
  38.  
  39. process_num = 3;
  40. }
  41.  
  42. // 查找当前优先级最高的就绪进程
  43. int findMaxPriority() {
  44. int max = -1;
  45. int index = -1;
  46. for (int i = 0; i < process_num; i++) {
  47. if (pcb[i].state == 'W' && pcb[i].priority > max) {
  48. max = pcb[i].priority;
  49. index = i;
  50. }
  51. }
  52. return index;
  53. }
  54.  
  55. // 显示所有进程信息
  56. void showProcess() {
  57. printf("\n当前进程状态:\n");
  58. printf("进程名\t优先级\t需运行时间\t已运行时间\t状态\n");
  59. for (int i = 0; i < process_num; i++) {
  60. printf("%s\t%d\t%d\t\t%d\t\t%c\n",
  61. pcb[i].name, pcb[i].priority,
  62. pcb[i].need_time, pcb[i].run_time, pcb[i].state);
  63. }
  64. }
  65.  
  66. // 进程调度主函数
  67. void scheduleProcess() {
  68. int finish_count = 0; // 完成进程数
  69. int current; // 当前运行进程下标
  70.  
  71. printf("===== 最高优先数优先调度算法开始 =====\n");
  72. showProcess();
  73.  
  74. while (finish_count < process_num) {
  75. current = findMaxPriority();
  76. if (current == -1) break; // 无就绪进程
  77.  
  78. // 选中进程运行
  79. pcb[current].state = 'R';
  80. printf("\n运行进程:%s\n", pcb[current].name);
  81. pcb[current].run_time++; // 运行时间+1
  82.  
  83. // 运行完成
  84. if (pcb[current].run_time == pcb[current].need_time) {
  85. pcb[current].state = 'F';
  86. finish_count++;
  87. printf("进程 %s 运行完毕!\n", pcb[current].name);
  88. } else {
  89. // 动态优先级:运行一次优先级-1
  90. pcb[current].priority--;
  91. pcb[current].state = 'W';
  92. }
  93. showProcess();
  94. }
  95. printf("\n===== 所有进程调度完成 =====\n");
  96. }
  97.  
  98. int main() {
  99. initProcess(); // 初始化并输入进程
  100. scheduleProcess();// 调度进程
  101. return 0;
  102. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
===== 最高优先数优先调度算法开始 =====

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

运行进程:cc

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

运行进程:cc

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

运行进程:aa

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

运行进程:cc

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

运行进程:aa

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

运行进程:cc

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

运行进程:aa

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

运行进程:bb

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

运行进程:cc

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

运行进程:aa

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

运行进程:bb

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

运行进程:cc

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

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