Board logo

标题: 时间片轮转法与优先调度算法,进程调度,C代码(2) [打印本页]

作者: yuyang911220    时间: 2015-5-28 08:50     标题: 时间片轮转法与优先调度算法,进程调度,C代码(2)

void create2(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL;
   finish=NULL;
   run=NULL;
   printf("Enter name and time ofround process\n");
   for(i=1;i<=N;i++)
   {
     p=(PCB *)malloc(sizeof(PCB));
     scanf("%s",na);
     scanf("%d",&time);
     strcpy(p->name,na);
     p->cputime=0;
     p->needtime=time;
     p->count=0;
     p->state='w';
     p->round=2;
     if(ready!=NULL)
  insert2(p);
     else
     {
  p->next=ready;
  ready=p;
  tail=p;
     }
   }
   //clrscr();
  printf("             output of round\n");
  printf("************************************************\n");
  prt(alg);  
  run=ready;
   ready=ready->next;
   run->state='R';
}



void priority(char alg)
{
while(run!=NULL)
   {
     run->cputime=run->cputime+1;
     run->needtime=run->needtime-1;
   if(run->needtime==0)
     {
   run->next=finish;
   finish=run;
   run->state='F';
   run=NULL;
  if(ready!=NULL)
       firstin();

     }
   prt(alg);
}
  }




void roundrun(char alg)
{
   while(run!=NULL)
   {
     run->cputime=run->cputime+1;
     run->needtime=run->needtime-1;
     run->count=run->count+1;
     if(run->needtime==0)
     {
  run->next=finish;
  finish=run;
  run->state='F';
  run=NULL;
  if(ready!=NULL)
    firstin();
     }
     else
if(run->count==run->round)
  {
    run->count=0;
    if(ready!=NULL)
    {
       run->state='W';
       insert2(run);
       firstin();
    }
  }
     prt(alg);
   }
}

void main(int argc, char* argv[])
{
   charalgo;
   //clrscr();
   printf("type thealgorithm/R(priority/roundrobin)\n");
   scanf("%c",&algo);
   printf("Enter processnumber\n");
   scanf("%d",&N);
   if(algo=='P'||algo=='p')
   {
     create1(algo);
     priority(algo);
   }
   else
     if(algo=='R'||algo=='r')
     {
  create2(algo);
  roundrun(algo);
     }
   //return 0;
}




欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) Powered by Discuz! 7.0.0