




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、實驗四 使用動態(tài)優(yōu)先權(quán)的進(jìn)程調(diào)度算法的模擬1、實驗?zāi)康耐ㄟ^動態(tài)優(yōu)先權(quán)算法的模擬加深對進(jìn)程概念和進(jìn)程調(diào)度過程的理解。2、實驗內(nèi)容(1)用C語言來實現(xiàn)對N個進(jìn)程采用動態(tài)優(yōu)先算法的進(jìn)程調(diào)度;(2)每個用來標(biāo)識進(jìn)程的進(jìn)程控制塊PCB用結(jié)構(gòu)來描述,包括以下字段:l 進(jìn)程標(biāo)識符idl 進(jìn)程優(yōu)先數(shù)priority,并規(guī)定優(yōu)先數(shù)越大的進(jìn)程,其優(yōu)先權(quán)越高;l 進(jìn)程已占用的CPU時間cputime;l 進(jìn)程還需占用的CPU時間alltime,當(dāng)進(jìn)程運(yùn)行完畢時,alltime變?yōu)?;l 進(jìn)程的阻塞時間startblock,表示當(dāng)進(jìn)程再運(yùn)行startblock個時間片后,進(jìn)程將進(jìn)入阻塞狀態(tài);l 進(jìn)程被阻塞的時間blo
2、cktime,表示已阻塞的進(jìn)程再等待blocktime個時間片后,將轉(zhuǎn)換成就緒態(tài)l 進(jìn)程狀態(tài)state;l 隊列指針next,用來將PCB排成隊列(3)優(yōu)先數(shù)改變的原則:l 進(jìn)程在就緒隊列中呆一個時間片,優(yōu)先數(shù)增加1l 進(jìn)程每運(yùn)行一個時間片,優(yōu)先數(shù)減3。(4)假設(shè)在調(diào)度前,系統(tǒng)中有5個進(jìn)程,它們的初始狀態(tài)如下:ID01234PRIORITY93830290CPUTIME00000ALLTIME33634STARTBLOCK2-1-1-1-1BLOCKTIME30000STATEREADYREADYREADYREADYREADY(5)為了清楚地觀察諸進(jìn)程的調(diào)度過程,程序應(yīng)將每個時間片內(nèi)的進(jìn)程的情
3、況顯示出來,參照的具體格式如下:RUNNING PROG: iREADY_QUEUE:->id1->id2BLOCK_QUEUE:->id3->id4=ID 01234PRIORITY P0P1P2P3P4CPUTIME C0C1C2C3C4ALLTIME A0A1A2A3A4STARTBLOCK T0T1T2T3T4BLOCKTIME B0B1B2B3B4STATE S0S1S2S3S43、思考題(1)在實際的調(diào)度中,除了按調(diào)度算法選擇下一個執(zhí)行的進(jìn)程外,還應(yīng)處理哪些工作?隊列實現(xiàn):#include<malloc.h>/#define NULL 0#def
4、ine M 10 typedef struct node int id; int pr; int ct; int at; int bt; int sb; struct node *next; jd;jd *max(jd *p) jd *maxnode=NULL,*p1,*p2,*p3=p; int maxnum; p1=p; p2=p; if(p->next=NULL) return NULL; maxnum=p->next->pr; while(p1->next!=NULL) p2=p1->next; if(maxnum <= p2->pr) max
5、node=p2; p3=p1; maxnum=p2->pr; p1=p1->next; p3->next=maxnode->next; maxnode->next=NULL; return maxnode; void blocktoready(jd *pblock,jd *pready) jd *p1=pblock,*p3; while(p1->next!=NULL) p3=p1->next; if(p3->bt=0) p1->next=p3->next; p3->next=pready->next; pready->
6、;next=p3; p1=p1->next; if(p1=NULL) break;void ready(jd *p) jd *p1=p->next; while(p1!=NULL) p1->pr+; p1=p1->next;void run(jd *p) jd *p1; if(p->next!=NULL) p1=p->next; p1->pr=p1->pr-3; p1->at-; p1->ct+; void block(jd *p) jd *p1=p->next; while(p1!=NULL) p1->bt-; p1=p
7、1->next;void runtoreadyorblock(jd *prun,jd *pready,jd *pblock) jd *p; if(prun->next=NULL) return; p=prun->next; if(p->at=0) prun->next=NULL; else if(p->ct=p->sb) p->next=pblock->next; pblock->next=p; prun->next=NULL; else p->next=pready->next; pready->next=p
8、; prun->next=NULL; jd *head(jd pcb,int L) int i; for(i=0;i<L;i+) printf("input pcd%d informationn",i); printf("PRIORITY:"); scanf("%d",&pcbi.pr); printf("ALLTIME:"); scanf("%d",&pcbi.at); printf("STARTBLOCK:"); scanf("%d&
9、quot;,&pcbi.sb); printf("BLOCKTIME:"); scanf("%d",&pcbi.bt); pcbi.id=i; pcbi.ct=0; for(i=0;i<L-1;i+) pcbi.next=&pcbi+1; pcbL-1.next=NULL; return &pcb0;void print(jd *p) jd *p1; if(p->next=NULL) printf("ttthe queue are emptyn"); while(p->next!=NU
10、LL) p1=p->next; printf("tt%dt%dt%dt%dt%dt%dn",p1->id,p1->pr,p1->ct,p1->at,p1->sb,p1->bt); p=p->next; if(p=NULL) break;int main() jd pcbM; jd *pready=(jd *)malloc(sizeof(jd); jd *prun=(jd *)malloc(sizeof(jd); jd *pblock=(jd *)malloc(sizeof(jd); int L,i,n=1; pready-&g
11、t;next=NULL; prun->next=NULL; pblock->next=NULL; printf("please input the number of pcb :n"); scanf("%d",&L); pready->next=head(pcb,L); while(1) prun->next=max(pready); run(prun); ready(pready); block(pblock); printf("running%d every pcb information:n",n
12、); printf("ttidtprtcttattsbtbtn"); printf("the ready pcb:n"); print(pready); printf("the run pcb:n"); print(prun); printf("the block pcb:n"); print(pblock); printf("the all pcb:n"); for(i=0;i<L;i+) printf("tt%dt%dt%dt%dt%dt%dn ",pcbi.id,
13、pcbi.pr,pcbi.ct,pcbi.at,pcbi.sb,pcbi.bt); printf("n"); blocktoready(pblock,pready); runtoreadyorblock(prun,pready,pblock); n+; if(pready->next=NULL && pblock->next=NULL) break;xt=head(pcb,L); while(1) prun->next=max(pready); run(prun); ready(pready); block(pblock); printf(
14、"running%d every pcb information:n",n); printf("ttidtprtcttattsbtbtn"); printf("the ready pcb:n"); print(pready); printf("the run pcb:n"); print(prun); printf("the block pcb:n"); print(pblock); printf("the all pcb:n"); for(i=0;i<L;i+) pr
15、intf("tt%dt%dt%dt%dt%dt%dn ",pcbi.id,pcbi.pr,pcbi.ct,pcbi.at,pcbi.sb,pcbi.bt); printf("n"); blocktoready(pblock,pready); runtoreadyorblock(prun,pready,pblock); n+; if(pready->next=NULL && pblock->next=NULL) break;xt=head(pcb,L); while(1) prun->next=max(pready); r
16、un(prun); ready(pready); block(pblock); printf("running%d every pcb information:n",n); printf("ttidtprtcttattsbtbtn"); printf("the ready pcb:n"); print(pready); printf("the run pcb:n"); print(prun); printf("the block pcb:n"); print(pblock); printf(&q
17、uot;the all pcb:n"); for(i=0;i<L;i+) printf("tt%dt%dt%dt%dt%dt%dn ",pcbi.id,pcbi.pr,pcbi.ct,pcbi.at,pcbi.sb,pcbi.bt); printf("n"); blocktoready(pblock,pready); runtoreadyorblock(prun,pready,pblock); n+; if(pready->next=NULL && pblock->next=NULL) break;運(yùn)行結(jié)果:r
18、ootlocalhost root# gcc -o a shiyan4.crootlocalhost root# ./a數(shù)組實現(xiàn): 實驗?zāi)康耐ㄟ^動態(tài)優(yōu)先權(quán)算法的模擬加深對進(jìn)程概念和進(jìn)程調(diào)度過程的理解。2、 實驗內(nèi)容:(1) 用C語言來實現(xiàn)對N個進(jìn)程采用動態(tài)優(yōu)先權(quán)優(yōu)先算法的進(jìn)程調(diào)度。(2) 每個用來標(biāo)識進(jìn)程的進(jìn)程控制塊PCB用結(jié)構(gòu)來描述,包括以下字段: 進(jìn)程標(biāo)識數(shù)ID 進(jìn)程優(yōu)先數(shù)PRIORITY,并規(guī)定優(yōu)先數(shù)越大的進(jìn)程
19、,其優(yōu)先權(quán)越高 進(jìn)程已占用的CPU時間CUPTIME 進(jìn)程還需占用的CPU時間ALLTIME。當(dāng)進(jìn)程運(yùn)行完畢時,ALLTIME變?yōu)? 進(jìn)程的阻塞時間STARTBLOCK,表示當(dāng)進(jìn)程再運(yùn)行STARTBLOCK個時間片后,進(jìn)程將進(jìn)入阻塞狀態(tài)。 進(jìn)程被阻塞的時間BLOCKTIME,表示已阻塞的進(jìn)程再等待BLOCKTIME個時間片后,將轉(zhuǎn)換成就緒狀態(tài)。 進(jìn)程狀態(tài)STATE. 隊列指針NEXT,用來將 PCB排成隊列。(3)
20、優(yōu)先數(shù)改變的原則: 進(jìn)程在就緒隊列中呆一個時間片,優(yōu)先數(shù)增加1。 進(jìn)程每運(yùn)行一個時間片,優(yōu)先數(shù)減3。(4) 假設(shè)在調(diào)度前,系統(tǒng)中有 5個進(jìn)程,它們的初始狀態(tài)如下:ID 0 1 2 3 4PRIORITY 9 38 3
21、0 29 0CPUTIME 0 0 0 0 0ALLTIME 3 3 6 3 4STARTBLOCK 2
22、160; -1 -1 -1 -1BLOCKTIME 3 0 0 0 0STATE READY READY READY READY R
23、EADY(5)為了清楚地觀察諸進(jìn)程的調(diào)度過程,程序?qū)⒚總€時間片內(nèi)的進(jìn)程的情況顯示出來,參照的具體格式如下: RUNING PROG:i READY_QUEUE: ->id1->id2 BLOCK_QUEUE: ->id3->id4ID 0 1 2 3&
24、#160; 4PRIORITY P0 P1 P2 P3 P4CPUTIME C0 C1 C2 C3 C4ALLTIME A0 A1 &
25、#160; A2 A3 A4STARTBLOCK T0 T1 T2 T3 T4BLOCKTIME B0 B1 B2 B3 B4STATE
26、0; S0 S1 S2 S3 S4#include <stdio.h> #include <iostream>using namespace std; int i;/循環(huán)值 int j;/還在阻塞或就緒隊列中的進(jìn)程數(shù) int s; int m;/最大priority的id struct pcb int id; int p; /priority int cputime; int alltime; int startbl
27、ock; int blocktime; int state; /0 表示ready 1表示end -1表示block ; struct pcb pro5= 0,9,0,3,2,3,0, 1,38,0,3,-1,0,0, 2,30,0,6,-1,0,0, 3,29,0,3,-1,0,0, 4,0,0,4,-1,0,0 ; int changestate0() if(pro0.startblock=0) pro0.state=-1; pro0.startblock-; return 1; if(pro0.blocktime=0) pro0.state=0; return 1; if(pro0.st
28、ate=0&&pro0.startblock!=-1) pro0.startblock-;return 1; if(pro0.state=-1&&pro0.blocktime!=0) pro0.blocktime-;return 1; int state0() changestate0(); s=pro0.p; if(pro0.state=-1) s=-100; return s; int maxp()/求出最大priority state0(); int max=s; m=pro0.id; for(i=0;i<j;i+) if(proi+1.p>p
29、roi.p) max=proi+1.p; m=proi+1.id; return m; void change() maxp(); int x;/得到m現(xiàn)在的數(shù)組編號 for(i=0;i<j;i+) proi.p+; for(i=0;i<j;i+) if(proi.id=m) x=i; prox.cputime+; prox.p=prox.p-4; prox.alltime-; if(prox.alltime=0) prox.state=1; void display() change(); cout<<"RUNNING PROG:"<<
30、m<<endl; cout<<"=n" cout<<"ID " for(i=0;i<j;i+) cout.width(10); cout<<proi.id; cout<<endl<<"PRIORITY " for(i=0;i<j;i+) cout.width(10); cout<<proi.p; cout<<endl<<"CPUTIME " for(i=0;i<j;i+) cout.widt
31、h(10); cout<<proi.cputime; cout<<endl<<"ALLTIME " for(i=0;i<j;i+) cout.width(10); cout<<proi.alltime; cout<<endl<<"STARTBLOCK" for(i=0;i<j;i+) cout.width(10); cout<<proi.startblock; cout<<endl<<"BLOCKTIME " for
32、(i=0;i<j;i+) cout.width(10); cout<<proi.blocktime; cout<<endl<<"STATE " for(i=0;i<j;i+) cout.width(10); cout<<proi.state; cout<<endl; int main() j=5;/剛開始有5個進(jìn)程 while(j!=0) for(i=0;i<j;i+) if(proi.state=1) for(;i<j;i+) proi=proi+1; j=j-1; display();
33、getchar(); 運(yùn)行結(jié)果:rootlocalhost root# g+ -o c c.crootlocalhost root# ./cRUNNING PROG:1=ID 0 1 2 3 &
34、#160; 4PRIORITY 10 35 31 30 1CPUTIME
35、 0 1 0 0 0ALLTIME &
36、#160; 3 2 6 3 4STARTBLOCK 1
37、60; -1 -1 -1 -1BLOCKTIME 3 0
38、160; 0 0 0STATE 0 0 0
39、60; 0 0RUNNING PROG:1=ID 0 1 2
40、 3 4PRIORITY 11 32 32 31 2CPUT
41、IME 0 2 0 0 0ALLTIME
42、0; 3 1 6 3 4STARTBLOCK 0
43、 -1 -1 -1 -1BLOCKTIME 3 0
44、; 0 0 0STATE 0 0 0
45、 0 0RUNNING PROG:1=ID 0 1 2
46、60; 3 4PRIORITY 12 29 33 32
47、60; 3CPUTIME 0 3 0 0 0ALLTIME &
48、#160; 3 0 6 3 4STARTBLOCK -1
49、160; -1 -1 -1 -1BLOCKTIME 3 0 &
50、#160; 0 0 0STATE -1 1 0 &
51、#160; 0 0RUNNING PROG:2=ID 0 2 3
52、0; 4PRIORITY 13 30 33 4CPUTIME 0
53、0; 1 0 0ALLTIME 3 5 3
54、60; 4STARTBLOCK -1 -1 -1 -1BLOCKTIME 2
55、0; 0 0 0STATE -1 0 0
56、0; 0RUNNING PROG:3=ID 0 2 3 4PRIORITY
57、; 14 31 30 5CPUTIME 0 1 &
58、#160; 1 0ALLTIME 3 5 2 4STARTB
59、LOCK -1 -1 -1 -1BLOCKTIME 1 0&
60、#160; 0 0STATE -1 0 0
61、60; 0RUNNING PROG:2=ID 0 2 3 4PRIORITY 15 &
62、#160; 28 31 6CPUTIME 0 2 1
63、0; 0ALLTIME 3 4 2 4STARTBLOCK &
64、#160; -1 -1 -1 -1BLOCKTIME 0 0
65、 0 0STATE -1 0 0 0RUNNING PROG:3=ID &
66、#160; 0 2 3 4PRIORITY 16 29
67、60; 28 7CPUTIME 0 2 2 0A
68、LLTIME 3 4 1 4STARTBLOCK -1
69、60; -1 -1 -1BLOCKTIME 0 0 0
70、60; 0STATE 0 0 0 0RUNNING PROG:2=ID
71、0 2 3 4PRIORITY 17 26 29
72、 8CPUTIME 0 3 2 0ALLTIME
73、 3 3 1 4STARTBLOCK -1 -1
74、 -1 -1BLOCKTIME 0 0 0 0STATE
75、160; 0 0 0 0RUNNING PROG:3=ID 0
76、; 2 3 4PRIORITY 18 27 26
77、 9CPUTIME 0 3 3 0ALLTIME 3
78、0; 3 0 4STARTBLOCK -1 -1 -1
79、60; -1BLOCKTIME 0 0 0 0STATE
80、160; 0 0 1 0RUNNING PROG:2=ID 0 2
81、60; 4PRIORITY 19 24 10CPUTIME 0 4
82、60; 0ALLTIME 3 2 4STARTBLOCK -1
83、; -1 -1BLOCKTIME 0 0 0STATE 0 &
84、#160; 0 0RUNNING PROG:2=ID 0 2 4PRIORITY
85、160; 20 21 11CPUTIME 0 5 0ALLTIME
86、60; 3 1 4STARTBLOCK -1 -1 -1BLOCKTIME
87、; 0 0 0STATE 0 0 0RUNNIN
88、G PROG:2=ID 0 2 4PRIORITY 21 18
89、; 12CPUTIME 0 6 0ALLTIME 3 0
90、160; 4STARTBLOCK -1 -1 -1BLOCKTIME 0
91、 0 0STATE 0 1 0RUNNING PROG:0=ID 0
92、160; 4PRIORITY 18 13CPUTIME 1 0ALLTIME
93、0; 2 4STARTBLOCK -1 -1BLOCKTIME 0 0STATE
94、160; 0 0RUNNING PROG:0=ID 0 4PRIORITY 15 14CPUTIME 2 0ALLTIME 1 4STARTBLOCK
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 解除撫養(yǎng)關(guān)系協(xié)議書
- 配套工廠供貨協(xié)議書
- 船舶公司保密協(xié)議書
- 違法房產(chǎn)贈予協(xié)議書
- 設(shè)備代工保密協(xié)議書
- 雙方家庭談婚房協(xié)議書
- 數(shù)據(jù)庫異常處理與修復(fù)試題及答案
- 湖南省岳陽市湘陰縣石塘鎮(zhèn)石塘中學(xué)、東塘鎮(zhèn)中學(xué)、石塘鎮(zhèn)白泥湖中學(xué)、三塘鎮(zhèn)中學(xué)四校2024-2025學(xué)年七年級下學(xué)期5月聯(lián)考道德與法治試題
- 河北省計算機(jī)試題及答案
- 財務(wù)成本與邏輯關(guān)系試題及答案
- MOOC 地學(xué)景觀探秘·審美·文化-重慶大學(xué) 中國大學(xué)慕課答案
- 安全生產(chǎn)事故報告處理制度范本
- (高清版)WST 311-2023 醫(yī)院隔離技術(shù)標(biāo)準(zhǔn)
- 2024年電梯安裝與維修工理論考試題庫及答案(通用版)
- 天耀中華合唱簡譜大劇院版
- 【《我國互聯(lián)網(wǎng)企業(yè)價值評估現(xiàn)狀與問題探析11000字》(論文)】
- 智慧農(nóng)業(yè)的無人機(jī)技術(shù)應(yīng)用
- 建筑裝飾裝修工程消耗量定額
- 北京市2023年中考備考語文專題復(fù)習(xí) 名著閱讀題(解析)
- 招聘需求分析報告
- 黃太吉融資商業(yè)計劃書
評論
0/150
提交評論