【實驗目的】
①理解有關進程控制塊、進程隊列的概念。
②掌握進程優先權調度算法和時間片輪轉調度算法的處理邏輯。
【實驗內容】
①設計進程控制塊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是個無參的函數。