操作系统进程调度C语言代码(共15页).docx
精选优质文档-倾情为你奉上/ sun.cpp : 定义控制台应用程序的入口点。/本算法包含四种调度:先到先服务,短作业优先,时间片轮转,优先级优先!#include"stdio.h"#define N 50void main() void sjp();void fcfs();void sjf();void yxj();int a;while(true)printf("nn");printf("tt/*/");printf("ntt/* 1、先到先服务 调 度 */");printf("ntt/* 2、短作业优先 调 度 */");printf("ntt/* 3、时间片轮转 调 度 */");printf("ntt/* 4、优先级优先 调 度 */");printf("ntt/* 0、 退 出 */n");printf("tt/*/");printf("nnt请选择菜单项:t");scanf("%d",&a);printf("n");switch(a)case 1: fcfs();break;case 2: sjf();break;case 3: sjp();break;case 4: yxj();break;default: break;if(a<0&&a>4) break;void sjp()int i,j,n,min,px,sjp,time;float sum1,sum2;bool flag=true;printf("t请输入有n个进程(0<n<=50):t");scanf("%d",&n);while(n>50|n<=0)printf("nt请重新输入: ");scanf("%d",&n);printf("nn");printf("t请输入时间片大小(0<sjp):t");scanf("%d",&sjp);while(sjp<=0)printf("nt请重新输入: ");scanf("%d",&sjp);struct Gzuoint id; /进程名字int dt; /到达时刻int st; /服务时间int wct; /完成时刻int st2; /标志是否完成float zt; /周转时间float dczt; /带权周转时间;Gzuo aN;for(i=0;i<n;i+)ai.id=i+1;printf("t到达时间: ");scanf("%d",&ai.dt);printf("t服务时间: ");scanf("%d",&ai.st);ai.st2 = ai.st;printf("n");for(j=n-1;j>=0;j-) for(i=0;i<j;i+)if(ai.dt>ai+1.dt) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.st2;ai.st2=ai+1.st2;ai+1.st2=min;min=ai.id;ai.id=ai+1.id;ai+1.id=min; time = a0.dt;/printf("赋值后TIME值为:%dn",time);min = 0;while(min<n)flag = true;for(i = 0;i<n;i+)if(ai.st2>0&&ai.dt<=time)flag = false;for(i=0;i<n;i+)if(ai.st2 > 0 )if(ai.dt<=time)/printf("当前a%d.st2值为:%dn",i,ai.st2);ai.st2 = ai.st2 - sjp;/printf("运算后当前a%d.st2值为:%dn",i,ai.st2);/printf("当前TIME值为:%dn",time);time = time + sjp;/printf("增加之后TIME值为:%dn",time);if(ai.st2<=0)ai.wct = time + ai.st2;ai.zt=(float)(ai.wct-ai.dt);ai.dczt=ai.zt/ai.st;min+;else if(flag)for(i=0;i<n;i+)if(ai.st2>0&&ai.dt>time)time = ai.dt;break;printf("t1、按id号依次输出n");printf("t2、按完成顺序依次输出n");printf("nt请选择输出顺序:t");scanf("%d",&px);printf("nid:到达时间t服务时间t完成时间t周转时间t带权周转时间n");sum1=0;sum2=0;switch(px)case 2: for(i=0;i<n;i+) printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break;case 1: for(j=0;j<n;j+) for(i=0;i<n;i+)if(ai.id=j+1)printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break; default: break;void fcfs()int i,j,n,min,px;float sum1,sum2;printf("t请输入有n个进程(0<n<=50):t");scanf("%d",&n);while(n>50|n<=0)printf("nt请重新输入: ");scanf("%d",&n);printf("nn");struct Gzuoint id; /进程名字int dt; /到达时刻int st; /服务时间int wct; /完成时刻float zt; /周转时间float dczt; /带权周转时间;Gzuo aN;for(i=0;i<n;i+)ai.id=i+1;printf("t到达时间: ");scanf("%d",&ai.dt);printf("t服务时间: ");scanf("%d",&ai.st);printf("n");for(j=n-1;j>=0;j-) for(i=0;i<j;i+)if(ai.dt>ai+1.dt) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min; a0.wct=a0.st+a0.dt;a0.zt=(float)a0.st;a0.dczt=a0.zt/a0.st;for(i=1;i<n;i+)if(ai.dt>ai-1.wct)ai.wct=ai.dt+ai.st;ai.zt=(float)ai.st;ai.dczt=ai.zt/ai.st;elseai.wct=ai-1.wct+ai.st;ai.zt=(float)(ai.wct-ai.dt);ai.dczt=ai.zt/ai.st; printf("t1、按id号依次输出n");printf("t2、按完成顺序依次输出n");printf("nt请选择输出顺序:t");scanf("%d",&px);printf("nid:到达时间t服务时间t完成时间t周转时间t带权周转时间n");sum1=0;sum2=0;switch(px)case 2: for(i=0;i<n;i+) printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break;case 1: for(j=0;j<n;j+) for(i=0;i<n;i+)if(ai.id=j+1)printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break; default: break;void sjf()int i,j,n,min,px;int b=0,z;float sum1,sum2;printf("ntt请输入有n个进程(0<n<=50):t");scanf("%d/n",&n);while(n>50|n<=0)printf("nt请重新输入: ");scanf("%d",&n);printf("n");struct Gzuoint id; /进程名字int dt; /到达时刻int st; /服务时间int wct; /完成时刻float zt; /周转时间float dczt; /带权周转时间;Gzuo aN;for(i=0;i<n;i+)ai.id=i+1;printf("t到达时间: ");scanf("%d",&ai.dt);printf("t服务时间: ");scanf("%d",&ai.st);printf("n");min=a0.dt; for(j=n-1;j>=0;j-) for(i=0;i<j;i+)if(ai.dt>ai+1.dt) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min;if(ai.dt=ai+1.dt&&ai.st>ai+1.st)min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min; a0.wct=a0.st+a0.dt;a0.zt=(float)a0.st;a0.dczt=a0.zt/a0.st;for(i=1;i<n;i+)if(ai.dt>a0.wct) ;else b=b+1;for(j=b-1;j>=1;j-) for(i=1;i<j;i+)if(ai.st>ai+1.st) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min; for(i=1;i<n;i+)if(ai.dt>ai-1.wct)ai.wct=ai.dt+ai.st;ai.zt=(float)ai.st;ai.dczt=ai.zt/ai.st;elseai.wct=ai-1.wct+ai.st;ai.zt=(float)(ai.wct-ai.dt);ai.dczt=ai.zt/ai.st; for(j=i+1,b=j;j<n;j+)if(aj.dt>ai.wct) ;else b=b+1;for(j=b-1;j>=i;j-) for(z=i;z<j;z+)if(az.st>az+1.st) min=az.dt;az.dt=az+1.dt;az+1.dt=min;min=az.st;az.st=az+1.st;az+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min; printf("nt请选择输出顺序n");printf("t1、按id号依次输出n");printf("t2、按完成顺序依次输出n");scanf("%d",&px);printf("nid:到达时间t服务时间t完成时间t周转时间t带权周转时间n");sum1=0;sum2=0;switch(px)case 2: for(i=0;i<n;i+) printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break;case 1: for(j=0;j<n;j+) for(i=0;i<n;i+)if(ai.id=j+1)printf("%d: %dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break; default: break;void yxj()int i,j,n,min,px;int b=0,z;float sum1,sum2;printf("ntt请输入有n个进程(0<n<=50):t");scanf("%d/n",&n);while(n>50|n<=0)printf("nt请重新输入: ");scanf("%d",&n);printf("n");struct Gzuoint id; /进程名字int dt; /到达时刻int st; /服务时间int yxj; /优先级int wct; /完成时刻float zt; /周转时间float dczt; /带权周转时间;Gzuo aN;for(i=0;i<n;i+)ai.id=i+1;printf("t到达时间: ");scanf("%d",&ai.dt);printf("t服务时间: ");scanf("%d",&ai.st);printf("t优先级: ");scanf("%d",&ai.yxj);printf("n");min=a0.dt; for(j=n-1;j>=0;j-) for(i=0;i<j;i+)if(ai.dt>ai+1.dt) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min;min=ai.yxj;ai.yxj=ai+1.yxj;ai+1.yxj=min;if(ai.dt=ai+1.dt&&ai.yxj<ai+1.yxj)min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min;min=ai.yxj;ai.yxj=ai+1.yxj;ai+1.yxj=min; a0.wct=a0.st+a0.dt;a0.zt=(float)a0.st;a0.dczt=a0.zt/a0.st;for(i=1;i<n;i+)if(ai.dt>a0.wct) ;else b+;for(j=b-1;j>=1;j-) for(i=1;i<j;i+)if(ai.yxj<ai+1.yxj) min=ai.dt;ai.dt=ai+1.dt;ai+1.dt=min;min=ai.st;ai.st=ai+1.st;ai+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min;min=ai.yxj;ai.yxj=ai+1.yxj;ai+1.yxj=min; for(i=1;i<n;i+)if(ai.dt>ai-1.wct)ai.wct=ai.dt+ai.st;ai.zt=(float)ai.st;ai.dczt=ai.zt/ai.st;elseai.wct=ai-1.wct+ai.st;ai.zt=(float)(ai.wct-ai.dt);ai.dczt=ai.zt/ai.st; for(j=i+1,b=j;j<n;j+)if(aj.dt>ai.wct) ;else b=b+1;for(j=b-1;j>=i;j-) for(z=i;z<j;z+)if(az.yxj<az+1.yxj) min=az.dt;az.dt=az+1.dt;az+1.dt=min;min=az.st;az.st=az+1.st;az+1.st=min; min=ai.id;ai.id=ai+1.id;ai+1.id=min; printf("nt请选择输出顺序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)case 2: for(i=0;i<n;i+) printf("%d: %dtt%dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.yxj,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break;case 1: for(j=0;j<n;j+) for(i=0;i<n;i+)if(ai.id=j+1)printf("%d: %dtt%dtt%dtt%dtt%.0ftt%.2fn",ai.id,ai.dt,ai.yxj,ai.st,ai.wct,ai.zt,ai.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf("n平均周转时间:%.2fn",sum1/n);printf("n平均带权周转时间:%.2fnn",sum2/n);break; default: break;专心-专注-专业