#include
#include
#include
#include
#define MAX 10//作业量
struct PCB
{
char name[15];
int sTime;//服务时间
int rTime;//开始运行时间
int fTime;
int wTime;
int tTime;//周转时间
int cTime;
float tTimeWW;//带权周转时间
float priority;
bool finish;
} work[MAX];
int num;
void input()
{
printf("输入作业数目:\n");
scanf("%d",&num);
for(int i=0; i
{
printf("输入进程名:\n");
scanf("%s",&work[i].name);
printf("进程名:%s\n",work[i].name);
printf("输入到达时间:");
scanf("%d",&work[i].cTime);
printf("到达时间:%d\n",work[i].cTime);
printf("输入服务时间:\n");
scanf("%d",&work[i].sTime);
printf("服务时间:%d\n\n",work[i].sTime);
work[i].priority=0;
work[i].finish=0;
}
}
int sort(int pre)
{
int current=0;
for(int i=0; i
{
if(i==0)
{
work[i].fTime=work[i].sTime+work[i].cTime;
}
work[i].wTime=work[pre].fTime-work[i].cTime;//前一个作业的完成时间减去这个作业的到达时间
work[i].priority=(work[i].wTime-work[i].sTime)/work[i].sTime;
}
for(int i=0; i
{
if(!work[i].finish)
{
current=i;
peak;
}
}
for(int j=(current+1); j
{
if(!work[j].finish)
{
if(work[current].cTime
{
if(work[j].cTimework[current].priority)
{
current=j;//找到之后优先级最大的
}
}
else//在上个作业完成之后到达
{
if(work[j].cTime
{
current=j;//先来先服务
}
}
if(work[j].cTime==work[current].cTime)
{
if(work[j].priority>work[current].priority)
{
current=j;//同时到达,找到优先级大的
}
}
}
}
return current;
}
void running(int i,int times,int pre)
{//i为当前
if(times==0)
{
work[i].rTime=work[i].cTime;
}
else
{
if(work[i].cTime
{
work[i].rTime=work[pre].fTime;
}//比上一个作业来的早的话
else
{
work[i].rTime=work[i].cTime;
}//在上个作业之后到达
}
work[i].fTime=work[i].rTime+work[i].sTime;
work[i].tTime=work[i].fTime-work[i].cTime;
work[i].tTimeWW=work[i].tTime/work[i].sTime;
work[i].finish=true;
}
void display(int times,int i)
{
if(times==0)
{
printf("________________________________________");
printf("进程名 到达时间 服务时间 开始执行时间 完成时间 周转时间 带权周转时间");
printf("________________________________________");
}
printf("%9s %9d %9d %9d %9d %9d %9f ",work[i].name,work[i].cTime,
work[i].sTime,work[i].rTime,work[i].fTime,work[i].tTime,work[i].tTimeWW);
}
void check()
{
int pre=0;
int current=0;
float average_tTime=0;
float average_tTimeWW=0;
for(int i=0;i
{
current=sort(pre);
running(current,i,pre);
display(current,i);
pre=current;
}
for(int i=0;i
{
average_tTime+=work[i].tTime;
average_tTimeWW+=work[i].tTimeWW;
}
printf("_________________________________________");
printf("平均周转时间:%f",average_tTime);
printf("平均带权周转时间:%f",average_tTimeWW);
printf("_________________________________________");
}
int main()
{
char again;
do
{
input();
check();
printf("continue(Y/N)");
do
{
again=getchar();
}while(again=="Y"||again=="y"||again=="N"||again=="n");
}while(again=="Y"||again=="y");
}
操作系统 高响应比优先算法 短进程优先算法相关文章: