进程调度问题之FCFS|进程调度问题之FCFS,SJF,HRRN

?? #include
#define N 5
typedef struct Process
{
char name;
int arrivetime;
int servetime;
int finishtime;
int cyclingtime;
double wcyclingtime;
int beginning;
};
struct Process pro[N]={//FCFS
{'A',0,3,0,0,0},
{'B',2,6,0,0,0},
{'C',4,4,0,0,0},
{'D',6,5,0,0,0},
{'E',8,2,0,0,0}
};
struct Process pro1[N]={//SJF
{'A',0,3,0,0,0},
{'B',2,6,0,0,0},
{'C',4,4,0,0,0},
{'D',6,5,0,0,0},
{'E',8,2,0,0,0}
};
struct Process pro2[N]={//HRRN
{'A',0,3,0,0,0},
{'B',2,6,0,0,0},
{'C',4,4,0,0,0},
{'D',6,5,0,0,0},
{'E',8,2,0,0,0}
};
void Input()
{
int m;
for(m=0; m<=4; m++)
{
printf("进程%d:\n",m);
printf("进程名:");
scanf("%s",&pro[m].name);
printf("到达时间:");
scanf("%d",&pro[m].arrivetime);
printf("服务时间:");
scanf("%d",&pro[m].servetime);
printf("\n");
}
}
void Display()
{
int a,b,c;
printf("进程名:\t\t");
for(a=0; aprintf("%c\t",pro[a].name);
printf("\n");
printf("到达时间:\t");
for(b=0; bprintf("%d\t",pro[b].arrivetime);
printf("\n");
printf("服务时间:\t");
for(c=0; cprintf("%d\t",pro[c].servetime);
printf("\n");
}
void sort()//按到达时间排序
{
int i,j,t,x;
char y;
for(j=0; j{
for(i=0; i{
if(pro[i].arrivetime>pro[i+1].arrivetime)
{
t=pro[i+1].arrivetime;
pro[i+1].arrivetime=pro[i].arrivetime;
pro[i].arrivetime=t;
x=pro[i+1].servetime;
pro[i+1].servetime=pro[i].servetime;
pro[i].servetime=x;
y=pro[i+1].name;
pro[i+1].name=pro[i].name;
pro[i].name=y;
}
}
}
}
void FCFS()
{
int k,d,j;
int current=0;
int n=0;
int iflag=0;
int flag[N]={0};
double sum1=0,sum2=0;
printf("先来先服务算法:\n\n");
while(n<5)
{
for(d=0; d{
if(current==pro[d].arrivetime&&flag[d]==0)
printf("%d\t进程%c到达内存\n\n",current,pro[d].name);

if(current>=pro[d].arrivetime&&iflag==0&&flag[d]==0)
{
iflag=1;
flag[d]=1;
pro[d].beginning=current;
printf("%d\t进程%c开始执行\n\n",pro[d].beginning,pro[d].name);
}

if((current==(pro[d].beginning+pro[d].servetime))&&iflag==1&&flag[d]==1)
{
iflag=0;
pro[d].finishtime=current;
printf("%d\t进程%c结束!\n\n",current,pro[d].name);
n++;

}
}
current++;
}
for(j=0; j{
pro[j].cyclingtime=pro[j].finishtime-pro[j].arrivetime;
pro[j].wcyclingtime=pro[j].cyclingtime*1.00/pro[j].servetime;
}

printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro[k].name,pro[k].arrivetime,pro[k].servetime,pro[k].finishtime,pro[k].cyclingtime,pro[k].wcyclingtime);

}
for(j=0; j{
sum1+=pro[j].cyclingtime;
sum2+=pro[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum1/N,sum2/N);
}

void sort1()//按服务时间排序
{
int i,j,t,x;
char y;
for(j=0; j{
for(i=0; i{
if(pro1[i].servetime>pro1[i+1].servetime)
{
t=pro1[i+1].arrivetime;
pro1[i+1].arrivetime=pro1[i].arrivetime;
pro1[i].arrivetime=t;
x=pro1[i+1].servetime;
pro1[i+1].servetime=pro1[i].servetime;
pro1[i].servetime=x;
y=pro1[i+1].name;
pro1[i+1].name=pro1[i].name;
pro1[i].name=y;
}
}
}
}
void SJF()
{
int d,k,j;
int flag1[N]={0};
int iflag=0;
int n=0;
int current=0;
double sum1=0,sum2=0;
printf("短作业优先算法:\n\n");
while(n<5)
{
for(d=0; d{
if(current==pro1[d].arrivetime&&flag1[d]==0)
printf("%d\t进程%c到达内存\n\n",current,pro1[d].name);

if(current>=pro1[d].arrivetime&&iflag==0&&flag1[d]==0)
{
iflag=1;
flag1[d]=1;
pro1[d].beginning=current;
printf("%d\t进程%c开始执行\n\n",current,pro1[d].name);
}

if((current==(pro1[d].beginning+pro1[d].servetime))&&iflag==1&&flag1[d]==1)
{
iflag=0;
pro1[d].finishtime=current;
printf("%d\t进程%c结束!\n\n",current,pro1[d].name);
n++;
if(d<4)
{
if(current>=pro1[d+1].arrivetime&&flag1[d+1]==0)
{
iflag=1;
flag1[d+1]=1;
pro1[d+1].beginning=current;
printf("%d\t进程%c开始执行\n\n",current,pro1[d+1].name);
}
}
else if(d==4)
{
iflag=1;
flag1[0]=1;
pro1[0].beginning=current;
printf("%d\t进程%c开始执行\n\n",current,pro1[0].name);
}
}

}
current++;
}
for(j=0; j{
pro1[j].cyclingtime=pro1[j].finishtime-pro1[j].arrivetime;
pro1[j].wcyclingtime=pro1[j].cyclingtime*1.00/pro1[j].servetime;
}
printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro1[k].name,pro1[k].arrivetime,pro1[k].servetime,pro1[k].finishtime,pro1[k].cyclingtime,pro1[k].wcyclingtime);

}
for(j=0; j{
sum1+=pro1[j].cyclingtime;
sum2+=pro1[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum1/N,sum2/N);
}
int priority(int flag2[N],int finishtime,int reach[N])
{
int d,ID;
double max;
double first[N]={0};
for(d=1; d{
if(flag2[d]==0&&reach[d]==1)
first[d]=(finishtime-pro2[d].arrivetime+pro2[d].servetime)*1.00/pro2[d].servetime; //计算优先级
}
max=0;
for(d=1; d{
if(flag2[d]==0&&reach[d]==1)
{
if(max{
max=first[d];
ID=d;
}
}
}
return ID;
}
void HRRN()
{
int d,k,j;
int n=0;
int iflag=0,flag2[N]={0},reach[N]={0};
int current=0,finishtime=0;
double sum1=0,sum2=0;
printf("高响应比调度算法:\n\n");
printf("%d\t进程%c到达内存\n\n",current,pro2[0].name);
reach[0]=1;
pro2[0].beginning=0;
printf("%d\t进程%c开始执行\n\n",current,pro2[0].name);
pro2[0].finishtime=pro2[0].servetime;
iflag=1;
flag2[0]=1;
while(n<5)
{
for(d=0; d{
if(current==pro2[d].arrivetime&&flag2[d]==0)
{
reach[d]=1;
printf("%d\t进程%c到达内存\n\n",current,pro2[d].name);
}


if((current==(pro2[d].beginning+pro2[d].servetime))&&iflag==1&&flag2[d]==1)
{
iflag=0;
pro2[d].finishtime=current;
finishtime=current;
printf("%d\t进程%c结束!\n\n",current,pro2[d].name);
n++;

if(n<=4)
{
k=priority(flag2,finishtime,reach);
if(reach[k]==1)
{
iflag=1;
flag2[k]=1;
pro2[k].beginning=current;
printf("%d\t进程%c开始执行\n\n",current,pro2[k].name);
}
}

}
}
current++;
}
//输出
for(j=0; j{
pro2[j].cyclingtime=pro2[j].finishtime-pro2[j].arrivetime;
pro2[j].wcyclingtime=pro2[j].cyclingtime*1.00/pro2[j].servetime;
}

printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro2[k].name,pro2[k].arrivetime,pro2[k].servetime,pro2[k].finishtime,pro2[k].cyclingtime,pro2[k].wcyclingtime);

}
for(j=0; j{
sum1+=pro2[j].cyclingtime;
sum2+=pro2[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum1/N,sum2/N);
}
void Compare()
{
int k,j;
double sum1=0,sum2=0,sum3=0,sum4=0,sum5=0,sum6=0;
printf("先来先服务算法:\n");
printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro[k].name,pro[k].arrivetime,pro[k].servetime,pro[k].finishtime,pro[k].cyclingtime,pro[k].wcyclingtime);

}
【进程调度问题之FCFS|进程调度问题之FCFS,SJF,HRRN】 for(j=0; j{
sum1+=pro[j].cyclingtime;
sum2+=pro[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum1/N,sum2/N);

printf("短作业优先算法:\n");
printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro1[k].name,pro1[k].arrivetime,pro1[k].servetime,pro1[k].finishtime,pro1[k].cyclingtime,pro1[k].wcyclingtime);

}
for(j=0; j{
sum3+=pro1[j].cyclingtime;
sum4+=pro1[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum3/N,sum4/N);

printf("高响应比调度算法:\n");
printf("进程名\t到达时间 服务时间 完成时间 周转时间 带权周转\n");
for(k=0; k{
printf("%3c\t %3d\t%3d\t%3d\t%3d\t%.2f\n",pro2[k].name,pro2[k].arrivetime,pro2[k].servetime,pro2[k].finishtime,pro2[k].cyclingtime,pro2[k].wcyclingtime);

}
for(j=0; j{
sum5+=pro2[j].cyclingtime;
sum6+=pro2[j].wcyclingtime;
}
printf("平均周转时间:%.2f\t\t平均带权周转时间:%.2f\n",sum5/N,sum6/N);
}
void Time()
{
}
void show()
{
printf("进程调度模拟程序\n");
printf("1、输入作业情况\n");
printf("2、显示作业情况\n");
printf("3、先来先服务算法\n");
printf("4、短作业优先算法\n");
printf("5、高响应比优先算法\n");
printf("6、时间片轮转调度算法\n");
printf("7、算法结果比对\n");
printf("0、退出\n");
}
void main()
{
int n;
show();
printf("请输入选择:");
scanf("%d",&n);
while(n<=6)
{
switch(n)
{
case 0:
break;
case 1:
Input();
break;
case 2:
Display();
break;
case 3:
Display();
FCFS(); //先来先服务算法
break;
case 4:
Display();
sort1();
SJF(); //短作业优先算法
break;
case 5:
Display();
HRRN(); //高响应比优先算法
break;
case 6:
Compare();
break;
}
if(n==0)
break;
printf("请输入选择:");
scanf("%d",&n);
}
}
运用结构体数组写的,可能不是最简单的,但是全部正确。

    推荐阅读