⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcb.cpp

📁 操作系统中的多级反馈队列调度算法
💻 CPP
字号:
#include<stdio.h>
#include"g:\file.cpp"
#define ERROR -1
#define ok  1
int i,h;
int m,a,b,c;
CHANGE(LinkQueue &P,LinkQueue &Q,QueuePtr q){
  QueuePtr s;
  if(Q.front==Q.rear) return  ERROR;
    q=Q.front->next;
    Q.front->next=q->next;
    if(Q.rear==q)Q.rear=Q.front;
  s=(QueuePtr)malloc(sizeof (QNode));
  if(!s) exit(OVERFLOW);
  s->ID=q->ID;s->Hour=q->Hour;s->Minute=q->Minute;s->Alltime=q->Alltime;
  s->next=NULL;
  P.rear->next=s;
  P.rear=s;
  free(q);
  return OK;
}
DeQueue(LinkQueue &P){
    if(P.front==P.rear)
       return  ERROR;
    p=P.front->next;
    i=p->ID;h=p->Hour;m=p->Minute;a=p->Alltime;
    P.front->next=p->next;
    if(P.rear==p)
      P.rear=P.front;
    free(p);
    return OK;
}
change(QueuePtr &p){
  int l,n;
  l=p->Hour;
  n=p->Minute;
  n=l*60+n;
  return n;
}
int time1,time2,time3,time4,time5,serve;
LinkQueue Q1,Q2,Q3,Q4,Q5;
int input,length;
int PCB[100];
void TIME()
{      int i=0,t;
       printf("Please input the first redayqueue's timer(int):\n");
       scanf("%d",&t);
       time1=t;
       time2=2*time1;
       time3=2*time2;
       time4=2*time3;
       time5=2*time4;
       printf("        Please input the key '1' to go on!\n\n");
       scanf("%d",&input);
       if(input==ok)
       printf("        The State of the processes Dequeue:\n\n");
       printf("        ID    nowtime  Queue  timer server_time left_time\n");
	 while(Q.front->next!=NULL)
loop:    {p=Q.front->next;
  CHANGE(Q1,Q,p);
       s=Q1.front->next;
       b=change(s);
       if(s->Alltime-time1<=0) //in the Q1 ,finish
       { b+=s->Alltime;
	 s->Hour=b/60;
	 s->Minute=b%60;
	 scanf("%d",&input);
	 if(input==ok)
	     printf("        %d\t%d:%d\tQ1\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time1,s->Alltime);
	 PCB[i]=s->ID;
	 i++;
	 DeQueue(Q1);
       }
     else        //don't finish
     {  b=b+time1;
	s->Hour=b/60;s->Minute=b%60;
	s->Alltime-=time1;
	scanf("%d",&input);
	if(input==ok)
	  printf("        %d\t%d:%d\tQ1\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time1,time1,s->Alltime);
	CHANGE(Q2,Q1,s);     //input Q2
	  while(Q2.front->next!=NULL)
	   {  s=Q2.front->next;
	      p=Q.front->next;
	      if(p!=NULL)       //have a new pcb
	       { p=Q.front->next;
		  c=change(p);
		  if(b>=c)      //new pcb arrive
		     goto loop;
		 else                //new pcb do not arrive
		  { if(b+time2-c>0)          //arrive when other doing
		    {  if(b+s->Alltime-c>0)      //work is not finish
			{ serve=c-b;
			  s->Alltime=s->Alltime-serve;
			  scanf("%d",&input);
			  if(input==ok)
			      printf("        %d\t%d:%d\tQ2\t%d\t%d\t    %d\n",s->ID,p->Hour,p->Minute,time2,serve,s->Alltime);
			  CHANGE(Q2,Q2,s);
			  goto loop;
			}
		       else           //finish
			{ b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
			      printf("        %d\t%d:%d\tQ2\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time2,s->Alltime);
			  PCB[i]=s->ID;
			  i++;
			  DeQueue(Q2);
			}
		    }
		   else // arrive when finish
		    { if(s->Alltime<=time2)  //work is finish
		       {  b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
			      printf("        %d\t%d:%d\tQ2\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time2,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q2);
		       }
		      else       //work is not finish
		      {  b+=time2;
			 s->Hour=b/60;s->Minute=b%60;
			 s->Alltime-=time2;
			 scanf("%d",&input);
			 if(input==ok)
			       printf("        %d\t%d:%d\tQ2\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time2,time2,s->Alltime);
			 CHANGE(Q3,Q2,s);
		      }//else
		   }//else
		  }//else
		}//else
	      else   //no new pcb
	      { if(s->Alltime-time2<=0) //in the Q2 ,finish
		 {  b+=s->Alltime;
		   s->Hour=b/60;s->Minute=b%60;
		   scanf("%d",&input);
		   if(input==ok)
		       printf("        %d\t%d:%d\tQ2\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time2,s->Alltime,s->Alltime);
		    PCB[i]=s->ID;
			  i++;
		   DeQueue(Q2);
		}
	       else        //don't finish
	       {  b=b+time2;
		  s->Hour=b/60;s->Minute=b%60;
		  s->Alltime-=time2;
		  scanf("%d",&input);
		  if(input==ok)
		       printf("        %d\t%d:%d\tQ2\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time2,time2,s->Alltime);
		  CHANGE(Q3,Q2,s);     //input Q3
	       }
	    }
	  } //while   //the whole pcbs of the Q2 are finish
	 while(Q3.front->next!=NULL)
	   {  s=Q3.front->next;
	      p=Q.front->next;
	      if(p!=NULL)       //have a new pcb
	       { p=Q.front->next;
		  c=change(p);
		  if(b>=c)      //new pcb arrive
		     goto loop;
		 else                //new pcb do not arrive
		  { if(b+time3-c>0)          //arrive when other doing
		    {  if(b+s->Alltime-c>0)      //work is not finish
			{ serve=c-b;
			  s->Alltime=s->Alltime-serve;
			  scanf("%d",&input);
			  if(input==ok)
			       printf("        %d\t%d:%d\tQ3\t%d\t%d\t    %d\n",s->ID,p->Hour,p->Minute,time3,serve,s->Alltime);
			  CHANGE(Q3,Q3,s);
			  goto loop;
			}
		       else           //finish
			{
			  b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
				   printf("        %d\t%d:%d\tQ3\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time3,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q3);
			}
		    }
		   else // arrive when finish
		    { if(s->Alltime<=time3)  //work is finish
		       {  b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			 scanf("%d",&input);
			 if(input==ok)
				      printf("        %d\t%d:%d\tQ3\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time3,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q3);
		       }
		      else       //work is not finish
		      {  b+=time3;
			 s->Hour=b/60;s->Minute=b%60;
			 s->Alltime-=time3;
			scanf("%d",&input);
			if(input==ok)
				  printf("        %d\t%d:%d\tQ3\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time3,time3,s->Alltime);
			 CHANGE(Q4,Q3,s);
		      }//else
		   }//else
		  }//else
		}//else
	      else   //no new pcb
	      { if(s->Alltime-time3<=0) //in the Q3 ,finish
		 {  b+=s->Alltime;
		   s->Hour=b/60;s->Minute=b%60;
		   scanf("%d",&input);
		   if(input==ok)
			printf("        %d\t%d:%d\tQ3\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time3,s->Alltime,s->Alltime);
		    PCB[i]=s->ID;
			  i++;
		   DeQueue(Q3);
		}
	       else        //don't finish
	       {  b=b+time3;
		  s->Hour=b/60;s->Minute=b%60;
		  s->Alltime-=time3;
		  scanf("%d",&input);
		  if(input==ok)
		       printf("        %d\t%d:%d\tQ3\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time3,time3,s->Alltime);
		  CHANGE(Q4,Q3,s);     //input Q4
	       }
	    }
	  } //while   //the whole pcbs of the Q3 are finish
	   while(Q4.front->next!=NULL)
	   {  s=Q4.front->next;
	      p=Q.front->next;
	      if(p!=NULL)       //have a new pcb
	       { p=Q.front->next;
		  c=change(p);
		  if(b>=c)      //new pcb arrive
		     goto loop;
		 else                //new pcb do not arrive
		  { if(b+time4-c>0)          //arrive when other doing
		    {  if(b+s->Alltime-c>0)      //work is not finish
			{ serve=c-b;
			  s->Alltime=s->Alltime-serve;
			  scanf("%d",&input);
			  if(input==ok)
				printf("        %d\t%d:%d\tQ4\t%d\t%d\t    %d\n",s->ID,p->Hour,p->Minute,time4,serve,s->Alltime);
			  CHANGE(Q4,Q4,s);
			  goto loop;
			}
		       else           //finish
			{ b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
				   printf("        %d\t%d:%d\tQ4\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time4,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q4);
			}
		    }
		   else // arrive when finish
		    { if(s->Alltime<=time4)  //work is finish
		       {  b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
				printf("        %d\t%d:%d\tQ4\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time4,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q4);
		       }
		      else       //work is not finish
		      {  b+=time4;
			 s->Hour=b/60;s->Minute=b%60;
			 s->Alltime-=time4;
			 scanf("%d",&input);
			 if(input==ok)
			     printf("        %d\t%d:%d\tQ4\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time4,time4,s->Alltime);
			 CHANGE(Q5,Q4,s);
		      }//else
		   }//else
		  }//else
		}//else
	      else   //no new pcb
	      { if(s->Alltime-time4<=0) //in the Q4 ,finish
		 {  b+=s->Alltime;
		   s->Hour=b/60;s->Minute=b%60;
		   scanf("%d",&input);
		   if(input==ok)
			   printf("        %d\t%d:%d\tQ4\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time4,s->Alltime,s->Alltime);
		    PCB[i]=s->ID;
			  i++;
		   DeQueue(Q4);
		}
	       else        //don't finish
	       {  b=b+time4;
		  s->Hour=b/60;s->Minute=b%60;
		  s->Alltime-=time4;
		  scanf("%d",&input);
		  if(input==ok)
			printf("        %d\t%d:%d\tQ4\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time4,time4,s->Alltime);
		  CHANGE(Q5,Q4,s);     //input Q5
	       }
	    }
	  } //while   //the whole pcbs of the Q4 are finish
	   while(Q5.front->next!=NULL)
	   {  s=Q5.front->next;
	      p=Q.front->next;
	      if(p!=NULL)       //have a new pcb
	       { p=Q.front->next;
		  c=change(p);
		  if(b>=c)      //new pcb arrive
		     goto loop;
		 else                //new pcb do not arrive
		  { if(b+time5-c>0)          //arrive when other doing
		    {  if(b+s->Alltime-c>0)      //work is not finish
			{ serve=c-b;
			  s->Alltime=s->Alltime-serve;
			  scanf("%d",&input);
			  if(input==ok)
			       printf("        %d\t%d:%d\tQ5\t%d\t%d\t    %d\n",s->ID,p->Hour,p->Minute,time5,serve,s->Alltime);
			  CHANGE(Q5,Q5,s);
			  goto loop;
			}
		       else           //finish
			{ b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
				printf("        %d\t%d:%d\tQ5\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time5,s->Alltime);
			  PCB[i]=s->ID;
			  i++;
			 DeQueue(Q5);
			}
		    }
		   else // arrive when finish
		    { if(s->Alltime<=time5)  //work is finish
		       {  b+=s->Alltime;
			  s->Hour=b/60;s->Minute=b%60;
			  scanf("%d",&input);
			  if(input==ok)
			       printf("        %d\t%d:%d\tQ5\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time5,s->Alltime);
			   PCB[i]=s->ID;
			  i++;
			  DeQueue(Q5);
		       }
		      else       //work is not finish
		      {  b+=time5;
			 s->Hour=b/60;s->Minute=b%60;
			 s->Alltime-=time5;
			 scanf("%d",&input);
			 if(input==ok)
			     printf("        %d\t%d:%d\tQ5\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time5,time5,s->Alltime);
			 CHANGE(Q5,Q5,s);
		      }//else
		   }//else
		  }//else
		}//else
	      else   //no new pcb
	      { if(s->Alltime-time5<=0) //in the Q5 ,finish
		 {  b+=s->Alltime;
		   s->Hour=b/60;s->Minute=b%60;
		   scanf("%d",&input);
		   if(input==ok)
		      printf("        %d\t%d:%d\tQ5\t%d\t%d\t    -\n",s->ID,s->Hour,s->Minute,time5,s->Alltime,s->Alltime);
		    PCB[i]=s->ID;
			  i++;
		   DeQueue(Q5);
		}
	       else        //don't finish
	       {  b=b+time5;
		  s->Hour=b/60;s->Minute=b%60;
		  s->Alltime-=time5;
		  scanf("%d",&input);
		  if(input==ok)
		      printf("        %d\t%d:%d\tQ5\t%d\t%d\t    %d\n",s->ID,s->Hour,s->Minute,time5,time5,s->Alltime);
		  CHANGE(Q5,Q5,s);     //input Q5
	       }
	    }
	  } //while   //the whole pcbs of the Q5 are finish
	  }
  }
  length=i;
}

void main()
{
  printf("                         *****************\n");
  printf("                           Now,begining!\n");
  printf("                         *****************\n\n");
  InitQueue(Q);
  file();
  InitQueue(Q1);
  InitQueue(Q2);
  InitQueue(Q3);
  InitQueue(Q4);
  InitQueue(Q5);
  TIME();
  scanf("%d",&input);
  if(input==ok)
  printf("\nThe order of Dequeue processes:");
  for(i=0;i<length;i++)
    printf("%d  ",PCB[i]);
  printf("\n");
  scanf("%d",&input);
  if(input==ok)
  printf("\n                         *****************\n");
  printf("                              The end!\n");
  printf("                         *****************\n");
  scanf("%d",&input);
  if(input==ok)
  printf("\nThe order of Dequeue processes:");
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -