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

📄 os.cpp

📁 linux 下的进程管理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include "iostream.h"
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
 class instruction{                  //指令
private:int  opcode ; //操作码
		int  oprand ; //操作数
public:
	instruction()
    {opcode = -1;
	 oprand = -1;
	}
	random()
	 {//srand(time(NULL));
	 opcode = rand()%5;
	// srand(time(NULL));
	 oprand = rand()%3;
	}
	int getopcode(){return opcode;}
	int getoprand(){return oprand;}
    setopcode(int a){opcode=a;}
	setoprand(int b){oprand=b;}
	void output()
	{cout<< opcode<<","<<oprand<<endl;}
};

class item {               //页表项
private: int pageNum;
		 int frameNum;
public:  item()
		 {pageNum=-1;
		 frameNum=-1;}
		 setpageNum(int pageNum1){pageNum=pageNum1;}
         setframeNum(int frameNum1){frameNum=frameNum1;}
         int getpageNum(){return pageNum;}
		 int getframeNum(){return frameNum;}
		 output()
		 {cout<<pageNum<<"       "<<frameNum<<endl;}
};


class pagetable{
public:item table[20];
		int exp; //替换指针
		int size;//页表当前的大小
		float count;//缺页次数
		float total;//访存的次数
public: pagetable1(int temp[10],int size1)
		{size=size1;
		 for(int i=0;i<=size-1;i++)
		 {table[i].setframeNum(temp[i]);
		 table[i].setpageNum(i);}
		 total=0;
		 count=0;
		 exp=0;
		}
        page (int pageNum)
		{total++;
		for(int i=0;i<=19;i++)
			if(table[i].getpageNum()==pageNum)
				break;
		if(i<=19)
			cout<<"系统中已有相应的页"<<endl;
		else
		{ cout<<"缺页"<<endl;
		exchange(pageNum);}
		}
		exchange(int pageNum)
         {table[exp].setpageNum(pageNum);  //FCFS
		  exp=(exp+1)%size;
		  count++;
		 }
     bool  free1(int temp[100],int &size1)
	 {  size1=size;
	    for(int i=0;i<=size-1;i++)
			temp[i]=table[i].getframeNum();
		   return true;
	 }
		 float frate()
		 {if(total==0)
		  return 0;
		  else
			return  count/total;
		 }
		 int decpt(){
			 if(size<=1)
			 {cout<<"错误:页表已无页架可回收"<<endl;
			  return -1;
			 }
			 size--;
			 if(exp==size)
				 exp=0;
			 table[size].setpageNum(-1);
			 return table[size].getframeNum();
		 }
		 void addpt(int temp){
			 size++;
			 table[size-1].setframeNum(temp);
			 exp=size-1;
		 }
		 output()
		 {cout<<endl;
			cout<<"进程发生缺页次数:"<<count<<endl;
		    cout<<"进程发生访存次数:"<<total<<endl;
			cout<<"缺页率:"<<frate()<<endl;
		  cout<<"页表一共有"<<size<<"项"<<endl;
		  cout<<"页号"<<"   页架号"<<endl; 
		 for(int i=0;i<=size-1;i++)
			  table[i].output();
		 cout<<endl;
		 }
		int getsize()
		{return size;
		}
};

class PCB {
public: int ID;//进程的ID
		int state;//进程的状态 1-ready 2-waiting 3-running
		//指令序列
		instruction A[30];//假定每个进程至多30条指令 以第一个4号指令结尾
		int PC;          //指向当前要执行的指令
		//虚存
		int VMsize;  //虚存大小  单位:页  
		pagetable PT; //页表
		//设备
		int ownsize; //占有的设备数  
		int own[5];  //占有的设备号
		int ownp;    //指向下一个空设备
		int request;//申请的设备号
		//运行时间
		int time;
        PCB*  next;    //指向下一个PCB
		PCB(){ID=-1;}
		refresh(int ID1 ,int temp[10] ,int size,int VMsize1)     
		{ ID=ID1;
		  state=1;
		  for(int i=0;i<=28;i++)
		  {   //srand(time(NULL));  //????????????????????????
		   int t1 =rand()%5;
		   int t2=0;
		   if(t1==1)
			   t2=rand()%20;
           else
		        t2=rand()%3;
		   A[i].setopcode(t1);
		   A[i].setoprand(t2);
		  }
		  A[29].setopcode(4);
		  PC=0;
		  VMsize=VMsize1;
		  PT.pagetable1(temp,size) ;
		  ownsize=0;
		  ownp=0;
		  for(i=0;i<=4;i++)
			  own[i]=-1;
		  request=-1;
		  time=0;
		}
		bool page(int pageNum)
		{ PT.page(pageNum);
		   return true;
		}
		bool free1(int temp[10],int &size)
		{ PT.free1(temp,size);
		  return true;
		}
	  float frate(){return PT.frate();}//返回缺页率
      int decpt(){return PT.decpt();} //
	  void addpt(int temp){PT.addpt(temp);}
		output(){
		   cout<<"************************************"<<endl;
			cout<<"进程号:"<<ID<<endl;
            cout<<"状态:";//<<state<<endl;
            switch(state)
			{case 1:cout<<"就绪"<<endl;
			        break;
			case 2: cout<<"等待"<<endl;
				    break;
			case 3: cout<<"运行"<<endl;
				    break;
			}
			cout<<"指令序列:"<<endl;
			for(int i=0;i<=29;i++)
				A[i].output();
             cout<<"PC : "<<PC<<endl;
			cout<<"虚存大小:"<<VMsize<<"M"<<endl;
			 cout<<"页表情况如下:"<<endl;
			 PT.output();
			 cout<<"占有"<<ownsize<<"台设备"<<endl;
			 for( i=0; i<=4;i++)
				 cout<<own[i]<<" , " ;
			     cout<<endl;
               cout<<"请求设备情况(-1为无):"<<request<<endl;	
				cout<<"运行时间:"<<time<<endl;
		 cout<<"************************************"<<endl;
		}		
     timer()
	 {time++;
	 }
};

//PCB的队列的定义
class pqueque{
private: PCB * head; //队头指针
		 PCB * tail; //队尾指针
		 int length; //队列长度
public: pqueque(){
			head=tail=new PCB();
			length=0;
		}
		void enqueque(PCB * p)
		{tail->next=p;
		 tail=tail->next;
		 length++;
		}
		bool dequeque(PCB* &p)
		{if(length==0)
		{cout<<"队列为空"<<endl;
		return false;
		}
		p=head->next;
		head->next=head->next->next;
		length--;
		if(length==0)
			tail=head;
		return true;
		}
		void front(PCB* p){
		 p->next=head->next;
		 head->next=p;
		 length++;
		 if(length==1)
			 tail=p;
		}
		int getlength()
		{ return length;
		}
		output(){
		cout<<"队列中一共有"<<length<<"个PCB"<<endl;
		PCB* t=head->next;
		for(int i=1;i<=length;i++)
		{t->output();
		t=t->next;}
		}
};

class mainmemory{
private: int frame[256];//页架序列 (一共256个页架)0-未用,1-已用
		 int  p;         //指向空页架的搜索指针
         int freesize;   //可用的空页架数
public:  mainmemory(){
			 for(int i=0;i<=255;i++)
				 frame[i]=0;
			 p=0;
			 freesize=256;}
		 int allocate(){
			 if(freesize==0)
			 {cout<<"错误:无物理存储空间可分配"<<endl;
			  return -1;
			 }
			 if(freesize==1)
			 {cout<<"警告:所有页架均已分配"<<endl;
              freesize--;
			  frame[p]=1;
              return p;}
			 int temp=p;
			 freesize--;
			 frame[p]=1;
			 while(frame[p]==1)
				 p=(p+1)%256;
			 return temp;
		 }
		void deallocate(int frameNum ){
			 p=frameNum;
			 frame[p]=0;
			 freesize++;
		 }
		 output(){
			 cout<<endl;
			 cout<<"物理空间共有页架数:256  页大小:4K "<<endl;
			 cout<<"其中可用空页架数:"<<freesize<<endl;
			 for(int i=0;i<=255;i++)
			 {cout<<frame[i]<<"   ";
			  if((i+1)%16==0)
				  cout<<endl;
			 }
			 cout<<"p="<<p<<endl;
			 cout<<endl;
		 }

};
class equip{
public:  int ID; //设备的编号
		 int state;//设备的状态   0-idle 1-busy
		 PCB* own ;//占有外设的进程的PCB
		 pqueque request ;//等待外设的进程队列
public:  
		 equip1(int ID1){
			 ID=ID1;
			 state=0;  //idle
		     own =new PCB();
		 }
		 int request1(PCB* a){
			 if(state==0)
			 { own = a;
			   state=1;
			   return 1;
			 }
			 if(state==1)
			 { if(a->ID==own->ID)
			 {cout<<"错误:该进程申请已经被它占有的设备"<<endl;
			 return -1;
			 }
			 else
			 {request.enqueque(a);
			   return 0;
			 }
			 }
			 return -1;
		 }
		 bool deallocate(PCB* &temp){
			 if(request.getlength()>0)
			 {request.dequeque(temp);
			  own=temp;
			  temp->state=1;
			  temp->own[temp->ownp++]=ID;
			  temp->ownsize++;
			 }
			 else
			 {state=0;
			  temp->state=-1;
			 }
			 return true;
		 }
		 output(){
	    cout<<endl;
	    cout<<"外设的编号:"<<ID<<endl;
		cout<<"外设的状态:";//<<state<<endl;
		if(state==0)
			cout<<"闲置"<<endl;
		else
			cout<<"忙碌"<<endl;
    	cout<<"占有外设的进程的编号:"<<own->ID<<endl;
		cout<<"等待队列的长度:"<<request.getlength()<<endl;
		 cout<<endl;

⌨️ 快捷键说明

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