計算機專業(yè)操作系統(tǒng)先來先服務(wù)算法詳解_第1頁
計算機專業(yè)操作系統(tǒng)先來先服務(wù)算法詳解_第2頁
計算機專業(yè)操作系統(tǒng)先來先服務(wù)算法詳解_第3頁
計算機專業(yè)操作系統(tǒng)先來先服務(wù)算法詳解_第4頁
計算機專業(yè)操作系統(tǒng)先來先服務(wù)算法詳解_第5頁
已閱讀5頁,還剩1頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、1、 設(shè)計一個按先來先服務(wù)調(diào)度的算法提示:(1)假設(shè)系統(tǒng)中有 5 個進程,每個進程由一個進程控制塊(PCB)來標識。進程控制塊內(nèi)容如圖 1-1 所示。進程名即進程標識。指針:按照進程到達系統(tǒng)的時間將處于就緒狀態(tài)的進程連接成衣個就緒隊列。指針下一個到達進程的進程控制塊首地址。最后一個進程的指針為 NULL。估計運行時間:可由設(shè)計者任意指定一個時間值。到達時間:進程創(chuàng)建時的系統(tǒng)時間或由用戶指定。調(diào)度時,總是選擇到達時間最早的進程。進程狀態(tài):為簡單起見,這里假定進程有兩種狀態(tài):就緒和完成。并假定進程一創(chuàng)建就處于就緒狀態(tài),用 R 表示。當一個進程運行結(jié)束時,就將其設(shè)置成完成態(tài),用 C 表示。(2)設(shè)置

2、一個隊首指針 head,用來在一起。最先進入系統(tǒng)的進程。各就緒進程通過指針連(3)處理機調(diào)度時總是選擇隊首指針指向的進程投入運行。由于本實驗是模擬實驗,所以對被選中進程并不實際啟動運行,而只是執(zhí)行:估計運行時間減 1。用這個操作來模擬進程的一次運行,而且省去進程的現(xiàn)場保護和現(xiàn)場恢復工作。(4)在所設(shè)計的程序中應(yīng)有顯示或打印語句,能顯示或打印正運行進程的進程名、已運行時間、時間、就緒隊列中的進程等。所有進程運行完成時,給出各進程的周轉(zhuǎn)時間和平均周轉(zhuǎn)時間。/*實驗一 先來先服務(wù)算法模擬程序writen by daysky* 2007-11-19*/#include #include #includ

3、e #include using namespatd;/控制塊結(jié)構(gòu)體 struct PCBchar name;/進程名PCB *next;/指針reach_time;/到達時間 left_time;/估計運行時間 begin_time;char sus;/R 就緒 c 完成PCB();PCB(charaname,*anext=NULL);areach_time,aleft_time,abegin_time=-1,charasus=R,PCBPCB(const PCB &from);PCB:CB()next=NULL; reach_time = -1;left_time = -1;begin_t

4、ime = -1; sus = R;PCB:CB(char aname,areach_time,aleft_time,abegin_time,char asus,PCB *anext)name = aname; reach_time = areach_time; left_time = aleft_time; begin_time = abegin_time; sus = asus;next = anext;PCB:CB(const PCB &from)name = ; next = NULL;reach_time = from.reach_time;left_time =

5、from.left_time;begin_time = -1; sus = R;/* 先來先服務(wù)類*/classServeprivate:systime;/系統(tǒng)時間list *ready_list,*all_task;/ 就緒隊列 所有任務(wù)together_time; ofstream fout; public:Serve();Serve(list *a_all_task,const char *logfile);bool run();void check_task(); void run_ready(); void pr_ready();Serve();Serve:Serve()systim

6、e=0; together_time = 0;ready_list=new list();all_task=new list();Serve:Serve(list *a_all_task,const char *logfile)systime=0; together_time = 0;ready_list = new list();fout.open(logfile,ios:trunc);/服務(wù)執(zhí)行總調(diào)度boolServe:run()num = all_task-size(); while(ready_list-empty()/添加新進程,同時從所有隊列中刪除剛添加的進程 check_task

7、();systime+;/運行直到有任務(wù)do/打印就緒隊列pr_ready();/執(zhí)行就緒隊列run_ready();systime+;check_task();while(!ready_list-empty();/打印平均周轉(zhuǎn)時間fout 平均周轉(zhuǎn)時間為: together_ti return true;m ! endl;/檢查到達的任務(wù),添加到就緒隊列的尾部voidServe:check_task()PCB *current;list:iterator it;it = all_task-begin();/這里用循環(huán)處理,因為可能有多個同時到達的任務(wù) while(it!=all_task-e

8、nd()current=(*it);if(current-reach_time=systime)PCB *a_pcb = new PCB(*current);/ a_pcb-sus = R;進程信息ready_list-push_back(a_pcb);/添加在就緒隊列的尾部 it = all_task-erase(it); /從所有任務(wù)中刪除這個任務(wù)fout 進程 name 在時刻: systime 進入就緒隊列! empty() return;/就緒隊列為空就不執(zhí)行,否則PCB *front = ready_list-front();if(front-begin_time = -1)/進程

9、第一次運行,設(shè)置運行起始時間front-begin_time = systime;front-left_time -;/執(zhí)行一次,估計時間減一fout 進程 name 執(zhí)行在時刻: systime ! endl;fout 進程 name 已運行時間: begin_time+1) ! endl;fout 進程 name left_time = 0)front-sus = C;時間為: left_time ! endl;/打印并計算周轉(zhuǎn)時間,systime-1 為完成時間fout 進程 name 在時刻: systime 結(jié)束! reach_time;together_time += a_time

10、;fout 進程 name 的周轉(zhuǎn)時間為: a_time ! pop_front();/刪除第一個元素voidServe:pr_ready()fout 就緒隊列中的進程有:;list:iterator it=ready_list-begin();while(it!=ready_list-end()fout name 、; it+;fout endl;Serve:Serve()folose();main()PCB *a_pcb5;list *all_task=new list();cout 正在初始化 endl;/五個進程的到達時間各不相同 a_pcb0 = new PCB(A,9,10);a_pcb1 = new PCB(B,1,30);a_pcb2 = new PCB(C,3,25)

溫馨提示

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

評論

0/150

提交評論