#include <iostream>
#include <iomanip>
using namespace std;
#define MaxNum 100//允许的最大进程数
static int n=0,m=0;//进程数
static char ID[MaxNum];//各进程ID
static int ExecuteTime[MaxNum];//各进程的开始执行时间
static int ArrivalTime[MaxNum];//各进程的达到时间
static int ServiceTime[MaxNum];//各进程的服务时间
static int ServiceTime_SJF[MaxNum];//各进程的服务时间(SJF的临时数组)
static int FinishTime[MaxNum];//各进程的完成时间
static int WholeTime[MaxNum];//各进程的周转时间
static double WeightWholeTime[MaxNum];//各进程的带权周转时间
static double AverageWT_FCFS=0,AverageWT_SJF=0;//FCFS和SJF的平均周转时间
static double AverageWWT_FCFS=0,AverageWWT_SJF=0;//FCFS和SJF的平均带权周转时间
static int SumWT_FCFS=0,SumWT_SJF=0;//FCFS、SJF中的周转时间总和
static double SumWWT_FCFS=0,SumWWT_SJF=0;;//FCFS、SJF中的带权周转时间总和
int Enter();//输入进程数、各进程到达、服务时间
int sortArrivalTime(int a, int b);//根据达时间排序
int sortServiceTime(int a, int b);//已到达的进程服务时间排序
int calculationInitTime(int a, int b);//计算第一个进程的各个时间
int calculationTime(int i);//计算完成时间、周转时间、带权周转时间、开始执行时间
int display(int a, int b);//输出ID、到达时间、服务时间、完成时间、周转时间、带权周转时间、开始执行时间、周转时间总和、带权周转时间总和、平均周转时间、平均带权周转时间
int outTimeState(int a, int b);//输出各时间状态
void FCFS();
void SJF();
int main(){
int i,j;
//初始化
for(i=0;i<MaxNum;i++) {
ID[i] = i+65;//自动将进程名字顺序编号为A、B、C、D、E等
}
FCFS();
SJF();
return 0;
}
//输入进程数、各进程到达、服务时间
int Enter(int arr[], int a, int b) {
int i;
for(i=a;i<b;i++) {
cin>>arr[i];
}
}
//根据达时间排序
int sortArrivalTime(int a, int b) {
int i,j;
int t1,t2,t3;
for(i=a;i<b;i++) {
for(j=i+1;j<b;j++) {
if(ArrivalTime[i] > ArrivalTime[j]) {
t1 = ArrivalTime[i];
ArrivalTime[i] = ArrivalTime[j];
ArrivalTime[j] = t1;
t2 = ServiceTime[i];
ServiceTime[i] = ServiceTime[j];
ServiceTime[j] = t2;
t3 = ID[i];
ID[i] = ID[j];
ID[j] = t3;
}
}
}
}
//已到达的进程服务时间排序
int sortServiceTime(int a, int b) {
int i,j;
int t1,t2,t3;
for(i=a;i<b;i++) {
if(ArrivalTime[i] <= FinishTime[a-1])
ServiceTime_SJF[i] = ServiceTime[i];
}
for(i=a;i<b;i++) {
for(j=i;j<b;j++) {
if(ServiceTime_SJF[i] > ServiceTime_SJF[j] && ServiceTime_SJF[i] != 0 && ServiceTime_SJF[j] != 0) {
t1 = ServiceTime_SJF[i];
ServiceTime_SJF[i] = ServiceTime_SJF[j];
ServiceTime_SJF[j] = t1;
}
}
}
for(i=a;i<b;i++) {
if(ServiceTime[i] == ServiceTime_SJF[a]) {
t1 = ArrivalTime[a];
ArrivalTime[a] = ArrivalTime[i];
ArrivalTime[i] = t1;
t2 = ServiceTime[a];
ServiceTime[a] = ServiceTime[i];
ServiceTime[i] = t2;
t3 = ID[a];
ID[a] = ID[i];
ID[i] = t3;
}
}
}
//计算第一个进程的各个时间
int calculationInitTime( int i) {
FinishTime[i] = ArrivalTime[i] + ServiceTime[i];
WholeTime[i] = FinishTime[i] - ArrivalTime[i];
WeightWholeTime[i] = (double)WholeTime[i]/(double)ServiceTime[i];
ExecuteTime[i] = ArrivalTime[i];
}
//计算完成时间、周转时间、带权周转时间、开始执行时间
int calculationTime(int i) {
if(ArrivalTime[i] <= FinishTime[i-1]) {
FinishTime[i] = FinishTime[i-1] + ServiceTime[i];
ExecuteTime[i] = FinishTime[i-1];
}
else {
FinishTime[i] = ArrivalTime[i] + ServiceTime[i];
ExecuteTime[i] = ArrivalTime[i];
}
WholeTime[i] = FinishTime[i] - ArrivalTime[i];
WeightWholeTime[i] = (double)WholeTime[i]/(double)ServiceTime[i];
}
//输出ID、到达时间、服务时间、完成时间、周转时间、带权周转时间、开始执行时间、周转时间总和、带权周转时间总和、平均周转时间、平均带权周转时间
int display(int a, int b) {
cout<<"ID"<<"\t"<<"ArrivalTime"<<"\t"<<"ServiceTime"<<"\t"<<"FinishTime"<<"\t"<<"WholeTime"<<"\t"<<"WeightWholeTime"<<"\t"<<"ExecuteTime"<<endl;
int i;
for(i=a;i<b;i++) {
cout<<ID[i]<<"\t"<<ArrivalTime[i]<<"\t"<<"\t"<<ServiceTime[i]<<"\t"<<"\t"<<FinishTime[i]<<"\t"<<"\t"<<WholeTime[i]<<"\t"<<"\t"<<setprecision(2)<<WeightWholeTime[i]<<"\t"<<"\t"<<ExecuteTime[i]<<endl;
}
if(a==0){
cout<<"SumWT_FCFS: "<<SumWT_FCFS<<endl<<"SumWWT_FCFS: ";
cout<<setprecision(4)<<SumWWT_FCFS<<endl<<"AverageWT_FCFS: ";
cout<<setprecision(4)<<AverageWT_FCFS<<endl<<"AverageWWT_FCFS: ";
cout<<setprecision(4)<<AverageWWT_FCFS<<endl;
}
else{
cout<<"SumWT_SJF: "<<SumWT_SJF<<endl<<"SumWWT_SJF: ";
cout<<setprecision(4)<<SumWWT_SJF<<endl<<"AverageWT_SJF: ";
cout<<setprecision(4)<<AverageWT_SJF<<endl<<"AverageWWT_SJF: ";
cout<<setprecision(4)<<AverageWWT_SJF<<endl;
}
}
//输出各时间状态
int outTimeState(int a, int b) {
int i,j;
for(i=0;i<=FinishTime[b-1];i++) {
cout<<"Time "<<i<<": ";
for(j=a;j<b;j++) {
if(ArrivalTime[j] == i && ExecuteTime[j] == i)
cout<<"ID "<<ID[j]<<" arrived and executing, ";
else if(ArrivalTime[j] == i)
cout<<"ID "<<ID[j]<<" arrived, ";
else if(ExecuteTime[j] == i)
cout<<"ID "<<ID[j]<<" executing, ";
else if(FinishTime[j] == i)
cout<<"ID "<<ID[j]<<" finished, ";
}
cout<<endl;
}
}
void FCFS(){
int i,j;
//FCFS输入进程数
cout<<"FCFS"<<endl;;
cout<<"Enter n: ";
cin>>n;
//FCFS各进程到达时间
cout<<"Enter ArrivalTime: ";
Enter(ArrivalTime,0,n);
//FCFS各进程服务时间
cout<<"Enter ServiceTime: ";
Enter(ServiceTime,0,n);
//根据达时间排序
sortArrivalTime(0,n);
//FCFS计算各时间
calculationInitTime(0);
for(i=1;i<n;i++) {
calculationTime(i);
SumWT_FCFS += WholeTime[i];
SumWWT_FCFS += WeightWholeTime[i];
}
SumWT_FCFS += WholeTime[0];
SumWWT_FCFS += WeightWholeTime[0];
AverageWT_FCFS = (double)SumWT_FCFS/(double)n;
AverageWWT_FCFS = (double)SumWWT_FCFS/(double)n;
//FCFS输出各时间
display(0,n);
//FCFS输出各时间状态
outTimeState(0,n);
}
void SJF(){
int i,j;
//SJF输入进程数
cout<<"SJF"<<endl;
cout<<"Enter m: ";
cin>>m;
//各进程到达时间
cout<<"Enter ArrivalTime: ";
Enter(ArrivalTime,n,n+m);
//各进程服务时间
cout<<"Enter ServiceTime: ";
Enter(ServiceTime,n,n+m);
//根据达时间排序
sortArrivalTime(n,n+m);
//SJF计算各时间
calculationInitTime(n);
for(i=n+1;i<n+m;i++) {
sortServiceTime(i,n+m);
calculationTime(i);
SumWT_SJF += WholeTime[i];
SumWWT_SJF += WeightWholeTime[i];
}
SumWT_SJF += WholeTime[n];
SumWWT_SJF += WeightWholeTime[n];
AverageWT_SJF = (double)SumWT_SJF/(double)m;
AverageWWT_SJF = (double)SumWWT_SJF/(double)m;
//SJF输出各时间
display(n,n+m);
//SJF输出各时间状态
outTimeState(n,n+m);
}
Sophia_fez
- 粉丝: 459
- 资源: 33
最新资源
- 11kw OBC 三相PFC仿真模型,三相功率因数校正技术,PLECS仿真 可以导入管子spice模型,仿出管子损耗,管子结温
- 基于Java开发的在线综合评教系统后端Security模块设计源码
- 基于Python语言的墨石信息安全协会PC端LinkStoneManager管理系统设计源码
- 基于Python Django框架的云借阅图书管理系统设计源码
- 基于C语言的libevent跨平台事件库设计源码解析
- MATLAB、PSIM全桥或者半桥LLC谐振DC DC变器的设计与仿真,内含开环仿真、电压闭环仿真两个仿真文件,并含有电路参数仿
- 背靠背两电平电路拓扑仿真 前级为两电平整流器,网侧相电压有效值为220V 采用双闭环前馈解耦控制,实现并网单位功率因数,稳定
- 基于PyQt、Python爬虫与百度Pyecharts的零食排行榜设计源码
- 15异构混合阶多智能体系统编队控制的分布式优化(无参考文献)
- 基于Java开发的学生信息管理系统设计源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈