實驗3-進程調度_第1頁
實驗3-進程調度_第2頁
實驗3-進程調度_第3頁
實驗3-進程調度_第4頁
實驗3-進程調度_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

實驗三進程調度1.目的和要求進程調度是處理機管理的核心內容。本實驗要求用C語言編寫和調試一個簡單的進程調度程序。通過本實驗可以加深理解有關進程控制塊、進程隊列的概念,并體會和了解優(yōu)先數(shù)和時間片輪轉調度算法的具體實施方法。2.實驗內容①設計進程控制塊PCB表結構,分別適用于優(yōu)先數(shù)調度算法和循環(huán)輪轉調度算法。②建立進程就緒隊列。對兩種不同算法編制入鏈子程序。③編制兩種進程調度算法:1〕優(yōu)先數(shù)調度;2〕循環(huán)輪轉調度3.實驗環(huán)境①PC兼容機②Windows、DOS系統(tǒng)、Turboc2.0③C語言4.實驗提示①本程序用兩種算法對五個進程進行調度,每個進程可有三個狀態(tài),并假設初始狀態(tài)為就緒狀態(tài)。②為了便于處理,程序中的某進程運行時間以時間片為單位計算。各進程的優(yōu)先數(shù)或輪轉時間數(shù)以及進程需運行的時間片數(shù)的初始值均由用戶給定。③在優(yōu)先數(shù)算法中,優(yōu)先數(shù)可以先取值為50-進程執(zhí)行時間,進程每執(zhí)行一次,優(yōu)先數(shù)減3,CPU時間片數(shù)加1,進程還需要的時間片數(shù)減1。在輪轉算法中,采用固定時間片〔即:每執(zhí)行一次進程,該進程的執(zhí)行時間片數(shù)為已執(zhí)行了2個單位〕,這時,CPU時間片數(shù)加2,進程還需要的時間片數(shù)減2,并排列到就緒隊列的尾上。④對于遇到優(yōu)先數(shù)一致的情況,采用FIFO策略解決。5.實驗程序#include<stdio.h>#include<dos.h>#include<stdlib.h>#include<conio.h>#include<io.h>#defineP_NUM3#defineP_TIME50enumstate{ ready, execute, block, finish};structpcb{ charname[4]; intpriority; intcputime; intneedtime; intcount; intround; enumstateprocess; structpcb*next;};structpcb*get_process();structpcb*get_process(){ structpcb*q; structpcb*t; structpcb*p; inti=0; printf("inputnameandneedtime,pleaseinput3processes\n"); while(i<P_NUM){ q=(structpcb*)malloc(sizeof(structpcb)); scanf("%s",&(q->name)); scanf("%d",&(q->needtime)); q->cputime=0; q->priority=P_TIME-q->needtime; q->process=ready; q->next=NULL; if(i==0){ p=q; t=q; } else{ t->next=q; t=q; } i++; }/*while*/ returnp;}voiddisplay(structpcb*p){ printf("namecputimeneedtimeprioritystate\n"); while(p){ printf("%s",p->name); printf(""); printf("%d",p->cputime); printf(""); printf("%d",p->needtime); printf(""); printf("%d",p->priority); printf(""); switch(p->process){ caseready: printf("ready\n"); break; caseexecute: printf("execute\n"); break; caseblock: printf("block\n"); break; casefinish: printf("finish\n"); break; } p=p->next; }}intprocess_finish(structpcb*q){ intbl=1; while(bl&&q){ bl=bl&&q->needtime==0; q=q->next; } returnbl;}voidcpuexe(structpcb*q){ structpcb*t=q; intmax_priority=0; while(q){ if(q->process!=finish){ q->process=ready; if(q->needtime==0){ q->process=finish; } } if(max_priority<q->priority&&q->process!=finish){ max_priority=q->priority; t=q; } q=q->next; } if(t->needtime!=0){ t->priority-=3; t->needtime--; t->process=execute; t->cputime++; }}voidpriority_cal(){ structpcb*p; intcpu=0; p=get_process(); while(!process_finish(p)){ cpu++; printf("cputime:%d\n",cpu); cpuexe(p); display(p); sleep(5); } printf("Allprocesseshavefinished,pressanykeytoexit"); getch();}voiddisplay_menu(){ printf("CHOOSETHEALGORITHM:\n"); printf("1PRIORITY\n"); printf("2ROUNDROBIN\n"); printf("3EXIT\n");}structpcb*get_process_round(){ structpcb*q; structpcb*t; structpcb*p; inti=0; printf("inputnameandtime,pleaseinput3processes\n"); while(i<P_NUM){ q=(structpcb*)malloc(sizeof(structpcb)); scanf("%s",&(q->name)); scanf("%d",&(q->needtime)); q->cputime=0; q->round=0; q->count=0; q->process=ready; q->next=NULL; if(i==0){ p=q; t=q; } else{ t->next=q; t=q; } i++; }/*while*/ returnp;}voidcpu_round(structpcb*q){ q->cputime+=2; q->needtime-=2; if(q->needtime<0){ q->needtime=0; } q->count++; q->round++; q->process=execute;}structpcb*get_next(structpcb*k,structpcb*head){ structpcb*t; t=k; do{ t=t->next; } while(t&&t->process==finish); if(t==NULL){ t=head; while(t->next!=k&&t->process==finish){ t=t->next; } } returnt;}voidset_state(structpcb*p){ while(p){ if(p->needtime==0){ p->process=finish; } if(p->process==execute){ p->process=ready; } p=p->next; }}voiddisplay_round(structpcb*p){ printf("NAMECPUTIMENEEDTIMECOUNTROUNDSTATE\n"); while(p){ printf("%s",p->name); printf(""); printf("%d",p->cputime); printf(""); printf("%d",p->needtime); printf(""); printf("%d",p->count); printf(""); printf("%d",p->round); printf(""); switch(p->process){ caseready: printf("ready\n"); break; caseexecute: printf("execute\n"); break; casefinish: printf("finish\n"); break; } p=p->next; }}voidround_cal(){ structpcb*p; structpcb*r; intcpu=0; p=get_process_round(); r=p; while(!process_finish(p)){ cpu+=2; cpu_round(r); r=get_next(r,p); printf("cpu%d\n",cpu); display_round(p); set_state(p); sleep(5); }printf("Allprocesseshavefinished,pressanykeytoexit"); getch();}/*主程序*/voidmain(){ intuser_input; display_menu(); scanf("%d",&user_input); switch(user_input){ case1:priority_cal();break; case2:round_cal();break;

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論