#include<stdio.h>
#include<stdlib.h>
struct jcb //作业的JCB块
{
char name[20];
float startime;
float arrivetime;
float servicetime;
float finishtime;
float zztime;
float dqzztime;
float Rp;
};
void input(struct jcb *p,int N)//输入作业相关信息
{
printf("请输入进程的名字 到达时间 服务时间:\n");
int i;
for(i=1;i<=N;i++)
{
printf("请输入进程%d的信息:\n", i);//i的初始值设为1,这样方便表示
printf("名称: ");
scanf("%s",&p[i-1].name);
printf("到达时间:");
scanf("%f",&p[i-1].arrivetime);
printf("所需cpu时间:");
scanf("%f",&p[i-1].servicetime);
printf("\n");
}
}
void sortfcfs(struct jcb *p,int N)//先来先服务的作业排序
{
int i,j;
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(p[j].arrivetime>p[j+1].arrivetime)
{
struct jcb temp;
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
}
void sortsjf(struct jcb *p,int N)//对start到i之间的进程进行排序
{ sortfcfs(p,N);
int k=0,i,t,j;
float end=0.0;//保存cpu已经运行的时间
for(i=0;i<N;i++){
k=i;//保存当前i的下表
while(p[i].arrivetime<=end&&i<N)//对从第i个之后的所有进程进行搜索
i++;//找到最后一个等待进程的下标(其实就是当前在等待的最后一个进程)
for ( t=k; t<i; t++)//对当前正在等待执行的进程按照服务时间排序
{
for (j = k+1; j<i; j++)
{
if(p[t].servicetime < p[j].servicetime)
continue;
else
{ struct jcb temp; //创建一个零时进程,便于进行交换
temp = p[t];
p[t] = p[j];
p[j] = temp;
}
}
}
i=k; //将i的下标还原
end+=p[i].servicetime;
}
}
void sorthrrf(struct jcb *p,int N)
{
float arrivetime=0,servicetime=0,startime=0,finishtime=0,zztime=0,dqzztime=0,Rp=0;
int m;
sortfcfs(p,N);
for( m=0;m<N-1;m++)
{
if(m==0)
p[m].finishtime=p[m].arrivetime+p[m].servicetime;
else
{
if(p[m-1].finishtime >=p[m].arrivetime )
{
p[m].startime=p[m-1].finishtime;
}
else
{
p[m].startime =p[m].arrivetime;
}
p[m].finishtime=p[m].startime+p[m].servicetime;
}
int i=0,n;
for(n=m+1;n<N;n++)
{
if(p[n].arrivetime<=p[m].finishtime)//按到达时间排序之后,有i个的后面的作业比m完成前到来
i++;
}
//按高响应比排序
p[m+1].Rp=(p[m].finishtime-p[m+1].arrivetime+p[m+1].servicetime)/p[m+1].servicetime; //Rp=上一个(m)完成时间-这个到达时间
float max=p[m+1].Rp;
int next=m+1,k;//m+1=n
for(k=m+1;k<m+i;k++)
{
p[k+1].Rp=(p[m].finishtime-p[k+1].arrivetime+p[k+1].servicetime)/p[k+1].servicetime;
if(p[k+1].Rp>max)
{
max=p[k+1].Rp;
next=k+1;
}
}
struct jcb temp;
temp=p[m+1];
p[m+1]=p[next];
p[next]=temp;
}
}
void run(struct jcb *p,int N)//计算每个作业的周转时间和带权周转时间
{
int i;
for(i=0;i<N;i++)
{
if(i==0){
p[i].startime=p[i].arrivetime;
p[i].finishtime=p[i].arrivetime+p[i].servicetime;
//周转时间=要求服务时间+等待时间
p[i].zztime=p[i].servicetime;
//其他进程:p[i].zztime=p[i].finishtime-p[i].arrivetime;
p[i].dqzztime=p[i].zztime/p[i].servicetime;
}else{
p[i].startime=p[i-1].finishtime;
p[i].finishtime=p[i].startime+p[i].servicetime;
p[i].zztime=p[i].finishtime-p[i].arrivetime;
p[i].dqzztime=p[i].zztime/p[i].servicetime;
}
}
}
void show(struct jcb *p,int N)//输出相应的时间
{
int i;
printf("调用算法后进程的运行顺序为:\n");
for(i=0;i<N;i++){
printf("%s",p[i].name);
printf("---->");
}
printf("\n");
printf("具体的进程调度信息为\n");
for(i=0;i<N;i++){
printf("进程为:%3s \n",p[i].name);
printf("到达时间为:%8.3f \n",p[i].arrivetime);
printf("服务时间为:%8.3f \n",p[i].servicetime);
printf("周转时间为:%8.3f\n",p[i].zztime);
printf("\n");
}
}
int main()//主函数
{
struct jcb a[100]; //创建100个结构体
int N; //进程数量
int x;
printf("请输入进程数目:");
scanf("%d",&N);
input(a,N);
printf("请选择算法(1是FCFS,2是SJF,3是HRRF)\n");
scanf("%d",&x);
if(x==1){
sortfcfs(a,N);
run(a,N);
show(a,N);
int i;
float average_zztime=0;
float average_dqzztime=0;
for(i=0;i<N;i++){
average_zztime+=a[i].zztime;
average_dqzztime+=a[i].dqzztime;
}
average_zztime/=N;
average_dqzztime/=N;
printf("采用FCFS算法算得平均周转时间为:%f",average_zztime);
printf("采用FCFS算法算得平均带权周转时间为:%f",average_dqzztime);
}
else if(x==2){
sortsjf(a,N);
run(a,N);
show(a,N);
int i;
float average_zztime=0;
float average_dqzztime=0;
for(i=0;i<N;i++){
average_zztime+=a[i].zztime;
average_dqzztime+=a[i].dqzztime;
}
average_zztime/=N;
average_dqzztime/=N;
printf("采用短作业优先算法算得平均周转时间为:%f",average_zztime);
printf("采用短作业优先算法算得平均带权周转时间为:%f",average_dqzztime);
}
else if(x==3){
sorthrrf(a,N);
run(a,N);
show(a,N);
int i;
float average_zztime=0;
float average_dqzztime=0;
for(i=0;i<N;i++){
average_zztime+=a[i].zztime;
average_dqzztime+=a[i].dqzztime;
}
average_zztime/=N;
average_dqzztime/=N;
printf("采用高响应比作业优先算法算得平均周转时间为:%f",average_zztime);
printf("采用高响应比作业优先算法算得平均带权周转时间为:%f",average_dqzztime);
}
else{
printf("error");
}
return 0;
}