?? lin.txt
字號:
#include<iostream>
#include<string>
#include <assert.h>
#include<queue>
using namespace std;
class dispatch{
friend std::istream& operator>>(std::istream& input,dispatch& Dispatch);//友元函數dispatch輸入流
friend std::ostream& operator<<(std::ostream& output,dispatch& Dispatch);//友元函數dispatch輸出流
private:
int count; //定義變量表示進程process的個數
bool SF_FC;//標志選擇的調度算法類型
double Ti,Tse,Wi;//總周轉時間和總帶權周轉時間
typedef bool (*compare_type)(process&,process&);//定義指向函數的指針,實現不同調度算法選擇的實現
compare_type compare_1,compare_2;//定義指向函數的指針,實現不同調度算法選擇的實現
priority_queue<process,vector<process>,compare_type> PQueue;//定義按到達時間順序的隊列PQueue
priority_queue<process,vector<process>,compare_type> PQueue1;//定義已到達并按短進程優先(或稱為“就緒隊列”)所排列的隊列PQueue1
queue<process> Queue;//定義此隊列表示完成的進程
public:
dispatch(bool sf_fc = true):count(0),SF_FC(sf_fc),Ti(0),Tse(0),Wi(0) {
compare_1 = &compare_FSFC;
compare_2 = &compare_SF;
if (sf_fc == false){//當給定的標志為真時,表示FSFC調度算法
PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
PQueue1 = priority_queue<process,vector<process>,compare_type>(compare_2);
cout<<"選擇的是最短進程優先(搶占式)調度算法"<<endl;
}
else{
PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
PQueue1 =priority_queue<process,vector<process>,compare_type>(compare_1);
cout<<"選擇的是先來先服務調度算法"<<endl;
}
}
void FSFC(){//先來先服務的實現...
int i =0;
double atime ; //標志當前內存中進程結束的時間點
process *Pro1 = &PQueue.top();// 取隊首元素
atime = (*Pro1).GETstart_time();
while(i < count)
{
(*Pro1).Set_bvisited();
cout<<(*Pro1)<<" ";
++i;
atime=atime+(*Pro1).GETrun_time();
(*Pro1).SETend_time(atime);
if((*Pro1).Get_bvisited()){//如果當前進程已經完成服務功能
// Ti = Ti + (*Pro1).GETrun_time();
Tse = Tse +((*Pro1).GETend_time()-(*Pro1).GETstart_time());
Wi = Wi + ((*Pro1).GETend_time()-(*Pro1).GETstart_time())/((*Pro1).GETrun_time());
Queue.push(*Pro1);
}
else
cout<<"error!"<<endl;
PQueue.pop();//刪除隊列中的隊首元素
if(PQueue.empty() == false)
process *Pro1 = &PQueue.top();// 取隊首元素
}
}
void Short_No1(){//搶占式短進程優先的實現...
int i=count,m=count,j=0;//標志PQueue中的進程數,標志就緒隊列PQueue1中的進程數
double atime=0,btime=0,ctime=0,dtime=0;//前者表示當前進程若不被搶占內存則完成的時間點,后者表示當前進程還需要多少時間長度才可以完成,ctime表示內存中進程開始此次運行的時間
process *ptr1 = &PQueue.top();
process *ptr2,*ptr3;
PQueue1.push(*ptr1);
PQueue.pop();
ptr2 = &PQueue.top(); //察看PQueue的隊首元素
ptr1 = &PQueue1.top();
dtime = (*ptr1).GETrun_time();
(*ptr1).SETend_time(dtime);
ctime = ctime + (*ptr1).GETstart_time();//賦予第一個進程開始執行的時間點ctime
//取出PQueue1就緒隊列的隊首元素,即假設此進程在內存中運行
while(m>0){
atime = ctime+(*ptr1).GETstart_time();
if((*ptr2).GETstart_time()<atime){
btime =atime - (*ptr2).GETstart_time();
(*ptr1).SETend_time(btime);
dtime = (*ptr2).GETrun_time();
(*ptr2).SETend_time(dtime);
while((*ptr2).GETstart_time()<atime &&(PQueue.empty() == false) ){//若PQueue隊列中有進程要進入就緒隊列,即其到達時間在內存中進程執行完時間點之前的情況
cout<<"內部3問題"<<endl;
PQueue1.push(*ptr2);//把該進程插入PQueue1就緒隊列
PQueue.pop();//把該滿要進入就緒隊列的進程從PQueue隊列中取出并刪除原有信息
--i;
if(PQueue.empty() == false){
ptr2 = &PQueue.top();//增量,控制循環...
dtime = (*ptr2).GETrun_time();
(*ptr2).SETend_time(dtime);
}
else
break;
}
//ptr1 = &PQueue1.top();
continue ;
}
else{//若沒有找到優先級更高的進程,則占內存的進程便執行完全
btime = 0;
ctime = atime; //此進程的結束時間點,可以是其他進程的開始點
ptr1 = &PQueue1.top();
cout<<*ptr1<<".....";
Tse = atime - (*ptr1).GETstart_time() + Tse;
Wi = Wi + (atime - (*ptr1).GETstart_time() )/((*ptr1).GETrun_time());
(*ptr1).Set_bvisited(true); //設置結束標志
(*ptr1).SETend_time(atime); //設置結束時間點
cout<<(*ptr1).GETend_time();
Queue.push(*ptr1); //插入完成隊列Queue末尾
--m;
PQueue1.pop();
if(PQueue1.empty() == false){//執行完內存中進程后選擇新進程
ptr3 = &PQueue1.top();
--j;
ptr1 = ptr3;
cout<<"&&&&&&&&"<<endl;
}
else{
if(!PQueue.empty()){
ptr2 = &PQueue.top();
PQueue1.push(*ptr2);
PQueue.pop();
--i;
ptr1 = &PQueue1.top();
ptr2 = &PQueue.top();
cout<<"********"<<endl;
}
else{
return ; //控制退出調度算
}
}
}
}
}
void print(){
process *ptr;
int i = 1;
//cout<<"進程調度信息如下:"<<endl;
cout<<" **運行順序*****進程名*****到達時間*****運行時間*****結束時間*****"<<endl;
while (Queue.empty() == false){
ptr = &Queue.front();
cout<<"**"<<i<<"*****"<<(*ptr).GETname();
cout<<"*****"<<(*ptr).GETstart_time();
cout<<"*****"<<(*ptr).GETrun_time();
cout<<"*****"<<(*ptr).GETend_time()<<"*****"<<endl;
++i;
Queue.pop();
}
}
};
std::istream& operator>>(std::istream& input,dispatch& Dispatch){
process Pro1;
int count1=0;
while(input>>(Pro1)){
Dispatch.PQueue.push(Pro1);
++count1;
}
Dispatch.count = count1;
cout<<"共有進程數為 "<<Dispatch.count;
return input;
}
std::ostream& operator<<(std::ostream& output,dispatch& Dispatch){
int i=1;
output<<"共有的進程數目:"<<Dispatch.count<<endl;
output<<"進程名 到達時間 運行時間 "<<endl;
while(i<= Dispatch.count){
cout<<"...."<<endl;
output<<Dispatch.PQueue.top()<<endl;
Dispatch.PQueue.pop();
++i;
cout<<"....."<<endl;
}
cout<<"*******"<<endl;
return output;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -