操作系統-進程調度實驗.docx_第1頁
操作系統-進程調度實驗.docx_第2頁
操作系統-進程調度實驗.docx_第3頁
操作系統-進程調度實驗.docx_第4頁
操作系統-進程調度實驗.docx_第5頁
免費預覽已結束,剩余18頁可下載查看

下載本文檔

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

文檔簡介

實驗五 進程調度模擬實驗/進程調度算法proc.c#include #include#include typedef struct pcb/定義PCB結構char name20; /*進程標識符*/int cputime; /*進程占用CPU時間*/int prio; /*進程優先數*/int needtime; /*進程到完成還需要的CPU時間*/struct pcb *next;/*鏈指針*/PCB;PCB *RUN,*READY,*RTAIL,*FINSH,*FTAIL;void PRINTLINK(int t)/*輸出3個隊列*/PCB *p;printf(CPU運行次數:_%d_n,t);printf(_n);printf(進程名t運行狀態t運行次數t還需要運行次數n);if(RUN!=NULL) printf(%st運行t%dt%dn,RUN-name,RUN-cputime,RUN-needtime); else printf(*運行狀態為空n);p=READY;if(p!=NULL) while(p!=NULL) printf(%st就緒t%dt%dn,p-name,p-cputime,p-needtime); p=p-next; else printf(*就緒隊列為空n);p=FINSH;if (p!=NULL) while(p!=NULL)/printf( 進程名字為:%sn,p-name);printf(%st完成t%dt%dn,p-name,p-cputime,p-needtime);p=p-next;elseprintf(*完成隊列為空n);getchar();PCB *CPCBLINK()/*建立就緒隊列*/ printf(建立就緒隊列nn);int i,n,nt,pr;PCB *p,*q,*head;n=0;while(1) printf(請輸入進程的個數(有效范圍1-100):); scanf(%d,&n); printf(n); if (n=1&nname);while(1) printf(需要的運行時間:); scanf(%d,&nt); if(nt0) break; else printf(輸入無效,重新輸入!n); getchar(); head-needtime=nt;printf(優先數:);scanf(%d,&pr);head-prio=pr; head-cputime=0;/*進程已獲得的運行時間*/head-next=NULL;q=head; for(i=1;iname); printf(需要的運行時間:); scanf(%d,&nt); p-needtime=nt; printf(優先數:); scanf(%d,&pr); p-prio=pr;p-cputime=0;/*進程已獲得的運行時間*/ p-next=NULL;q-next=p;q=p; RTAIL=q;return head;void JXDLPX()/*就緒隊列按優先級從大到小排序*/ PCB *p,*q,*t; char s10; int L=0,ct,pr,nt; p=READY; t=(struct pcb* )malloc(sizeof(struct pcb); while(p-next!=NULL) L=0; q=p-next; t=p; while(q!=NULL) if(t-prioprio) t=q; L=1;/*表示有比它優先級大的進程*/ q=q-next; if(L=1) strcpy(s,t-name); ct=t-cputime; pr=t-prio; nt=t-needtime; q=p-next; while(strcmp(q-name,s)!=0) q=q-next; strcpy(q-name,p-name); q-cputime=p-cputime; q-prio=p-prio; q-needtime=p-needtime; strcpy(p-name,s); p-cputime=ct; p-prio=pr; p-needtime=nt; p=p-next; void YXS()/*調用優先數調度算法*/PCB *p;int t=0,nt,ct,pr;printf(您選擇的是:優先級調度算法n);READY=CPCBLINK();/*建立就緒隊列*/p=(struct pcb* )malloc(sizeof(struct pcb);while(READY!=NULL) JXDLPX();/*就緒隊列按優先級從大到小排序*/ p=READY; READY=READY-next; p-next=NULL; pr=p-prio; pr=pr-3; p-prio=pr;/*運行1次進程優先級縮小3*/ nt= p-needtime; nt=nt-1; p-needtime=nt; ct=p-cputime; ct=ct+1; p-cputime=ct; RUN=p; PRINTLINK(t);/*輸出3個隊列*/ if( RUN-needtimenext=p; FTAIL=FTAIL-next; RUN=NULL; else /*若運行沒結束進入就緒隊列*/ if (READY=NULL)/*當就緒隊列為空*/ READY=p; RTAIL=p; else RTAIL-next=p; RTAIL=p; RUN=NULL; t+;void SJP()/*調用時間片循環輪轉算法*/PCB *p;printf(您選擇的是:時間片循環輪轉調度算法n); int t=0,nt,ct;READY=CPCBLINK();/*建立就緒隊列*/p=(struct pcb* )malloc(sizeof(struct pcb);while(READY!=NULL) p=READY; READY=READY-next; p-next=NULL; nt= p-needtime; nt=nt-2; if(ntneedtime=nt; ct=p-cputime; ct=ct+2; p-cputime=ct; RUN=p; PRINTLINK(t);/*輸出3個隊列*/ if( RUN-needtimenext=p; FTAIL=FTAIL-next; RUN=NULL; else/*若運行沒結束進入就緒隊列*/ if (READY=NULL)/*當就緒隊列為空*/ READY=p; RTAIL=p; else RTAIL-next=p; RTAIL=p; RUN=NULL; t+;/*主程序*/int main() int N;RUN=(struct pcb* )malloc(sizeof(struct pcb); while(1)RUN =NULL; READY =NULL; RTAIL=NULL; FINSH=NULL; FTAIL=NULL;printf(=n);printf(進程調度算法演示程序 n);printf(=n); printf( 1:優先級調度算法n); printf( 2:時間片循環輪轉算法n); printf( 3:退出n); printf(n); printf( 請選擇:); scanf(%d,&N);if(N=1)YXS();/*調用優先數調度算法*/else if(N=2)SJP();/*調用時間片循環輪轉算法*/else if(N=3) break; elseprintf(您輸入的信息有誤,請重新輸入!nn);getchar();printf(演示程序結束!nn); getchar();return 0;1、 流程圖2、 實驗結果3、 編寫簡化的多級反饋隊列調度算法要求只有三級隊列,進程在第一級隊列中時,采用時間片輪轉執行;如果該進程沒有執行完畢,其進入第二級隊列,第二級隊列中也采用時間片輪轉算法,但時間片長度是第一級隊列時間片長度的二倍,以此類推,長進程在第三級隊列中最終執行完畢。如果進程在第一級隊列中就已經執行完畢,則在第一級隊列中結束進程,如在第二級隊列中執行完畢,則在第二級隊列中結束進程。 #include #include#include typedef struct pcb/定義PCB結構char name20; /*進程標識符*/int cputime; /*進程占用CPU時間*/int prio; /*進程優先數*/int needtime; /*進程到完成還需要的CPU時間*/struct pcb *next;/*鏈指針*/ int ltime; /*時間片長度*/PCB;PCB *RUN,*READY,*RTAIL,*FINSH,*FTAIL;void PRINTLINK(int t)/*輸出3個隊列*/PCB *p;printf(CPU運行次數:_%d_n,t);printf(_n);printf(進程名t運行狀態t運行次數t還需要運行次數n);if(RUN!=NULL) printf(%st運行t%dt%dn,RUN-name,RUN-cputime,RUN-needtime); else printf(*運行狀態為空n);p=READY;if(p!=NULL) while(p!=NULL) printf(%st就緒t%dt%dn,p-name,p-cputime,p-needtime); p=p-next; else printf(*就緒隊列為空n);p=FINSH;if (p!=NULL) while(p!=NULL)/printf( 進程名字為:%sn,p-name);printf(%st完成t%dt%dn,p-name,p-cputime,p-needtime);p=p-next;elseprintf(*完成隊列為空n);getchar();PCB *CPCBLINK()/*建立就緒隊列*/ printf(建立就緒隊列nn);int i,n,nt,pr;PCB *p,*q,*head;n=0;while(1) printf(請輸入進程的個數(有效范圍1-100):); scanf(%d,&n); printf(n); if (n=1&nname);while(1) printf(需要的運行時間:); scanf(%d,&nt); if(nt0) break; else printf(輸入無效,重新輸入!n); getchar(); head-needtime=nt;printf(優先數:);scanf(%d,&pr);head-prio=pr; head-cputime=0;/*進程已獲得的運行時間*/head-next=NULL;q=head; for(i=1;iname); printf(需要的運行時間:); scanf(%d,&nt); p-needtime=nt; printf(優先數:); scanf(%d,&pr); p-prio=pr;p-cputime=0;/*進程已獲得的運行時間*/ p-next=NULL;q-next=p;q=p; RTAIL=q;return head;void SJP()/*調用時間片循環輪轉算法*/PCB *p;printf(您選擇的是:時間片循環輪轉調度算法n); int t=0,nt,ct;READY=CPCBLINK();/*建立就緒隊列*/p=(struct pcb* )malloc(sizeof(struct pcb);while(READY!=NULL) p=READY; READY=READY-next; p-next=NULL; nt= p-needtime; nt=nt-ltime; if(ntneedtime=nt; ct=p-cputime; ct=ct+ltime; p-cputime=ct; p-ltime*=2; RUN=p; PRINTLINK(t);/*輸出3個隊列*/ if( RUN-needtimenext=p; FTAIL=FTAIL-next; RUN=NULL; else/*若運行沒結束進入就緒隊列*/ if (READY=NULL)/*當就緒隊列為空*/ READY=p; RTAIL=p; else RTAIL-next=p; RTAIL=p; RUN=NULL; t+;/*主程序*/void main() int N;RUN=(struct pcb* )malloc(sizeof(struct pcb); while(1)RUN =NULL; READY =NU

溫馨提示

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

評論

0/150

提交評論