Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1056455
  • 博文数量: 573
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 66
  • 用 户 组: 普通用户
  • 注册时间: 2016-06-28 16:21
文章分类

全部博文(573)

文章存档

2018年(3)

2016年(48)

2015年(522)

分类: LINUX

2015-12-04 09:52:59

模拟进程调度

#include
#include
#include          
/* 所用头文件 */

#define PACE 5           
/* 每次时间片长度 */

 typedef struct running{
 int num;
 int priority;
 int time;
}Run;                   
/* 处于运行状态的进程 */

 typedef struct waiting{
 int num;
 int priority;
 int time;
 struct waiting *next;
}Wait;                  
/* 处于等待状态的进程 */

 typedef struct ready{
 int num;
 int priority;
 int time;
 struct ready *next;
}Ready;

 Run *runPCB;
 Wait *waitPCB;
 Ready *readyPCB;
 int num=0,PCBno=0;

 void addReadyPCB();
 void displayReadyPCB();
 void ReadytoRunning();
 void displayRunPCB();
 void Runningtowaiting();
 void displayWaitPCB();
 void WaitingtoReady();


void RunningtoReady();
 int readyNUM();       
/* 所用子函数 */

 void main(){          
	/* 主函数 */
 int l,j;

 readyPCB=NULL;
 runPCB=NULL;
 waitPCB=NULL;         
	/* 初始化PCB列表 */ 

 while(!kbhit()){
    clrscr();
    if(readyPCB==NULL)addReadyPCB();
if(runPCB==NULL)
	ReadytoRunning();
    displayRunPCB();
    displayReadyPCB();
    displayWaitPCB();
	sleep(PACE);       
	/* 显示当前状态PACE秒钟 */
    runPCB->time-=PACE;
 if(runPCB->time==0){
	free(runPCB);runPCB=NULL;}
 clrscr();
	 if((!(rand()%4))&&(readyNUM()<5))addReadyPCB();
    if(!(rand()%2))RunningtoReady();
    Runningtowaiting();
    WaitingtoReady();
    displayRunPCB();
    displayReadyPCB();
    displayWaitPCB();
	sleep(1);          
	/* 显示转化时一秒钟 */ 
   }
}

 void addReadyPCB(){   
	/* 添加就绪状态的PCB */
 Ready *p,*p1;
 int creatno,i;

 clrscr();
 creatno=rand()%3+1;
 p=NULL;
 for(i=0;inum=num++;
    if(PCBno==0)p1->priority=1;
	    else p1->priority=rand()%PCBno+1;
    PCBno++;
    p1->time=(rand()%5+1)*PACE;
    p1->next=NULL;
    if(p==NULL)p=p1;
    else {p1->next=p;p=p1;}
   }
 if(readyPCB==NULL)readyPCB=p;
 else{
    p1=readyPCB;
    while(p1->next!=NULL)
       p1=p1->next;
    p1->next=p;
   }
}

 void displayReadyPCB(){   
	/* 显示就绪状态的PCB */
 Ready *p;
 p=readyPCB;
 if(p==NULL)return;
 printf("The ready PCB(name priority time):\n");
 while(p!=NULL){
    printf("%-3d %-3d %-3d\n",p->num,p->priority,p->time);
    p=p->next;
   }
}

 void ReadytoRunning(){   /* 就绪状态向运行状态转换 */
 Ready *p,*hp,*p1,*info;
 p=readyPCB;
 clrscr();
 if(p==NULL){printf("There is still no ready PCB!Error!\n");return;}
 hp=p;
 p1=info=NULL;
 while(p!=NULL){
     if(p->prioritypriority){hp=p;info=p1;}
     p1=p;
     p=p->next;
    }
 runPCB=(Run *)malloc(sizeof(Run));
 runPCB->num=hp->num;
 runPCB->priority=hp->priority;
 runPCB->time=hp->time;
 if(info==NULL)readyPCB=readyPCB->next;
 else {info->next=hp->next;free(hp);}
}

 void displayRunPCB(){    /* 显示运行状态 */    
 if(runPCB==NULL)return;
 printf("The running PCB(name priority time):\n");
 printf("%-3d %-3d %-3d\n",runPCB->num,runPCB->priority,runPCB->time);
}

 void Runningtowaiting(){  /* 运行状态阻塞到等待状态 */
 Wait *p,*p1;
 int randomno=rand()%2+1;
 if(runPCB==NULL)return;
 clrscr();
 if(randomno==2)return;
 else{
   if(waitPCB==NULL){
      waitPCB=(Wait *)malloc(sizeof(Wait));
      waitPCB->num=runPCB->num;
      waitPCB->priority=runPCB->priority;
      waitPCB->time=runPCB->time;
      waitPCB->next=NULL;
     }
   else{
      p=waitPCB;
      while(p->next!=NULL)
	p=p->next;
      p1=(Wait *)malloc(sizeof(Wait));
      p1->num=runPCB->num;
      p1->priority=runPCB->priority;
      p1->time=runPCB->time;
      p1->next=NULL;
      p->next=p1;
     }
    free(runPCB);
    runPCB=NULL;
  }
}

 void displayWaitPCB(){  
/* 显示等待状态的PCB */
 Wait *p;
 p=waitPCB;
 if(waitPCB==NULL)return;
 printf("The waiting PCB(name priority time):\n");
 while(p!=NULL){
    printf("%-3d %-3d %-3d\n",p->num,p->priority,p->time);
    p=p->next;
   }
}

 void WaitingtoReady(){  /* 等待状态等待事件发生,转化为就绪状态 */
 Wait *p,*p1;
 Ready *q,*q1;
 p=waitPCB;
 p1=NULL;
 clrscr();
 if(p==NULL)return;
 while(p!=NULL){
   if(rand()%3){
      q=(Ready *)malloc(sizeof(Ready));
      q->num=p->num;
      q->priority=p->priority;
      q->time=p->time;
      q->next=NULL;
      q1=readyPCB;
      if(q1==NULL)readyPCB=q;
      else{
	while(q1->next!=NULL)
	    q1=q1->next;
	q1->next=q;
       }
      if(p1==NULL)
	{
	 if(waitPCB->next==NULL){free(waitPCB);waitPCB=NULL;p=waitPCB;break;}
	 else{
	    p=waitPCB->next;
	    free(waitPCB);
	    waitPCB=p;
	    continue;
	   }
	}
      else{
	p1->next=p->next;
	free(p);
	p=p1;
       }
     }
   p1=p;
   p=p->next;
  }
}

 void RunningtoReady(){   
/* 运行状态时间片到了,转化为就绪状态 */
 Ready *p,*p1;
 if(runPCB==NULL)return;
 clrscr();
 p=readyPCB;
 if(p==NULL){
   readyPCB=(Ready *)malloc(sizeof(Ready));
   readyPCB->num=runPCB->num;
   readyPCB->priority=runPCB->priority;
   readyPCB->time=runPCB->time;
   readyPCB->next=NULL;
  }
 else{
   while(p->next!=NULL)
      p=p->next;
   p1=(Ready *)malloc(sizeof(Ready));
   p1->num=runPCB->num;
   p1->priority=runPCB->priority;
   p1->time=runPCB->time;
   p1->next=NULL;
   p->next=p1;
  }
 free(runPCB);
 runPCB=NULL;
}

 int readyNUM(){       /*返回就绪状态的就绪PCB的个数 */
 int i=0;
 Ready *p;
 p=readyPCB;
 while(p!=NULL)
   {
    i++;
    p=p->next;
   }
 return i;
}
阅读(469) | 评论(0) | 转发(0) |
0

上一篇: C语言红黑树

下一篇:模拟进程调度2

给主人留下些什么吧!~~