计算机操作系统---高响应比调度算法

实验要求:

     用高级语言实现简单的进程调度,通过本实验加深对进程调度的理解。设计PCB结构,建立进程就绪队列,编制高响应比优先调度算法进程调度算法。

实验原理:

高响应比优先调度算法则是既考虑了作业的等待时间,又考虑作业运行时间的调度算法,因此既照顾了短作业,又不致使长作业的等待时间过长,从而改善了处理机调度的性能。

实验内容:

(1)通过用户输入进程个数,如下图:

进程名

到达时间

服务时间

1

11

10

2

22

12

3

28

32

(2)根据用户输入进程号,进程名、进程到达时间、进程的完成时间存储,进行高响应比运算得出调度结果。

(3)通过函数计算,得出周转时间、带权周转时间、优先权验证。

实验代码:

#include<dos.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

//定义string 为含有10个字符元素的字符数组类型
typedef char string[10];
struct task
{
int ID; //进程号
string name;    //进程名
int arrivetime; //到达时间
int servicetime; //服务时间
int     waittime; //等待时间
int starttime; //开始运行时间
int finishtime; //结束运行时间
float turnaroundtime; //周转时间
float weightedturnaroundtime; //带权周转时间
int priority; //优先权
int     finish; //是否已经完成
}PCB[10];
int num;
void input()
{
int i;
system("cls");
printf("\n请输入作业数量:");
scanf("%d",&num);
for(i=0;i<num;i++)
{
printf("\n请输入进程%d:\n",i);
printf("进程名 到达时间 服务时间\n");
scanf("%s%9d%9d",PCB[i].name,&PCB[i].arrivetime,&PCB[i].servicetime);
PCB[i].priority=0;
PCB[i].finish=0;
}
}
int HRN(int pre)
{
int current=1,i,j; //优先权=(等待时间+要求服务时间)/要求服务时间
for(i=0;i<num;i++)
{
PCB[i].waittime=PCB[i].finishtime-PCB[i].arrivetime; //等待时间=上一个进程的完成时间-到达时间
PCB[i].priority=(PCB[i].waittime+PCB[i].servicetime)/PCB[i].servicetime;
}
for(i=0;i<num;i++)
{
if(!PCB[i].finish)
{
current=i; //找到第一个还没完成的作业
break;
}
}
for(j=i;j<num;j++) //和后面的作业比较
{
if(!PCB[i].finish) //还没完成(运行)
{
if(PCB[current].arrivetime<=PCB[pre].finishtime) //如果进程在上一个进程完成之前到达
{
if(PCB[j].arrivetime<=PCB[pre].finishtime && PCB[j].priority>PCB[current].priority)
current=j; //找出到达时间在上一个进程完成之前,优先权高的进程
}
else //如果进程是在上一个进程完成之后到达
{
if(PCB[j].arrivetime<PCB[current].arrivetime)
current=j; //找出比较早到达的一个
if(PCB[j].arrivetime==PCB[current].arrivetime) //如果同时到达
if(PCB[j].priority>PCB[current].priority)
current=j; //找出服务时间比较短的一个
}
}
}
return current; //返回当前进程
}
void running(int i,int times,int pre,int statime,int endtime)
{
if(times==0)
{
PCB[i].starttime=PCB[i].arrivetime;
PCB[i].finishtime=PCB[i].starttime+PCB[i].servicetime;
PCB[i].turnaroundtime=PCB[i].servicetime;
PCB[i].weightedturnaroundtime=1.0;
statime=PCB[i].starttime;
}
else
{
if(PCB[i].arrivetime>PCB[pre].finishtime)
PCB[i].starttime=PCB[i].arrivetime;
else
PCB[i].starttime=PCB[pre].finishtime;
PCB[i].finishtime=PCB[i].starttime+PCB[i].servicetime;
PCB[i].turnaroundtime=PCB[i].finishtime-PCB[i].arrivetime;
PCB[i].weightedturnaroundtime=PCB[i].turnaroundtime/PCB[i].servicetime;
}
if(times==num-1)
endtime=PCB[i].finishtime;
PCB[i].finish=1;
}
void print(int i,int times)
{
if(times==0)
{
printf("ID 进程名 到达时间 服务时间 开始时间 完成时间 周转时间 带权周转时间\n");
}
printf("%d%9s%9d%9d%9d%9d%10.1f%10.2f\n",PCB[i].ID,PCB[i].name,PCB[i].arrivetime,PCB[i].servicetime,
PCB[i].starttime,PCB[i].finishtime,PCB[i].turnaroundtime,PCB[i].weightedturnaroundtime);
}
void check()
{
int i;
int statime,endtime,sumturtime=0.00,sumwtutime=0.00,aveturtime,avewtutime;
int current=0,times=0,pre=0;
PCB[pre].finishtime=0;
for(i=0;i<num;i++)
{
PCB[i].finish=0;
}
statime,endtime,sumturtime=0.00,sumwtutime=0.00,aveturtime,avewtutime;
current=0;times=0;pre=0;
PCB[pre].finishtime=0;
printf("\n");
for(i=0;i<num;i++)
{
PCB[i].finish=0;
}
statime,endtime,sumturtime=0.00,sumwtutime=0.00,aveturtime,avewtutime;
current=0;times=0;pre=0;
PCB[pre].finishtime=0;
printf("\n");
for(times=0;times<num;times++)
{
current=HRN(pre);
running(current,times,pre,statime,endtime);
print(current,times);
pre=current;
}
for(i=0;i<num;i++)
{
sumturtime+=PCB[i].turnaroundtime;
sumwtutime+=PCB[i].weightedturnaroundtime;
}
aveturtime=sumturtime/num;
avewtutime=sumwtutime/num;
printf("(计与平均值)                %9d%9d%9d%9d\n",NULL,sumturtime,aveturtime,avewtutime);
printf("\n");
}
void main()
{
char again;
do{
system("cls"); //清屏
printf("请输入4组数据:\n");
input();
check();
printf("continue(y/n):");
do{
again=getch();
}while(again!='Y'&&again!='y'&&again!='N'&&again!='n');
}while(again=='Y' || again=='y');
}

测试结果:



猜你喜欢

转载自blog.csdn.net/weixin_38694789/article/details/80737769