fork download
  1. /*
  2.  * 实验二:处理器调度模拟
  3.  * 编译:gcc -o scheduler scheduler.c
  4.  * 运行:./scheduler
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #define MAX_PROCESS 10
  12.  
  13. typedef struct {
  14. int pid; // 进程ID
  15. int arrival; // 到达时间
  16. int burst; // 运行时间(CPU区间)
  17. int remaining; // 剩余运行时间(RR用)
  18. int completion; // 完成时间
  19. int turnaround; // 周转时间
  20. float w_turnaround;// 带权周转时间
  21. } Process;
  22.  
  23. // 打印结果
  24. void print_result(Process p[], int n, char *algo) {
  25. printf("\n========== %s ==========\n", algo);
  26. printf("PID\t到达\t运行\t完成\t周转\t带权周转\n");
  27. float avg_tat = 0, avg_wtat = 0;
  28. for (int i = 0; i < n; i++) {
  29. printf("%d\t%d\t%d\t%d\t%d\t%.2f\n",
  30. p[i].pid, p[i].arrival, p[i].burst,
  31. p[i].completion, p[i].turnaround, p[i].w_turnaround);
  32. avg_tat += p[i].turnaround;
  33. avg_wtat += p[i].w_turnaround;
  34. }
  35. avg_tat /= n;
  36. avg_wtat /= n;
  37. printf("平均周转时间:%.2f\n", avg_tat);
  38. printf("平均带权周转时间:%.2f\n", avg_wtat);
  39. }
  40.  
  41. // 先来先服务
  42. void fcfs(Process p[], int n) {
  43. Process q[MAX_PROCESS];
  44. memcpy(q, p, sizeof(Process) * n);
  45. int time = 0;
  46. for (int i = 0; i < n; i++) {
  47. if (time < q[i].arrival) time = q[i].arrival;
  48. q[i].completion = time + q[i].burst;
  49. time = q[i].completion;
  50. q[i].turnaround = q[i].completion - q[i].arrival;
  51. q[i].w_turnaround = (float)q[i].turnaround / q[i].burst;
  52. }
  53. print_result(q, n, "FCFS");
  54. }
  55.  
  56. // 短作业优先(非抢占)
  57. void sjf(Process p[], int n) {
  58. Process q[MAX_PROCESS];
  59. memcpy(q, p, sizeof(Process) * n);
  60. int completed = 0, time = 0;
  61. int finish[MAX_PROCESS] = {0};
  62. while (completed < n) {
  63. int idx = -1;
  64. int min_burst = 99999;
  65. // 找已到达且未完成的最短作业
  66. for (int i = 0; i < n; i++) {
  67. if (!finish[i] && q[i].arrival <= time && q[i].burst < min_burst) {
  68. min_burst = q[i].burst;
  69. idx = i;
  70. }
  71. }
  72. if (idx == -1) { // 没有进程到达,时间推进
  73. time++;
  74. continue;
  75. }
  76. time += q[idx].burst;
  77. q[idx].completion = time;
  78. q[idx].turnaround = q[idx].completion - q[idx].arrival;
  79. q[idx].w_turnaround = (float)q[idx].turnaround / q[idx].burst;
  80. finish[idx] = 1;
  81. completed++;
  82. }
  83. print_result(q, n, "SJF(非抢占)");
  84. }
  85.  
  86. // 时间片轮转(RR),时间片=2
  87. void rr(Process p[], int n, int quantum) {
  88. Process q[MAX_PROCESS];
  89. memcpy(q, p, sizeof(Process) * n);
  90. for (int i = 0; i < n; i++) q[i].remaining = q[i].burst;
  91.  
  92. int time = 0, completed = 0;
  93. int queue[MAX_PROCESS * 10], head = 0, tail = 0;
  94. int in_queue[MAX_PROCESS] = {0};
  95.  
  96. // 初始化:把到达时间<=0的进程加入队列
  97. for (int i = 0; i < n; i++) {
  98. if (q[i].arrival <= time && !in_queue[i]) {
  99. queue[tail++] = i;
  100. in_queue[i] = 1;
  101. }
  102. }
  103.  
  104. while (completed < n) {
  105. if (head == tail) { // 队列空,时间推进
  106. time++;
  107. // 检查新到达进程
  108. for (int i = 0; i < n; i++) {
  109. if (q[i].arrival <= time && !in_queue[i]) {
  110. queue[tail++] = i;
  111. in_queue[i] = 1;
  112. }
  113. }
  114. continue;
  115. }
  116. int idx = queue[head++];
  117. int exec = (q[idx].remaining < quantum) ? q[idx].remaining : quantum;
  118. time += exec;
  119. q[idx].remaining -= exec;
  120.  
  121. // 执行期间检查新到达进程(模拟到达)
  122. for (int i = 0; i < n; i++) {
  123. if (q[i].arrival <= time && !in_queue[i] && i != idx) {
  124. queue[tail++] = i;
  125. in_queue[i] = 1;
  126. }
  127. }
  128.  
  129. if (q[idx].remaining == 0) {
  130. completed++;
  131. q[idx].completion = time;
  132. q[idx].turnaround = q[idx].completion - q[idx].arrival;
  133. q[idx].w_turnaround = (float)q[idx].turnaround / q[idx].burst;
  134. } else {
  135. // 未完成,重新入队
  136. queue[tail++] = idx;
  137. }
  138. }
  139. print_result(q, n, "RR(时间片=2)");
  140. }
  141.  
  142. int main() {
  143. // 示例进程:PID, 到达时间, 运行时间
  144. Process p[MAX_PROCESS] = {
  145. {1, 0, 5},
  146. {2, 1, 3},
  147. {3, 2, 8},
  148. {4, 3, 2},
  149. {5, 4, 4}
  150. };
  151. int n = 5;
  152.  
  153. printf("进程列表:\n");
  154. printf("PID\t到达\t运行\n");
  155. for (int i = 0; i < n; i++) {
  156. printf("%d\t%d\t%d\n", p[i].pid, p[i].arrival, p[i].burst);
  157. }
  158.  
  159. fcfs(p, n);
  160. sjf(p, n);
  161. rr(p, n, 2);
  162.  
  163. return 0;
  164. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
进程列表:
PID	到达	运行
1	0	5
2	1	3
3	2	8
4	3	2
5	4	4

========== FCFS ==========
PID	到达	运行	完成	周转	带权周转
1	0	5	5	5	1.00
2	1	3	8	7	2.33
3	2	8	16	14	1.75
4	3	2	18	15	7.50
5	4	4	22	18	4.50
平均周转时间:11.80
平均带权周转时间:3.42

========== SJF(非抢占) ==========
PID	到达	运行	完成	周转	带权周转
1	0	5	5	5	1.00
2	1	3	10	9	3.00
3	2	8	22	20	2.50
4	3	2	7	4	2.00
5	4	4	14	10	2.50
平均周转时间:9.60
平均带权周转时间:2.20

========== RR(时间片=2) ==========
PID	到达	运行	完成	周转	带权周转
1	0	5	16	16	3.20
2	1	3	13	12	4.00
3	2	8	22	20	2.50
4	3	2	10	7	3.50
5	4	4	18	14	3.50
平均周转时间:13.80
平均带权周转时间:3.34