实验三(FCFS ,SJF,HRRN)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include <time.h>
#define N 30
int File();
int input();
void quit();
typedef struct JOB{
char jobname[10];//作业的名称
int submittime;//作业提交时刻
int runtime;//作业运行时间
int starttime;//作业开始时刻
int finishtime;//作业完成时刻
int turnovertime;//作业周转时间
float turnnumber;//作业周转系数
char jobrun[10];//作业处于运行状态
float rp;//作业响应比
}job;
job jArray[N];
job temp[N];
int count=0;
int Menu()
{
// struct JOB p;
// int n;
// n=File();
//n=input();
int choic;
printf("\n*****************作业调度算法***********************\n");
printf("1.先来先服务(FCFS)调度算法\n");
printf("2.短作业优先 (SJF) 调度算法\n");
printf("3.响应比高者优先(HRRN)调度算法\n");
printf("4.退出系统\n");
printf("****************************************\n");
printf("请选择菜单号(1——4):");
while((!scanf("%d",&choic))||choic<1||choic>4)
{
getchar();
printf("\n输入错误!!!!!!请重新输入\n");
printf("\n*****************作业调度算法***********************\n");
printf("1.先来先服务(FCFS)调度算法\n");
printf("2.短作业优先 (SJF) 调度算法\n");
printf("3.响应比高者优先(HRRN)调度算法\n");
printf("4.退出系统\n");
printf("****************************************\n");
printf("请选择菜单号(1——4):");
}
return choic;
}
int input(int n)
{
int i;
/*printf("请用户输入相应的数据:\n");
printf("请输入作业个数(2~24):");
while(!scanf("%d",&n)||n<2||n>24)
{
getchar();
printf("输入错误!!!!!!请重新输入\n");
printf("请输入作业个数(2~24):");
}*/
for(i=0;i<n;i++)
{
printf("请输入第%d个作业的名称:",i+1);
scanf("%s",jArray[i].jobname);
printf("请输入第%d个作业的到达时间:",i+1);
scanf("%d",&jArray[i].submittime);
printf("请输入第%d个作业的要求服务时间:",i+1);
scanf("%d",&jArray[i].runtime);
printf("\n");
}
return n;
}
void sort(int n)
{
int i,j;
printf("用户输入的原始数据\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d\t\t%d\n",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
}
printf("按提交时间排序后,还未进入后备队列的任务!\n");
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(jArray[j].submittime<jArray[i].submittime)
{
temp[j]=jArray[j];
jArray[j]=jArray[i];
jArray[i]=temp[j];
}
}
}
printf("作业名称\t提交时刻\t运行时间\n");
for(i=0;i<n;i++)
{
printf("%s\t\t%d\t\t%d\n",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
}
}
void FCFS(int n)
{
int time=-1,i=0,j,l,k;
float averageround=0.00;
float average=0.00;
printf("\n\n\n**********先来先服务算法FCFS*************\n");
sort(n);
while(i<n)
{
l=0;
while(jArray[i].submittime>time)
{
l++;
time++;
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
if(l==0)
{
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
printf("\n还未进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i+1;j<n;j++)
{
if(jArray[j].submittime>time)
{
printf("%s\t\t%d\t\t%d\n",jArray[j].jobname,jArray[j].submittime,jArray[j].runtime);
}
}
printf("\n进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i;j<n;j++)
{
if(jArray[j].submittime<=time)
{
printf("%s\t\t%d\t\t%d\n",jArray[j].jobname,jArray[j].submittime,jArray[j].runtime);
}
}
printf("\n处于运行的作业为:%s\n",jArray[i].jobname);
jArray[i].starttime=time;
jArray[i].finishtime=jArray[i].starttime+jArray[i].runtime;
jArray[i].turnovertime=jArray[i].finishtime-jArray[i].submittime;
jArray[i].turnnumber=(float)jArray[i].turnovertime/jArray[i].runtime;
time=jArray[i].finishtime;
printf("已完成的作业!\n");
printf("\n作业名称 提交时刻 运行时间 开始时刻 完成时间 周转时间 带权周转时间\n");
for(k=0;k<=i;k++)
{
averageround=averageround+jArray[i].turnovertime;
average=average+jArray[i].turnnumber;
printf(" %s %d %d %d %d %d %.2f\n\n",jArray[k].jobname,jArray[k].submittime,jArray[k].runtime,jArray[k].starttime,jArray[k].finishtime,jArray[k].turnovertime,jArray[k].turnnumber);
}
averageround=averageround/n;
printf("\n平均作业周转时间=%.2f\n",averageround);
average=average/n;
printf("\n平均作业带权周转时间=%.2f\n\n\n\n",average);
i++;
}
}
void SJFsort(int a,int b)
{
int i,j;
for(i=a;i<b;i++)
{
for(j=i+1;j<=b;j++)
{
if(jArray[i].runtime>jArray[j].runtime)
{
temp[j]=jArray[j];
jArray[j]=jArray[i];
jArray[i]=temp[j];
}
}
}
for(i=a;i<=b;i++)
{
printf("%s\t\t%d\t\t%d\n",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
}
}
void SJF(int n)
{
int time=-1,i=0,j,l,k,z,a,b;
float averageround=0.00;
float average=0.00;
sort(n);
while(i<n)
{
l=0;
z=0;
while(jArray[i].submittime>time)
{
l++;
time++;
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
if(l==0)
{
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
printf("\n还未进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i+1;j<n;j++)
{
if(jArray[j].submittime>time)
{
printf("%s\t\t%d\t\t%d\n",jArray[j].jobname,jArray[j].submittime,jArray[j].runtime);
}
}
printf("\n进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i;j<n;j++)
{
if(jArray[j].submittime<=time)
{
z++;
}
}
a=i;
b=i+z-1;
SJFsort(a,b);
printf("\n处于运行的作业为:%s\n",jArray[i].jobname);
jArray[i].starttime=time;
jArray[i].finishtime=jArray[i].starttime+jArray[i].runtime;
jArray[i].turnovertime=jArray[i].finishtime-jArray[i].submittime;
jArray[i].turnnumber=(float)jArray[i].turnovertime/jArray[i].runtime;
time=jArray[i].finishtime;
printf("已完成的作业!\n");
printf("\n作业名称 提交时刻 运行时间 开始时刻 完成时间 周转时间 带权周转时间\n");
for(k=0;k<=i;k++)
{
averageround=averageround+jArray[i].turnovertime;
average=average+jArray[i].turnnumber;
printf(" %s %d %d %d %d %d %.2f\n\n",jArray[k].jobname,jArray[k].submittime,jArray[k].runtime,jArray[k].starttime,jArray[k].finishtime,jArray[k].turnovertime,jArray[k].turnnumber);
}
averageround=averageround/n;
printf("\n平均作业周转时间=%.2f\n",averageround);
average=average/n;
printf("\n平均作业带权周转时间=%.2f\n\n\n\n",average);
i++;
}
}
void RP(int a,int b)
{
int i,j;
for(i=a;i<b;i++)
{
for(j=i+1;j<=b;j++)
{
if(jArray[i].rp<jArray[j].rp)
{
temp[j]=jArray[j];
jArray[j]=jArray[i];
jArray[i]=temp[j];
}
}
}
for(i=a;i<=b;i++)
{
printf("%s\t\t%d\t\t%d\n",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
}
}
void HRRN(int n)
{
int time=-1,i=0,j,l,k,z,a,b;
float averageround=0.00;
float average=0.00;
sort(n);
while(i<n)
{
l=0;
z=0;
while(jArray[i].submittime>time)
{
l++;
time++;
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
if(l==0)
{
printf("\n当前系统时间为:%d\n",time);
printf("\n请按任意键继续......");
fflush(stdin);
getch();
}
printf("\n还未进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i+1;j<n;j++)
{
if(jArray[j].submittime>time)
{
printf("%s\t\t%d\t\t%d\n",jArray[j].jobname,jArray[j].submittime,jArray[j].runtime);
}
}
printf("\n进入后备队列的作业!\n");
printf("作业名称\t提交时刻\t运行时间\n");
for(j=i;j<n;j++)
{
if(jArray[j].submittime<=time)
{
z++;
}
}
a=i;
b=i+z-1;
for(j=a;j<=b;j++)
{
jArray[j].finishtime=time+jArray[j].runtime;
jArray[j].turnovertime=jArray[j].finishtime-jArray[j].submittime;
jArray[j].turnnumber=(float)jArray[j].turnovertime/jArray[j].runtime;
jArray[j].rp=1+(float)(time-jArray[j].submittime)/jArray[j].runtime;
}
RP(a,b);
printf("\n处于运行的作业为:%s\n",jArray[i].jobname);
jArray[i].starttime=time;
jArray[i].finishtime=jArray[i].starttime+jArray[i].runtime;
jArray[i].turnovertime=jArray[i].finishtime-jArray[i].submittime;
jArray[i].turnnumber=(float)jArray[i].turnovertime/jArray[i].runtime;
time=jArray[i].finishtime;
printf("已完成的作业!\n");
printf("\n作业名称 提交时刻 运行时间 开始时刻 完成时间 周转时间 带权周转时间\n");
for(k=0;k<=i;k++)
{
averageround=averageround+jArray[i].turnovertime;
average=average+jArray[i].turnnumber;
printf(" %s %d %d %d %d %d %.2f\n\n",jArray[k].jobname,jArray[k].submittime,jArray[k].runtime,jArray[k].starttime,jArray[k].finishtime,jArray[k].turnovertime,jArray[k].turnnumber);
}
averageround=averageround/n;
printf("\n平均作业周转时间=%.2f\n",averageround);
average=average/n;
printf("\n平均作业带权周转时间=%.2f\n\n\n\n",average);
i++;
}
}
void quit()
{
printf("谢谢使用该系统!\n");
exit(0);
}
int File()
{
int m=0;
FILE *fp;
if((fp=fopen("cmd.txt","a+"))==NULL)
{
printf("File open error!\n");
exit(0);
}
printf("\n 作业名称\t提交时刻\t运行时间\n");
while(!feof(fp)&&fgetc(fp)!=EOF)
{
fseek(fp,-1L,SEEK_CUR);
fscanf(fp,"%s%d%d",jArray[count].jobname,&jArray[count].submittime,&jArray[count].runtime);
printf("\n%3s%15d%17d",jArray[count].jobname,jArray[count].submittime,jArray[count].runtime);
count++;
}
if(fclose(fp))
{
printf("Can not close the file!\n");
exit(0);
}
m=count-1;
return m;
}
/*void display()
{
for(int i=0;i<count;i++)
{
printf("%-10s%d%d\n",strupr(jArray[count].jobname),&jArray[count].submittime,&jArray[count].runtime);//将字符串s转换为大写形式,如:char* s2=strupr(s);把s数组中的字符串转换成大写并且返回s用来初始化s2
strlwr(jArray[count].jobname);//将字符串s转换为小写形式
}
}*/
int sand(int n)
{
srand((unsigned)time(0));
for(int j=0;j<n;j++)
{
printf("请输入第%d个作业的名称:",j+1);
scanf("%s",jArray[j].jobname);
}
printf("\n 作业名称\t提交时刻\t运行时间\n");
for(int i=0;i<n;i++)
{
// jArray[i].jobname=rand()%9;
jArray[i].submittime=rand() %24;
jArray[i].runtime=rand() %100;
printf("\n%3s%15d%17d",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
}
/* for(int j=0;j<=n;j++)
{
printf("\n%3s%15d%17d\n",jArray[j].jobname,jArray[j].submittime,jArray[j].runtime);
}*/
// printf("\n%3s%15d%17d",jArray[i].jobname,jArray[i].submittime,jArray[i].runtime);
return n;
}
int Menu1()
{
int n;
printf("******************************************\n");
printf("1、调用文本写入数据\n");
printf("2、调用伪随机数得产生的数据\n");
printf("3、调用自己输入模拟数据\n");
printf("4、退出程序\n");
printf("******************************************\n");
printf("请选择菜单项:");
scanf("%d",&n);
return n;
}
void main()
{
int choic,n,choise;
while(1)
{
choise=Menu1();
switch(choise)
{
case 1:
n=6;
File();
break;
case 2:
printf("请用户输入相应的数据:\n");
printf("请输入作业个数(2~24):");
while(!scanf("%d",&n)||n<2||n>24)
{
getchar();
printf("输入错误!!!!!!请重新输入\n");
printf("请输入作业个数(2~24):");
}
sand(n);
break;
case 3:
printf("请用户输入相应的数据:\n");
printf("请输入作业个数(2~24):");
while(!scanf("%d",&n)||n<2||n>24)
{
getchar();
printf("输入错误!!!!!!请重新输入\n");
printf("请输入作业个数(2~24):");
}
input(n);
break;
case 4:
quit();
break;
}
choic=Menu();
switch(choic)
{
case 1:
FCFS(n);
break;
case 2:
SJF(n);
break;
case 3:
HRRN(n);
break;
case 4:
quit();
break;
}
}
}