【操作系统】进程优先权调度算法
【实验目的】
①理解有关进程控制块、进程队列的概念。
②掌握进程优先权调度算法和时间片轮转调度算法的处理逻辑。
【实验内容】
①设计进程控制块PCB的结构,分别适用于优先权调度算法和时间片轮转调度算法。
②建立进程就绪队列。
③编制两种进程调度算法:优先权调度和时间片轮转调度。
【实验步骤】
1.1 数据结构Struct pcb
{
int nam;//进程名
int priority;//优先权
int cputime;//CPU运行时间
int needtime;//进程运行所需时间
int count;//进程执行次数
int round;//时间片轮转轮次
state process;//进程状态
pcb*next;
};
1.2 程序流程图
1.3 实验代码
1 #include<stdio.h> 2 3 #include <dos.h> 4 5 #include<stdlib.h> 6 7 #include<conio.h> 8 9 #include<iostream> 10 11 #define P_NUM 5 12 13 #define P_TIME 50 14 15 enum state{ 16 17 ready, 18 19 execute, 20 21 block, 22 23 finish 24 25 }; 26 27 28 29 struct pcb 30 31 { 32 33 int name; 34 35 int priority; 36 37 int cputime; 38 39 int needtime; 40 41 int count; 42 43 int round; 44 45 state process; 46 47 pcb * next; 48 49 }; 50 51 52 53 pcb * get_process() 54 55 { 56 57 pcb *q; 58 59 pcb *t; 60 61 pcb *p; 62 63 int i=0; 64 65 printf("input name and time\n"); 66 67 while (i<P_NUM) 68 69 { 70 71 q=(struct pcb *)malloc(sizeof(pcb)); 72 73 scanf("%d",&q->name); 74 75 scanf("%d",&q->needtime); 76 77 q->cputime=0; 78 79 q->priority=P_TIME-q->needtime; 80 81 q->process=ready; 82 83 q->next=NULL; 84 85 if (i==0){ 86 87 p=q; 88 89 t=q; 90 91 } 92 93 else{ 94 95 t->next=q; 96 97 t=q; 98 99 } 100 101 i++; 102 103 } //while 104 105 return p; 106 107 } 108 109 110 111 void display(pcb *p) 112 113 { 114 115 printf("name\t"); 116 117 printf("cputime\t"); 118 119 printf("needtime\t"); 120 121 printf("priority\t"); 122 123 printf("state\n"); 124 125 while(p) 126 127 { 128 129 printf("%d\t",p->name); 130 131 printf("%d\t",p->cputime); 132 133 printf("%d\t\t",p->needtime); 134 135 printf("%d\t\t",p->priority); 136 137 switch(p->process){ 138 139 case ready:printf("ready\n");break; 140 141 case execute:printf("execute\n");break; 142 143 case block:printf("block\n");break; 144 145 case finish:printf("finish\n");break; 146 147 } 148 149 p=p->next; 150 151 } 152 153 } 154 155 156 157 int process_finish(pcb *q) 158 159 { 160 161 int bl=1; 162 163 while(bl&&q) 164 165 { 166 167 bl=bl&&q->needtime==0; 168 169 q=q->next; 170 171 } 172 173 return bl; 174 175 } 176 177 178 179 void cpuexe(pcb *q) 180 181 { 182 183 pcb *t=q; 184 185 int tp=0; 186 187 while(q){ 188 189 if (q->process!=finish) 190 191 { 192 193 q->process=ready; 194 195 if(q->needtime==0) 196 197 { 198 199 q->process=finish; 200 201 } 202 203 } 204 205 if(tp<q->priority&&q->process!=finish) 206 207 { 208 209 tp=q->priority; 210 211 t=q; 212 213 } 214 215 q=q->next; 216 217 } 218 219 if(t->needtime!=0) 220 221 { 222 223 t->priority-=3; 224 225 t->needtime--; 226 227 t->process=execute; 228 229 t->cputime++; 230 231 } 232 233 } 234 235 236 237 void priority_cal() 238 239 { 240 241 pcb * p; 242 243 system("cls"); 244 245 p=get_process(); 246 247 int cpu=0; 248 249 system("cls"); 250 251 while(!process_finish(p)) 252 253 { 254 255 cpu++; 256 257 printf("\ncpu运行时间:%d\n",cpu); 258 259 cpuexe(p); 260 261 display(p); 262 263 } 264 265 printf("\nAll processes have finished,press any key to exit\n"); 266 267 getch(); 268 269 } 270 271 272 273 void display_menu() 274 275 { 276 277 printf("选择相应算法:\n"); 278 279 printf("1 优先级调度算法\n"); 280 281 printf("2 时间片轮转算法\n"); 282 283 printf("3 退出\n"); 284 285 } 286 287 288 289 pcb * get_process_round() 290 291 { 292 293 pcb *q; 294 295 pcb *t; 296 297 pcb *p; 298 299 int i=0; 300 301 printf("输入名字与时间\n"); 302 303 while (i<P_NUM) 304 305 { 306 307 q=(struct pcb *)malloc(sizeof(pcb)); 308 309 scanf("%d",&q->name); 310 311 scanf("%d",&q->needtime); 312 313 q->cputime=0; 314 315 q->round=0; 316 317 q->count=0; 318 319 q->process=ready; 320 321 q->next=NULL; 322 323 if (i==0){ 324 325 p=q; 326 327 t=q; 328 329 } 330 331 else{ 332 333 t->next=q; 334 335 t=q; 336 337 } 338 339 i++; 340 341 } //while 342 343 return p; 344 345 } 346 347 348 349 void cpu_round(pcb *q) 350 351 { 352 353 q->cputime+=2; 354 355 q->needtime-=2; 356 357 if(q->needtime<0) 358 359 { 360 361 q->needtime=0; 362 363 } 364 365 q->count++; 366 367 q->round++; 368 369 q->process=execute; 370 371 372 373 } 374 375 376 377 pcb * get_next(pcb * k,pcb * head) 378 379 { 380 381 pcb * t; 382 383 t=k; 384 385 do 386 387 { 388 389 t=t->next; 390 391 } 392 393 while (t && t->process==finish); 394 395 if(t==NULL) 396 397 { 398 399 t=head; 400 401 while (t->next!=k && t->process==finish) 402 403 { 404 405 t=t->next; 406 407 } 408 409 } 410 411 return t; 412 413 } 414 415 void set_state(pcb *p) 416 417 { 418 419 while(p) 420 421 { 422 423 if (p->needtime==0) 424 425 { 426 427 p->process=finish; 428 429 } 430 431 432 433 if (p->process==execute) 434 435 { 436 437 p->process=ready; 438 439 } 440 441 p=p->next; 442 443 } 444 445 } 446 447 448 449 void display_round(pcb *p) 450 451 { 452 453 printf("name\t"); 454 455 printf("cputime\t"); 456 457 printf("needtime\t"); 458 459 printf("priority\t"); 460 461 printf("state\n"); 462 463 while(p) 464 465 { 466 467 printf("%d\t",p->name); 468 469 printf("%d\t",p->cputime); 470 471 printf("%d\t\t",p->needtime); 472 473 printf("%d\t\t",p->priority); 474 475 switch(p->process) 476 477 { 478 479 case ready:printf("ready\n");break; 480 481 case execute:printf("execute\n");break; 482 483 case finish:printf("finish\n");break; 484 485 } 486 487 p=p->next; 488 489 } 490 491 } 492 493 494 495 void round_cal() 496 497 { 498 499 pcb * p; 500 501 pcb * r; 502 503 system("cls"); 504 505 p=get_process_round(); 506 507 int cpu=0; 508 509 system("cls"); 510 511 r=p; 512 513 while(!process_finish(p)) 514 515 { 516 517 cpu+=2; 518 519 cpu_round(r); 520 521 r=get_next(r,p); 522 523 printf("cpu:%d\n",cpu); 524 525 display_round(p); 526 527 set_state(p); 528 529 } 530 531 } 532 533 534 535 int main() 536 537 { 538 539 display_menu(); 540 541 int k; 542 543 scanf("%d",&k); 544 545 switch(k) 546 547 { 548 549 case 1:priority_cal();break; 550 551 case 2:round_cal();break; 552 553 case 3:break; 554 555 display_menu(); 556 557 scanf("%d",&k); 558 559 } 560 561 }
1.4 实验结果
优先级调度算法
时间片轮转算法
【实验结果分析】
1. 该实验利用进程调度中的优先级算法调度进程,开始给每一个进程设定一个优先级数,对于优先级高的进程先调度,优先级低的进程后调度。
2. 时间片轮转:划分成一个时间片,就绪队列中的进程轮流运行一个时间片。当时间片结束时,进程让出CPU,等待下一次调度。
【实验体会总结】
1. 通过这个实验学会了用优先级调度算法:从就绪队列中选择一个优先权最高的进程,让其获得CPU执行,非抢占式,就是会一直执行下去,抢占式:如果有个优先级高的,会执行这个,原来的被迫退出cpu。
2. 头文件要引用正确,合理,int 和void void是个无参的函数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)