操作系统设计问题,希望大家帮看下!
题目是:单道批处理系统作业调度的模拟1、至少实现有三种的作业调度算法:先来先服务、短作业优先和响应比优先算法。
2、能根据不同的调度算法算出每个作业的周转时间和带权周转时间,并通过一组作业算出系统的平均周转时间和平均带权周转时间,比较各种算法的优缺点。
3、设计一个较实用的用户界面,以便选择不同的作业调度算法。
我有两个程序:
第一个是:
#include<iostream.h>
#define N 8
struct time{
float arriveTime;
float runTime;
float finishTime;
float totalTime;
float weightTotalTime;
char name;
};
void InputTime(time *p)
{
int i;//counter
cout<<"input name & arrive time & run time:"<<endl;
for(i=0;i<=N-1;i++){
float temp1,temp2;
char name;
cout<<"作业名:"<<endl;
cin>>name;
p[i].name=name;
cout<<"到达时:"<<endl;
cin>>temp1;
p[i].arriveTime=temp1;
cout<<"运行时间:"<<endl;
cin>>temp2;
p[i].runTime=temp2;
}
}
void Print(time *p,float totalTimeSum,float weightTotalTimeSum){
cout<<"运行次序:"<<endl;
for(int k=0;k<=N-1;k++)
{
; cout<<p[k].name<<" ";
}
cout<<endl;
cout<<"平均周转时间:"<<totalTimeSum/N<<endl;
cout<<"平均带权周转时间:"<<weightTotalTimeSum/N<<endl;
}
void sort(time *p)
{
for(int i=0;i<=N-1;i++)
for(int j=0;j<=i;j++)
if(p[i].arriveTime<p[j].arriveTime)
{
time temp;
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
void deal(time *p,float &totalTimeSum,float &weightTotalTimeSum)
{
int k;//counter
for(k=0;k<=N-1;k++)
{
if(k==0)
p[k].finishTime=p[k].arriveTime+p[k].runTime;
else
p[k].finishTime=p[k-1].finishTime+p[k].runTime;
}
for(k=0;k<=N-1;k++)
{
p[k].totalTime=p[k].finishTime-p[k].arriveTime;
p[k].weightTotalTime=p[k].totalTime/p[k].runTime;
totalTimeSum+=p[k].totalTime;
weightTotalTimeSum+=p[k].weightTotalTime;
}
}
void FCFS(time *p)
{
float totalTimeSum=0,weightTotalTimeSum=0;
sort(p);
deal(p,totalTimeSum,weightTotalTimeSum);
cout<<"先来先服务:"<<endl;
Print(p,totalTimeSum,weightTotalTimeSum);
}
void SWF(time *p)
{
float totalTimeSum=0,weightTotalTimeSum=0;
sort(p);
for(int m=0;m<N-1;m++)
{
if(m==0)
p[m].finishTime=p[m].arriveTime+p[m].runTime;
else
p[m].finishTime=p[m-1].finishTime+p[m].runTime;
int i=0;
for(int n=m+1;n<=N-1;n++)
{
if(p[n].arriveTime<=p[m].finishTime)
i++;
}
float min=p[m+1].runTime;
int follow=m+1;
for(int k=m+1;k<m+i;k++)
{
if(p[k+1].runTime<min)
{min=p[k+
1].runTime;
follow=k+1;}
}
time temp;
temp=p[m+1];
p[m+1]=p[follow];
p[follow]=temp;
}
deal(p,totalTimeSum,weightTotalTimeSum);
cout<<"短作业优先:"<<endl;
Print(p,totalTimeSum,weightTotalTimeSum);
}
void TRRF(time *p){
float totalTimeSum=0,weightTotalTimeSum=0;
sort(p);
for(int m=0;m<N-1;m++)
{
if(m==0)
p[m].finishTime=p[m].arriveTime+p[m].runTime;
else
p[m].finishTime=p[m-1].finishTime+p[m].runTime;
int i=0;
for(int n=m+1;n<=N-1;n++)
{
if(p[n].arriveTime<=p[m].finishTime)
i++;
}
float max=(p[m].finishTime-p[m+1].arriveTime)/p[m+1].runTime;
; int follow=m+1;
for(int k=m+1;k<m+i;k++)
{
if(max<=(p[m].finishTime-p[k+1].arriveTime)/p[k+1].runTime){
max=(p[m].finishTime-p[k+1].arriveTime)/p[k+1].runTime;
follow=k+1;
}
}
time temp;
temp=p[m+1];
p[m+1]=p[follow];
p[follow]=temp;
}
deal(p,totalTimeSum,weightTotalTimeSum);
cout<<"最高响应比优先:"<<endl;
Print(p,totalTimeSum,weightTotalTimeSum);
}
void main(){
time a[N];
InputTime(a);
time *b=a;time *c=a;
FCFS(a);
SWF(b);
TRRF(c);
}
第二个是:
#include "stdio.h"
#include "stdlib.h"
#define getjcb(type)(type*)malloc(sizeof(type))
#define NULL 0
struct jcb{char name[10];
int htime;
int ntime;
char state;
struct jcb *link;
} *ready=NULL,*p;
typedef struct jcb JCB;
int input()
{int num,i,t=0;
void sort();
printf("\n请输入作业个数:");
scanf("%d",&num);
for(i=1;i<=num;i++)
{printf("\n作业 %d",i);
p=(JCB*)malloc(sizeof(JCB));
printf("\n作业名:");
scanf("%s",p->name);
printf("\n作业运行时间:");
scanf("%d",&p->ntime);
p->htime=t; p->state='w'; p->link=NULL;
sort();
t++;
}
return t;
}
void sort()
{JCB *flag;
if(ready==NULL)
ready=p;
else{flag=ready;
while(flag->link!=NULL)
flag=flag->link;
flag->link=p;
}
}
void show()
{JCB *pr;
printf("\n***正在运行的作业是 :%s ",p->name);
printf("\nname\thtime\tntime\tstate\n");
printf("%s\t",p->name);
printf("%d\t",p->htime);
printf("%d\t",p->ntime);
printf("%c\t",p->state);
printf("\n\n***就绪队列");
for(pr=ready;pr!=NULL;pr=pr->link)
{
printf("\nname\thtime\tntime\tstate\n");
printf("%s\t",pr->name);
printf("%d\t",pr->htime);
printf("%d\t",pr->ntime);
printf("%c\t",pr->state);
}
}
void running()
{printf("\n作业 [%s]已经完成\n",p->name);
free(p);
}
main()
{
char c;
int t=0; int tb,tc,ti,wi;
int tis=0,wis=0,i=0;
t=input();
while(ready!=NULL)
{c=getchar();
++i;
p=ready;
ready=p->link;
p->link=NULL;
p->state='r';
tb=t;
show();
//printf("\npress any key to continue...");
c=getchar();
running();
tc=tb+p->ntime;//完成时间
ti=tc-p->htime;//周转时间
tis+=ti;
wi=ti/p->ntime;//平均带权周转
wis+=wi;
printf("\n作业 %d 开始: \n", t);
printf("\n作业[%d] 完成:\n",t);
printf("\n周转时间:%d\n",ti);
printf("\n带权周转时间: %d\n",wi);
t=t+p->ntime;
//printf("\npress any key to continue...");
c=getchar();
}
printf("\n***所有作业都已经完成");
printf("\n总周转时间:%d\n",tis/i);
printf("\n总带权周转时间:%d\n",wis/i);
//printf("\npress any key to continue...");
c=getchar();
}
第一个得出的是:通过一组作业算出系统的平均周转时间和平均带权周转时间
第二个是:每个作业的周转时间和带权周转时间
不知能不能将以上程序合并呢?