├── README.md ├── head.h ├── makefile ├── process └── process.c /README.md: -------------------------------------------------------------------------------- 1 | # ProcessScheduling 2 | 进程调度算法的C语言实现:先到先服务、短作业优先、优先级调度、时间片轮转 3 | -------------------------------------------------------------------------------- /head.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | typedef struct PCB { 4 | char name; //进程名 5 | int ArrivalTime; //到达时间 6 | int ServiceTime; //服务时间 7 | int StartTime; //开始时间 8 | int FinishTime; //完成时间 9 | int ResidualTime; //剩余服务时间 10 | double WholeTime; //周转时间 11 | double weightWholeTime; //带权周转时间 12 | int Priority; //优先级 13 | }PCB; 14 | typedef PCB DataType; 15 | typedef struct Node { 16 | DataType data; 17 | struct Node *next; 18 | }Node; 19 | typedef struct Queue { 20 | Node *front; 21 | Node *rear; 22 | size_t size; //队列中有几个元素 23 | }Queue; 24 | 25 | 26 | typedef struct linkList { 27 | Node *head; 28 | Node *tail; 29 | size_t size; 30 | }linkList; 31 | 32 | void QueueInit(Queue *pqueue) { //初始化 33 | assert(pqueue != NULL); 34 | pqueue->front = NULL; 35 | pqueue->rear = NULL; 36 | pqueue->size = 0; 37 | } 38 | 39 | void QueueDestroy(Queue *pqueue) 40 | { 41 | Node *cur = NULL; 42 | Node *del = NULL; 43 | assert(pqueue != NULL); 44 | if (pqueue->front == NULL) { 45 | return; 46 | } 47 | cur = pqueue->front; 48 | while (cur != NULL) { 49 | del = cur; 50 | cur = cur->next; 51 | free(del); 52 | del = NULL; 53 | } 54 | } 55 | 56 | Node *CreatNode(DataType data) 57 | { 58 | Node *ret = (Node *)malloc(sizeof(Node)); 59 | if (ret == NULL) { 60 | exit(0); 61 | } 62 | ret->data = data; 63 | ret->next = NULL; 64 | return ret; 65 | } 66 | 67 | void QueuePush(Queue *pqueue, DataType data) 68 | { 69 | assert(pqueue != NULL); 70 | Node *temp = CreatNode(data); 71 | if (pqueue->front == NULL) { 72 | pqueue->front = temp; 73 | pqueue->rear = temp; 74 | //pqueue->rear = pqueue->front = temp; 75 | ++pqueue->size; 76 | } 77 | else { 78 | pqueue->rear->next = temp; 79 | pqueue->rear = pqueue->rear->next; 80 | //pqueue->rear = temp; 81 | ++pqueue->size; 82 | } 83 | } 84 | 85 | void QueuePop(Queue *pqueue) 86 | { 87 | assert(pqueue != NULL); 88 | Node *del = NULL; 89 | assert(pqueue != NULL); 90 | if (pqueue->front == NULL) { 91 | return; 92 | } 93 | del = pqueue->front; 94 | pqueue->front = pqueue->front->next; 95 | free(del); 96 | del = NULL; 97 | ++pqueue->size; 98 | 99 | } 100 | 101 | Node *QueueTop(Queue *pqueue) 102 | { 103 | return pqueue->front; 104 | } 105 | 106 | 107 | bool QueueEmpty(const Queue *pqueue) 108 | { 109 | return pqueue->front == NULL; 110 | } 111 | 112 | size_t QueueSize(const Queue *pqueue) 113 | { 114 | return pqueue->size; 115 | } 116 | 117 | 118 | 119 | 120 | void linkListInitialize(linkList *plinklist) 121 | { 122 | assert(plinklist != NULL); 123 | plinklist->head = NULL; 124 | plinklist->tail = NULL; 125 | plinklist->size = 0; 126 | 127 | } 128 | 129 | void linkListDestroy(linkList *plinklist) 130 | { 131 | Node *cur = NULL; 132 | Node *del = NULL; 133 | assert(plinklist != NULL); 134 | if (plinklist->head == NULL) { 135 | return; 136 | } 137 | cur = plinklist->head; 138 | while (cur != NULL) { 139 | del = cur; 140 | cur = cur->next; 141 | free(del); 142 | del = NULL; 143 | } 144 | } 145 | 146 | void linkListPushBack(linkList *plinklist, DataType data) 147 | { 148 | assert(plinklist != NULL); 149 | Node *temp = CreatNode(data); 150 | if (plinklist->head == NULL) { 151 | plinklist->head = temp; 152 | plinklist->tail = temp; 153 | //pqueue->rear = pqueue->front = temp; 154 | ++plinklist->size; 155 | } 156 | else { 157 | plinklist->tail->next = temp; 158 | plinklist->tail = plinklist->tail->next; 159 | //pqueue->rear = temp; 160 | ++plinklist->size; 161 | } 162 | } 163 | 164 | void linkListErase(linkList *plinklist, Node *pnode) 165 | { 166 | Node *cur = NULL; 167 | assert(plinklist != NULL); 168 | cur = plinklist->head; 169 | if ((plinklist->head) == NULL) { 170 | return; 171 | } 172 | if ((plinklist->head) == pnode) { 173 | plinklist->head = pnode->next; 174 | free(pnode); 175 | pnode = NULL; 176 | --(plinklist->size); 177 | return; 178 | } 179 | while(cur != NULL) { 180 | if (cur->next == pnode) { 181 | cur->next = pnode->next; 182 | free(pnode); 183 | pnode = NULL; 184 | --(plinklist->size); 185 | break; 186 | } 187 | cur = cur->next; 188 | } 189 | } 190 | 191 | bool linkListEmpty(const linkList *plinkList) 192 | { 193 | return plinkList->head == NULL; 194 | } 195 | 196 | size_t linkListSize(const linkList *plinklist) 197 | { 198 | return plinklist->size; 199 | } 200 | -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | process:process.c 2 | gcc -g $^ -o $@ 3 | -------------------------------------------------------------------------------- /process: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Mmmmmmi/ProcessScheduling/12c1fb35572e3f205419a8c03c38bbd7bf95f4a8/process -------------------------------------------------------------------------------- /process.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "head.h" 8 | 9 | void FCFS(Queue *pqueue, PCB pcbs[], int size) 10 | { 11 | int pcbnumber = size; 12 | int time = 0; 13 | // 14 | // |_______|_______|_______|_______|_______|_______|_______|_______|___ 15 | // 0 1 2 3 4 5 6 7 8 16 | 17 | while (1) { 18 | //等待进程 19 | for (int i = 0; i < size; i++) { 20 | if (pcbs[i].ArrivalTime == time) { 21 | QueuePush(pqueue, pcbs[i]); 22 | printf("PCB %c ARRIVAL TIME IS %d\n", pcbs[i].name, time); 23 | pcbnumber--; 24 | } 25 | } 26 | if (QueueEmpty(pqueue) && pcbnumber == 0) { 27 | printf("进程执行完毕!\n"); 28 | break; 29 | } 30 | // 31 | //显示时间 32 | // printf("time is %d \n", time); 33 | 34 | //执行进程 35 | if (!QueueEmpty(pqueue) && QueueTop(pqueue)->data.ResidualTime == 0) { 36 | printf("PCB %c FINISH TIME IS %d\n", QueueTop(pqueue)->data.name, time); 37 | QueuePop(pqueue); 38 | } 39 | 40 | if (!QueueEmpty(pqueue)) { 41 | QueueTop(pqueue)->data.ResidualTime--; 42 | } 43 | 44 | time++; 45 | // sleep(1); 46 | } 47 | } 48 | 49 | void RR(Queue *pqueue, PCB pcbs[], int size) 50 | { 51 | //这里是时间片的大小 52 | int q = 0; 53 | printf("please input q:"); 54 | scanf("%d", &q); 55 | //这个表示本次时间片q剩下多少 56 | int lessq = 0; 57 | int pcbnumber = size; 58 | //表示本进程还需要的服务时间 59 | int time = 0; 60 | // 61 | // |_______|_______|_______|_______|_______|_______|_______|_______|___ 62 | // 0 1 2 3 4 5 6 7 8 63 | 64 | while (1) { 65 | //等待进程 66 | for (int i = 0; i < size; i++) { 67 | if (pcbs[i].ArrivalTime == time) { 68 | QueuePush(pqueue, pcbs[i]); 69 | // printf("QueueTop(pqueue)->next == %p\n", QueueTop(pqueue)->next); 70 | printf("PCB %c ARRIVAL TIME IS %d\n", pcbs[i].name, time); 71 | pcbnumber--; 72 | // printf("pcbnumber == %d\n", pcbnumber); 73 | } 74 | } 75 | if (QueueEmpty(pqueue) && pcbnumber == 0) { 76 | printf("进程执行完毕!\n"); 77 | break; 78 | } 79 | // printf("time is %d \n", time); 80 | // 81 | // 82 | //执行进程 83 | if (lessq == 0) { 84 | if (!QueueEmpty(pqueue) && QueueTop(pqueue)->data.ResidualTime == 0) { 85 | printf("PCB %c FINISH TIME IS %d\n", QueueTop(pqueue)->data.name, time); 86 | QueuePop(pqueue); 87 | } 88 | else { 89 | if (!QueueEmpty(pqueue) && 90 | (QueueTop(pqueue)->data.ServiceTime != 91 | QueueTop(pqueue)->data.ResidualTime)) { 92 | pqueue->rear->next = pqueue->front; 93 | pqueue->rear = pqueue->rear->next; 94 | pqueue->front = pqueue->front->next; 95 | pqueue->rear->next = NULL; 96 | } 97 | } 98 | lessq = q; 99 | }else { 100 | if (!QueueEmpty(pqueue) && QueueTop(pqueue)->data.ResidualTime == 0) { 101 | printf("PCB %c FINISH TIME IS %d\n", QueueTop(pqueue)->data.name, time); 102 | QueuePop(pqueue); 103 | lessq = q; 104 | } 105 | } 106 | if (!QueueEmpty(pqueue)) { 107 | QueueTop(pqueue)->data.ResidualTime--; 108 | } 109 | lessq--; 110 | time++; 111 | //sleep(1); 112 | } 113 | } 114 | 115 | 116 | void SPF(linkList *plinklist, PCB pcbs[], int size) 117 | { 118 | 119 | int pcbid = 0; 120 | int pcbnumber = size; 121 | int time = 0; 122 | Node *curnode = NULL; 123 | Node *minnode = NULL; 124 | // 125 | // |_______|_______|_______|_______|_______|_______|_______|_______|___ 126 | // 0 1 2 3 4 5 6 7 8 127 | while (1) { 128 | //等待进程 129 | for (int i = 0; i < size; i++) { 130 | if (pcbs[i].ArrivalTime == time) { 131 | linkListPushBack(plinklist, pcbs[i]); 132 | // printf("QueueTop(pqueue)->next == %p\n", QueueTop(pqueue)->next); 133 | printf("PCB %c ARRIVAL TIME IS %d\n", pcbs[i].name, time); 134 | pcbnumber--; 135 | // printf("pcbnumber == %d\n", pcbnumber); 136 | } 137 | } 138 | 139 | if (linkListEmpty(plinklist) && pcbnumber == 0) { 140 | printf("进程执行完毕!\n"); 141 | break; 142 | } 143 | // printf("time is %d \n", time); 144 | // 145 | //执行进程 146 | // 147 | if (minnode != NULL && (minnode->data).ResidualTime == 0) { 148 | printf("PCB %c FINISH TIME IS %d\n", (minnode->data).name, time); 149 | linkListErase(plinklist, minnode); 150 | minnode = NULL; 151 | } 152 | 153 | if (!linkListEmpty(plinklist) && minnode == NULL) { 154 | minnode = NULL; 155 | curnode = plinklist->head; 156 | while(curnode != NULL) { 157 | if (minnode == NULL || curnode->data.ServiceTime < minnode->data.ServiceTime) { 158 | minnode = curnode; 159 | } 160 | curnode = curnode->next; 161 | } 162 | } 163 | 164 | 165 | if (minnode != NULL) { 166 | --(minnode->data).ResidualTime; 167 | } 168 | 169 | time++; 170 | // sleep(1); 171 | } 172 | } 173 | 174 | 175 | void PSA(linkList *plinklist, PCB pcbs[], int size) 176 | { 177 | 178 | int pcbid = 0; 179 | int pcbnumber = size; 180 | int time = 0; 181 | Node *curnode = NULL; 182 | Node *minnode = NULL; 183 | // 184 | // |_______|_______|_______|_______|_______|_______|_______|_______|___ 185 | // 0 1 2 3 4 5 6 7 8 186 | while (1) { 187 | //等待进程 188 | for (int i = 0; i < size; i++) { 189 | if (pcbs[i].ArrivalTime == time) { 190 | linkListPushBack(plinklist, pcbs[i]); 191 | // printf("QueueTop(pqueue)->next == %p\n", QueueTop(pqueue)->next); 192 | printf("PCB %c ARRIVAL TIME IS %d\n", pcbs[i].name, time); 193 | pcbnumber--; 194 | // printf("pcbnumber == %d\n", pcbnumber); 195 | } 196 | } 197 | 198 | if (linkListEmpty(plinklist) && pcbnumber == 0) { 199 | printf("进程执行完毕!\n"); 200 | break; 201 | } 202 | // printf("time is %d \n", time); 203 | // 204 | //执行进程 205 | // 206 | if (minnode != NULL && (minnode->data).ResidualTime == 0) { 207 | printf("PCB %c FINISH TIME IS %d\n", (minnode->data).name, time); 208 | linkListErase(plinklist, minnode); 209 | minnode = NULL; 210 | } 211 | 212 | if (!linkListEmpty(plinklist) && minnode == NULL) { 213 | minnode = NULL; 214 | curnode = plinklist->head; 215 | while(curnode != NULL) { 216 | if (minnode == NULL || curnode->data.Priority < minnode->data.Priority) { 217 | minnode = curnode; 218 | } 219 | curnode = curnode->next; 220 | } 221 | } 222 | 223 | 224 | if (minnode != NULL) { 225 | --(minnode->data).ResidualTime; 226 | } 227 | 228 | time++; 229 | // sleep(1); 230 | } 231 | } 232 | 233 | 234 | void DPSA(linkList *plinklist, PCB pcbs[], int size) 235 | { 236 | 237 | int pcbid = 0; 238 | int pcbnumber = size; 239 | int time = 0; 240 | Node *curnode = NULL; 241 | Node *minnode = NULL; 242 | // 243 | // |_______|_______|_______|_______|_______|_______|_______|_______|___ 244 | // 0 1 2 3 4 5 6 7 8 245 | while (1) { 246 | //等待进程 247 | for (int i = 0; i < size; i++) { 248 | if (pcbs[i].ArrivalTime == time) { 249 | linkListPushBack(plinklist, pcbs[i]); 250 | // printf("QueueTop(pqueue)->next == %p\n", QueueTop(pqueue)->next); 251 | printf("PCB %c ARRIVAL TIME IS %d\n", pcbs[i].name, time); 252 | pcbnumber--; 253 | // printf("pcbnumber == %d\n", pcbnumber); 254 | } 255 | } 256 | 257 | if (linkListEmpty(plinklist) && pcbnumber == 0) { 258 | printf("进程执行完毕!\n"); 259 | break; 260 | } 261 | // printf("\n\ntime is %d \n", time); 262 | // 263 | //执行进程 264 | // 265 | if (minnode != NULL && (minnode->data).ResidualTime == 0) { 266 | printf("PCB %c FINISH TIME IS %d\n", (minnode->data).name, time); 267 | linkListErase(plinklist, minnode); 268 | minnode = NULL; 269 | } 270 | 271 | if (!linkListEmpty(plinklist)) { 272 | minnode = NULL; 273 | curnode = plinklist->head; 274 | while(curnode != NULL) { 275 | if (minnode == NULL || curnode->data.Priority < minnode->data.Priority) { 276 | minnode = curnode; 277 | } 278 | curnode = curnode->next; 279 | } 280 | } 281 | 282 | 283 | if (minnode != NULL) { 284 | --(minnode->data).ResidualTime; 285 | ++(minnode->data.Priority); 286 | printf("PCB %c IS DO\n", (minnode->data).name); 287 | } 288 | curnode = plinklist->head; 289 | while(curnode != NULL) { 290 | if (curnode == minnode) { 291 | printf("%c priority == %d ", curnode->data.name , curnode->data.Priority); 292 | curnode = curnode->next; 293 | continue; 294 | } 295 | (curnode->data.Priority) = (curnode->data.Priority - 1) > 0 ? (curnode->data.Priority) - 1 : 0; 296 | printf("%c priority == %d ", curnode->data.name , curnode->data.Priority); 297 | curnode = curnode->next; 298 | } 299 | printf("\n"); 300 | time++; 301 | // sleep(1); 302 | } 303 | } 304 | 305 | 306 | int main() 307 | { 308 | int processnumber = 0; 309 | Queue queue; 310 | linkList linklist; 311 | // char name; 312 | // int ArrivalTime; //到达时间 313 | // int ServiceTime; //服务时间 314 | // int StartTime; //开始时间 315 | // int FinishTime; //完成时间 316 | // int ResidualTime; //剩余服务时间 317 | // double WholeTime; //周转时间 318 | // double weightWholeTime; //带权周转时间 319 | // int Priority; //优先级 320 | // PCB pcbs[] = {p1}; 321 | printf("please input process number: "); 322 | scanf("%d", &processnumber); 323 | PCB *pcbs = (PCB *) malloc (sizeof(PCB) * processnumber); 324 | memset(pcbs, 0, sizeof(PCB) * processnumber); 325 | for (int i = 0; i < processnumber; i++) { 326 | printf("process->name: "); 327 | //这里有坑 : 328 | // 可以用 scanf(" %c") // %c 前面加一个空格 329 | // 或者在上一行加一个getchar() 330 | // getchar(); 331 | scanf(" %c",&(pcbs[i].name)); 332 | printf("process->ArricalTime: "); 333 | scanf("%d",&(pcbs[i].ArrivalTime)); 334 | printf("process->ServiceTime: "); 335 | scanf("%d",&(pcbs[i].ServiceTime)); 336 | pcbs[i].ResidualTime = pcbs[i].ServiceTime; 337 | printf("process->Priority: "); 338 | scanf("%d",&(pcbs[i].Priority)); 339 | } 340 | int input = 0; 341 | QueueInit(&queue); 342 | linkListInitialize(&linklist); 343 | printf("1. FCFS 2.RR 3.SPF 4.PSA 5.DPSA\n"); 344 | printf("Please input number: "); 345 | scanf("%d", &input); 346 | switch (input) 347 | { 348 | case 1: 349 | FCFS(&queue, pcbs, processnumber); 350 | break; 351 | case 2: 352 | RR(&queue, pcbs, processnumber); 353 | break; 354 | case 3: 355 | SPF(&linklist, pcbs, processnumber); 356 | break; 357 | case 4: 358 | PSA(&linklist, pcbs, processnumber); 359 | break; 360 | case 5: 361 | DPSA(&linklist, pcbs, processnumber); 362 | break; 363 | default: 364 | break; 365 | } 366 | linkListDestroy(&linklist); 367 | QueueDestroy(&queue); 368 | return 0; 369 | } 370 | --------------------------------------------------------------------------------