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

📄 processe.cpp

📁 操作系统中的进程调度优先算法和轮转法算法的实现
💻 CPP
字号:
//进程调度
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<iomanip.h>
typedef struct Processe
{
   char Name[10];  /*进程标识符*/
   int Prio;   /*进程优先数*/
   int Round;  /*进程时间轮转时间片*/
   int CPUTime; /*进程占用CPU时间*/
   int NeedTime; /*进程到完成还要的时间*/
   int Count;  /*计数器*/
   char State; /*进程的状态*/
   struct Processe *next; /*链指针*/
}PCB;
PCB *finish,*ready,*tail,*run; /*队列指针*/
int N; /*进程数*/
/*将就绪队列中的第一个进程投入运行*/
void Ready_Frist_Run()
{
   run=ready;   /*就绪队列头指针赋值给运行头指针*/
   run->State='R';   /*进程状态变为运行态*/
   ready=ready->next;  /*就绪对列头指针后移到下一进程*/
}
/*标题输出函数*/
void Output_Title(char a)
{
   if(toupper(a)=='P') /*优先数法*/
	  cout<<setiosflags(ios::left)<<setw(10)<<"Name"<<setw(10)<<"CPUTime"<<setw(10)<<"NeedTime"<<setw(10)<<"Priority"<<setw(10)<<"State"<<endl;
   else
	 cout<<setiosflags(ios::left)<<setw(10)<<"Name"<<setw(10)<<"CPUTime"<<setw(10)<<"NeedTime"<<setw(10)<<"Count "<<setw(10)<<"Round"<<setw(10)<<"State"<<endl;
}
/*进程PCB输出*/
void Output_Process(char a,PCB *q)
{
   if(toupper(a)=='P')  /*优先数法的输出*/
cout<<setiosflags(ios::left)<<setw(10)<<q->Name<<setw(10)<<q->CPUTime<<setw(10)<<q->NeedTime<<setw(10)<<q->Prio<<setw(10)<<q->State<<endl;
   else/*轮转法的输出*/
cout<<setiosflags(ios::left)<<setw(10)<<q->Name<<setw(10)<<q->CPUTime<<setw(10)<<q->NeedTime<<setw(10)<<q->Count<<setw(10)<<q->Round<<setw(10)<<q->State<<endl;
}
/*输出函数*/
void Output_Function(char Arithmetic_Sign)
{
   PCB *p;
   Output_Title(Arithmetic_Sign);  /*输出标题*/
   if(run!=NULL) /*如果运行指针不空*/
      Output_Process(Arithmetic_Sign,run); /*输出当前正在运行的PCB*/
   p=ready;  /*输出就绪队列PCB*/
   while(p!=NULL)
   {
      Output_Process(Arithmetic_Sign,p);
      p=p->next;
   }
   p=finish;  /*输出完成队列的PCB*/
   while(p!=NULL)
   {
      Output_Process(Arithmetic_Sign,p);
      p=p->next;
   }
   getch();  /*压任意键继续*/
}
/*优先数的插入算法*/
void Insert_Priority(PCB *q)
{
   PCB *p1,*s,*r;
   int b;
   s=q;  /*待插入的PCB指针*/
   p1=ready; /*就绪队列头指针*/
   r=p1; /*r做p1的前驱指针*/
   b=1;
   while((p1!=NULL)&&b)  /*根据优先数确定插入位置*/
      if(p1->Prio>=s->Prio)
      {
	 r=p1;
	 p1=p1->next;
      }
      else
	 b=0;
   if(r!=p1)  /*如果条件成立说明插入在r与p1之间*/
   {
      r->next=s;
      s->next=p1;
   }
   else
   {
      s->next=p1;  /*否则插入在就绪队列的头*/
      ready=s;
   }
}
/*轮转法插入函数*/
void Insert_Round(PCB *p2)
{
   tail->next=p2;  /*将新的PCB插入在当前就绪队列的尾*/
   tail=p2;
   p2->next=NULL;
}
/*优先数创建初始PCB信息*/
void Create_Priority_PCB(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL; /*就绪队列头指针*/
   finish=NULL;  /*完成队列头指针*/
   run=NULL; /*运行队列指针*/
   cout<<"Enter Name and time of process"<<endl; /*输入进程标识和所需时间创建PCB*/
   for(i=1;i<=N;i++)
   {
	   p=new PCB[sizeof(PCB)];
	   cin>>na;
	   cin>>time;
      strcpy(p->Name,na);
      p->CPUTime=0;
      p->NeedTime=time;
      p->State='w';
      p->Prio=50-time;
      if(ready!=NULL) /*就绪队列不空调用插入函数插入*/
	 Insert_Priority(p);
      else
      {
	 p->next=ready; /*创建就绪队列的第一个PCB*/
	 ready=p;
      }
   }
   system("cls");
   //printf("          output of Priority:\n");
   cout<<"             OutPut Of Priority                 "<<endl;
   cout<<"************************************************"<<endl;
   Output_Function(alg);  /*输出进程PCB信息*/
   run=ready; /*将就绪队列的第一个进程投入运行*/
   ready=ready->next;
   run->State='R';
}
/*轮转法创建进程PCB*/
void Create_Run_PCB(char alg)
{
   PCB *p;
   int i,time;
   char na[10];
   ready=NULL;
   finish=NULL;
   run=NULL;
  cout<<"Enter Name and time of Round process."<<endl;
   for(i=1;i<=N;i++)
   {
	   p=new PCB[sizeof(PCB)];
	   cin>>na;
	   cin>>time;
      strcpy(p->Name,na);
      p->CPUTime=0;
      p->NeedTime=time;
      p->Count=0; /*计数器*/
      p->State='w';
      p->Round=2;  /*时间片*/
      if(ready!=NULL)
	 Insert_Round(p);
      else
      {
	 p->next=ready;
	 ready=p;
	 tail=p;
      }
   }
   system("cls");
   cout<<"              OutPut Of Round           "<<endl;
   cout<<"************************************************"<<endl;
   Output_Function(alg);   /*输出进程PCB信息*/
   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;
      run->Prio=run->Prio-3; /*每运行一次优先数降低3个单位*/
      if(run->NeedTime==0)  /*如所需时间为0将其插入完成队列*/
      {
	 run->next=finish;
	 finish=run;
	 run->State='F';  /*置状态为完成态*/
	 run=NULL;  /*运行队列头指针为空*/
	 if(ready!=NULL) /*如就绪队列不空*/
	    Ready_Frist_Run(); /*将就绪对列的第一个进程投入运行*/
      }
      else /*没有运行完同时优先数不是最大,则将其变为就绪态插入到就绪队列*/
	 if((ready!=NULL)&&(run->Prio<ready->Prio))
	 {
	    run->State='W';
	    Insert_Priority(run);
	    Ready_Frist_Run(); /*将就绪队列的第一个进程投入运行*/
	 }
      Output_Function(alg); /*输出进程PCB信息*/
   }
}
/*时间片轮转法*/
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)
	    Ready_Frist_Run(); /*就绪对列不空,将第一个进程投入运行*/
      }
      else
	 if(run->Count==run->Round)  /*如果时间片到*/
	 {
	    run->Count=0;  /*计数器置0*/
	    if(ready!=NULL) /*如就绪队列不空*/
	    {
	       run->State='W'; /*将进程插入到就绪队列中等待轮转*/
	       Insert_Round(run);
	       Ready_Frist_Run(); /*将就绪对列的第一个进程投入运行*/
	    }
	 }
      Output_Function(alg); /*输出进程信息*/
   }
}
/*主函数*/
void main()
{
   char Arithmetic_Sign;  /*算法标记*/
system("cls");
   cout<<"Type The Algorithm:P/R(Priority/Roundrobin)"<<endl;
   //scanf("%c",&Arithmetic_Sign); /*输入字符确定算法*/
   cin>>Arithmetic_Sign;
   cout<<"Enter Process Number"<<endl;
   //scanf("%d",&N); /*输入进程数*/
   cin>>N;
   if(Arithmetic_Sign=='P'||Arithmetic_Sign=='p')
   {
      Create_Priority_PCB(Arithmetic_Sign); /*优先数法*/
      Priority(Arithmetic_Sign);
   }
   else
      if(Arithmetic_Sign=='R'||Arithmetic_Sign=='r')
      {
	 Create_Run_PCB(Arithmetic_Sign); /*轮转法*/
	 RoundRun(Arithmetic_Sign);
      }
}

⌨️ 快捷键说明

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