實驗A.4使用動態優先權的進程調度算法的模擬_第1頁
實驗A.4使用動態優先權的進程調度算法的模擬_第2頁
實驗A.4使用動態優先權的進程調度算法的模擬_第3頁
實驗A.4使用動態優先權的進程調度算法的模擬_第4頁
實驗A.4使用動態優先權的進程調度算法的模擬_第5頁
已閱讀5頁,還剩10頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

1、實驗四 使用動態優先權的進程調度算法的模擬1、實驗目的通過動態優先權算法的模擬加深對進程概念和進程調度過程的理解。2、實驗內容(1)用C語言來實現對N個進程采用動態優先算法的進程調度;(2)每個用來標識進程的進程控制塊PCB用結構來描述,包括以下字段:l 進程標識符idl 進程優先數priority,并規定優先數越大的進程,其優先權越高;l 進程已占用的CPU時間cputime;l 進程還需占用的CPU時間alltime,當進程運行完畢時,alltime變為0;l 進程的阻塞時間startblock,表示當進程再運行startblock個時間片后,進程將進入阻塞狀態;l 進程被阻塞的時間blo

2、cktime,表示已阻塞的進程再等待blocktime個時間片后,將轉換成就緒態l 進程狀態state;l 隊列指針next,用來將PCB排成隊列(3)優先數改變的原則:l 進程在就緒隊列中呆一個時間片,優先數增加1l 進程每運行一個時間片,優先數減3。(4)假設在調度前,系統中有5個進程,它們的初始狀態如下:ID01234PRIORITY93830290CPUTIME00000ALLTIME33634STARTBLOCK2-1-1-1-1BLOCKTIME30000STATEREADYREADYREADYREADYREADY(5)為了清楚地觀察諸進程的調度過程,程序應將每個時間片內的進程的情

3、況顯示出來,參照的具體格式如下:RUNNING PROG: iREADY_QUEUE:->id1->id2BLOCK_QUEUE:->id3->id4=ID 01234PRIORITY P0P1P2P3P4CPUTIME C0C1C2C3C4ALLTIME A0A1A2A3A4STARTBLOCK T0T1T2T3T4BLOCKTIME B0B1B2B3B4STATE S0S1S2S3S43、思考題(1)在實際的調度中,除了按調度算法選擇下一個執行的進程外,還應處理哪些工作?隊列實現:#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;運行結果:r

18、ootlocalhost root# gcc -o a shiyan4.crootlocalhost root# ./a數組實現:   實驗目的通過動態優先權算法的模擬加深對進程概念和進程調度過程的理解。2、    實驗內容:(1)    用C語言來實現對N個進程采用動態優先權優先算法的進程調度。(2)    每個用來標識進程的進程控制塊PCB用結構來描述,包括以下字段:   進程標識數ID   進程優先數PRIORITY,并規定優先數越大的進程

19、,其優先權越高   進程已占用的CPU時間CUPTIME   進程還需占用的CPU時間ALLTIME。當進程運行完畢時,ALLTIME變為0   進程的阻塞時間STARTBLOCK,表示當進程再運行STARTBLOCK個時間片后,進程將進入阻塞狀態。   進程被阻塞的時間BLOCKTIME,表示已阻塞的進程再等待BLOCKTIME個時間片后,將轉換成就緒狀態。   進程狀態STATE.   隊列指針NEXT,用來將 PCB排成隊列。(3)   

20、優先數改變的原則:    進程在就緒隊列中呆一個時間片,優先數增加1。   進程每運行一個時間片,優先數減3。(4)    假設在調度前,系統中有 5個進程,它們的初始狀態如下: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)為了清楚地觀察諸進程的調度過程,程序將每個時間片內的進程的情況顯示出來,參照的具體格式如下:    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;/循環值 int j;/還在阻塞或就緒隊列中的進程數 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現在的數組編號 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個進程 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(); 運行結果: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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論