《操作系统进程调度C语言代码(共15页).docx》由会员分享,可在线阅读,更多相关《操作系统进程调度C语言代码(共15页).docx(15页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、精选优质文档-倾情为你奉上/ sun.cpp : 定义控制台应用程序的入口点。/本算法包含四种调度:先到先服务,短作业优先,时间片轮转,优先级优先!#includestdio.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/*
2、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(a4) break;void sjp()int i,j,n,min,px,sjp,time;float sum1,sum2;bool flag=true;printf(t请输入有n个进程(
3、0n50|n=0)printf(nt请重新输入: );scanf(%d,&n);printf(nn);printf(t请输入时间片大小(0sjp):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=0;j-) for(i=0;iai+1.dt) min=ai
4、.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(minn)flag = true;for(i = 0;i0&ai.dt=time)flag = false;for(i=0;i 0 )if(ai.dt=time)/printf(当前a%d.st2值为:
5、%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;i0&ai.dttime)time = ai.dt;break;printf(t1、按i
6、d号依次输出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;in;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平均带权周转时间:%
7、.2fnn,sum2/n);break;case 1: for(j=0;jn;j+) for(i=0;in;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;pr
8、intf(t请输入有n个进程(0n50|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=0;j-) for(i=0;iai+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
9、.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;iai-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,&p
10、x);printf(nid:到达时间t服务时间t完成时间t周转时间t带权周转时间n);sum1=0;sum2=0;switch(px)case 2: for(i=0;in;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;jn;j+) for(i=0;in;i+)if(ai
11、.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个进程(0n50|n=0)printf(nt请重新输入: );scanf(%d,
12、&n);printf(n);struct Gzuoint id; /进程名字int dt; /到达时刻int st; /服务时间int wct; /完成时刻float zt; /周转时间float dczt; /带权周转时间;Gzuo aN;for(i=0;i=0;j-) for(i=0;iai+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.stai+1.st)min=ai
13、.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;ia0.wct) ;else b=b+1;for(j=b-1;j=1;j-) for(i=1;iai+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;
14、min=ai.id;ai.id=ai+1.id;ai+1.id=min; for(i=1;iai-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;jai.wct) ;else b=b+1;for(j=b-1;j=i;j-) for(z=i;zaz+1.st) min=az.dt;az.dt=az+1.dt;az+1.dt=min;min=a
15、z.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;in;i+) printf(%d: %dtt%dtt%dtt%.0ftt%.2fn,ai.id,ai.dt,ai.st,ai.wct,ai.zt,ai
16、.dczt);sum1+=ai.zt;sum2+=ai.dczt;printf(n平均周转时间:%.2fn,sum1/n);printf(n平均带权周转时间:%.2fnn,sum2/n);break;case 1: for(j=0;jn;j+) for(i=0;in;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平均带权周转时间:%.2
17、fnn,sum2/n);break; default: break;void yxj()int i,j,n,min,px;int b=0,z;float sum1,sum2;printf(ntt请输入有n个进程(0n50|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=0;j-) for(i=0;
18、iai+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.yxjai+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;
19、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;ia0.wct) ;else b+;for(j=b-1;j=1;j-) for(i=1;ij;i+)if(ai.yxjai+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.y
20、xj=ai+1.yxj;ai+1.yxj=min; for(i=1;iai-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;jai.wct) ;else b=b+1;for(j=b-1;j=i;j-) for(z=i;zj;z+)if(az.yxjaz+1.yxj) min=az.dt;az.dt=az+1.dt;az+1.dt=min;m
21、in=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;in;i+) printf(%d: %dtt%dtt%dtt%dtt%.0ftt%.2fn,ai.id,ai.dt,ai.yxj,ai
22、.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;jn;j+) for(i=0;in;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;专心-专注-专业