设计程序模拟进程的先来先服务FCFS和短作业优先SJF调度过程。假设有n个进程分别在T1, … ,Tn时刻到达系统,它们需要的服务时间分别为S1, … ,Sn。分别采用先来先服务FCFS和短作业优先SJF进程调度算法进行调度,计算每个进程的完成时间,周转时间和带权周转时间,并且统计n个进程的平均周转时间和平均带权周转时间。
using namespace std;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Number 5
void fcfs();
void sjf();
int getMin(int fuwu[],int process[],int run); //返回符合条件的要求服务时间最短的进程编号
int daoda[Number], s_dao[Number]; //到达时间和排序后的到达时间
int fuwu[Number], s_fu[Number]; //要求服务时间和排序后的要求服务时间
int wancheng[Number] = { 0 }, zhou[Number]; //完成时间和周转时间(完成时间-到达时间)
float dqzhou[Number]; //带权周转时间 (周转时间/服务时间)
void fcfs() //先来先服务调度算法
{
int run = 0; //记录当前已运行时间
cout<<"FCFS:\n";
for (int i = 0; i < Number; i++)
{
if (daoda[i] == s_dao[0]) //找到最先到达的进程对应的编号
{
cout << "时刻" << s_dao[0] <<"运行进程"<<i+1<<endl;
run = wancheng[i] = s_dao[0] + fuwu[i];
zhou[i] = fuwu[i];
dqzhou[i] = 1.0;
}
}
for (int i = 1; i<Number; i++)
{
for (int j = 0; j<Number; j++)
if (s_dao[i] == daoda[j]) //找到对应进程编号
{
if (run < s_dao[i])
{
cout << "时刻" << s_dao[i] << "运行进程" << j + 1 << endl;
wancheng[j] = s_dao[i] + fuwu[j];
zhou[j] = fuwu[j];
run = s_dao[i]+fuwu[j];
}
else
{
cout << "时刻" << run << "运行进程" << j + 1 << endl;
wancheng[j] = run + fuwu[j];
run += fuwu[j];
zhou[j] = wancheng[j] - daoda[j];
}
dqzhou[j] = zhou[j] * 1.0 / fuwu[j];
}
}
cout<<" 进程ID\t完成时间\t周转时间\t带权周转时间\n";
float sum_zhou = 0, sum_dqzhou = 0;
for (int i = 0; i<Number; i++)
{
printf(" %5d\t%d\t\t%d\t\t%5.2f\n", i + 1, wancheng[i], zhou[i], dqzhou[i]);
sum_zhou += zhou[i];
sum_dqzhou += dqzhou[i];
}
printf("平均周转时间为:%.2f\n", sum_zhou / 5);
printf("平均带权周转时间为:%.2f\n", sum_dqzhou / 5);
}
int getMin(int fuwu[],int process[],int run)
{
int i,j,min = 1000;
for(i = 0;i<Number;i++)
{
if(fuwu[i]<min&&process[i]==0&&daoda[i]<=run)
{
min = fuwu[i];
j = i;
}
}
return j;
}
void sjf()
{
int run = 0,i,j;
int process[5] = {0}; //进程完成标识位
cout << "SJF:\n";
for (i = 0; i < Number; i++)
{
if (daoda[i] == s_dao[0])
{
cout << "时刻" << s_dao[0] <<"运行进程"<<i+1<<endl;
run = wancheng[i] = s_dao[0] + fuwu[i];
zhou[i] = fuwu[i];
dqzhou[i] = 1.0;
process[i] = 1;
}
}
for (i = 1; i<Number; i++)
{
j = getMin(fuwu,process,run);
cout << "时刻" << run << "运行进程" << j + 1 << endl;
wancheng[j] = run+fuwu[j];
run += fuwu[j];
process[j] = 1;
zhou[j] = wancheng[j] - daoda[j];
dqzhou[j] = zhou[j] * 1.0 / fuwu[j];
}
cout << " 进程ID\t完成时间\t周转时间\t带权周转时间\n";
float sum_zhou = 0, sum_dqzhou = 0;
for (int i = 0; i<Number; i++)
{
printf(" %5d\t%d\t\t%d\t\t%5.2f\n", i + 1, wancheng[i], zhou[i], dqzhou[i]);
sum_zhou += zhou[i];
sum_dqzhou += dqzhou[i];
}
printf("平均周转时间为:%.2f\n", sum_zhou / 5);
printf("平均带权周转时间为:%.2f\n", sum_dqzhou / 5);
}
int main()
{
int n;
cout << "请依次输入进程到达时间:\n";
for (int i = 0; i < Number; i++)
{
cin >> daoda[i];
}
cout << "请依次输入进程服务时间:\n";
for (int i = 0; i < Number; i++)
{
cin >> fuwu[i];
}
memcpy(s_dao, daoda, sizeof(daoda));
memcpy(s_fu, fuwu, sizeof(fuwu));
sort(s_dao, s_dao + Number);
sort(s_fu, s_fu + Number);
printf("请输入n:\n");
cin >> n;
while (n<1 || n>3)
{
cout << "输入的n有误,请重新输入n,n= \n";
cin >> n;
}
if (n == 1)
{
fcfs();
}
else if (n == 2)
{
sjf();
}
else
{
fcfs();
printf("\n");
sjf();
}
return 0;
}