更多精彩
当前位置: 首页 > 伤感文章 > >正文

操作系统 高响应比优先算法 短进程优先算法

时间:2020-09-10 来源:晋江文学网
 

#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;

}//在上个作业之后到达

}

wo癫痫病吃什么药效果比较好rk[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

{

c原发性癫痫病能不能治好呢urrent=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");

}

Tags:

推荐阅读

热门阅读