操作系统进程调度C语言代码.docx
《操作系统进程调度C语言代码.docx》由会员分享,可在线阅读,更多相关《操作系统进程调度C语言代码.docx(25页珍藏版)》请在冰豆网上搜索。
操作系统进程调度C语言代码
//sun.cpp:
定义控制台应用程序的入口点。
//本算法包含四种调度:
先到先服务,短作业优先,时间片轮转,优先级优先!
#include"stdio.h"
#defineN50
voidmain()
{voidsjp();
voidfcfs();
voidsjf();
voidyxj();
inta;
while(true)
{
printf("\n\n");
printf("\t\t/*************************/");
printf("\n\t\t/*1、先到先服务调度
printf("\n\t\t/*2、短作业优先调度
printf("\n\t\t/*3、时间片轮转调度
printf("\n\t\t/*4、优先级优先调度
printf("\n\t\t/*0、退出*/\n");
printf("\t\t/*************************/");
printf("\n\n\t请选择菜单项:
\t");
scanf("%d",&a);
printf("\n");
switch(a)
{
case1:
fcfs();break;
case2:
sjf();break;
case3:
sjp();break;
case4:
yxj();break;
default:
break;
}
if(a<0&&a>4)break;
}
}
voidsjp()
{
inti,j,n,min,px,sjp,time;
floatsum1,sum2;
boolflag=true;
printf("\t请输入有n个进程(0\t");
scanf("%d",&n);
while(n>50||n<=0)
{
printf("n\t请重新输入:
");
scanf("%d",&n);*/");
*/");
*/");
*/");
}
printf("\n\n");
printf("\t请输入时间片大小(0\t");
scanf("%d",&sjp);
while(sjp<=0)
{
printf("n\t请重新输入:
");
scanf("%d",&sjp);
}
structGzuo{
intid;//进程名字
intdt;//到达时刻
intst;//服务时间
intwct;//完成时刻
intst2;//标志是否完成
floatzt;//周转时间
floatdczt;//带权周转时间
};
Gzuoa[N];
for(i=0;i{
a[i].id=i+1;
printf("\t到达时间:
");
scanf("%d",&a[i].dt);
printf("\t服务时间:
");
scanf("%d",&a[i].st);
a[i].st2=a[i].st;
printf("\n");
}
for(j=n-1;j>=0;j--)
{
for(i=0;i{
if(a[i].dt>a[i+1].dt)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].st2;
a[i].st2=a[i+1].st2;
a[i+1].st2=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
time=a[0].dt;
//printf("赋值后TIME值为:
%d\n",time);
min=0;
while(min{
flag=true;
for(i=0;i{
if(a[i].st2>0&&a[i].dt<=time)
flag=false;
}
for(i=0;i{
if(a[i].st2>0)
{
if(a[i].dt<=time)
{
//printf("当前a[%d].st2值为:
%d\n",i,a[i].st2);
a[i].st2=a[i].st2-sjp;
//printf("运算后当前a[%d].st2值为:
%d\n",i,a[i].st2);
//printf("当前TIME值为:
%d\n",time);
time=time+sjp;
//printf("增加之后TIME值为:
%d\n",time);
if(a[i].st2<=0)
{
a[i].wct=time+a[i].st2;
a[i].zt=(float)(a[i].wct-a[i].dt);
a[i].dczt=a[i].zt/a[i].st;
min++;
}
}elseif(flag)
{
for(i=0;i{
if(a[i].st2>0&&a[i].dt>time)
{
time=a[i].dt;
break;
}}
}
}
}
}
printf("\t1、按id号依次输出\n");
printf("\t2、按完成顺序依次输出\n");
printf("\n\t请选择输出顺序:
\t");
scanf("%d",&px);
printf("\nid:
到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n");sum1=0;
sum2=0;
switch(px)
{
case2:
{
for(i=0;i{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
case1:
{
for(j=0;j{
for(i=0;iif(a[i].id==j+1)
{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
default:
break;
}
}
voidfcfs()
{
inti,j,n,min,px;
floatsum1,sum2;
printf("\t请输入有n个进程(0\t");
scanf("%d",&n);
while(n>50||n<=0)
{
printf("n\t请重新输入:
");
scanf("%d",&n);
}
printf("\n\n");
structGzuo{
intid;//进程名字
intdt;//到达时刻
intst;//服务时间
intwct;//完成时刻
floatzt;//周转时间
floatdczt;//带权周转时间
};
Gzuoa[N];
for(i=0;i{
a[i].id=i+1;
printf("\t到达时间:
");
scanf("%d",&a[i].dt);
printf("\t服务时间:
");
scanf("%d",&a[i].st);
printf("\n");
}
for(j=n-1;j>=0;j--)
{
for(i=0;i{
if(a[i].dt>a[i+1].dt)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
a[0].wct=a[0].st+a[0].dt;
a[0].zt=(float)a[0].st;
a[0].dczt=a[0].zt/a[0].st;
for(i=1;i{
if(a[i].dt>a[i-1].wct)
{
a[i].wct=a[i].dt+a[i].st;
a[i].zt=(float)a[i].st;
a[i].dczt=a[i].zt/a[i].st;
}
else
{
a[i].wct=a[i-1].wct+a[i].st;
a[i].zt=(float)(a[i].wct-a[i].dt);
a[i].dczt=a[i].zt/a[i].st;
}
}
printf("\t1、按id号依次输出\n");
printf("\t2、按完成顺序依次输出\n");
printf("\n\t请选择输出顺序:
\t");
scanf("%d",&px);
printf("\nid:
到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n");sum1=0;
sum2=0;
switch(px)
{
case2:
{
for(i=0;i{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
case1:
{
for(j=0;j{
for(i=0;iif(a[i].id==j+1)
{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
default:
break;
}
}
voidsjf()
{
inti,j,n,min,px;
intb=0,z;
floatsum1,sum2;
printf("\n\t\t请输入有n个进程(0\t");
scanf("%d/n",&n);
while(n>50||n<=0)
{
printf("n\t请重新输入:
");
scanf("%d",&n);
}
printf("\n");
structGzuo{
intid;//进程名字
intdt;//到达时刻
intst;//服务时间
intwct;//完成时刻
floatzt;//周转时间
floatdczt;//带权周转时间
};
Gzuoa[N];
for(i=0;i{
a[i].id=i+1;
printf("\t到达时间:
");
scanf("%d",&a[i].dt);
printf("\t服务时间:
");
scanf("%d",&a[i].st);
printf("\n");
}
min=a[0].dt;
for(j=n-1;j>=0;j--)
{
for(i=0;i{
if(a[i].dt>a[i+1].dt)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
if(a[i].dt==a[i+1].dt&&a[i].st>a[i+1].st)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
a[0].wct=a[0].st+a[0].dt;
a[0].zt=(float)a[0].st;
a[0].dczt=a[0].zt/a[0].st;
for(i=1;i{
if(a[i].dt>a[0].wct);
elseb=b+1;
}
for(j=b-1;j>=1;j--)
{
for(i=1;i{
if(a[i].st>a[i+1].st)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
for(i=1;i{
if(a[i].dt>a[i-1].wct)
{
a[i].wct=a[i].dt+a[i].st;
a[i].zt=(float)a[i].st;
a[i].dczt=a[i].zt/a[i].st;
}
else
{
a[i].wct=a[i-1].wct+a[i].st;
a[i].zt=(float)(a[i].wct-a[i].dt);
a[i].dczt=a[i].zt/a[i].st;
}
for(j=i+1,b=j;j{
if(a[j].dt>a[i].wct);
elseb=b+1;
}
for(j=b-1;j>=i;j--)
{
for(z=i;z{
if(a[z].st>a[z+1].st)
{
min=a[z].dt;
a[z].dt=a[z+1].dt;
a[z+1].dt=min;
min=a[z].st;
a[z].st=a[z+1].st;
a[z+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
}
printf("\n\t请选择输出顺序\n");
printf("\t1、按id号依次输出\n");
printf("\t2、按完成顺序依次输出\n");
scanf("%d",&px);
printf("\nid:
到达时间\t服务时间\t完成时间\t周转时间\t带权周转时间\n");sum1=0;
sum2=0;
switch(px)
{
case2:
{
for(i=0;i{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
case1:
{
for(j=0;j{for(i=0;iif(a[i].id==j+1)
{
printf("%d:
%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
default:
break;
}
}
voidyxj()
{
inti,j,n,min,px;
intb=0,z;
floatsum1,sum2;
printf("\n\t\t请输入有n个进程(0\t");
scanf("%d/n",&n);
while(n>50||n<=0)
{
printf("n\t请重新输入:
");
scanf("%d",&n);
}
printf("\n");
structGzuo{
intid;//进程名字
intdt;//到达时刻
intst;//服务时间
intyxj;//优先级
intwct;//完成时刻
floatzt;//周转时间
floatdczt;//带权周转时间
};
Gzuoa[N];
for(i=0;i{
a[i].id=i+1;
printf("\t到达时间:
");
scanf("%d",&a[i].dt);
printf("\t服务时间:
");
scanf("%d",&a[i].st);
printf("\t优先级:
");
scanf("%d",&a[i].yxj);
printf("\n");
}
min=a[0].dt;
for(j=n-1;j>=0;j--)
{
for(i=0;i{
if(a[i].dt>a[i+1].dt)
{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
min=a[i].yxj;
a[i].yxj=a[i+1].yxj;
a[i+1].yxj=min;
}
if(a[i].dt==a[i+1].dt&&a[i].yxj{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
min=a[i].yxj;
a[i].yxj=a[i+1].yxj;
a[i+1].yxj=min;
}
}
}
a[0].wct=a[0].st+a[0].dt;
a[0].zt=(float)a[0].st;
a[0].dczt=a[0].zt/a[0].st;
for(i=1;i{
if(a[i].dt>a[0].wct);
elseb++;
}
for(j=b-1;j>=1;j--)
{
for(i=1;i{
if(a[i].yxj{
min=a[i].dt;
a[i].dt=a[i+1].dt;
a[i+1].dt=min;
min=a[i].st;
a[i].st=a[i+1].st;
a[i+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
min=a[i].yxj;
a[i].yxj=a[i+1].yxj;
a[i+1].yxj=min;
}
}
}
for(i=1;i{
if(a[i].dt>a[i-1].wct)
{
a[i].wct=a[i].dt+a[i].st;
a[i].zt=(float)a[i].st;
a[i].dczt=a[i].zt/a[i].st;
}
else
{
a[i].wct=a[i-1].wct+a[i].st;
a[i].zt=(float)(a[i].wct-a[i].dt);
a[i].dczt=a[i].zt/a[i].st;
}
for(j=i+1,b=j;j{
if(a[j].dt>a[i].wct);
elseb=b+1;
}
for(j=b-1;j>=i;j--)
{
for(z=i;z{
if(a[z].yxj{
min=a[z].dt;
a[z].dt=a[z+1].dt;
a[z+1].dt=min;
min=a[z].st;
a[z].st=a[z+1].st;
a[z+1].st=min;
min=a[i].id;
a[i].id=a[i+1].id;
a[i+1].id=min;
}
}
}
}
printf("\n\t请选择输出顺序\n");
printf("\t1、按id号依次输出\n");
printf("\t2、按完成顺序依次输出\n");
scanf("%d",&px);
printf("\nid:
到达时间\t服务时间\t优先级\t完成时间\t周转时间\t带权周转时间\n");sum1=0;
sum2=0;
switch(px)
{
case2:
{
for(i=0;i{
printf("%d:
%d\t\t%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].yxj,a[i].st,a[i].wct,a[i].zt,a[i].dczt);
sum1+=a[i].zt;
sum2+=a[i].dczt;
}
printf("\n平均周转时间:
%.2f\n",sum1/n);
printf("\n平均带权周转时间:
%.2f\n\n",sum2/n);
break;
}
case1:
{
for(j=0;j{for(i=0;iif(a[i].id==j+1)
{
printf("%d:
%d\t\t%d\t\t%d\t\t%d\t\t%.0f\t\t%.2f\n",a[i].id,a[i].dt,a[i].yx