首页 新闻 会员 周边 捐助

进程调度问题,按最长剩余时间进行排序,这个程序有什么问题?

0
悬赏园豆:40 [已关闭问题] 关闭于 2013-10-31 15:26

#include<stdio.h> #include <stdlib.h>        typedef struct pcb     {  char name[5];        int yruntime;             int arrivetime;           int runtime;  int finishtime;  int time;  char state;          struct pcb* link;      }PCB;    PCB *ready = NULL, *p; void disp(PCB* p);

PCB * sort(PCB * p)       {  PCB *first, *second;  int insert = 0;    if (ready == NULL || (p->runtime-p->yruntime) >(ready->runtime-ready->yruntime))

 {   p->link = ready;          ready = p;         }  else  if(( p->runtime-p->yruntime)==(ready->runtime-ready->yruntime))  {   if(p->arrivetime<ready->arrivetime)   {    p->link=ready;    ready=p;   }   else   {        p=ready->link;    p->link=ready->link->link;   }  }  else if((p->runtime-p->yruntime)<(ready->runtime-ready->yruntime))  {   first = ready;   second = first->link;   while (second != NULL)   {    if ((p->runtime-p->yruntime) > (second->runtime-second->yruntime))    {     p->link = second;     first->link = p;      insert = 1;         break;         }    else if((p->runtime-p->yruntime)==(second->runtime-second->yruntime))    {     if(p->arrivetime<second->arrivetime)     {       p->link = second;             first->link = p;        insert = 1;              break;                 }     else     {      p=second->link;      p->link=second->link->link;               }

   }    else if((p->runtime-p->yruntime)<(second->runtime-second->yruntime))    {           first = first->link;     second = second->link;    }

  }   if (insert == 0)    {    first->link = p;    p->link = NULL;   }    }  return ready; }

 

 

void input()                 {  int i,j;  printf("\n请输入进程个数:");  scanf("%d",&j);                 for(i=1;i<=j;i++)          {   printf("\n第%d个进程\n",i);   p=(PCB*)malloc(sizeof(PCB));    printf("\n输入进程名:");   scanf("%s",p->name);   printf("\n输入进程需运行的时间:");   scanf("%d",&(p->runtime));   printf("\n输入进程的到达时间:");   scanf("%d",&(p->arrivetime));   p->state='R';   p->yruntime=0;      p->link=NULL;         sort(p);             }  //printf("按进程的优先级进行排序!\n");      p=ready;         while (p != NULL)        {    disp(p);    p = p->link;   }   }

int maxnumber(PCB *p) {  int max;  max=p->arrivetime;  while(p->link!=NULL)  {   if(max<p->link->arrivetime)   {    max=p->link->arrivetime;   }   p=p->link;  }   return(max); }  

 

void disp(PCB* p)         {  printf("进程名 到达时间 运行时间 状态\n");     printf("%s\t",p->name);     printf("%d\t",p->arrivetime);     printf("%d\t",p->runtime);  printf("%c\t\n",p->state); }

void run(PCB *p) {      PCB *s,*q;  int time=0,a,sum=0,n=0;     float sum1=0.0;     printf("进程的调度开始!\n");     p=ready;    s=p;  a=maxnumber(p);  while(p!=NULL)  {   if(p->state=='C')   {    break;   }

  while(s!=NULL)     {    n=n+1;         s->time=s->runtime;        s=s->link;     }   printf("进程%s运行一次\n",p->name);   p->yruntime++;      time=time+1;   p->finishtime=time+a;            if(p->runtime==p->yruntime)                         {    p->state='C';        printf("进程名为%s,其周转时间为 %d\n",p->name,(p->finishtime)-p->arrivetime);                                         sum+=(p->finishtime)-(p->arrivetime);

 printf("进程名为%s的带权周转时间%f\n",p->name,((p->finishtime)-(p->arrivetime))*1.0/(p->time));      sum1+=(time*1.0)/((p->finishtime)-(p->arrivetime));              ready=p->link;                                               p->link=NULL;          p=ready;     }   else   {      ready=p->link;    p->link=NULL;

      p=sort(p);   }             

       }                  printf("平均周转时间为%f\n",sum*1.0/n);      printf("平均带权周转时间%f\n",sum1*1.0/n);            

    

}  

 

 

 

void main() {  input();  p=ready;  run(p); }

bkhappy的主页 bkhappy | 初学一级 | 园豆:164
提问于:2013-10-28 22:12
< >
分享
所有回答(1)
0

神呀,你也用代码编辑器贴出来呀,这样岂不是吓人吗?

空明流光 | 园豆:111 (初学一级) | 2013-10-29 09:56

我知道,你可以把它放到编译器里去修改运行一下啊!求帮忙!

支持(0) 反对(0) bkhappy | 园豆:164 (初学一级) | 2013-10-29 17:17

@bkhappy: 并不是每个给你解决问题的人都有开发环境的,眼睛有时比编译器好使

支持(0) 反对(0) 空明流光 | 园豆:111 (初学一级) | 2013-10-29 22:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册