操作系統(tǒng)五種進(jìn)程調(diào)度算法的代碼_第1頁
操作系統(tǒng)五種進(jìn)程調(diào)度算法的代碼_第2頁
操作系統(tǒng)五種進(jìn)程調(diào)度算法的代碼_第3頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、進(jìn)程調(diào)度算法的模擬實(shí)現(xiàn)實(shí)驗(yàn)?zāi)康? 本實(shí)驗(yàn)?zāi)M在單處理機(jī)情況下的處理機(jī)調(diào)度問題,加深對(duì)進(jìn)程調(diào)度的理解。2. 利用程序設(shè)計(jì)語言編寫算法,模擬實(shí)現(xiàn)先到先服務(wù)算法 FCFS輪轉(zhuǎn)調(diào)度算法RR最短作業(yè)優(yōu)先算法 SJF、優(yōu)先級(jí)調(diào)度算法 PRIOR最短剩余時(shí)間優(yōu)先算法SRTF3. 進(jìn)行算法評(píng)價(jià),計(jì)算平均等待時(shí)間和平均周轉(zhuǎn)時(shí)間。實(shí)驗(yàn)內(nèi)容及結(jié)果1.先來先服務(wù)算法QB C:V I 1 tlow isy ilc e J2Lmd cj e選擇調(diào)度算袪ji; FCFS 2:時(shí)間片輪換 tt輸人進(jìn)程個(gè)數(shù)=3;優(yōu)先級(jí)調(diào)度4:最短作業(yè)優(yōu)先5:最短乘愫時(shí)間優(yōu)先輸入此進(jìn)程時(shí)司片大小10輸人第1個(gè)述程的名字、P1 10 1時(shí)間和優(yōu)先

2、級(jí)I歸工個(gè)進(jìn)程的并I輸入第3個(gè)ii程的容宇、3 ID 3說明:在本序所列迸穆話申優(yōu)先級(jí)一頂期旨進(jìn)程運(yùn)行涪的優(yōu)先級(jí)*)進(jìn)程名宇共需占用CPU時(shí)間還需耍白用吋間優(yōu)先級(jí) 狀態(tài)PIP2P31010清按芒蔦錐繼純- 進(jìn)程P±己經(jīng)執(zhí)彳二完畢*P216roio*按任意越繼續(xù)-進(jìn)程P2已經(jīng)執(zhí)行完畢*請(qǐng)按任意犍雉續(xù)-019運(yùn)行102運(yùn)訐0P1P210 ia選擇諧度算法:、 i= fgfs a=時(shí)闔片輪按輸A.進(jìn)程亍敎:輸人此進(jìn)程時(shí)間片大小:盂AM 1個(gè)進(jìn)程的名字、1 1M 12 輪轉(zhuǎn)調(diào)度算法請(qǐng)按任意撻越絞-.進(jìn)程P3已經(jīng)執(zhí)行齊畢?請(qǐng)按任意犍雄績一一一.執(zhí)行完且IranI ndojw.ytFm J Xc

3、rrdzF3=優(yōu)先扱調(diào)度趴最短作業(yè)優(yōu)先箕最短剌余時(shí)間優(yōu)先cpu時(shí)間前優(yōu)黃;級(jí):強(qiáng)口時(shí)間利優(yōu)九及:還需要占用時(shí)間優(yōu)先級(jí)請(qǐng)捋任意錯(cuò)繼繕 - 請(qǐng)擰任宣.說明: 迅辟字請(qǐng)按任竟鎮(zhèn)紳續(xù).-.進(jìn)程P2已經(jīng)執(zhí)行完畢r請(qǐng)按任意鍵繼縱進(jìn)程pi已經(jīng)執(zhí)行気畢'綁畛行完畢r仕扣I呈序所列進(jìn)程信息4優(yōu)先級(jí)一壩是獵進(jìn)程運(yùn)懺右的優(yōu)先級(jí)花 共需占用CPU時(shí)間輸負(fù)第2個(gè)遺程的名字、2 1M 23.優(yōu)先級(jí)調(diào)度算法C:Wi ndo ssyste in3 尹 crrd,#xe3:優(yōu)先頷調(diào)度4:最短作業(yè)優(yōu)先5:最短余時(shí)間優(yōu)先-x輸人蛻進(jìn)程時(shí)間片大?。簜€(gè)進(jìn)程的名字、0輸入第11 IQ Z輸入第22 10 1個(gè)進(jìn)程的名寧、gjma

4、E寸間和尤先級(jí);輸入第33 10 3個(gè)進(jìn)程的名字、3U時(shí)間和先級(jí):<說明:在本耳呈序肝列進(jìn)程信忌中,先級(jí)一頂懇扌旨進(jìn)程運(yùn)行后的優(yōu)元級(jí)M >進(jìn)程容字共需占用叭時(shí)間還需要占用時(shí)間優(yōu)先級(jí)狀態(tài)1010e io10運(yùn)行請(qǐng)按任意鍵縫績一-P1P3P21Q10101018需待請(qǐng)按任意撻堆按.也p P口 土.Z1土已已fl運(yùn)行進(jìn)程P2己經(jīng)執(zhí)行芫毛!4.最短時(shí)間優(yōu)先算法S3 C:Windcws'.system32cmd.exe:讐磐騙間片輪換3=優(yōu)先級(jí)調(diào)度書最短作業(yè)優(yōu)先5:最思剩余時(shí)間優(yōu)先上1輸入進(jìn)程亍數(shù): 篇入此進(jìn)程時(shí)間片大小輸入第1個(gè)進(jìn)程的象時(shí)間和優(yōu)先級(jí):P1 20 2:輸入笫址個(gè)進(jìn)程的

5、容字、咧時(shí)間和優(yōu)先級(jí)=P2丄。丄t說明:在本程序孑列進(jìn)促信息中,優(yōu)弟及一項(xiàng)是指進(jìn)程運(yùn)行后的優(yōu)先縱M )1進(jìn)程名字共需占用時(shí)間還需要占用時(shí)間憂先級(jí)狀態(tài)P210idmiHfP1202&2零待請(qǐng)按任意鍵継績-進(jìn)程P2三經(jīng)執(zhí)行完畢»請(qǐng)按任意鍵維續(xù)-P120101運(yùn)行請(qǐng)校怪意鍵繼綾P120«運(yùn)行*按任意鍵継統(tǒng)進(jìn)程P1已經(jīng)執(zhí)行完畢!¥青按仕意鍵繼續(xù)5.最短剩余時(shí)間優(yōu)先算法C:V/ i 11 duvrt»ylern 3 2c me. ex uJ諳輸入計(jì)算機(jī)中的述程數(shù)目土 臉入笫丄-他程的到達(dá)時(shí)間用剩余執(zhí)行旳間:e 3輸人第龍個(gè)進(jìn)穢的到達(dá)時(shí)闔及剩余執(zhí)彳丁時(shí)環(huán)J1

6、 4陽入第仝個(gè)進(jìn)理的到達(dá)吋間圧劇余執(zhí)行時(shí)間:腳入笫研謹(jǐn)程的到達(dá)時(shí)間忌乘爍執(zhí)行時(shí)間=進(jìn)程按順庠運(yùn)廳依初為:12 4 13平均等待時(shí)間是;b .、爾0初卸平均周轉(zhuǎn)時(shí)間是匕13.60080i青按任意犍継蘆一一一實(shí)驗(yàn)總結(jié)在此次模擬過程中,將 SRTF單獨(dú)拿了出來用指針表示,而其余均用數(shù)組表示。完整代碼【Srtf.cpp代碼如下:】/最短剩余時(shí)間優(yōu)先算法的實(shí)現(xiàn)#include <stdio.h>#include <stdlib.h>#include <time.h>typedef structint remain_time;int arrive_time;int Tp

7、;int Tc;int To;int number;Process_Block;/進(jìn)程剩余執(zhí)行時(shí)間/進(jìn)程到達(dá)時(shí)間/進(jìn)入就緒隊(duì)列的時(shí)間/進(jìn)入執(zhí)行隊(duì)列的時(shí)間/進(jìn)程執(zhí)行結(jié)束的時(shí)間/進(jìn)程編號(hào)/定義進(jìn)程模塊typedef struct _QueueProcess_Block PB; struct _Queue *next;_Block,*Process;/定義一個(gè)進(jìn)程模塊隊(duì)列中結(jié)點(diǎn)typedef structProcess head;Process end;/隊(duì)列頭指針/隊(duì)列尾指針/ 進(jìn)程隊(duì)列Process_Queue;Process_Queue PQ; intt;ProcessRun_Now;/ 定義

8、一個(gè)全局隊(duì)列變量/ 全局時(shí)間/ 當(dāng)前正在運(yùn)行的進(jìn)程,作為全局變量void InitQueue(Process_Queue PQ)PQ.head ->next = NULL;PQ.end ->next = PQ.head;/* 初始化隊(duì)列 */int IsEmpty(Process_Queue PQ)if (PQ.end->next = PQ.head)return 1; / 隊(duì)列空的條件為頭指針指向尾指針并且尾指針指向頭指針 elsereturn 0;/* 判定隊(duì)列是否為空隊(duì)列 */void EnQueue(Process_Queue PQ,Process P)Process

9、 temp =(Process)malloc(sizeof (_Block);temp = PQ.end;temp->next->next = P;PQ.end->next = P;/* 插入隊(duì)列操作 */Process DeQueue(Process_Queue PQ)if (IsEmpty(PQ)return NULL;Process temp = PQ.head->next;PQ.head->next= temp ->next;if (PQ.end->next = temp)PQ.end->next = PQ.head;return tem

10、p;/* 出列操作 */Process ShortestProcess(Process_Queue PQ)if (IsEmpty(PQ) / 如果隊(duì)列為空,返回if (!Run_Now)return NULL;elsereturn Run_Now;Process temp,shortest,prev;int min_time;if (Run_Now)shortest = Run_Now;min_time = Run_Now->PB.remain_time;elseshortest = PQ.head->next;min_time = PQ.head->next->PB.

11、remain_time; temp = PQ.head; prev = temp;while (temp->next)if (temp->next->PB.remain_time <min_time)shortest = temp->next;min_time = shortest->PB.remain_time;prev=temp;temp=temp->next;if (shortest = PQ.end->next)PQ.end->next = prev;prev->next = shortest->next; return

12、 shortest;/* 調(diào)度最短剩余時(shí)間的進(jìn)程至隊(duì)頭 */void Run()Run_Now->PB.remain_time-;return ;/* 運(yùn)行函數(shù) */void Wait()return ;int sum( int array , int n)int i,sum=0;for (i=0;i<n;i+)sum+=array i;/ 如果當(dāng)前有進(jìn)程正在執(zhí)行,/ 那么最短進(jìn)程初始化為當(dāng)前正在執(zhí)行的進(jìn)程,/ 如果當(dāng)前沒有進(jìn)程執(zhí)行,/ 則最短進(jìn)程初始化為隊(duì)列中第一個(gè)進(jìn)程/ 如果當(dāng)前進(jìn)程的剩余時(shí)間比 min_time 短,/ 則保存當(dāng)前進(jìn)程,/ 及其前驅(qū)/ 如果最短剩余時(shí)間進(jìn)程是隊(duì)

13、列中最后一個(gè)進(jìn)程,/ 則需要修改尾指針指向其前驅(qū)/ 修改指針將最短剩余時(shí)間進(jìn)程插入到隊(duì)頭/ 某一時(shí)間運(yùn)行它的剩余時(shí)間減return sum;int main()PQ.head= (Process)malloc(sizeof (_Block);PQ.end= (Process)malloc(sizeof (_Block);Run_Now= (Process)malloc(sizeof (_Block);Run_Now=NULL;InitQueue(PQ);int i,N,Total_Time=0;printf( " 請(qǐng)輸入計(jì)算機(jī)中的進(jìn)程數(shù)目:n" );scanf( "

14、;%d",&N);Process *P,temp;P = (Process*)malloc(N* sizeof (Process);/Total_Time 為所有進(jìn)程的執(zhí)行時(shí)間之和int *wt,*circle_t;wt =(int *)malloc(N* sizeof ( int ); circle_t =(int *)malloc(N* sizeof (int );for (i=0;i<N;i+)Pi = (Process)malloc( sizeof (_Block);Pi->PB.number =i+1;Pi->next=NULL;wti=0;cir

15、cle_ti=0;printf("輸入第c個(gè)進(jìn)程的到達(dá)時(shí)間及剩余執(zhí)行時(shí)間:n" ,i+1);scanf( "%d %d",&Pi->PB.arrive_time,&Pi->PB.remain_time);for (i=0;i<N;i+)Total_Time+=Pi->PB.remain_time;printf( "n 進(jìn)程按順序運(yùn)行依次為 :n" );i=0;int k=0;for (t=0;t+)if (Run_Now)/ 如果當(dāng)前有進(jìn)程正在執(zhí)行Run();if (t = Pi->PB.

16、arrive_time)/ 如果當(dāng)前時(shí)間正好有進(jìn)程進(jìn)入if (Pi->PB.remain_time < Run_Now->PB.remain_time)temp = Pi;/ 則調(diào)度它至運(yùn)行隊(duì)列中,Pi = Run_Now; Run_Now = temp; Run_Now->PB.Tp=t;Run_Now->PB.Tc=t;wtRun_Now->PB.number-1+=Run_Now->PB.Tc-Run_Now->PB.Tp;printf( "%d " ,Run_Now->PB.number);EnQueue(PQ,

17、Pi); / 并將當(dāng)前運(yùn)行進(jìn)程重新插入隊(duì)列中 Pi->PB.Tp=t;k+;i=(i+1)>(N-1)?(N-1):(i+1);/ 如果當(dāng)前進(jìn)程運(yùn)行結(jié)束,/ 進(jìn)程運(yùn)行結(jié)束的時(shí)間if (Run_Now->PB.remain_time = 0)Run_Now->PB.To=t;circle_tRun_Now->PB.number-1 +=t-Run_Now->PB.arrive_time;free(Run_Now); / 則將它所占資源釋放掉,Run_Now =NULL;/ 并修改 Run_Nov為 NULLRun_Now = ShortestProcess(P

18、Q); / 從就緒隊(duì)列中調(diào)出最短剩余時(shí)間進(jìn)程至隊(duì)頭, if (!Run_Now)/ 如果隊(duì)列為空,轉(zhuǎn)為等待狀態(tài)if (IsEmpty(PQ) && k >= N) break ;Wait();continue ; elseRun_Now->PB.Tc=t; wtRun_Now->PB.number-1+=Run_Now->PB.Tc-Run_Now->PB.Tp; printf( "%d " ,Run_Now->PB.number);/ 如果當(dāng)前運(yùn)行進(jìn)程為空,那么/ 如果正好這時(shí)有進(jìn)程入隊(duì)/ 則直接被調(diào)入運(yùn)行隊(duì)列中else

19、if (t = Pi->PB.arrive_time)k+;EnQueue(PQ,Pi);Run_Now = DeQueue(PQ);Run_Now->PB.Tp=t;Run_Now->PB.Tc=t;printf( "%d " ,Run_Now->PB.number);i=(i+1)>(N-1)?(N-1):(i+1);elseWait();continue printf( "n" );printf( " 平均等待時(shí)間是 :n%fn" ,( float )sum(wt,N)/N);printf( &qu

20、ot; 平均周轉(zhuǎn)時(shí)間是 :n%fn" ,( float )sum(circle_t,N)/N); return 0;/ 【 Process.cpp 代碼如下:】 #include <iostream> #include <string> using namespace std;class Processpublic :string ProcessName; / 進(jìn)程名字intTime; /進(jìn)程需要時(shí)間intleval; /進(jìn)程優(yōu)先級(jí)intLeftTime;/ 進(jìn)程運(yùn)行一段時(shí)間后還需要的時(shí)間;void Copy ( Process procl, Process

21、proc2);/ 把proc2賦值給 proclvoid Sort( Process pr,int size) ;/ 此排序后按優(yōu)先級(jí)從大到小排列void sort1(Process pr,int size) ;/ 此排序后按需要的cpu時(shí)間從小到大排列void Fcfs( Process pr, int num, int Timepice); / 先來先服務(wù)算法void TimeTurn( Process process,intnum,int Timepice);/時(shí)間片輪轉(zhuǎn)算法void Priority( Process process,intnum,int Timepice);/優(yōu)先級(jí)算

22、法void main()int a;cout<<endl;cout<< " 選擇調(diào)度算法: "<<endl;cout<< " 1: FCFS 2:時(shí)間片輪換 3: 優(yōu)先級(jí)調(diào)度 4: 最短作業(yè)優(yōu)先 5: 最短剩余時(shí)間優(yōu)先 "<<endl;cin>>a;const int Size =30;Process processSize ;int num;int TimePice;cout<< " 輸入進(jìn)程個(gè)數(shù) :" <<endl;cin>>

23、;num;cout<< " 輸入此進(jìn)程時(shí)間片大小 : " <<endl;cin>>TimePice;for ( int i=0; i< num; i+)string name;int CpuTime;int Leval;cout<< " 輸入第 "<< i+1<< " 個(gè)進(jìn)程的名字、 cpu 時(shí)間和優(yōu)先級(jí) :" <<endl; cin>>name;cin>> CpuTime>>Leval; processi.P

24、rocessName =name;processi.Time =CpuTime; processi.leval =Leval;cout<<endl;for ( int k=0;k<num;k+)processk.LeftTime=processk.Time ; / 對(duì)進(jìn)程剩余時(shí)間初始化cout<< " ( 說明 : 在本程序所列進(jìn)程信息中,優(yōu)先級(jí)一項(xiàng)是指進(jìn)程運(yùn)行后的優(yōu)先級(jí) ! )" ; cout<<endl; cout<<endl;coutvv "進(jìn)程名字"<<"共需占用CP時(shí)間

25、" <<"還需要占用時(shí)間" <<"優(yōu)先級(jí)"<< 狀態(tài)"vvendl; if (a=1)Fcfs(process,num,TimePice);else if (a=2)TimeTurn( process, num, TimePice);else if (a=3)Sort( process, num);Priority( process , num, TimePice);else / 最短作業(yè)算法,先按時(shí)間從小到大排序,再調(diào)用 Fcfs 算法即可sort1(process,num); Fcfs(proc

26、ess,num,TimePice);void Copy ( Process proc1, Process proc2)proc1.leval =proc2.leval ; proc1.ProcessName =proc2.ProcessName ; proc1.Time =proc2.Time ;void Sort( Process pr, int size) / 以進(jìn)程優(yōu)先級(jí)高低排序/ 直接插入排序for ( int i=1;i<size;i+)Process temp;temp = pri;int j=i;while (j>0 && temp.leval<

27、prj-1.leval)prj = prj-1;j-;prj = temp; / 直接插入排序后進(jìn)程按優(yōu)先級(jí)從小到大排列for ( int d=size-1;d>size/2;d-)Process temp;temp=pr d;pr d = pr size-d-1;pr size-d-1=temp; / 此排序后按優(yōu)先級(jí)從大到小排列/* 最短作業(yè)優(yōu)先算法的實(shí)現(xiàn) */void sort1 ( Process pr, int size) / 以進(jìn)程時(shí)間從低到高排序 / 直接插入排序for ( int i=1;i<size;i+)Process temp;temp = pri;int j

28、=i;while (j>0 && temp.Time < prj-1.Time )prj = prj-1;j-;prj = temp;/* 先來先服務(wù)算法的實(shí)現(xiàn) */void Fcfs( Process process, int num, int Timepice)/ process是輸入的進(jìn)程,nun是進(jìn)程的數(shù)目,Timepice是時(shí)間片大小while (true )if (nun=0)cout<< " 所有進(jìn)程都已經(jīng)執(zhí)行完畢 !" <<endl; exit(1);if (process0.LeftTine=0)cout

29、<< " 進(jìn)程 "<<process0.ProcessNan e<<" 已經(jīng)執(zhí)行完畢 !" <<endl;for ( int i=0;i<nun;i+)processi=processi+1;nun-;else if (processnun-1.LeftTine=0)cout<< " 進(jìn)程 "<<processnun -1.ProcessNan e<<" 已經(jīng)執(zhí)行完畢 !" <<endl;nun-;elsecout

30、<<endl;/ 輸出正在運(yùn)行的進(jìn)程process0.LeftTine=process0.LeftTine- Tinepice;process0.leval =process0.leval-1;cout<<" " <<process0.ProcessNane <<"" <<process0.Tine <<cout<<process0.LeftTine <<<<process0.leval<<運(yùn)行cout<<endl;for

31、(int s=1;s<nun;s+)cout<< " " <<processs.ProcessNane <<<<processs.Tine <<cout<<processs.LeftTine <<<<endl; ;<<processs.leval<< "等待/ elsecout<<endl;systen( " pause" );cout<<endl; / while/* 時(shí)間片輪轉(zhuǎn)調(diào)度算法實(shí)現(xiàn) *

32、/ void TimeTurn( Process process, int num, int Timepice) while (true )if (num=0)cout<< " 所有進(jìn)程都已經(jīng)執(zhí)行完畢 !" <<endl;exit(1);if (process0.LeftTime=0)cout<< " 進(jìn)程 " <<process0.ProcessName<< " 已經(jīng)執(zhí)行完畢 !" <<endl; for ( int i=0;i<num;i+)proces

33、si=processi+1;num-;if ( processnum-1.LeftTime =0 )已經(jīng)執(zhí)行完畢 ! " <<endl;<<process0.Time <<cout<< " 進(jìn)程 " << processnum-1.ProcessName << num-;else if (process0.LeftTime > 0)cout<<endl; / 輸出正在運(yùn)行的進(jìn)程 process0.LeftTime=process0.LeftTime- Timepice; pr

34、ocess0.leval =process0.leval-1;cout<< " " <<process0.ProcessName <<cout<<process0.LeftTime <<<<process0.leval<<運(yùn)行cout<<endl;for (int s=1;s<num;s+)<<processs.Time <<cout<< " " <<processs.ProcessName <<

35、;<<processs.leval;cout<<processs.LeftTime <<if (s=1)cout<< "就緒 " <<endl;elsecout<<等待" <<endl;Process temp;temp = process0;for ( int j=0;j<num;j+) processj = processj+1; processnum-1 = temp; / else cout<<endl;system( " pause" ); cout<<endl; / while/* 優(yōu)先級(jí)調(diào)度算法的實(shí)現(xiàn) */ void Priority( Process process, int num, int Timepice

溫馨提示

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

評(píng)論

0/150

提交評(píng)論