?? dispatch.txt
字號:
#include<iostream>
#include<string>
#include <assert.h>
#include<queue>
#include<vector>
using namespace std;
class dispatch{
friend std::istream& operator>>(std::istream& input,dispatch& Dispatch);//友元函數(shù)dispatch輸入流
friend std::ostream& operator<<(std::ostream& output,dispatch& Dispatch);//友元函數(shù)dispatch輸出流
private:
int count; //定義變量表示進程process的個數(shù)
bool SF_FC;//標志選擇的調(diào)度算法類型
double Tse,Wi;//總周轉(zhuǎn)時間和總帶權(quán)周轉(zhuǎn)時間
typedef bool (*compare_type)(process&,process&);//定義指向函數(shù)的指針,實現(xiàn)不同調(diào)度算法選擇的實現(xiàn)
compare_type compare_1,compare_2;//定義指向函數(shù)的指針,實現(xiàn)不同調(diào)度算法選擇的實現(xiàn)
priority_queue<process,vector<process>,compare_type> PQueue;//定義按到達時間順序的隊列PQueue
queue<process> Queue;//定義此隊列表示完成的進程
vector<process> vec;
public:
dispatch(bool sf_fc = true):count(0),SF_FC(sf_fc),Tse(0),Wi(0) {
compare_1 = &compare_FSFC;
compare_2 = &compare_SF;
if (sf_fc == false){//當給定的標志為真時,表示FSFC調(diào)度算法
PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
cout<<"選擇的是最短進程優(yōu)先(搶占式)調(diào)度算法"<<endl;
}
else{
PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
cout<<"選擇的是先來先服務(wù)調(diào)度算法"<<endl;
}
}
void FSFC(){//先來先服務(wù)的實現(xiàn)...
int i =0;
double atime ; //標志當前內(nèi)存中進程結(jié)束的時間點
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()){//如果當前進程已經(jīng)完成服務(wù)功能
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(){
double atime;//標志當前內(nèi)存中進程結(jié)束的時間點
double btime;//表示內(nèi)存中進程剩余的運行時間
double ctime;//表示執(zhí)行順序時間點
int count1 = count;
vector<process>::iterator iter,iter1,iter2;
process *ptr = &PQueue.top();//對隊首元素的end_time()進行初始化
(*ptr).SETend_time( (*ptr).GETrun_time());
vec.push_back(PQueue.top());//取出第一個到達的進程,放入vector
PQueue.pop();
cout<<"******1"<<endl;
iter=vec.begin();
ctime = (*iter).GETstart_time();
atime = (*iter).GETrun_time()+(*iter).GETstart_time();
while(count1>0){
cout<<"******2"<<endl;
process *ptr;
if(PQueue.empty()==false){
ptr= &PQueue.top();//對隊首元素的end_time()進行初始化
(*ptr).SETend_time( (*ptr).GETrun_time());
}
//ctime = (*ptr).GETstart_time();
//btime = atime - ctime;
//(*iter).SETend_time(btime);//設(shè)置運行進程的剩余時間
while( PQueue.empty()==false && atime>(*ptr).GETstart_time()){//如果在進程運行期間有進程到達
cout<<"******3"<<endl;
ctime = (*ptr).GETstart_time();
btime = atime - ctime;
(*iter).SETend_time(btime);//設(shè)置運行進程的剩余時間
cout<<"******4"<<endl;
vec.push_back(*ptr);//把到達的進程放進vector中
cout<<"******5"<<endl;
iter = vec.begin();
cout<<"*iter1 : "<<*iter<<endl;
cout<<"atime :"<<atime<<endl;
if( (*iter).GETend_time()>(*ptr).GETrun_time() ){
cout<<"******6"<<endl;
cout<<"*ptr :"<<*ptr<<endl;
atime =(*ptr).GETstart_time() +(*ptr).GETrun_time();
cout<<"atime :"<<atime<<endl;
//ctime =(*ptr).GETstart_time();
//btime =atime-ctime;
//(*iter).SETend_time(btime);//設(shè)置運行進程的剩余時間
sort(vec.begin(),vec.end(),compare_2); //若新進來的進程優(yōu)先級高,則排序,令優(yōu)先高的進程位置于vector第一個元素
iter = vec.begin();
cout<<"*iter2 :"<<*iter<<endl;
if(PQueue.empty()==false){
PQueue.pop();//刪除在到達時間隊列中的隊首進程信息
if(PQueue.empty()==false)
ptr =&PQueue.top();//取得修改后的到達時間隊列中的新隊首進程
}
//找到優(yōu)先級高的進程,則退出此循環(huán),重新進行小進程運行并查找時候有更高優(yōu)先級的進程到達
}
else{
cout<<"******7"<<endl;
if(PQueue.empty()==false){
PQueue.pop(); //刪除在到達時間隊列中的隊首進程信息
if(PQueue.empty()==false)
ptr =&PQueue.top();//取得修改后的到達時間隊列中的新隊首進程
}
}
}
cout<<"******8"<<endl;
iter1 =vec.begin();//取優(yōu)先級最高的進程
ctime = atime;
cout<<"atime :"<<atime<<endl;
(*iter1).Set_bvisited(true);
(*iter1).SETend_time(atime);
Tse = Tse+(atime-(*iter1).GETstart_time());
Wi = Wi +(atime-(*iter1).GETstart_time())/((*iter).GETrun_time());
Queue.push(*iter1);
cout<<*iter1<<endl;
vec.erase(iter1);
--count1;//標志完全結(jié)束一個進程的運行
cout<<"******9"<<endl;
iter1=vec.begin();iter2=vec.end();
if(iter1==iter2){
if(PQueue.empty()==false){
vec.push_back(PQueue.top());//取出第一個到達的進程,放入vector
PQueue.pop();
iter=vec.begin();
ctime = (*iter).GETstart_time();
atime = (*iter).GETend_time()+(*iter).GETstart_time();
}
break;
}
else{
iter =vec.begin();
atime = (*iter).GETend_time()+ctime;
}
}
}
void print(){
process *ptr;
int i = 1;
//cout<<"進程調(diào)度信息如下:"<<endl;
cout<<" **運行順序****進程名****到達時間****運行時間****結(jié)束時間****"<<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();
}
cout<<"平均周轉(zhuǎn)時間:"<<(Tse/count)<<endl;
cout<<"平均帶權(quán)周轉(zhuǎn)時間:"<<(Wi/count)<<endl;
}
};
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<<"共有進程數(shù)為 "<<Dispatch.count;
return input;
}
std::ostream& operator<<(std::ostream& output,dispatch& Dispatch){
int i=1;
output<<"共有的進程數(shù)目:"<<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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -