说明
尝试实现了FCFS SRJF FPF三种处理器调度算法,虽然存在不足,这里还是进行记录,以便此后继续学习。
代码
// An highlighted block
/*
作者:hy-angry pig
处理器调度算法模拟实现
目标是实现 先到先服务算法 短进程优先算法 高响应比优先算法
先到先服务算法:根据到达时刻完成排序
短进程优先算法 按照执行时间长短进行排序,然后判断当前结束时间和到达时间的大小进行选择
高响应比优先算法:fresh 当前所有进程的priority + sort根据响应比排序
*/
#include<iostream>
#include<algorithm>
#include<vector>
#include<fstream>
#include<windows.h>
#define M 5
//进程结构体 需要到达时刻
using namespace std;
//new当前时刻;
int time_now = 0;
struct process
{
public:
string id;//进程号
double arrive;//到达时刻
double start;//开始时刻
double end;//完成时刻
double time_s;//要求服务时间
double time_cost;//周转时间
double time_p;//带权周转时刻
double priority;//响应比
};
vector<process> vec;//进程存储变量
process temp;
bool cmp1(process A,process B)
{//先到先先服务算法专用排序
return A.arrive<=B.arrive;
}
bool cmp2(process A,process B)
{//短进程优先算法专用排序
return A.time_s<=B.time_s;
}
bool cmp3(process A,process B)
{//高响应比优先算法专用排序
return A.priority >= B.priority;
}//优先级大的在前 cmp编写时注意特点
void show(process A)
{
cout<<A.id<<'\t'<<A.arrive<<'\t'<<A.start<<'\t'<<A.time_s<<'\t'<<A.end<<'\t'<<A.time_cost<<'\t'<<A.time_p<<endl;
}
void fresh_priority()
{//高响应比优先算法专用
for(int i = 0; i < vec.size(); i++)
{
if(vec[i].end != 0)
vec[i].priority = -1;//已经执行完毕;优先级最低
else
vec[i].priority = (time_now - vec[i].arrive) * 1.0 / vec[i].time_s + 1.0;
}
}
//先来先服务算法
void FCFS()
{
sort(vec.begin(),vec.end(),cmp1);
cout << "作业号" << " " << "到达时刻" <<" "<< "开始时刻" <<" "<<"需求时长"<< " " << "完成时刻" << " " << "周转时间" << " " << "带权周转时间" << '\t' << endl;
//处理
sort(vec.begin(),vec.end(),cmp1);
//开始运算
for(int i = 0; i<vec.size(); i++)
{
vec[i].start = time_now;//开始时刻
vec[i].end = vec[i].start + vec[i].time_s;//结束时刻
vec[i].time_cost = vec[i].end - vec[i].arrive;//周转时间
vec[i].time_p = vec[i].time_cost * 1.0 / vec[i].time_s;//带权周转时刻
time_now = vec[i].end;
}
//显示
double avg_c,avg_s_p;//计算平均值
for(int i = 0; i<vec.size(); i++)
{
show(vec[i]);
avg_c += vec[i].time_cost;
avg_s_p += vec[i].time_p;
}
avg_c = avg_c *1.0 / (double) vec.size();
avg_s_p = avg_s_p *1.0 / (double) vec.size();
cout<<"平均周转时间"<<endl;
cout<<avg_c<<endl;
cout<<"平均带权周转时间"<<endl;
cout<<avg_s_p<<endl;
}
void SRJF()
{//短进程优先算法
double avg_c,avg_s_p;//计算平均值
sort(vec.begin(),vec.end(),cmp2);
cout << "作业号" << " " << "到达时刻" <<" "<< "开始时刻" <<" "<<"需求时长"<< " " << "完成时刻" << " " << "周转时间" << " " << "带权周转时间" << '\t' << endl;
//处理
// vector<process>::iterator it;
// it = vec.begin();//第一个位置 用来删除元素
for(int j = 0; j<vec.size(); j++)
{
int i;
for(i = 0; vec[i].arrive > time_now || vec[i].end != 0; i++){}//找到第一个已经到达的 这个地方注意符号
vec[i].start = time_now;//开始时间
vec[i].end = time_now + vec[i].time_s;//结束时间
time_now = vec[i].end;//结束时间
vec[i].time_cost = vec[i].end - vec[i].arrive;//周转时间
vec[i].time_p = vec[i].time_cost * 1.0 / vec[i].time_s;
show(vec[i]);
avg_c += vec[i].time_cost;
avg_s_p += vec[i].time_p;
}
avg_c = avg_c *1.0 / (double) vec.size();
avg_s_p = avg_s_p *1.0 / (double) vec.size();
cout<<"平均周转时间"<<endl;
cout<<avg_c<<endl;
cout<<"平均带权周转时间"<<endl;
cout<<avg_s_p<<endl;
}
void HRRN()
{//高相应比优先算法
double avg_c,avg_s_p;//计算平均值
cout << "作业号" << " " << "到达时刻" <<" "<< "开始时刻" <<" "<<"需求时长"<< " " << "完成时刻" << " " << "周转时间" << " " << "带权周转时间" << '\t' << endl;
for(int i = 0; i<vec.size(); i++)
{
fresh_priority();
sort(vec.begin(),vec.end(),cmp3);
vec[0].start = time_now;//开始时间
vec[0].end = time_now + vec[0].time_s;//结束时间
time_now = vec[0].end;//结束时间
vec[0].time_cost = vec[0].end - vec[0].arrive;//周转时间
vec[0].time_p = vec[0].time_cost * 1.0 / vec[0].time_s;
show(vec[0]);
avg_c += vec[0].time_cost;
avg_s_p += vec[0].time_p;
}
//数据平均值的计算和输出
avg_c = avg_c *1.0 / (double) vec.size();
avg_s_p = avg_s_p *1.0 / (double) vec.size();
cout<<"平均周转时间"<<endl;
cout<<avg_c<<endl;
cout<<"平均带权周转时间"<<endl;
cout<<avg_s_p<<endl;
}
int main()
{
while(1)
{
int choose;
ifstream infile;
cout<<"choos the model you want to display"<<endl;
cout<<"1--->FCFS"<<endl;
cout<<"2--->SJF"<<endl;
cout<<"3--->HRRN"<<endl;
cin>>choose;
infile.open("data.txt");
cout<<"loading";
for(int i = 0; i<8; i++)
{
Sleep(100);//大写
cout<<".";
}
cout<<endl;
cout<<"complete!"<<endl;
cout<<endl;
for(int i = 0; i<M; i++)
{
// cout<<"please set the id arrive_time time_server"<<endl;
infile>>temp.id>>temp.arrive>>temp.time_s;
vec.push_back(temp);
}
if(choose == 1)
FCFS();
else if(choose == 2)
SRJF();
else if(choose == 3)
HRRN();
default
cout<<"Input error"<<endl;
vec.clear();
time_now = 0;
cout<<endl;
cout<<endl;
}
return 0;
}