首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

C语言实现的进程调度,该怎么解决

2012-03-08 
C语言实现的进程调度#include stdio.h#include stdlib.h#include string.htypedef struct node{char

C语言实现的进程调度
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
typedef struct node  
{  
  char name[20];  
  int prio;  
  int round;  
  int cputime;  
  int needtime;  
  char state;  
  int count;  
  struct node *next;  
}PCB;  
PCB *ready=NULL,*run=NULL,*finish=NULL;  
int num;  
void GetFirst();  
void Output();  
void InsertPrio(PCB *in);  
void InsertTime(PCB *in);  
void InsertFinish(PCB *in);  
void PrioCreate();  
void TimeCreate();  
void Priority();  
void RoundRun();  
int main(void)  
{  
  char chose;  
  printf("请输入要创建的进程数目:\n");  
  scanf("%d",&num);  
  getchar();  
  printf("输入进程的调度方法:(P/R)\n");  
  scanf("%c",&chose);  
  switch(chose)  
  {  
  case 'P':  
  case 'p':  
  PrioCreate();  
  Priority();  
  break;  
  case 'R':  
  case 'r':  
  TimeCreate();  
  RoundRun();  
  break;  
  default:break;  
  }  
  Output();  
  return 0;  
}  
void GetFirst()  
{  
  run = ready;  
   
  if(ready!=NULL)  
  {  
  run ->state = 'R';  
  ready = ready ->next;  
  run ->next = NULL;  
  }  
}  
void Output()  
{  
  PCB *p;  
  p = ready;  
  printf("进程名\t优先级\t轮数\tcpu时间\t需要时间\t进程状态\t计数器\n");  
  while(p!=NULL)  
  {  
  printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);  
  p = p->next;  
  }  
  p = finish;  
  while(p!=NULL)  
  {  
  printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);  
  p = p->next;  
  }  
  p = run;  
  while(p!=NULL)  
  {  
  printf("%s\t%d\t%d\t%d\t%d\t\t%c\t\t%d\n",p->name,p->prio,p->round,p->cputime,p->needtime,p->state,p->count);  
  p = p->next;  
  }  
}  
void InsertPrio(PCB *in)  
{  
  PCB *fst,*nxt;  
  fst = nxt = ready;  
   
  if(ready == NULL)  
  {  
  in->next = ready;  
  ready = in;  
  }  
  else  
  {  
  if(in ->prio >= fst ->prio)  
  {  
  in->next = ready;  
  ready = in;  
  }  
  else  
  {  
  while(fst->next != NULL)  
  {  
  nxt = fst;  
  fst = fst->next;  


  }  
   
  if(fst ->next == NULL)  
  {  
  in ->next = fst ->next;  
  fst ->next = in;  
  }  
  else  
  {  
  nxt = in;  
  in ->next = fst;  
  }  
  }  
  }  
}  
void InsertTime(PCB *in)  
{  
  PCB *fst;  
  fst = ready;  
   
  if(ready == NULL)  
  {  
  in->next = ready;  
  ready = in;  
  }  
  else  
  {  
  while(fst->next != NULL)  
  {  
  fst = fst->next;  
  }  
  in ->next = fst ->next;  
  fst ->next = in;  
  }  
}  
void InsertFinish(PCB *in)  
{  
  PCB *fst;  
  fst = finish;  
   
  if(finish == NULL)  
  {  
  in->next = finish;  
  finish = in;  
  }  
  else  
  {  
  while(fst->next != NULL)  
  {  
  fst = fst->next;  
  }  
  in ->next = fst ->next;  
  fst ->next = in;  
  }  
}  
void PrioCreate()  
{  
  PCB *tmp;  
  int i;  
   
  printf("输入进程名字和进程所需时间:\n");  
  for(i = 0;i < num; i++)  
  {  
  if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)  
  {  
  perror("malloc");  
  exit(1);  
  }  
  scanf("%s",tmp->name);  
  getchar();  
  scanf("%d",&(tmp->needtime));  
  tmp ->cputime = 0;  
  tmp ->state ='W';  
  tmp ->prio = 50 - tmp->needtime;  
  tmp ->round = 0;  
  tmp ->count = 0;  
  InsertPrio(tmp);  
  }  
}  
void TimeCreate()  
{  
  PCB *tmp;  
  int i;  
   
  printf("输入进程名字和进程时间片所需时间:\n");  
  for(i = 0;i < num; i++)  
  {  
  if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)  
  {  
  perror("malloc");  
  exit(1);  
  }  
  scanf("%s",tmp->name);  
  getchar();  
  scanf("%d",&(tmp->needtime));  
  tmp ->cputime = 0;  
  tmp ->state ='W';  
  tmp ->prio = 0;  
  tmp ->round = 2;  
  tmp ->count = 0;  
  InsertTime(tmp);  
  }  
}  
void Priority()  
{  
  int flag = 1;  
   
  GetFirst();  
  while(run != NULL)  
  {  
  Output();  
  while(flag)  
  {  
  run->prio -= 3;  


  run->cputime++;  
  run->needtime--;  
  if(run->needtime == 0)  
  {  
  run ->state = 'F';  
  run->count++;  
  InsertFinish(run);  
  flag = 0;  
  }  
  else  
  {  
  run->state = 'W';  
  run->count++;  
  InsertTime(run);  
  flag = 0;  
  }  
  }  
  flag = 1;  
  GetFirst();  
  }  
}  
void RoundRun()  
{  
   
  int flag = 1;  
   
  GetFirst();  
  while(run != NULL)  
  {  
  Output();  
  while(flag)  
  {  
  run->count++;  
  run->cputime++;  
  run->needtime--;  
  if(run->needtime == 0)  
  {  
  run ->state = 'F';  
  InsertFinish(run);  
  flag = 0;  
  }  
  else if(run->count == run->round)  
  {  
run->state = 'W'; 
}
  }
  }
}

希望各位大侠给我讲解一些 写一下注释

[解决办法]

热点排行