fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_PROCESS 10 // 最大进程数
  6.  
  7. // 进程状态枚举
  8. typedef enum {
  9. READY, // 就绪态
  10. RUNNING, // 运行态
  11. FINISHED // 完成态
  12. } ProcessState;
  13.  
  14. // 进程控制块 PCB
  15. typedef struct PCB {
  16. char name[20]; // 进程名
  17. int priority; // 优先数(数值越大优先级越高)
  18. int needTime; // 需要运行的时间
  19. int runTime; // 已运行时间
  20. ProcessState state; // 进程状态
  21. struct PCB *next; // 指向下一个进程的指针
  22. } PCB;
  23.  
  24. PCB *readyQueue = NULL; // 就绪队列头指针
  25. PCB *runningProcess = NULL; // 当前运行进程
  26.  
  27. // 创建新进程
  28. PCB* createProcess(char *name, int priority, int needTime) {
  29. PCB *p = (PCB*)malloc(sizeof(PCB));
  30. strcpy(p->name, name);
  31. p->priority = priority;
  32. p->needTime = needTime;
  33. p->runTime = 0;
  34. p->state = READY;
  35. p->next = NULL;
  36. return p;
  37. }
  38.  
  39. // 按优先数降序插入就绪队列(优先数高的在前)
  40. void insertByPriority(PCB *newProcess) {
  41. if (readyQueue == NULL) {
  42. readyQueue = newProcess;
  43. return;
  44. }
  45.  
  46. // 如果新进程优先级比队首还高
  47. if (newProcess->priority > readyQueue->priority) {
  48. newProcess->next = readyQueue;
  49. readyQueue = newProcess;
  50. return;
  51. }
  52.  
  53. // 找到合适位置插入
  54. PCB *current = readyQueue;
  55. while (current->next != NULL && current->next->priority >= newProcess->priority) {
  56. current = current->next;
  57. }
  58. newProcess->next = current->next;
  59. current->next = newProcess;
  60. }
  61.  
  62. // 从就绪队列取出优先级最高的进程
  63. PCB* getHighestPriority() {
  64. if (readyQueue == NULL) return NULL;
  65. PCB *p = readyQueue;
  66. readyQueue = readyQueue->next;
  67. p->next = NULL;
  68. return p;
  69. }
  70.  
  71. // 显示当前所有进程状态
  72. void displayProcesses() {
  73. printf("\n========== 当前进程状态 ==========\n");
  74.  
  75. // 显示运行态进程
  76. if (runningProcess != NULL) {
  77. printf("[运行态] 进程名: %s | 优先数: %d | 还需时间: %d | 已运行: %d\n",
  78. runningProcess->name, runningProcess->priority,
  79. runningProcess->needTime - runningProcess->runTime,
  80. runningProcess->runTime);
  81. }
  82.  
  83. // 显示就绪队列
  84. printf("\n【就绪队列】\n");
  85. if (readyQueue == NULL) {
  86. printf(" (空)\n");
  87. } else {
  88. PCB *p = readyQueue;
  89. int count = 1;
  90. while (p != NULL) {
  91. printf(" [%d] 进程名: %s | 优先数: %d | 还需时间: %d | 状态: 就绪\n",
  92. count++, p->name, p->priority,
  93. p->needTime - p->runTime);
  94. p = p->next;
  95. }
  96. }
  97. printf("==================================\n");
  98. }
  99.  
  100. // 调度算法(动态优先数:每运行一次优先数-1)
  101. void schedule() {
  102. int time = 0;
  103.  
  104. printf("\n********** 开始进程调度 **********\n");
  105.  
  106. while (readyQueue != NULL || runningProcess != NULL) {
  107. printf("\n>>> 时间片 %d <<<\n", time++);
  108.  
  109. // 如果当前没有运行进程,从就绪队列取一个
  110. if (runningProcess == NULL) {
  111. runningProcess = getHighestPriority();
  112. if (runningProcess != NULL) {
  113. runningProcess->state = RUNNING;
  114. printf("调度进程 [%s] 进入运行态\n", runningProcess->name);
  115. }
  116. }
  117.  
  118. // 运行当前进程一个时间片
  119. if (runningProcess != NULL) {
  120. runningProcess->runTime++;
  121. printf("进程 [%s] 运行中... (已运行: %d/%d)\n",
  122. runningProcess->name,
  123. runningProcess->runTime,
  124. runningProcess->needTime);
  125.  
  126. // 检查是否完成
  127. if (runningProcess->runTime >= runningProcess->needTime) {
  128. runningProcess->state = FINISHED;
  129. printf("✓ 进程 [%s] 运行完成!\n", runningProcess->name);
  130. free(runningProcess);
  131. runningProcess = NULL;
  132. } else {
  133. // 动态优先数调整:优先数减1
  134. runningProcess->priority--;
  135. printf("进程 [%s] 优先数降为 %d\n",
  136. runningProcess->name, runningProcess->priority);
  137.  
  138. // 时间片用完,放回就绪队列
  139. runningProcess->state = READY;
  140. PCB *temp = runningProcess;
  141. runningProcess = NULL;
  142. insertByPriority(temp);
  143. }
  144. }
  145.  
  146. displayProcesses();
  147. }
  148.  
  149. printf("\n********** 所有进程调度完成 **********\n");
  150. }
  151.  
  152. int main() {
  153. int n, i;
  154. char name[20];
  155. int priority, needTime;
  156.  
  157. printf("===== 最高优先数优先调度算法 =====\n");
  158. printf("请输入进程数量: ");
  159. scanf("%d", &n);
  160.  
  161. for (i = 0; i < n; i++) {
  162. printf("\n--- 输入第 %d 个进程信息 ---\n", i + 1);
  163. printf("进程名: ");
  164. scanf("%s", name);
  165. printf("优先数(数值越大优先级越高): ");
  166. scanf("%d", &priority);
  167. printf("需要运行时间: ");
  168. scanf("%d", &needTime);
  169.  
  170. PCB *p = createProcess(name, priority, needTime);
  171. insertByPriority(p);
  172. }
  173.  
  174. printf("\n初始就绪队列:");
  175. displayProcesses();
  176.  
  177. // 开始调度
  178. schedule();
  179.  
  180. return 0;
  181. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
===== 最高优先数优先调度算法 =====
请输入进程数量: 
初始就绪队列:
========== 当前进程状态 ==========

【就绪队列】
  (空)
==================================

********** 开始进程调度 **********

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