




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
-.z實驗一模擬進程狀態轉換及其PCB的變化一、實驗目的:自行編制模擬程序,通過形象化的狀態顯示,使學生理解進程的概念、進程之間的狀態轉換及其所帶來的PCB內容、組織的變化,理解進程與其PCB間的一一對應關系。二、實驗內容及要求:〔1〕、設計并實現一個模擬進程狀態轉換及其相應PCB內容、組織構造變化的程序?!?〕、獨立編寫、調試程序。進程的數目、進程的狀態模型〔三狀態、五狀態、七狀態或其它〕以及PCB的組織形式可自行選擇?!?〕、合理設計與進程PCB相對應的數據構造。PCB的內容要涵蓋進程的根本信息、控制信息、資源需求及現場信息?!?〕、設計出可視性較好的界面,應能反映出進程狀態的變化引起的對應PCB內容、組織構造的變化。〔5〕、代碼書寫要規*,要適當地參加注釋?!?〕、鼓勵在實驗中參加新的觀點或想法,并加以實現?!?〕、認真進展預習,完成預習報告?!?〕、實驗完成后,要認真總結,完成實驗報告。三、實現:數據構造structPCB{charname;intpriority;intneedtime;booloperator<(constPCB&b)const{returnpriority>b.priority;}};五狀態進程模型最高優先數優先調度算法流程圖四、運行結果:圖1創立2個進程,因為這時cpu空閑所以內核調度,b優先級高先執行圖2超時,因為這時cpu空閑所以內核調度,b優先級還是比a高所以先執行圖32個進程均被阻塞,其中一旦進程被阻塞就會引發調度圖4喚醒1個進程,從阻塞隊列取隊首放到就緒隊列隊尾,由于這時cpu空閑所以內核調度五、源代碼:*include<cstdio>*include<algorithm>usingnamespacestd;intReady_len=0;intBlocked_len=0;intCPU_state=0;structPCB{ charname; intpriority; intneedtime;booloperator<(constPCB&b)const{returnpriority>b.priority;}};PCBReady[100];PCBBlocked[100];PCBCpu;booldispatch();boolcreat(intNUM){//創立一個新的進程while(NUM--){printf("輸入進程名(一個字符)、所需時間(一個整數)、優先級(一個整數):\n");scanf("%s%d%d",&(Ready[Ready_len].name),&(Ready[Ready_len].needtime),&(Ready[Ready_len].priority));getchar();Ready_len++;}if(CPU_state==0)//如果CPU空閑,則調度dispatch();}booldispatch(){ if(CPU_state==0){ if(Ready_len!=0){sort(Ready,Ready+Ready_len);C=Ready[0].name;Cpu.needtime=Ready[0].needtime;Cpu.priority=Ready[0].priority;if(Ready_len!=1)//就緒隊列剔除隊首元素for(intind*=1;ind*<Ready_len;ind*++){Ready[ind*-1].name=Ready[ind*].name;Ready[ind*-1].needtime=Ready[ind*].needtime;Ready[ind*-1].priority=Ready[ind*].priority;}Ready_len--;CPU_state=1;printf("***%c進程送往CPU執行\n",C);Cpu.needtime--;Cpu.priority--;}else{printf("***就緒隊列為空,無法調度\n");returnfalse; } }else{printf("***CPU忙,無法調度\n");}}booltime_out(){ if(CPU_state==1){if(Cpu.needtime==0)printf("***%c時間片用完,并且執行完畢,被釋放\n",C);else{Ready[Ready_len].name=C;Ready[Ready_len].needtime=Cpu.needtime;Ready[Ready_len].priority=Cpu.priority;Ready_len++;printf("***%c時間片用完\n",C);}CPU_state=0;C=0;Cpu.needtime=0;Cpu.priority=0;if(Ready_len!=0)//時間片用完,如果就緒隊列不為空,則調度dispatch(); }else{ printf("***沒有進程在CPU中,無法超時\n"); }}boolevent_wait(){ if(CPU_state==1){ Blocked[Blocked_len].name=C;Blocked[Blocked_len].needtime=Cpu.needtime;Blocked[Blocked_len].priority=Cpu.priority; Blocked_len++; printf("***%c被阻塞\n",C);CPU_state=0;if(Ready_len!=0)//進程被阻塞,如果就緒隊列不為空,則調度dispatch();}else printf("***沒有進程在CPU中,無法阻塞\n");}boolevent_occur(){ if(Blocked_len!=0){ //sort(Blocked,Blocked+Blocked_len);Ready[Ready_len].name=Blocked[0].name;Ready[Ready_len].needtime=Blocked[0].needtime;Ready[Ready_len].priority=Blocked[0].priority;Ready_len++;if(Blocked_len!=1)//阻塞隊列剔除隊首元素for(intind*=1;ind*<Blocked_len;ind*++){Blocked[ind*-1].name=Blocked[ind*].name;Blocked[ind*-1].needtime=Blocked[ind*].needtime;Blocked[ind*-1].priority=Blocked[ind*].priority;}Blocked_len--;//printf("%d%d",Blocked_len,Ready_len);printf("***%c被喚醒\n",Ready[Ready_len-1].name);if(CPU_state==0)//如果CPU空閑,則調度dispatch();//printf("%d%d",Blocked_len,Ready_len);}else printf("***阻塞隊列為空,無法喚醒\n");}intmain(){intCputime=1; while(1){printf("\n1:New\t\t\t2:Dispatch\n"); printf("3:Timeout\t\t4:Eventwait\n");printf("5:Eventoccur\t\t0:e*it\n");printf("輸入1--5實現相應的功能:\n");intselect; scanf("%d",&select);getchar(); switch(select){case1:intnum;printf("輸入要創立的進程數:\n");scanf("%d",&num);getchar();creat(num);break; case2: dispatch(); break; case3: time_out(); break; case4: event_wait();break; case5: event_occur(); break;case0:e*it(0);break; } printf("****************************Cputime:%3d****************************\n",Cputime); printf("狀態\t\t進程名\t\t需要時間\t\t優先級\n"); if(CPU_state){//顯示CPU中的進程printf("Running:\t%c\t\t",C);printf("%d\t\t\t",Cpu.needtime);printf("%d\n",Cpu.priority); } if(Ready_len){//顯示Ready隊列中的進程for(inta=0;a<Ready_len;a++){printf("Ready%d:\t\t",a);printf("%c\t\t",Ready[a].name);printf("%d\t\t\t",Ready[a].needtime);printf("%d\n",Ready[a].priority); } } if(Blocked_len){//顯示Blocked隊列中的程序for(intb=0;b<Blocked_len;b++){printf("Blocked%d:\t",b);
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 行政組織理論的知識拓展策略試題及答案
- 計算機二級MySQL與數據安全試題及答案
- 2025年N1叉車司機理論考試題及答案
- 網絡技術領域的標桿企業分析試題及答案
- 行政組織溝通與協調考題及答案
- 數據庫結構設計的規范試題及答案
- 公司工會干部管理制度
- 學校軍訓安全管理制度
- 在建油庫安全管理制度
- 土地報批部門管理制度
- 繪本故事:睡睡鎮
- 酒店住宿水單模板1
- 供應鏈信息管理教學課件
- 工藝管道安裝質量控制
- 人工智能導論知到章節答案智慧樹2023年東北石油大學
- 麻醉與肝臟專題知識講座
- 定位搶救在危重患者搶救中的應用
- (職業技能考試題庫)起重機械指揮知識點練習題庫、安裝起重工技能理論知識點題庫
- 體育保健學復習題
- 高中英語完形填空試題(有答案和解析)
- 糧食作物栽培學:江蘇紅粒冬小麥產業發展趨勢展望
評論
0/150
提交評論