操作系統(tǒng)課程設(shè)計(jì)報(bào)告書(shū)進(jìn)程調(diào)度模擬程序_第1頁(yè)
操作系統(tǒng)課程設(shè)計(jì)報(bào)告書(shū)進(jìn)程調(diào)度模擬程序_第2頁(yè)
操作系統(tǒng)課程設(shè)計(jì)報(bào)告書(shū)進(jìn)程調(diào)度模擬程序_第3頁(yè)
操作系統(tǒng)課程設(shè)計(jì)報(bào)告書(shū)進(jìn)程調(diào)度模擬程序_第4頁(yè)
操作系統(tǒng)課程設(shè)計(jì)報(bào)告書(shū)進(jìn)程調(diào)度模擬程序_第5頁(yè)
已閱讀5頁(yè),還剩15頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、目 錄進(jìn)程調(diào)度模擬程序1一設(shè)計(jì)目的及要求1二概要設(shè)計(jì)1三算法流程圖2四源程序及注釋6五運(yùn)行結(jié)果及分析16六課程設(shè)計(jì)總結(jié)19七參考文獻(xiàn)19進(jìn)程調(diào)度模擬程序一設(shè)計(jì)目的及要求編寫(xiě)一個(gè)進(jìn)程調(diào)度程序,允許多個(gè)進(jìn)程并行執(zhí)行。本次設(shè)計(jì)將采用三種算法實(shí)現(xiàn)進(jìn)程的并行執(zhí)行,分別是最高優(yōu)先數(shù)優(yōu)先的調(diào)度算法(即把處理機(jī)分配給優(yōu)先數(shù)最高的進(jìn)程)、先來(lái)先服務(wù)算法、按時(shí)間片輪轉(zhuǎn)調(diào)度算法。 二概要設(shè)計(jì) 每個(gè)進(jìn)程有一個(gè)進(jìn)程控制塊(pcb)表示。進(jìn)程控制塊可以包含如下信息:進(jìn)程名、優(yōu)先數(shù)、到達(dá)時(shí)間、需要運(yùn)行時(shí)間、已用cpu時(shí)間、進(jìn)程狀態(tài)。 進(jìn)程的優(yōu)先數(shù)及需要的運(yùn)行時(shí)間可以事先人為地指定(也可以由隨機(jī)數(shù)產(chǎn)生)。進(jìn)程的到達(dá)時(shí)間為輸入

2、進(jìn)程的時(shí)間。進(jìn)程的運(yùn)行時(shí)間以時(shí)間片為單位進(jìn)行計(jì)算。 每個(gè)進(jìn)程的狀態(tài)可以是就緒 w(wait)、運(yùn)行r(run)、或完成f(finish)三種狀態(tài)之一。 建立一個(gè)pcbobj的數(shù)組來(lái)存放進(jìn)程隊(duì)列。建立一個(gè)類pcbobj存放進(jìn)程的各種信息并對(duì)這些信息的操作。建立一個(gè)類wait_manager 來(lái)對(duì)就緒進(jìn)程進(jìn)行管理。建立一個(gè)類cpu_manager 來(lái)對(duì)運(yùn)行的進(jìn)程進(jìn)行管理。三算法流程圖開(kāi)始初始化進(jìn)程信息fcfs算法結(jié)束rr算法hpf算法y調(diào)用fcfs算法調(diào)用rr算法y調(diào)用hpf算法ynnn圖11 主程序流程圖 start信息初始化進(jìn)程調(diào)度完畢輸出該時(shí)刻的進(jìn)程狀態(tài)表endy更新進(jìn)程信息打印進(jìn)程狀態(tài)就緒

3、隊(duì)列不為空&&cpu空閑y將就緒隊(duì)列的第一個(gè)進(jìn)程給cpucpu為busy態(tài)ny更新運(yùn)行的進(jìn)程信息運(yùn)行進(jìn)程運(yùn)行完畢cpu.remove()yn時(shí)間記錄加一n 圖12 fcfs算法流程圖start信息初始化進(jìn)程調(diào)度完畢輸出該時(shí)刻的進(jìn)程狀態(tài)表endny更新進(jìn)程信息打印進(jìn)程狀態(tài)就緒隊(duì)列不為空&&cpu空閑y將就緒隊(duì)列的第一個(gè)進(jìn)程給cpucpu為busy態(tài)ny更新運(yùn)行的進(jìn)程信息進(jìn)程時(shí)間加一運(yùn)行進(jìn)程運(yùn)行完畢cpu.remove()進(jìn)程時(shí)間清零yn時(shí)間記錄加一是否超時(shí)進(jìn)程加到就緒隊(duì)列進(jìn)程時(shí)間清零ynn 圖13 rr算法流程圖start信息初始化進(jìn)程調(diào)度完畢輸出該時(shí)刻的進(jìn)程狀態(tài)

4、表endy更新進(jìn)程信息打印進(jìn)程狀態(tài)就緒隊(duì)列不為空&&cpu空閑y將就緒隊(duì)列的第一個(gè)進(jìn)程給cpucpu為busy態(tài)ny更新運(yùn)行的進(jìn)程信息運(yùn)行進(jìn)程運(yùn)行完畢進(jìn)程加到就緒隊(duì)列記錄進(jìn)程臨時(shí)信息nn時(shí)間記錄加一ncpu.remove()y 圖14 hpf算法流程圖四源程序及注釋 -pcbobj.h -#include <string>using namespace std;#ifndef pcbobj_h_#define pcbobj_h_enum pstatuswait,run,finish,ungo;/定義進(jìn)程的四種狀態(tài)class pcbobj public:pcbobj(

5、);pcbobj(string name,int id,int reach,int execute);virtual pcbobj(); int getexectime() const; int gethasexectime() const; int getid() const; int getreachtime() const; string getname() const; pstatus getstatus() const; void sethasexectime(int hasexectime); void setid(int pid); void setstatus(pstatus

6、status); void setexectime(int exectime); void setname(string pname); void setreachtime(int reachtime); bool isfinish();/進(jìn)程是否運(yùn)行完畢 bool last_statu;/判斷進(jìn)程上一個(gè)cpu是否是運(yùn)行態(tài)private: string pname; /進(jìn)程名 int pid; /進(jìn)程優(yōu)先數(shù) int reachtime; /進(jìn)程到達(dá)時(shí)間 int exectime; /進(jìn)程需要運(yùn)行的時(shí)間 int hasexectime; /進(jìn)程已經(jīng)運(yùn)行 的 時(shí)間 pstatus status;t

7、ypedef pcbobj *pcbobj;#endif /* pcbobj_h_ */-pcbobj.cpp代碼-#include "pcbobj.h"pcbobj:pcbobj() hasexectime=0;status=ungo;last_statu=false;pcbobj:pcbobj(string name,int id,int reach,int execute)pname=name;pid=id;reachtime=reach;exectime=execute;hasexectime=0;status=ungo;pcbobj:pcbobj() int pc

8、bobj:getexectime() const return exectime;int pcbobj:gethasexectime() const return hasexectime;int pcbobj:getid() const return pid;string pcbobj:getname() const return pname;int pcbobj:getreachtime() const return reachtime;pstatus pcbobj:getstatus() const return status;void pcbobj:sethasexectime(int

9、hasexectime) this->hasexectime = hasexectime;void pcbobj:setid(int pid) this->pid = pid;void pcbobj:setstatus(pstatus status) this->status = status;void pcbobj:setexectime(int exectime) this->exectime = exectime;void pcbobj:setname(string pname) this->pname = pname;void pcbobj:setreac

10、htime(int reachtime) this->reachtime = reachtime;bool pcbobj:isfinish()bool flag=false;if(exectime = hasexectime)status=finish;flag=true;return flag;-waitmanager.h-#include<iostream>#include <list>#include"pcbobj.h"#ifndef waitmanager_h_#define waitmanager_h_class waitmanage

11、r public:waitmanager();virtual waitmanager();void add(pcbobj new_obj);void insert(pcbobj new_name);void remove(string name);void remove();bool empty();pcbobj get();pcbobj last();private:list<pcbobj> wait_pcb_list;#endif /* waitmanager_h_ */-waitmanager.cpp-#include "waitmanager.h"wai

12、tmanager:waitmanager() waitmanager:waitmanager() void waitmanager:add(pcbobj new_obj)wait_pcb_list.push_back(new_obj);new_obj->setstatus(wait);bool waitmanager:empty()return wait_pcb_list.empty();void waitmanager:insert(pcbobj new_obj)if(wait_pcb_list.empty()add(new_obj);elsebool insert_info=fals

13、e;/查看是否已經(jīng)插入進(jìn)去for(list<pcbobj>:iterator it=wait_pcb_list.begin();it!=wait_pcb_list.end();it+)if(new_obj->getid()>(*it)->getid()wait_pcb_list.insert(it,new_obj);new_obj->setstatus(wait);insert_info=true;break;if(!insert_info)wait_pcb_list.push_back(new_obj);new_obj->setstatus(wait

14、);void waitmanager:remove(string name)if(empty()cout<<"沒(méi)有進(jìn)程在就緒隊(duì)列中"<<endl;elsebool remove_info=false;for(list<pcbobj>:iterator it=wait_pcb_list.begin();it!=wait_pcb_list.end();it+)if(*it)->getname() = name)wait_pcb_list.erase(it);remove_info=true;break;if(!remove_info)c

15、out<<name<<"進(jìn)程"<<"不存在"<<endl;void waitmanager:remove()if(empty()cout<<"沒(méi)有就緒進(jìn)程在隊(duì)列中"<<endl;elsewait_pcb_list.pop_front();pcbobj waitmanager:get()return wait_pcb_list.front();pcbobj waitmanager:last()return wait_pcb_list.back();-cpumanag

16、er.h-#include "pcbobj.h"#ifndef cpu_manager_h_#define cpu_manager_h_enum cpu_statusfree,busy;/cpu的狀態(tài)class cpu_manager public:cpu_manager();virtual cpu_manager(); void remove(); cpu_status getcpu_info() const; pcbobj getcpu_pcb() const; void add(pcbobj cpu_pcb); void update(); void update(i

17、nt args); int cpu_timeslice;private:cpu_status cpu_info;pcbobj cpu_pcb;#endif /* cpu_manager_h_ */-cpumanager.cpp-#include "cpu_manager.h"cpu_manager:cpu_manager() cpu_pcb=0;cpu_info=free;cpu_timeslice=4;cpu_manager:cpu_manager() cpu_pcb=0;cpu_status cpu_manager:getcpu_info() const return

18、cpu_info;pcbobj cpu_manager:getcpu_pcb() const return cpu_pcb;void cpu_manager:add(pcbobj cpu_pcb1) cpu_pcb = cpu_pcb1; cpu_info=busy;void cpu_manager:remove()this->cpu_pcb=0;cpu_info=free;void cpu_manager:update()cpu_pcb->setstatus(run);cpu_pcb->sethasexectime(cpu_pcb->gethasexectime()+

19、1);void cpu_manager:update(int args)update();cpu_pcb->setid(cpu_pcb->getid()- args);- schedul_simulate.h-#include "waitmanager.h"#include "cpu_manager.h"#ifndef schedul_simulate_h_#define schedul_simulate_h_void choice();string convert_statu(pstatus statu);void process_init

20、(pcbobj process);void process_update(pcbobj process,int time_record,waitmanager& wait_manager);void process_update_id(pcbobj process,int time_record,waitmanager& wait_manager);/-打印進(jìn)程信息-void process_record(pcbobj process,int time_record);/-從wait到run-void waittorun(waitmanager& wait_manage

21、r,cpu_manager& cpu_manager);void runtowait(waitmanager& wait_manager,cpu_manager& cpu_manager); void fcfs(pcbobj process);void rr(pcbobj process);void hpf(pcbobj process);#endif /* schedul_simulate_h_ */- schedul_simulate.cpp-#include"schedul_simulate.h"void choice()cout<<

22、;"1:fcfs"<<endl;cout<<"2:rr"<<endl;cout<<"3:hpf"<<endl;cout<<"4:quit"<<endl;string convert_statu(pstatus statu) string value = "ungo"switch (statu) case wait:value = "wait"break;case run:value = &qu

23、ot;run"break;case finish:value = "finish"break;case ungo:value = "ungo"return value;void process_init(pcbobj process) string name;int process_id;int process_reach;int process_execute;cout << "請(qǐng)按順序輸入進(jìn)程的唯一標(biāo)識(shí)-進(jìn)程名 進(jìn)程的優(yōu)先級(jí) 進(jìn)程的到達(dá)時(shí)間 進(jìn)程的運(yùn)行時(shí)間" << endl;for (int i

24、= 0; i < 4; i+) /process里的hasexectime和status已經(jīng)被初始化cout << "請(qǐng)輸入第" << (i + 1) << "個(gè)進(jìn)程的信息" << endl;cin >> name;cin >> process_id;cin >> process_reach;cin >> process_execute;(process + i)->setname(name);(process + i)->setid(pro

25、cess_id);(process + i)->setreachtime(process_reach);(process + i)->setexectime(process_execute);cout << "第" << (i + 1) << "個(gè)進(jìn)程信息已經(jīng)初始化" << endl;cout << "所有進(jìn)程信息已經(jīng)全部完成" << endl;void process_update(pcbobj process, int time_record, w

26、aitmanager& wait_manager) for (int i = 0; i < 4; i+) if (process + i)->getreachtime() = time_record) wait_manager.add(process + i);/直接加到末尾void process_update_id(pcbobj process, int time_record, waitmanager& wait_manager)for (int i = 0; i < 4; i+) if (process + i)->getreachtime()

27、= time_record) wait_manager.insert(process + i);void process_record(pcbobj process,int time_record)cout << "時(shí)刻:" << time_record << endl;cout << "進(jìn)程名t優(yōu)先數(shù)t到達(dá)時(shí)間t運(yùn)行時(shí)間t已經(jīng)運(yùn)行時(shí)間t狀態(tài)" << endl;cout << "-" << endl;for (int i = 0; i < 4;

28、i+) cout << (process + i)->getname() << "t" << (process + i)->getid() <<"t" << (process + i)->getreachtime() << "t" << (process + i)->getexectime() << "t" << (process + i)->gethasexectime() &

29、lt;< "tt"if(process+i)->last_statu)cout<< convert_statu(run) << endl;elsecout<<convert_statu(process+i)->getstatus()<<endl;(process+i)->last_statu=false;cout << "-" << endl;void waittorun(waitmanager& wait_manager, cpu_manager&a

30、mp; cpu_manager) cpu_manager.add(wait_manager.get();wait_manager.remove();void runtowait(waitmanager& wait_manager, cpu_manager& cpu_manager) pcbobj temp=cpu_manager.getcpu_pcb();temp->last_statu=true;wait_manager.add(temp);void fcfs(pcbobj process) int time_record = 0;pcbobj *process_las

31、t = process + 3;waitmanager wait_manager;cpu_manager cpumanager;while (process_last->getstatus() != finish) process_update(process,time_record,wait_manager);process_record(process,time_record);if (!wait_manager.empty() && cpumanager.getcpu_info() = free)waittorun(wait_manager, cpumanager)

32、;if (cpumanager.getcpu_info() = busy)cpumanager.update();if(cpumanager.getcpu_pcb()->isfinish()cpumanager.remove();time_record+;if (process_last->getstatus() != ungo && !wait_manager.empty()/已經(jīng)到達(dá)process_last = wait_manager.last();process_record(process,time_record);void rr(pcbobj proce

33、ss) int time_record = 0;pcbobj *process_last = process + 3;waitmanager wait_manager;cpu_manager cpumanager;int k=0;/記錄時(shí)間片 滿則為零while (process_last->getstatus() != finish) process_update(process,time_record,wait_manager);process_record(process,time_record);if (!wait_manager.empty() && cpuma

34、nager.getcpu_info() = free)waittorun(wait_manager, cpumanager);if (cpumanager.getcpu_info() = busy)cpumanager.update();k+;if (cpumanager.getcpu_pcb()->isfinish()cpumanager.remove();k=0;else if(k>=cpumanager.cpu_timeslice)runtowait(wait_manager,cpumanager);cpumanager.remove();k=0;time_record+;i

35、f (process_last->getstatus() != ungo && !wait_manager.empty()/已經(jīng)到達(dá)process_last = wait_manager.last();process_record(process,time_record);void hpf(pcbobj process) int time_record = 0;pcbobj *process_last = process + 3;waitmanager wait_manager;cpu_manager cpumanager;pcbobj temp;while (process_last->getstatus() != finish) process_update_id(process,time_record,wait_manager);process_record(process,time_record);if (!wait_manager.empty() && cpumanager.getcpu_info() = free)waittorun(wait_manager, cpumanager);if (cpumanager.g

溫馨提示

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

評(píng)論

0/150

提交評(píng)論