📄 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);//友元函数dispatch输入流
friend std::ostream& operator<<(std::ostream& output,dispatch& Dispatch);//友元函数dispatch输出流
private:
int count; //定义变量表示进程process的个数
bool SF_FC;//标志选择的调度算法类型
double Tse,Wi;//总周转时间和总带权周转时间
typedef bool (*compare_type)(process&,process&);//定义指向函数的指针,实现不同调度算法选择的实现
compare_type compare_1,compare_2;//定义指向函数的指针,实现不同调度算法选择的实现
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调度算法
PQueue = priority_queue<process,vector<process>,compare_type>(compare_1);
cout<<"选择的是最短进程优先(抢占式)调度算法"<<endl;
}
else{
PQueue = 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()){//如果当前进程已经完成服务功能
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;//标志当前内存中进程结束的时间点
double btime;//表示内存中进程剩余的运行时间
double ctime;//表示执行顺序时间点
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);//设置运行进程的剩余时间
while( PQueue.empty()==false && atime>(*ptr).GETstart_time()){//如果在进程运行期间有进程到达
cout<<"******3"<<endl;
ctime = (*ptr).GETstart_time();
btime = atime - ctime;
(*iter).SETend_time(btime);//设置运行进程的剩余时间
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);//设置运行进程的剩余时间
sort(vec.begin(),vec.end(),compare_2); //若新进来的进程优先级高,则排序,令优先高的进程位置于vector第一个元素
iter = vec.begin();
cout<<"*iter2 :"<<*iter<<endl;
if(PQueue.empty()==false){
PQueue.pop();//删除在到达时间队列中的队首进程信息
if(PQueue.empty()==false)
ptr =&PQueue.top();//取得修改后的到达时间队列中的新队首进程
}
//找到优先级高的进程,则退出此循环,重新进行小进程运行并查找时候有更高优先级的进程到达
}
else{
cout<<"******7"<<endl;
if(PQueue.empty()==false){
PQueue.pop(); //删除在到达时间队列中的队首进程信息
if(PQueue.empty()==false)
ptr =&PQueue.top();//取得修改后的到达时间队列中的新队首进程
}
}
}
cout<<"******8"<<endl;
iter1 =vec.begin();//取优先级最高的进程
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;//标志完全结束一个进程的运行
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<<"进程调度信息如下:"<<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();
}
cout<<"平均周转时间:"<<(Tse/count)<<endl;
cout<<"平均带权周转时间:"<<(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<<"共有进程数为 "<<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 + -