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

📄 pcb.cpp

📁 操作系统的进程控制块
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"      //控制台输入输出头文件
#include "iostream.h"
#include "windows.h"   //包含Sleep()函数的头文件!
#define SEC 3
#define NULL 0
typedef struct PCB
{
  int PID;
  int UID;
  struct PCB * next;
}PCB;
PCB *ready , *excute , *wait;

int enqueue(PCB *head , PCB *node)
{
   PCB *p;
   p = head;
   if(p -> next == NULL)
   {
	 head -> next = node;
	 return 1;
   }
   while(p)
   {
     if(p -> next == NULL)
	 {
    	p -> next = node;
    	return 1;
	 }
     else 
		 p = p -> next;
   }
}

/*dequeue 出队列 */
PCB * dequeue(PCB *head)
{
  PCB *p;
  p = head;
  if(p -> next == NULL)
	{
	return NULL;
	}
  else
  {
	p = p -> next;
	head -> next = p -> next;
	p -> next = NULL;
	return p;
  }
}/*dequeue*/

/*新建进程,创建->就绪*/
int create()
{
	PCB *p;
	p = new PCB;
	p -> next = NULL;
	cout<<"input PID and UID to a new process"<<endl;
	cin>>p->PID>>p->UID; 
	if(enqueue(ready , p))
        cout<<"create a process:PID="<<p->PID<<"  UID="<<p->UID<<endl; 
	else
		cout<<"create failed"<<endl;
	return 1;
}/*create*/

/*就绪->执行*/
int fexcute()
{
   PCB *p = dequeue(ready);
   if(p == NULL)
   {
	   cout<<"NO process in queue "<<endl;
       return 0;
   }
   else
   {
    	enqueue(excute , p);
		cout<<"add a process into excute queue process: PID ="<<p->PID<<"  UID="<<p->UID<<endl;
    	return 1;
   }
}

/*执行->就绪:时间片完(延缓)*/
int suspend()
{
  PCB *p = dequeue(excute);
   if(p == NULL)
  {
	cout<<"NO process in queue"<<endl;
	return 0;
  }
   else
  {
	enqueue(ready , p);
	cout<<"add a process into ready queue process: PID ="<<p->PID<<"  UID="<<p->UID<<endl;
	return 1;
  }
}

/*阻塞->就绪*/
int wake()
{
  PCB *p = dequeue(wait);
   if(p == NULL)
  {
	cout<<"NO process in queue"<<endl;
	return 0;
  }
   else
  {
	enqueue(ready , p);
	cout<<"add a process into ready queue process: PID ="<<p->PID<<"  UID="<<p->UID<<endl;
	return 1;
  }
}
/*执行->阻塞*/
int block()
{
  PCB *p = dequeue(excute);
   if(p == NULL)
  {
	cout<<"NO process in queue"<<endl;
	return 0;
  }
   else
  {
	enqueue(wait , p);
	cout<<"add a process into wait queue process: PID ="<<p->PID<<"  UID="<<p->UID<<endl;
	return 1;
  }
}/*block*/

/*输出队列 outputqueue*/
int outputqueue(PCB *head)
{
   PCB *p;
   if(head -> next == NULL)
   {                               
       cout<<"queue is null"<<endl;
       return 1;
   }                  /*队列为空*/
   p = head -> next; 
   while(p)
	{
       cout<<"            "<<"PID = "<<p->PID<<"  UID = "<<p->UID<<endl;
       p = p -> next;
   }                  /*打印process id UID*/
   return 0;
}

/*output输出*/
void output()
{
  cout<<"READY QUEUE:";
  outputqueue(ready);
  cout<<endl;
  cout<<"EXCUTE QUEUE:";
  outputqueue(excute);
  cout<<endl;
  cout<<"WAIT QUEUE: ";
  outputqueue(wait);
  cout<<endl;
}

/*init 初始化*/
int init()
{
  PCB *p;
  system("CLS");  //用于清屏的C库函数  
  ready=new PCB;
  ready -> next=NULL;
  excute=new PCB;
  excute -> next=NULL;
  wait=new PCB;
  wait -> next = NULL;
  cout<<"--------------PROCESS MENU---------------"<<endl;
  cout<<"now initing....................."<<endl;
  cout<<"input PID and UID as integer , 0 0 as over"<<endl;
  while(1)
  {
    p=new PCB;
	p -> next = NULL;
	cin>>p->PID>>p->UID;
	if(p -> PID == 0 && p -> UID == 0)
		break;
	else
	  {
		if(enqueue(ready , p))
		  {
	       	 cout<<"new process PID = "<<p->PID<<" UID = "<<p->UID<<" added!"<<endl;
		  }
		else 
			return 0;
		}
  }
  return 1;
} /*init*/

/*运行一个process*/
int run()
{
  PCB *p = excute;
  int s = SEC;
  if(excute -> next == NULL)
  {
   cout<<"no process in excute queue "<<endl;
   return 0;
  }
  else
  {
	p = excute -> next;
	cout<<"system will sleep "<<s<<"s as process running"<<endl;
	Sleep(3);
	cout<<"process: PID = "<<p->PID<<"  UID = "<<p->UID<<" excute successed.."<<endl;
	excute -> next = p -> next;
	free(p);
	return 1;
  }
}
int leave()
{
  PCB *p,*t;
  while(ready->next || excute->next || wait->next)
  {
	p = ready -> next;
	while(p)
	{
	  t = p -> next;
	  free(p);
	  p = t;
	}
	ready -> next = NULL;
	p = wait -> next;
	while(p)
	{
	  t = p -> next;
	  free(p);
	  p = t;
	}
	wait -> next = NULL;
	p = excute -> next;
	while(p)
	{
	  t = p -> next;
	  free(p);
	  p = t;
	}
	excute -> next = NULL;
  }
  exit(0);
}/*leace*/

void help()
{
  cout<<"_____________________HELP MENU_____________________"<<endl;
  cout<<"\t-h HELP show help option"<<endl;
  cout<<"\t-c CREATE create a new process , and put to ready queue"<<endl;
  cout<<"\t-b BLOCK block a process in excute queue"<<endl;
  cout<<"\t-w WAKE wake a process in wait queue"<<endl;
  cout<<"\t-e EXCUTE excute a process in ready queue"<<endl;
  cout<<"\t-s SUSPEND suspend a process in excute queue"<<endl;
  cout<<"\t-o OUTPUT output all processes in queues"<<endl;
  cout<<"\t-r RUN excute a process in excute queue"<<endl;
  cout<<"\t-x EXIT exit this program"<<endl;
  cout<<"___________________________________________________"<<endl;
  cout<<"\t type 'H' will show this menu"<<endl;
}/*help*/

int main()
{
  char COMMAND = NULL;
  if( init() != 1)
	{
	  cout<<"init falied ! "<<endl;
	  getch();
	  exit(0);
	}
  else
	{
	  cout<<"init...OK"<<endl;
	  output();
	  help();
	}
  while(1)
  {
	/*当三队列都不空 执行调度 */
	cout<<">";
	cin>>COMMAND;
	switch(COMMAND)
	{
	  case '\n': break;
	  case 'H':
	  case 'h': help(); break;
	  case 'C':
	  case 'c': create(); break;
	  case 'B':
	  case 'b': block(); break;
	  case 'W':
	  case 'w': wake(); break;
	  case 'S':
	  case 's': suspend(); break;
	  case 'E':
	  case 'e': fexcute(); break;
	  case 'O':
	  case 'o': output(); break;
	  case 'X':
	  case 'x': leave(); break;
	  case 'R':
	  case 'r': run(); break;
	}
 }
  return 1;
}

⌨️ 快捷键说明

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