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

📄 cpp1.cpp

📁 一个小的进程调度
💻 CPP
字号:

#include <iostream.h>
#include <malloc.h>
#include "stdio.h"

#define null 0
#define running 1 /*用running 表示进程处于运行态*/
#define ready 2 /*用aready表示进程处于就绪态*/
#define blocking 3 /*用blocking表示进程处于等待态*/
//#define n 10 /*假定系统允许进程个数为10*/


struct pcb {
	        
		     int name;//进程的名字(程序的入口地址)
             int state;//进程状态
			 int pr; //
			 struct pcb *next;
		   };
int m,n,k;//分别记录就绪队列,阻塞队列的进程数,正在运行的进程的个数
struct pcb *ready_head,*r,*block_head,*b,*run_head,*run;


//----------------------------------------------------------
void enReadyqueue(struct pcb *p)//进入就绪队列
{  
	if( ready_head==null){ 
	                      ready_head=p;
						  r=ready_head;
                          r->next=p->next;
                       }
	else  { r->next=p;
	         
			 r=p;
			 }
	  
  m++;
}

	
//----------------------------------------------------------

void create( )//创建进程,并把该进程压进就绪队列
{ struct pcb *p;
  p=(struct pcb *)malloc(sizeof(struct pcb));
  p->next=null;
  p->state=2;
  p->pr=1;
  cout<<"please enter the program's name"<<endl;
  cin>>p->name;
  enReadyqueue(p);
  cout<<"creat complete"<<endl;
}
//----------------------------------------------------------
void enBlockqueue( )//从运行队列中取头节点进入就阻塞队列
{  struct pcb *p,*q;
    p=run_head;
	if(run_head==null)cout<<"run queue is empty,error"<<endl;
	else{ if( block_head==null){
	                   block_head=p;
					   b=block_head;
                       b->next=null;
					   cout<<"block finish"<<endl;
                       }
    	  else  {
			  b->next=p;
	          b=p;
			  b->next=null;
			
			 }
		  q=run_head;
		  run_head=run_head->next;
		  free(q);
	      k--;
	   
	}
	  
   n++;
}
//------------------------------------------------------------
void enRunqueue(struct pcb *p)//正在运行的进程进入运行队列
{  if( run_head==null){
	                   run_head=p;
					   run=run_head;
                       run->next=p->next;
                       }
	else  
	{ run->next=p;
	         run=p;
			 
	}
	  
   k++;
}

//------------------------------------------------------------
void wakeup()//唤醒进程(把阻塞队列的头节点插入就绪队列尾)
{ struct pcb *p,*q;
  
	if(block_head==null) cout<<"blocked queue is empty,error"<<endl;
   else { p=(struct pcb*)malloc(sizeof(struct pcb));
	      p->state=ready;
          p->pr=1;
          p->name=block_head->name;
	      p->next=null;
	      enReadyqueue(p);
		  q=block_head;
          block_head=block_head->next;
		  free(q);
		  n--;
		  cout<<"wake up one program"<<endl;
        }
   
}
//------------------------------------------------------------
void show()//显示当前正在运行的进程
{ struct pcb *p;
    p=run_head;
	if(p==null) cout<<"run queue is empty,error"<<endl;
	else cout<<"the number of running program is:   "<<k<<endl;
    while(p)
	{   cout<<"the running program's name is:   "<<p->name<<endl;
	    cout<<"the running program's state is:   "<<p->state<<endl;
		
	 p=p->next;
	}
}
//------------------------------------------------------------
void Run()//从就绪队列中去头节点运行
{  struct pcb *p;
     
    p=(struct pcb*)malloc(sizeof(struct pcb));
	if(ready_head==null) cout<<"ready queue is empty,error"<<endl;
   else { p->state=running;
          p->pr=1;
          p->name=ready_head->name;
	      p->next=null;
	      enRunqueue(p);
          ready_head=ready_head->next;
		 m--;
		 cout<<"a program is pushed into run_queue"<<endl;
        }
}



//------------------------------------------------------------------
void delect(int e )//结束一个进程(从运行状态队列中删掉一个进程)
{
	struct pcb *p,*q;
  int find;//用来判断是否找到名字为e的进程
  find=0;
  q=p=run_head;
 if(run_head==null)cout<<"the running queue is empty,error"<<endl;
 else if(run_head->name==e)
        {  
	      run_head=run_head->next;
          free(q);
		  find=1;
        }
   else{ p=run_head->next;
        while(p)
		{  
           if(p->name==e) 
		   { 
		    find=1;
            q->next=p->next;
            free(p);
		    k--;
		   }
	      else 
		  {
		   q=p;
	       p=p->next;
		  }
		}
  }
   if(find) cout<<"delect finish"<<endl;
     else cout<<"error,can't find the program"<<endl;
}
//-----------------------------------------------------------------
void main()
{

b=block_head;
run=run_head;
int i; char j;

i=1;
   while(i)
   {      cout<<"--------------------------------------"<<endl;
	      cout<<"creat a program please enter c."<<endl;
	      cout<<"delect a program please enter d."<<endl;
	      cout<<"run a program please enter r."<<endl;
	      cout<<"block a program please enter b."<<endl;
	      cout<<"wake up a program please enter w."<<endl;
		  cout<<"watch the running programs please enter s."<<endl;
		  cout<<"--------------------------------------"<<endl;
          cin>>j;
     
	switch(j)
		{
		case 'c':
			create();
			break;
		case 's':
			show();
			break;
		case 'r':
			Run();
			break;
		case 'b':
		     enBlockqueue();
			break;
		case 'w':
			wakeup();
			break;
		case'd':
			int e;
			cout<<"enter the program's name that you want to delect"<<endl;
			cin>>e;
            delect(e);
		    break;
		default: 
			break;
		}
	cout<<"if you want continnue please enter 1"<<endl;
	cin>>i;
   }

}

⌨️ 快捷键说明

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