多级队列反馈调度算法

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct processNode    /*进程节点信息*/
{
 char name[20];      /*进程的名字*/
 int prio;       /*进程的优先级*/
 int round;       /*分配CPU的时间片*/
 int cputime;      /*CPU执行时间*/
 int needtime;      /*进程执行所需要的时间*/
 char state;       /*进程的状态,W——就绪态,R——执行态,F——完成态*/
 int count;       /*记录执行的次数*/
 struct processNode *next;   /*链表指针*/
}PCB;
typedef struct Queue     /*多级就绪队列节点信息*/
{
 PCB *LinkPCB;      /*就绪队列中的进程队列指针*/
 int prio;       /*本就绪队列的优先级*/
 int round;       /*本就绪队列所分配的时间片*/
 struct Queue *next;     /*指向下一个就绪队列的链表指针*/
}ReadyQueue;
PCB *running=NULL,*finished=NULL;  /*定义三个队列,就绪队列,执行队列和完成队列*/
ReadyQueue *Head = NULL; /*定义第一个就绪队列*/
static ReadyQueue *ready=NULL;
int num;        /*进程个数*/
int ReadyNum;       /*就绪队列个数*/
void Output();       /*进程信息输出函数*/
void Insertfinished(PCB *in);   /*将进程插入到完成队列尾部*/
void InsertReadyQueue(ReadyQueue *in); /*插入就绪队列,规定优先数越小,优先级越低*/
void ReadyQueueInit();     /*创建就绪队列初始化函数*/
void pop(ReadyQueue *queue);   /*取得某一个就绪队列中的队头进程*/
void push(PCB *in,ReadyQueue *queue); /*将进程插入到就绪队列尾部*/
void ProcessCreate();     /*进程创建函数*/
void RoundRobin(ReadyQueue *timechip);  /*时间片轮转调度算法*/
void MultiDispatch();     /*多级调度算法,每次执行一个时间片*/


void Output()       /*进程信息输出函数*/
{
 PCB *p = NULL; 
 
 p = ready->LinkPCB;
 if(p!=NULL)
  printf("就绪队列.../n");
 while(ready !=NULL)
 {
  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;
  }
  ready = ready->next;
  if(ready!=NULL)
   p = ready->LinkPCB;
 }

 p = finished;
 if(p!=NULL)
  printf("完成队列.../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;
 }
 printf("/n/n");
 p = running;
 if(p!= NULL)
  printf("运行队列.../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;
 }
}
void Insertfinished(PCB *in)   /*将进程插入到完成队列尾部*/
{
 PCB *fst;
 fst = finished;

 if(finished == NULL)
 {
   in->next = finished;
   finished = in;
 }
 else
 {
   while(fst->next != NULL)
   {
    fst = fst->next;
   }
   in ->next = fst ->next;
   fst ->next = in;
 } 
 printf("the %s has finished!!!/n",in->name); 
 running = NULL;
 Output();
}
void InsertReadyQueue(ReadyQueue *in) /*创建就绪队列,规定优先数越小,优先级越低*/
{
 ReadyQueue *fst,*nxt;
 fst = nxt = Head;

 if(Head == NULL)     /*如果没有队列,则为第一个元素*/
 {
   in->next = Head;
   Head = in;
 }
 else        /*查到合适的位置进行插入*/
 {
   if(in ->prio >= fst ->prio)  /*比第一个还要大,则插入到队头*/
   {
    in->next = Head;
    Head = in;
   
   }
   else
   {
    while(fst->next != NULL)   /*移动指针查找第一个比它小的元素的位置进行插入*/
    {
  nxt = fst;  
  fst = fst->next;
  if(in ->prio >= fst ->prio)
  {
   in ->next = fst;
   nxt ->next = in;
   return;
  }
    }
  
    if(fst ->next == NULL)   /*已经搜索到队尾,则其优先级数最小,将其插入到队尾即可*/
    {
  in ->next = fst ->next;
  fst ->next = in;
  return;
    }  
   }
 } 
}
void ReadyQueueInit()     /*创建就绪队列输入函数*/
{
 ReadyQueue *tmp;
 int i=0, j=1;

 printf("输入就绪队列的个数:/n");
 scanf("%d",&ReadyNum);

 printf("输入每个就绪队列的CPU时间片:/n");
 for(i = 0;i < ReadyNum; i++)
 {
   if((tmp = (ReadyQueue *)malloc(sizeof(ReadyQueue)))==NULL)
   {
    perror("malloc failed!!!");
    exit(1);
   }
   printf("请输入就绪队列的时间片 : ");
   scanf("%d",&(tmp->round));  /*输入此就绪队列中给每个进程所分配的CPU时间片*/
   tmp ->prio = 50 - tmp->round;  /*设置其优先级,时间片越高,其优先级越低*/
   tmp ->LinkPCB = NULL;    /*初始化其连接的进程队列为空*/
   tmp ->next = NULL;
   InsertReadyQueue(tmp);   /*按照优先级从高到低,建立多个就绪队列*/
 }

 printf("**********Multiple ReadyQueue Information**********/n");
 ReadyQueue *rq = Head;
 
  while(rq != NULL){
   printf("第 %d 个就绪队列/n",j++);
   printf("优先级=%d/t 该就绪队列的时间片=%d/n",rq->prio,rq->round);
   rq = rq->next;
  } 
 
}
void pop(ReadyQueue *queue)    /*取得某一个就绪队列中的队头进程*/
{
 running = queue ->LinkPCB;

 if(queue ->LinkPCB != NULL)
 {
   running ->state = 'R';
   queue ->LinkPCB = queue ->LinkPCB ->next;
   ready = queue;
   running ->next = NULL;  
 }
}
void push(PCB *in,ReadyQueue *queue) /*将进程插入到就绪队列尾部*/
{
 PCB *fst;
 fst = queue->LinkPCB;

 if( queue->LinkPCB == NULL)
 {
   in->next =  queue->LinkPCB;
   queue->LinkPCB = in;
 }
 else
 {
   while(fst->next != NULL)
   {
    fst = fst->next;
   }
   in ->next = fst ->next;
   fst ->next = in;
 }

 printf("push the %s into Ready Queue!!!/n",in->name);
}
void ProcessCreate()     /*进程创建函数*/
{
 PCB *tmp;
 int i,j=1,k;

 printf("**********输入进程的个数**********/n");
 scanf("%d",&num);

 for(i = 0;i < num; i++)
 {
   if((tmp = (PCB *)malloc(sizeof(PCB)))==NULL)
   {
    perror("malloc failed!!!");
    exit(1);
   }
   k=j;
   printf("输入第%d个进程名字:/n",j++);
   scanf("%s",tmp->name);
   getchar();       /*吸收回车符号*/
   printf("输入第%d个进程需要的时间:/n",k);
   scanf("%d",&(tmp->needtime));
   tmp ->cputime = 0;
   tmp ->state ='W';
   tmp ->prio = 50 - tmp->needtime;  /*设置其优先级,需要的时间越多,优先级越低*/
   tmp ->round = Head ->round;
   tmp ->count = 0;
   push(tmp,Head);      /*按照优先级从高到低,插入到就绪队列*/
 }
 printf("***The first time ReadyQueue Information***/n");
   PCB *prq = Head->LinkPCB;  
   printf("进程名/t优先级/t轮数/tcpu时间/t需要时间/t进程状态/t计数器/n");
   while(prq != NULL)
   { 
    printf("%s/t%d/t%d/t%d/t%d/t/t%c/t/t%d/n",prq->name,prq->prio,prq->round,prq->cputime,prq->needtime,prq->state,prq->count); 
    prq = prq->next;
   }
}
void RoundRobin(ReadyQueue *timechip)   /*时间片轮转调度算法*/
{

 int flag = 1;

 pop(timechip);
 while(running != NULL)
 {
   while(flag)
   {
    running->count++;
    running->cputime++;
    running->needtime--;
    if(running->needtime == 0)   /*进程执行完毕*/
    {
  running ->state = 'F';
  Insertfinished(running);
  flag = 0;
    }
    else if(running->count == timechip ->round)/*时间片用完*/
    {
  running->state = 'W';
  running->count = 0;     /*计数器清零,为下次做准备*/
  push(running,timechip);
  flag = 0;
    }
   }
   flag = 1;
   pop(timechip);
 }
}
void MultiDispatch()     /*多级调度算法,每次执行一个时间片*/
{
 int flag = 1;
 ReadyQueue *point;
 point = Head;
 pop(point);
 while(running != NULL)
 {
    if(Head ->LinkPCB!=NULL)
     point = Head;
    while(flag)
    {
      running->count++;
      running->cputime++;
      running->needtime--;
      if(running->needtime == 0)   /*进程执行完毕*/
      {
    running ->state = 'F';
    Insertfinished(running);
    flag = 0;
      }
      else if(running->count == running->round)/*时间片用完*/
      {
     running->state = 'W';
     running->count = 0;     /*计数器清零,为下次做准备*/
     if(point ->next!=NULL)
     {
       running ->round = point->next ->round;/*设置其时间片是下一个就绪队列的时间片*/
       push(running,point->next);   /*将进程插入到下一个就绪队列中*/
       flag = 0;
       Output();
     }
     else
     {
       RoundRobin(point);     /*如果为最后一个就绪队列就调用时间片轮转算法*/
       break;
     }
    }      
   }
    flag = 1;
    if(point ->LinkPCB == NULL)   /*就绪队列指针下移,进入下一级就绪队列!!!*/
     point =point->next;
    if(point ->next ==NULL)
    {
      RoundRobin(point);
      break;
    }  
    pop(point);
 }
 
}

int main(void)
{
 ReadyQueueInit();     /*创建就绪队列*/
 ProcessCreate();     /*创建就绪进程队列*/
 MultiDispatch();     /*算法开始*/ 
 return 0;
}

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值