進(jìn)程調(diào)度算法spffpf時(shí)間片輪轉(zhuǎn)算法實(shí)現(xiàn)_第1頁(yè)
進(jìn)程調(diào)度算法spffpf時(shí)間片輪轉(zhuǎn)算法實(shí)現(xiàn)_第2頁(yè)
進(jìn)程調(diào)度算法spffpf時(shí)間片輪轉(zhuǎn)算法實(shí)現(xiàn)_第3頁(yè)
進(jìn)程調(diào)度算法spffpf時(shí)間片輪轉(zhuǎn)算法實(shí)現(xiàn)_第4頁(yè)
進(jìn)程調(diào)度算法spffpf時(shí)間片輪轉(zhuǎn)算法實(shí)現(xiàn)_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

node{}};

sum=-booloperator()(nodea,nodeb){node{}};

sum=-booloperator()(nodea,nodeb){i(a.time==b.timeretura.start>b.start;return

int1,ans=0,avg=0.00;a.time>b.time;

j=0,num=ready.size(),ok=1;

調(diào)度的基本概念:從就緒隊(duì)列中按照?定的算法選擇?個(gè)進(jìn)程并將處理機(jī)分配給它運(yùn)?,以實(shí)現(xiàn)進(jìn)程并發(fā)地執(zhí)?。

進(jìn)程信息

1structstrinname//進(jìn)程名稱(chēng)inid//進(jìn)程idintime//進(jìn)程服務(wù)時(shí)間inrtime//進(jìn)程服務(wù)時(shí)間(主要?于時(shí)間?輪轉(zhuǎn)算法)inlevel//進(jìn)程優(yōu)先級(jí)instart//進(jìn)程提交時(shí)間inlst//進(jìn)程調(diào)度時(shí)間};

1set<string>pname;//存放進(jìn)程名稱(chēng),防?創(chuàng)建重復(fù)進(jìn)程queue<node>qq//時(shí)間?輪轉(zhuǎn)時(shí)?到的就緒隊(duì)列queue<node>pp//進(jìn)程的執(zhí)?隊(duì)列queue<node>db//時(shí)間?算法中的調(diào)度順序priority_queue<node,vector<node>,cmpspf>spf//短時(shí)間優(yōu)先算法隊(duì)列priority_queue<node,vector<node>,cmpfpf>fpf//優(yōu)先級(jí)算法隊(duì)列vector<node>ready//就緒隊(duì)列vector<node>emy//已刪除的進(jìn)程

?vector容器存放就緒的進(jìn)程(每插??個(gè),sort?下,依據(jù)進(jìn)程提交時(shí)間升序排列)

spf(短作業(yè)優(yōu)先算法)

算法思想:服務(wù)時(shí)間短的進(jìn)程在就緒隊(duì)列前?。

算法規(guī)則:要求服務(wù)時(shí)間最短的進(jìn)程/作業(yè)優(yōu)先得到服務(wù)。

算法實(shí)現(xiàn):模擬時(shí)間,將已提交的進(jìn)程插?優(yōu)先隊(duì)列,在依據(jù)時(shí)間是否到達(dá)完成時(shí)間來(lái)判斷哪個(gè)進(jìn)程被移?內(nèi)存中運(yùn)?

代碼:

struccmpspf{234567891//sum作為在執(zhí)?進(jìn)程的完成時(shí)間1i(t==1){1for(inti=0;i<=100000;i++){1i(i==sum)//?先判斷是否到達(dá)在執(zhí)?進(jìn)程的完成時(shí)間1nodetemp;1temp=spf.top();spf.pop();1temp.lst=i-temp.start//計(jì)算周轉(zhuǎn)時(shí)間1ans+=temp.lst//總的周轉(zhuǎn)時(shí)間1avg+=double(temp.lst)/double(temp.time);//總的帶權(quán)周轉(zhuǎn)時(shí)間1pp.push(temp);/執(zhí)?完畢的進(jìn)程放?執(zhí)?隊(duì)列2i(!spf.empty())sum+=spf.top().time;2}2whil(j<num&&i==ready[j].start)//將到達(dá)進(jìn)程提交時(shí)間的進(jìn)程放?就緒隊(duì)列2spf.push(ready[j]);2//當(dāng)CPU執(zhí)?過(guò)程中出現(xiàn)空閑時(shí),更新sum值2i(i>sum&&sum<=spf.top().start)sum=spf.top().start+spf.top().time;2j++;2}2i(ok&&!spf.empty())//第?個(gè)執(zhí)?的進(jìn)程的完成時(shí)間2sum=i+spf.top().time;3ok=0;3}3i(j==num&&spf.empty()break;//所有進(jìn)程執(zhí)?完畢3}3printfprintf進(jìn)程周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);3whil(!pp.empty()){3nodeout;3ou=pp.front();pp.pop();3cout<<;3printfprintf(,out.lst,double(out.lst)/double(out.time));4}4printfprintf平均平均周轉(zhuǎn)時(shí)間為:周轉(zhuǎn)時(shí)間為:,double(ans)/double(num));

,avg);printf(進(jìn)周轉(zhuǎn)時(shí),avg);printf(進(jìn)周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間

printf(,out.lst,

printf(平均周轉(zhuǎn)時(shí)間為:平均周轉(zhuǎn)時(shí)間為:,double(ans)/double(num));printf(平均帶權(quán)周轉(zhuǎn)時(shí)間為平均帶權(quán)周轉(zhuǎn)時(shí)間為,avg);

fpf(優(yōu)先級(jí)調(diào)度算法)

算法思想:進(jìn)程優(yōu)先級(jí)?的進(jìn)程在就緒隊(duì)列前?。

算法規(guī)則:要求進(jìn)程優(yōu)先級(jí)?的進(jìn)程/作業(yè)優(yōu)先得到服務(wù)。

算法實(shí)現(xiàn):模擬時(shí)間,將已提交的進(jìn)程插?優(yōu)先隊(duì)列,在依據(jù)時(shí)間是否到達(dá)完成時(shí)間來(lái)判斷哪個(gè)進(jìn)程被移?內(nèi)存中運(yùn)?

代碼:邏輯跟spf算法是?樣的這?不過(guò)多敘述。

1//spf、fpf的區(qū)別就在于優(yōu)先隊(duì)列中的規(guī)則不同2structcmpfpf{booloperator()(nodea,nodeb){if(a.level==b.levelretura.start>b.start;retura.level<b.level;}};

for(inti=0;i<=10000;i++){if(i==sum){nodetemp;temp=fpf.top();fpf.pop();temp.lst=i-temp.start;ans+=temp.lst;avg+=double(temp.lst)/double(temp.time);pp.push(temp);if(!fpf.empty())sum+=fpf.top().time;}whil(j<num&&i==ready[j].start){fpf.push(ready[j]);if(i>sum&&sum<=fpf.top().start)sum=fpf.top().start+fpf.top().time;j++;}if(ok&&!fpf.empty()){sum=i+fpf.top().time;ok=0;}if(j==num&&fpf.empty()break;}printf();whil(!pp.empty()){nodeout;out=pp.front();pp.pop();cout<<;printf(double(out.lst)/double(out.time));}printf(printf(

時(shí)間?輪轉(zhuǎn)算法

算法思想:公平的、輪流的為各個(gè)進(jìn)程服務(wù),讓每個(gè)進(jìn)程在?定時(shí)間間隔內(nèi)都可以得到響應(yīng)

算法規(guī)則:系統(tǒng)根據(jù)FCFS策略,將所有的就緒進(jìn)程排成?個(gè)就緒隊(duì)列。

輪流讓各個(gè)進(jìn)程執(zhí)??個(gè)時(shí)間?的,若進(jìn)程未在?個(gè)時(shí)間?內(nèi)執(zhí)?完,則被剝奪處理機(jī),將進(jìn)程放到就緒隊(duì)列隊(duì)尾重新排隊(duì)。

算法實(shí)現(xiàn):利?隊(duì)列模擬就緒隊(duì)列,模擬時(shí)間,每次時(shí)間增加?個(gè)時(shí)間?長(zhǎng)度,先判斷是否有進(jìn)程在時(shí)間?內(nèi)結(jié)束,如果有的話(huà),就對(duì)時(shí)間進(jìn)?修改回退到剛完成進(jìn)程的時(shí)間,再判斷時(shí)間?內(nèi)是否有進(jìn)程提交,有的話(huà)加?隊(duì)列。

代碼

1printfprintf(請(qǐng)?jiān)O(shè)置請(qǐng)?jiān)O(shè)置時(shí)間?時(shí)間?????);sf(m);3fointi=0;i<=100000;i+=m){//每次?增?個(gè)時(shí)間?4if(!qq.empty()){/當(dāng)運(yùn)?隊(duì)列有進(jìn)程時(shí),則運(yùn)?該進(jìn)程5nodetemp;temp=qq.front();qq.pop();db.push(temp);if(temp.time>m)//若進(jìn)程不能在該時(shí)間?內(nèi)運(yùn)?完畢,則將服務(wù)時(shí)間減去時(shí)間?,再重新放?隊(duì)列,這也是使rtime?計(jì)算帶權(quán)周轉(zhuǎn)時(shí)間的原因9temp.time-=m;10qq.push(temp);11}12els//反之回退時(shí)間,并將已完成的進(jìn)程放?執(zhí)?完畢隊(duì)列1i=i-m+temp.time;14temp.lst=i-temp.start;

1ans+=temp.lst;16pp.push(temp);17}18}19whil(j<num&&i>=ready[j].start)//到達(dá)時(shí)間?的進(jìn)程放?隊(duì)列2if(ok||qq.empty()){21i=ready[j].start;22ok=0;23}24ready[j].rtime=ready[j].time;25qq.push(ready[j]);26j++;27}28if(j==num&&qq.empty()break;29}30printfprintf(進(jìn)程調(diào)度順序:進(jìn)程調(diào)度順序:);31while(!db.empty()){cout<<db.front().name<<(!db.empty()){cout<<db.front().name<<;db.pop();}32printfprintf(進(jìn)程執(zhí)?完畢順序周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);33whil(!pp.empty()){34nodeout;35out=pp.front();pp.pop();36cout<<;37printfprintf(,out.lst,double(out.lst)/double(out.rtime));38avg+=double(out.lst)/double(out.rtime);39}40printfprintf(平均周轉(zhuǎn)時(shí)間平均周轉(zhuǎn)時(shí)間,double(ans)/double(num));41printfprintf(平均帶權(quán)周轉(zhuǎn)時(shí)間為平均帶權(quán)周轉(zhuǎn)時(shí)間為,avg/double(num));

總代碼如下:

1#include<bits/stdc++.h>2usingnamespacstd;typedeflonglonLL;typedefvectorint>vi;typedefpairint,int>ii;6#defininf1e97#definFfirst8#definSsecond9#define#define10#definrep(i,j,k)for(inti=(j);i<=(k);i++)11#definrep__(i,j,k)for(inti=(j);i<(k);i++)12#definper(i,j,k)for(inti=(j);i>=(k);i--)13#definper__(i,j,k)for(inti=(j);i>(k);i--)14#definmst(a,b)memset(a,b,sizeof(a))15#define#define16#define#define17#define#define18#define#define19#define#define20constintN=1e3+10;2priority_queueint,vector<int>,less<int>>q;22intt,x,sum,ans,m;23doublavg;24strink;25structnode{2strinname;//進(jìn)程名稱(chēng)27intid;//進(jìn)程id2inttime;//進(jìn)程服務(wù)時(shí)間29intrtime;//進(jìn)程服務(wù)時(shí)間(主要?于時(shí)間?輪轉(zhuǎn)算法)30intlevel;//進(jìn)程優(yōu)先級(jí)31intstart;//進(jìn)程提交時(shí)間32intlst;//進(jìn)程調(diào)度時(shí)間33};34structcmpspf{3booloperator()(nodea,nodeb){3if(a.time==b.timeretura.start>b.start;3retura.time>b.time;3}3};40structcmpfpf{4booloperator()(nodea,nodeb){4if(a.level==b.levelretura.start>b.start;4retura.level<b.level;4}4};46set<string>pname;//存放進(jìn)程名稱(chēng),防?創(chuàng)建重復(fù)進(jìn)程47queue<node>qq//時(shí)間?輪轉(zhuǎn)時(shí)?到的就緒隊(duì)列48queue<node>pp//進(jìn)程的執(zhí)?隊(duì)列49queue<node>db//時(shí)間?算法中的調(diào)度順序50priority_queue<node,vector<node>,cmpspf>spf//短時(shí)間優(yōu)先算法隊(duì)列51priority_queue<node,vector<node>,cmpfpf>fpf//優(yōu)先級(jí)算法隊(duì)列52vector<node>ready//就緒隊(duì)列

53vector<node>emy//已刪除的進(jìn)程54boolcmpconstnode&a,constnode&b){5retura.start<b.start;5}57voicreate(){5nodea;5printfprintf(請(qǐng)輸?新進(jìn)程的名稱(chēng)請(qǐng)輸?新進(jìn)程的名稱(chēng));6cin>>;6if(pname.find()!=pname.end()){6printfprintf(進(jìn)程已存在,請(qǐng)從新輸?:進(jìn)程已存在,請(qǐng)從新輸?:);6create();6return;6}6pname.insert();6printfprintf(請(qǐng)輸?新進(jìn)程的到達(dá)時(shí)間、服務(wù)時(shí)間請(qǐng)輸?新進(jìn)程的到達(dá)時(shí)間、服務(wù)時(shí)間);6sf(a.start);sf(a.time);6printfprintf(請(qǐng)輸?新進(jìn)程的請(qǐng)輸?新進(jìn)程的);sf(a.id);7printfprintf(請(qǐng)輸?新進(jìn)程的優(yōu)先級(jí)請(qǐng)輸?新進(jìn)程的優(yōu)先級(jí));sf(a.level);7ready.push_back(a);7sort(ready.begin(),ready.end(),cmp);7}74voikill(){7nodeb;7printfprintf(請(qǐng)輸?要終?的進(jìn)程名字請(qǐng)輸?要終?的進(jìn)程名字);7cin>>k;7if(pname.find(k)!=pname.end()){7intnum=ready.size();8fointi=0;i<num;i++){8if(ready[i].name==k){8b=ready[i];8emy.push_back(b);8ready.erase(ready.begin()+i);8printfprintf(終?進(jìn)程成功!終?進(jìn)程成功!);8}8if(num==ready.size()){8printfprintf(該進(jìn)程已在空隊(duì)列中該進(jìn)程已在空隊(duì)列中);8}9}9}9els{9printfprintf(該進(jìn)程不存在,請(qǐng)輸?正確的進(jìn)程名該進(jìn)程不存在,請(qǐng)輸?正確的進(jìn)程名);9kill();9return;9}9}98voidisplay(){9whil(!pp.empty())pp.pop();10whil(!spf.empty())spf.pop();10whil(!fpf.empty())fpf.pop();10whil(!qq.empty())qq.pop();10if(ready.empty()){10printfprintf(就緒隊(duì)列為空!就緒隊(duì)列為空!);10return;10}10printfprintf(請(qǐng)選擇調(diào)度算法請(qǐng)選擇調(diào)度算法);10printfprintf(、spf調(diào)度算法調(diào)度算法);10printfprintf(、fpf調(diào)度算法調(diào)度算法);11printfprintf(、時(shí)間?輪轉(zhuǎn)算法、時(shí)間?輪轉(zhuǎn)算法);11printfprintf(、返回菜單、返回菜單);11sf(t);11intj=0,num=ready.size(),ok=1;11sum=1,ans=0,avg=0.00;11//sum作為在執(zhí)?進(jìn)程的完成時(shí)間11if(t==1){11rep(i,0,100000){11if(i==sum)//?先判斷是否到達(dá)在執(zhí)?進(jìn)程的完成時(shí)間11nodetemp;12temp=spf.top();spf.pop();12temp.lst=i-temp.start//計(jì)算周轉(zhuǎn)時(shí)間12ans+=temp.lst//總的周轉(zhuǎn)時(shí)間12avg+=double(temp.lst)/double(temp.time);//總的帶權(quán)周轉(zhuǎn)時(shí)間12pp.push(temp);12if(!spf.empty())sum+=spf.top().time;12}12whil(j<num&&i==ready[j].start)//將到達(dá)進(jìn)程提交時(shí)間的進(jìn)程放?就緒隊(duì)列12spf.push(ready[j]);12//當(dāng)CPU執(zhí)?過(guò)程中出現(xiàn)空閑時(shí),更新sum值13if(i>sum&&sum<=spf.top().start)sum=spf.top().start+spf.top().time;13j++;13}13if(ok&&!spf.empty())//第?個(gè)執(zhí)?的進(jìn)程的完成時(shí)間13sum=i+spf.top().time;13ok=0;13}

13if(j==num&&spf.empty()break;//所有進(jìn)程執(zhí)?完畢13}13printfprintf(進(jìn)程周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);14whil(!pp.empty()){14nodeout;14out=pp.front();pp.pop();14cout<<;14printfprintf(,out.lst,double(out.lst)/double(out.time));14}14printfprintf(平均周轉(zhuǎn)時(shí)間為:平均周轉(zhuǎn)時(shí)間為:,double(ans)/double(num));14printfprintf(平均帶權(quán)周轉(zhuǎn)時(shí)間為平均帶權(quán)周轉(zhuǎn)時(shí)間為,avg);14}14elseif(t==2){15rep(i,0,100000){15if(i==sum){15nodetemp;15temp=fpf.top();fpf.pop();15temp.lst=i-temp.start;15ans+=temp.lst;15avg+=double(temp.lst)/double(temp.time);15pp.push(temp);15if(!fpf.empty())sum+=fpf.top().time;15}16whil(j<num&&i==ready[j].start){16fpf.push(ready[j]);16if(i>sum&&sum<=fpf.top().start)sum=fpf.top().start+fpf.top().time;16j++;16}16if(ok&&!fpf.empty()){16sum=i+fpf.top().time;16ok=0;16}16if(j==num&&fpf.empty()break;17}17printfprintf(進(jìn)周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);17whil(!pp.empty()){17nodeout;17out=pp.front();pp.pop();17cout<<;17printfprintf(,out.lst,double(out.lst)/double(out.time));17}17printfprintf(平均周轉(zhuǎn)時(shí)間為:平均周轉(zhuǎn)時(shí)間為:,double(ans)/double(num));17printfprintf(平均帶權(quán)周轉(zhuǎn)時(shí)間為平均帶權(quán)周轉(zhuǎn)時(shí)間為,avg);18}18elseif(t==3){18printfprintf(請(qǐng)?jiān)O(shè)置時(shí)間???請(qǐng)?jiān)O(shè)置時(shí)間???);18sf(m);18fointi=0;i<=100000;i+=m){//每次?增?個(gè)時(shí)間?18if(!qq.empty())//當(dāng)運(yùn)?隊(duì)列有進(jìn)程時(shí),則運(yùn)?該進(jìn)程18nodetemp;18temp=qq.front();qq.pop();18db.push(temp);18if(temp.time>m)//若進(jìn)程不能在該時(shí)間?內(nèi)運(yùn)?完畢,則將服務(wù)時(shí)間減去時(shí)間?,再重新放?隊(duì)列,這也是使?rtime計(jì)算帶權(quán)周轉(zhuǎn)時(shí)間的原因19temp.time-=m;19qq.push(temp);19}19els//反之回退時(shí)間,并將已完成的進(jìn)程放?執(zhí)?完畢隊(duì)列19i=i-m+temp.time;19temp.lst=i-temp.start;19ans+=temp.lst;19pp.push(temp);19}19}20whil(j<num&&i>=ready[j].start)//到達(dá)時(shí)間?的進(jìn)程放?隊(duì)列20if(ok||qq.empty()){20i=ready[j].start;20ok=0;20}20ready[j].rtime=ready[j].time;20qq.push(ready[j]);20j++;20}20if(j==num&&qq.empty()break;21}21printfprintf(進(jìn)程調(diào)度順序:進(jìn)程調(diào)度順序:);21whil(!db.empty()){cout<<db.front().name<<(!db.empty()){cout<<db.front().name<<;db.pop();}21printfprintf(進(jìn)程執(zhí)?完畢順序周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);21printfprintf(進(jìn)程周轉(zhuǎn)時(shí)帶權(quán)周轉(zhuǎn)時(shí)間帶權(quán)周轉(zhuǎn)時(shí)間);21whil(!pp.empty()){21nodeout;21out=pp.front();pp.pop();21cout<<;21printfprintf(,out.lst,double(out.lst)/double(out.rtime));22avg+=double(out.lst)/double(out.rtime);

22}22prin

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 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ì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論