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

📄 39176271.txt

📁 进程优先级调度
💻 TXT
字号:
#include <iostream.h>
#include <iomanip.h>

int i,j;
struct thread
{
 int id;
 int priority;
 int alltime;
 int startblock;
 int blocktime;
 int state;//0表示ready;  1表示run; 2表示block;
 int readycount;
}thread[5]={{0,9,3,2,6,0,0},{1,38,3,-1,0,0,0},{2,30,6,-1,0,0,0},{3,30,3,-1,0,0,0},{4,0,4,-1,0,0,0}},t;

void run()
{
 cout<<thread[i].id<<"** run **"<<setw(5)<<setiosflags(ios::left)<<thread[i].priority;
 thread[i].readycount=0;
 thread[i].priority-=3;
 thread[i].alltime-=1;
 thread[i].state=0;
 if(thread[i].startblock>0)
  thread[i].startblock--;
}

void ready()
{ 
 cout<<thread[i].id<<"**ready**"<<setw(5)<<setiosflags(ios::left)<<thread[i].priority;
 thread[i].readycount++;
 thread[i].priority++;
 thread[i].state=0;
}

void block()
{
 cout<<thread[i].id<<"**block**"<<setw(5)<<setiosflags(ios::left)<<thread[i].priority;
 thread[i].blocktime--;
 thread[i].state=0;
 if(thread[i].blocktime==0)
 thread[i].startblock--;
}

void main()
{
 int threadcount=5;

 for(int n=1;n<=50;n++)
 {
  cout<<"时间片 "<<n;
  cout<<endl;
  
  //如果进程结束删除
  for(i=0;i<threadcount;i++)
  {
   if(thread[i].alltime==0)
   {
    threadcount--;
    for(j=i;j<threadcount;j++)
    {
     thread[j]=thread[j+1];
    }
   }
  }

  //进程排序优先级由高至低
  for(j=0;j<threadcount-1;j++)
  {
   for(i=0;i<threadcount-1-j;i++)
   {
    if((thread[i].priority)<(thread[i+1].priority))
    {
  	 t=thread[i];
	 thread[i]=thread[i+1];
	 thread[i+1]=t;
    }
	//如果优先级相等,进程等待时间片数readycount大的排在前
    if(thread[i].priority==thread[i+1].priority)
     if(thread[i].readycount<thread[i+1].readycount)
     {
      t=thread[i];
      thread[i]=thread[i+1];
      thread[i+1]=t;
     }
   }
  }
    
  for(i=0;i<threadcount;i++)
  {
   if(thread[i].startblock==0)
	  thread[i].state=2;
   else
   {
	  thread[i].state=1;
      break;
   }
  }

  for(i=0;i<threadcount;i++)
  {
  if(thread[i].state==1)
  run();
  else
  if(thread[i].state==0)
  ready();
  else
	  block();
  }

  cout<<endl;

  if(threadcount==1)
	  if(thread[0].alltime==0)
   break;
 }
 
}

⌨️ 快捷键说明

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