CPU排程演算法C++模擬實現——(FCFS,SJF,RR)
阿新 • • 發佈:2018-12-26
FisrtComeFirstServe——先到先服務演算法
按照程序進入就緒佇列的順序,來執行程序。用一個佇列維護即可。
ShortestJobFirst——短作業優先演算法
我實現的是非搶佔式SJF,可以通過優先佇列來判斷已ready且cpu burst最短的程序,但我不太熟,就寫了一個getnext()來實現。
RoundRobin——輪詢演算法
用佇列來維護,具體看程式碼吧。
=。=沒加註釋,完了補上。
#include <iostream> #include <fstream> #include <iomanip> #include <algorithm> #include <queue> using namespace std; struct PCB{ int id; int exectime; int readytime; int start; int end; int prio; int index; bool operator < (PCB a)const { return exectime > a.exectime; } }pcb[10]; int pAmt; int timeslice; bool cmpfcfs(PCB a, PCB b){ if(a.readytime!=b.readytime) return a.readytime <b.readytime; return a.prio<b.prio; } void fcfs(){ ofstream f2("./outputFCFS.txt", ios::app); f2<<"fcfs-------------"<<endl; sort(pcb, pcb+pAmt,cmpfcfs); for(int i=0;i<10;i++){ pcb[i].index=i; } int cputime=0; for(int i=0;i<pAmt;i++){ pcb[i].start=cputime; pcb[i].end=pcb[i].start+pcb[i].exectime; cputime+=pcb[i].exectime; f2<<pcb[i].id<< " "; } f2<<"<--- turn"<<endl; float sum=0; for(int i=0;i<10;i++){ int around=pcb[i].end-pcb[i].readytime; f2<<pcb[i].id<<" "<<pcb[i].start<< " "<<pcb[i].end<<" "<<around<<endl; sum+=around; } f2<<"average is: "<<sum/10.0<<endl; } int getnext(int cputime){ int next; int temp=0xfffffff; int flag=1; for(int i=0;i<10;i++){ if(pcb[i].start==-1 && pcb[i].readytime<=cputime){ if(temp>pcb[i].exectime){ next=i; temp=pcb[i].exectime; flag=0; } } } if(flag){ for(int i=0;i<10;i++){ if(pcb[i].start==-1){ next=i; break; } } } return next; } void sjf(){ ///sort ofstream f2("./outputSJF.txt", ios::app); f2<<"sjf-------------"<<endl; sort(pcb, pcb+pAmt,cmpfcfs); for(int i=0;i<10;i++){ pcb[i].index=i; } int cnt=0; int cputime=0; int next=0; while(cnt<10){ pcb[next].start=cputime; cputime+=pcb[next].exectime; pcb[next].end=cputime; cnt++; f2<<pcb[next].id<<" "; next=getnext(cputime); } int sum=0; f2<<"<--- turn"<<endl; for(int i=0;i<10;i++){ int around=pcb[i].end-pcb[i].readytime; sum+=around; f2<<pcb[i].id<<" "<<pcb[i].start<< " "<<pcb[i].end<<" "<<around<<endl; } f2<<"average is: "<<sum/10<<endl; } void rr(){ ofstream f2("./outputRR.txt", ios::app); f2<<"rr-------------"<<endl; sort(pcb, pcb+pAmt,cmpfcfs); for(int i=0;i<10;i++){ pcb[i].index=i; } queue <PCB> que; int cnt=0; int head[14]={}; int cputime=0; int finish[16]={}; PCB cur[10]; for(int i=0;i<10;i++){ cur[i].readytime=pcb[i].readytime; cur[i].exectime=pcb[i].exectime; cur[i].id=pcb[i].id; cur[i].prio=pcb[i].prio; cur[i].index=pcb[i].index; } que.push(cur[0]); while(cnt<10){ PCB temp=que.front(); int index=temp.index; if(head[index]==0){ pcb[index].start=cputime; head[index]=1; } if(cur[index].exectime<=timeslice){ cputime+=cur[index].exectime; cur[index].exectime=0; pcb[index].end=cputime; f2<<cur[index].id<< " "; cnt++; finish[index]=1; que.pop(); int flag=1; for(int i=0;i<10;i++){ if(finish[i]==0 && (cur[i].readytime<=cputime) &&cur[i].readytime>cputime-timeslice){ que.push(cur[i]); flag=0; } } if(flag&&que.empty()){ for(int i=0;i<10;i++){ if(finish[i]==0){ que.push(cur[i]); cputime=cur[i].readytime; break; } } } } else{ cputime+=timeslice; cur[index].exectime-=timeslice; f2<<cur[index].id<< " "; int flag=1; for(int i=0;i<10;i++){ if(finish[i]==0 && (cur[i].readytime<=cputime) &&cur[i].readytime>cputime-timeslice ){ que.push(cur[i]); flag=0; } } que.pop(); que.push(cur[index]); if(flag&&que.empty()){ for(int i=0;i<10;i++){ if(finish[i]==0){ que.push(cur[i]); cputime=cur[i].readytime; break; } } } } } int sum=0; f2<<"<--- turn"<<endl; for(int i=0;i<10;i++){ int around=pcb[i].end-pcb[i].readytime; sum+=around; f2<<pcb[i].id<<" "<<pcb[i].start<< " "<<pcb[i].end<<" "<<around<<endl; } f2<<"average is: "<<sum/10<<endl; } int main(){ ifstream f1("C:\\Users\\74161\\Downloads\\input.txt"); int cases; f1>>cases; while(cases--){ f1>>pAmt>>timeslice; for(int j=0;j<pAmt;j++){ f1>>pcb[j].id>>pcb[j].readytime>>pcb[j].exectime>>pcb[j].prio; pcb[j].start=-1; } //fcfs(); //sjf(); rr(); } return 0; }