新手求大神帮忙,进程的三个调度,不知道哪里出问题了。短作业和响应比里面的循环进不去。求大神解答。如果能顺便帮我优化下就更好了。

qq_41588438 2018-06-13 11:05:17
#include<stdio.h>
int main(){
int name[5]={0,0,0,0,0};//进程名称
int n; //进程的数目
double stime[5];//到达时间
double ntime[5];//运行所需时间
double achieve=0;//完成时间
double turn=0;//周转时间
double weight=0;//加权周转
int i,j,c;//循环控制变量
double tmp=0; //冒泡排序时数组stime、ntime用来交换的变量
int tmp1=0; //冒泡排序时数组name用来交换的变量
double MeanTurn,MeanWeight; //平均周转时间和加权周转时间
int t; //记录当前进程完成时后续有几个进程已经到达
double rp[5];//响应比

//获取需要的相应信息
printf("请输入进程的数目(到达时间相同的请先输入运行时间短的,最多输入5个进程):\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++){
name[i]=i+1;
printf("请输入进程%d的到达时间和所需时间\n",i+1);
scanf("%lf%lf",&stime[i],&ntime[i]);
}

//冒泡排序
for(i=0;i<n-1;i++){ //按到达时间从小到大
for(j=i+1;j<n;j++){
if(stime[i]>stime[j]){
tmp1=name[i];
name[i]=name[j];
name[j]=tmp1;
tmp=stime[i];
stime[i]=stime[j];
stime[j]=tmp;
tmp=ntime[i];
ntime[i]=ntime[j];
ntime[j]=tmp;
}
}
}

//先来先服务(FCFS)

printf("先来先服务(FCFS):\n");
MeanTurn=0; //初始化平均周转时间
MeanWeight=0; //初始化平均加权周转时间
printf("进程名称 到达时间 所需时间 完成时间 周转时间 加权周转时间\n");
achieve=stime[0]; //进程的开始时间
for(i=0;i<=n-1;i++){
achieve=achieve+ntime[i]; //进程运行的完成时间
turn=achieve-stime[i]; //周转时间
weight=turn/ntime[i]; //加权周转时间
printf(" %d %.2lf %.2lf %.2lf %.2lf %.2lf\n",name[i],stime[i],ntime[i],achieve,turn,weight);
MeanTurn=MeanTurn+turn; //非第一个进程的平均周转时间
MeanWeight=MeanWeight+weight; //非第一个进程的平均加权周转时间
}
printf("平均周转时间%.2lf\n平均加权周转时间%.2lf\n\n\n",MeanTurn/n,MeanWeight/n);





//最短作业优先(SJF)
t=0;
printf("最短作业优先(SJF):\n");
MeanTurn=0; //初始化平均周转时间
MeanWeight=0; //初始化平均加权周转时间
printf("进程名称 到达时间 所需时间 完成时间 周转时间 加权周转时间\n");
achieve=stime[0]+ntime[0]; //第一个进程运行的完成时间
turn=achieve-stime[0]; //第一个进程运行的周转时间
weight=turn/ntime[0]; //第一个进程运行的加权周转时间
MeanTurn=MeanTurn+turn; //平均周转时间
MeanWeight=MeanWeight+weight; //平均加权周转时间
printf(" %d %.2lf %.2lf %.2lf %.2lf %.2lf\n",name[0],stime[0],ntime[0],achieve,turn,weight);
for(i=1;i<n;i++){
if(achieve<stime[i+1]&&i+1<4){ //判断一个进程完成时,下下个进程是否到达
t=1;
if(achieve<stime[i+2]&&i+2<4){
t=2;
if(achieve<stime[i+3]&&i+3<4){
t=3;
}
}
}
if(t!=0){
for(c=i;c<t;c++){ //对满足要求的进程重新排序
for(j=c+1;j<t+1;j++){
if(ntime[c]>ntime[j]){
tmp1=name[c];
name[c]=name[j];
name[j]=tmp1;
tmp=stime[c];
stime[c]=stime[j];
stime[j]=tmp;
tmp=ntime[c];
ntime[c]=ntime[j];
ntime[j]=tmp;
}
}
}
}
achieve=achieve+ntime[i]; //进程运行的完成时间
turn=achieve-stime[i]; //周转时间
weight=turn/ntime[i]; //加权周转时间
printf(" %d %.2lf %.2lf %.2lf %.2lf %.2lf\n",name[i],stime[i],ntime[i],achieve,turn,weight);
MeanTurn=MeanTurn+turn; //平均周转时间
MeanWeight=MeanWeight+weight; //平均加权周转时间
}
printf("平均周转时间%.2lf\n平均加权周转时间%.2lf\n\n\n",MeanTurn/n,MeanWeight/n);


//响应比高者优先(HRN)
//冒泡排序
for(i=0;i<n-1;i++){ //按到达时间从小到大
for(j=i+1;j<n;j++){
if(stime[i]>stime[j]){
tmp1=name[i];
name[i]=name[j];
name[j]=tmp1;
tmp=stime[i];
stime[i]=stime[j];
stime[j]=tmp;
tmp=ntime[i];
ntime[i]=ntime[j];
ntime[j]=tmp;
}
}
}
printf("(非抢占式)响应比高者优先(HRN):\n");
t=0;
MeanTurn=0; //初始化平均周转时间
MeanWeight=0; //初始化平均加权周转时间
printf("进程名称 到达时间 所需时间 完成时间 周转时间 加权周转时间\n");
achieve=stime[0]+ntime[0]; //第一个进程运行的完成时间
turn=achieve-stime[0]; //第一个进程运行的周转时间
weight=turn/ntime[0]; //第一个进程运行的加权周转时间
MeanTurn=MeanTurn+turn; //平均周转时间
MeanWeight=MeanWeight+weight; //平均加权周转时间
printf(" %d %.2lf %.2lf %.2lf %.2lf %.2lf\n",name[0],stime[0],ntime[0],achieve,turn,weight);
for(i=1;i<n;i++){
if(achieve<stime[i+1]&&i+1<4){ //判断在上一个进程完成时有多少进程到了
t=1;
if(achieve<stime[i+2]&&i+2<4){
t=2;
if(achieve<stime[i+3]&&i+3<4){
t=3;
}
}
}
if(t!=0){
for(c=i;c<t;c++){
rp[c]=(ntime[c]+achieve-stime[c])/ntime[c];//计算响应比
}
for(c=i;c<t;c++){
for(j=c+1;j<t+1;j++){
if(rp[c]>rp[j]){
tmp1=name[c];
name[c]=name[j];
name[j]=tmp1;
tmp=stime[c];
stime[c]=stime[j];
stime[j]=tmp;
tmp=ntime[c];
ntime[c]=ntime[j];
ntime[j]=tmp;
}
}
}
}
achieve=achieve+ntime[i]; //进程运行的完成时间
turn=achieve-stime[i]; //周转时间
weight=turn/ntime[i]; //加权周转时间
printf(" %d %.2lf %.2lf %.2lf %.2lf %.2lf\n",name[i],stime[i],ntime[i],achieve,turn,weight);
MeanTurn=MeanTurn+turn; //平均周转时间
MeanWeight=MeanWeight+weight; //平均加权周转时间
}
printf("平均周转时间%.2lf\n平均加权周转时间%.2lf\n\n\n",MeanTurn/n,MeanWeight/n);

return 0;
}
...全文
576 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-06-14
  • 打赏
  • 举报
回复
百度搜相关关键字。

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧