【操作系统】作业调度
程序员文章站
2022-07-05 11:02:42
...
采用先来先服务(FCFS),最短作业优先(SJF)、响应比高者优先(HRRN)的调度算法
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+50;
struct JCB
{
string name;
int subtime;
int runtime;
int resource;
int t;
int tc;
double w;
string state;
int idx=0;
bool operator <(const JCB &oth)const
{
return oth.runtime<runtime;
}
}a[maxn];
int n;
bool cmp(JCB x,JCB y)
{
return x.subtime <y.subtime;
}
void pt(int i)
{
cout<<"作业名 "<<a[i].name<<" 提交时间 "<<a[i].subtime<<" 运行时间 "<<a[i].runtime<<endl;
cout<<"完成时间 "<<a[i].tc<<" 周转时间 "<<a[i].t<<" 带权周转时间 "<<a[i].w<<endl;
}
void FCFS()
{
printf("FCFS\n");
int tc=0;
double allt=0,allw=0;
queue<JCB>que;
for(int i=1;i<=n;i++) que.push(a[i]);
while(!que.empty())
{
JCB tmp = que.front();
que.pop();
a[tmp.idx].state = "R";
if(tc<tmp.subtime) tc = tmp.subtime;
tc += tmp.runtime;
a[tmp.idx].tc = tc;
a[tmp.idx].t = tc-tmp.subtime;
a[tmp.idx].w = a[tmp.idx].t *1.0/tmp.runtime;
pt(tmp.idx);
a[tmp.idx].state = "F";
allt += a[tmp.idx].t;
allw += a[tmp.idx].w;
}
cout<<"平均周转时间 "<<allt*1.0/n<<" 平均带权周转时间 "<<allw*1.0/n<<endl;
}
void SJF()
{
printf("SJF\n");
priority_queue<JCB> pq;
int tc =0;
double allt=0;
double allw = 0;
for(int i=1;i<=n;i++) pq.push(a[i]);
while(!pq.empty())
{
JCB tmp = pq.top();
pq.pop();
a[tmp.idx].state = "R";
if(tc<tmp.subtime) tc = tmp.subtime;
tc += tmp.runtime;
a[tmp.idx].tc = tc;
a[tmp.idx].t = tc-tmp.subtime;
a[tmp.idx].w = a[tmp.idx].t *1.0/tmp.runtime;
pt(tmp.idx);
a[tmp.idx].state = "F";
allt += a[tmp.idx].t;
allw += a[tmp.idx].w;
}
cout<<"平均周转时间 "<<allt*1.0/n<<" 平均带权周转时间 "<<allw*1.0/n<<endl;
}
int ed=0;
struct work
{
string name;
int subtime;
int runtime;
int resource;
int t;
int tc;
double w;
string state;
int idx=0;
double p;//响应比
bool operator<(const work &oth)const
{
return oth.p>p;
}
}b[maxn];
void pt2(int i)
{
cout<<"作业名 "<<b[i].name<<" 提交时间 "<<b[i].subtime<<" 运行时间 "<<b[i].runtime<<" 响应比 "<<b[i].p<<endl;
cout<<"完成时间 "<<b[i].tc<<" 周转时间 "<<b[i].t<<" 带权周转时间 "<<b[i].w<<endl;
}
void HRRN()
{
printf("HRRN\n");
priority_queue<work>pq;
int tc=0;
double allt=0;
double allw = 0;
for(int i=1;i<=n;i++)
{
b[i].idx = a[i].idx; b[i].name = a[i].name;
b[i].resource = a[i].resource;b[i].runtime = a[i].runtime;
b[i].state=a[i].state;b[i].subtime = a[i].subtime;
b[i].p = (ed-a[i].subtime)*1.0/a[i].runtime;
pq.push(b[i]);
}
while(!pq.empty())
{
work tmp = pq.top();
pq.pop();
b[tmp.idx].state = "R";
if(tc<tmp.subtime) tc = tmp.subtime;
tc += tmp.runtime;
b[tmp.idx].tc = tc;
b[tmp.idx].t = tc-tmp.subtime;
b[tmp.idx].w = a[tmp.idx].t *1.0/tmp.runtime;
pt2(tmp.idx);
b[tmp.idx].state = "F";
allt += b[tmp.idx].t;
allw += b[tmp.idx].w;
}
cout<<"平均周转时间 "<<allt*1.0/n<<" 平均带权周转时间 "<<allw*1.0/n<<endl;
}
int main()
{
srand((unsigned)time(NULL));
printf("请输入作业数量\n");
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
a[i].runtime = rand()%20+1;
a[i].subtime = rand()%20+1;
a[i].state = "W";
a[i].resource = rand()%30+1;
ed= max(ed,a[i].subtime);
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++) {a[i].name = to_string(i); a[i].idx = i;
cout<<"作业名 "<<a[i].name<<" 提交时间 "<<a[i].subtime<<" 运行时间 "<<a[i].runtime<<" 所需资源 "<<a[i].resource<<endl;
}
FCFS();
SJF();
HRRN();
system("pause");
return 0;
}
上一篇: Windows API