码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-01-13 23:25:24      阅读:283      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>

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

/*
www.quzhuanpan.com
转载请务必告知
*/
int num;

void input()
{
printf("输入作业数目:\n");
scanf("%d",&num);
for(int i=0; i<num; 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<num; 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<num; i++)
{
if(!work[i].finish)
{
current=i;
break;
}

}

for(int j=(current+1); j<num; j++)
{
if(!work[j].finish)
{
if(work[current].cTime<=work[pre].fTime)//在上个作业完成之前到达
{
if(work[j].cTime<work[pre].fTime&&work[j].priority>work[current].priority)
{
current=j;//找到之后优先级最大的
}
}

else//在上个作业完成之后到达
{
if(work[j].cTime<work[current].cTime)
{
current=j;//先来先服务
}

}

if(work[j].cTime==work[current].cTime)
{
if(work[j].priority>work[current].priority)
{
current=j;//同时到达,找到优先级大的
}
}
}
}
return current;
}
/*
www.oksousou.com
转载请务必告知
*/

void running(int i,int times,int pre)
{//i为当前
if(times==0)
{
work[i].rTime=work[i].cTime;
}
else
{
if(work[i].cTime<=work[pre].fTime)
{
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<num;i++)
{
current=sort(pre);
running(current,i,pre);
display(current,i);
pre=current;
}
for(int i=0;i<num;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‘);
}

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

标签:

原文地址:http://www.cnblogs.com/hardmore/p/5128674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!