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

📄 os3dlg.cpp

📁 linux 下的进程管理
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			temp->ownsize++;
		}
		else
		{
			state=0;
			temp->state=-1;
		}
		return true;
	}
	CString output()
	{	   
		CString strOutput,str1;	
		str1.Format("%d",ID);
		strOutput="\r\n外设的编号:"+str1+"\r\n";	    
		strOutput+="外设的状态:";		
		if(state==0)			
			strOutput+="闲置\r\n";
		else			
			strOutput+="忙碌\r\n";
		str1.Format("%d",own->ID);
		strOutput+="占有外设的进程的编号:"+str1+"\r\n";    	
		str1.Format("%d",request.getlength());
		strOutput+="等待队列的长度:"+str1+"\r\n";		
		return strOutput;
	}
	int	getstate()
	{
		return state;
	}
};

//定义"调度"类
class schedule{
public: 
	PCB process[10];//假定系统最多只有10个进程
	int Psize;      //系统当前拥有的进程数
	int ep;         //指向一个空进程模块
	mainmemory M;   //主存
	equip a[3];     //三台外设
	pqueque F[3];	 //多级反馈队列
	int count;      //用以记录二次时间
	int OSMEM[10];  //模拟OS占用主存
	int time;        //系统运行时间
public:    
	schedule()
	{
		for(int i=0;i<=9;i++)     //操作系统占用了10个页
			OSMEM[i]=M.allocate();
		//初始进程
		Psize=ep=1;
		int temp[20];//请求的页数
		for(int j=0;j<=19;j++)
			temp[j]=M.allocate();
		process[0].refresh(ep,temp,10,100);
		F[0].enqueque(&process[0]);//放入调度队列中
		count=1;
		//初始化外设
		for(j=0;j<=2;j++)
			a[j].equip1(j);
		time=0;
	}
	bool step()
	{
		PCB* temp=new PCB();
		if(F[0].getlength()!=0)  //首先看第一级队列中是否有等待执行的进程
		{
			F[0].dequeque(temp);//从队列中取出一个进程		
			execute(temp);//执行该进程
			back(temp,1);//执行一个时间片之后又进入队列
			return true;
		}
		else 
			if(F[1].getlength()!=0)	
			{
				F[1].dequeque(temp);
				execute(temp);
				back(temp,2);
				return true;
			}
			else //看第二级队列中是否有等待执行的进程
				if(F[2].getlength()!=0)
				{
					F[2].dequeque(temp);
					execute(temp);
					back(temp,3);
					return true;
				}
				else{
					cout<<"系统中已经无用户进程"<<endl;// this line need  to be modified			
					return false;
				}
	}
	bool execute(PCB* P) //执行进程
	{
		P->state=3;  //running
		instruction  IR=P->A[P->PC]; //取指令
		P->PC++;
		int t=IR.getopcode();//指令译码
		switch(t)
		{
		case 0: //进程创建子进程
			{
				Psize++; //进程数加一
				ep++;
				int temp[10];
				for(int i=0;i<=9;i++)
					temp[i]=M.allocate();
				process[ep-1].refresh(ep,temp,10,IR.getoprand());
				F[0].enqueque(&process[ep-1]);//子进程进入就绪队列
				return true;
			}
		case 1: //进程执行JMP指令进行跳转
			{ 
				P->page(IR.getoprand());				
				return true;
			}			  
		case 2://进程申请某种资源
			{
				int t=IR.getoprand();
				int test=a[t].request1(P);//申请设备
				if(test==1)//申请成功
				{
					P->own[P->ownp++]=t;
					P->ownsize++;
				}
				if(test==0)//等待外设
				{
					P->request=t;
					P->state=2;}
				if(test==-1)//申请失败
					cout<<"申请失败"<<endl;// this line need  to be modified			
				return true;
			}   
		case 3://进程释放资源
			{
				int t=IR.getoprand();
				PCB* temp=new PCB();
				if(a[t].own->ID==P->ID && a[t].getstate()==1)
				{
					a[t].deallocate(temp);//释放设备
					if(temp->state==1)
						F[0].enqueque(temp);//进入就绪队列
					int j=0;
					while(P->own[j]!=t)
						j++;
					P->own[j]=-1;
					P->ownsize--;
					return true;
				}
				else
				{
					cout<<"错误:进程请求释放一个并没有占有的设备"<<endl;// this line need  to be modified			
					return false;
				}
			}
		case 4://进程退出
			{PCB* t=new PCB();
			int temp[100],size;
			P->free1(temp,size);//释放内存空间
			for(int i=0;i<=size-1;i++)
				M.deallocate(temp[i]); 
			for(int j=0;j<=2;j++)
				if(a[j].own->ID==P->ID && a[j].getstate()==1)
				{
					a[j].deallocate(t);
					if(t->state==1)			
						F[0].enqueque(t);
				}
				Psize--;
				P->ID=-1;
				P->state=-1;
				return true;
			}
		}
		return true;
	}
	bool back(PCB* P,int s) //从运行状态回到就绪队列
	{
		if(P->state==3)
		{
			P->state=1;
			switch(s)
			{
			case 1:
				{
					F[1].enqueque(P);
					return true;
				}
			case 2:
				{
					if(count==1)
					{
						F[1].front(P);
						count--;
					}
					else
					{
						F[2].enqueque(P);
						count++;
					}
					return true;
				}
			case 3:
				{
					F[2].front(P);		
					return true;
				}		   
			}		   
		}
		return true;
	}
    void pagefaultcheck()
	{
		//控制缺页率  (0.2<=pagefault rate<=0.5)		i
		int temp=0;
		if(time%10 ==0)
			for(int i=0;i<=9;i++)
				if(process[i].ID!=-1)
				{
					if(process[i].frate()<0.2) //缺页率太低,则删除一些页表项
					{
						temp=process[i].decpt();	
						M.deallocate(temp);
					}	
					if(process[i].frate()>0.5)//缺页率太高,则添加一些页表项
					{
						temp=M.allocate();
						process[i].addpt(temp);
					}
				}
	}
	CString  menu(int kind)
	{
		CString strOutInfo=" ";
		switch(kind)
		{
		case 0: //进程信息
			{
				for(int i=0;i<=9;i++)
					if(process[i].ID!=-1)
						strOutInfo=process[i].output();						
					break;
			}
		case 1:   //内存信息
			strOutInfo=M.output();
			break;
		case 2:  //设备信息
			{
				for(int i=0;i<=2;i++)
					strOutInfo+=a[i].output();
				break;
			}
		case 3:   //就绪队列
			{
				CString str1;							
				for(int i=0;i<=2;i++)
				{
					str1.Format("%d",i);
					strOutInfo+="\r\n第"+str1+"队:\r\n";
					strOutInfo+=F[i].output();
				}
				break;
			}
		case 4:   //等待队列
			{
				CString str1;
				for(int i=0;i<=2;i++)
				{
					str1.Format("%d",i);
					strOutInfo+="\r\n第"+str1+"号设备的等待队列:\r\n";
					strOutInfo+=a[i].request.output();}
				break;
			}
		case 5:    //下一步
			{  					
				strOutInfo="\r\n执行下一步\r\n";
				time++;
				for(int i=0;i<=9;i++)
                    if(process[i].ID!=-1)
						process[i].timer();
					step();
					pagefaultcheck();
					break;
			}
		default:
			strOutInfo="错误!";
		}
		return strOutInfo;	
	}
};

//全局变量a添加在这里
schedule a;

/////////////////////////////////////////////////////////////////////////////
// COs3Dlg dialog

COs3Dlg::COs3Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(COs3Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(COs3Dlg)
	m_result = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//以下四行代码表示初始化进程
	a.process[0].A[0].setopcode(0);
	a.process[0].A[0].setoprand(2);
   	a.process[0].A[1].setopcode(0);
	a.process[0].A[1].setoprand(2);	
}

void COs3Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COs3Dlg)
	DDX_Control(pDX, IDC_TEXT_RESULT, m_ctrl_info);
	DDX_Text(pDX, IDC_TEXT_RESULT, m_result);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COs3Dlg, CDialog)
	//{{AFX_MSG_MAP(COs3Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_PROCESS, OnProcess)
	ON_BN_CLICKED(IDC_MEMORY, OnMemory)
	ON_BN_CLICKED(IDC_READY, OnReady)
	ON_BN_CLICKED(IDC_WAIT, OnWait)
	ON_BN_CLICKED(IDC_NEXT, OnNext)
	ON_BN_CLICKED(IDC_DEVICE, OnDevice)
	ON_BN_CLICKED(IDC_CLEAR, OnClear)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COs3Dlg message handlers

BOOL COs3Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void COs3Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR COs3Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void COs3Dlg::OnProcess() //显示进程信息
{
	// TODO: Add your control notification handler code here
	CString str=a.menu(0);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);	
}

void COs3Dlg::OnMemory() //显示内存信息
{
	// TODO: Add your control notification handler code here

	CString str=a.menu(1);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);	
}

void COs3Dlg::OnDevice() //显示设备信息
{
	// TODO: Add your control notification handler code here
	CString str=a.menu(2);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);		
}

void COs3Dlg::OnReady() //显示就绪队列
{
	// TODO: Add your control notification handler code here
	CString str=a.menu(3);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);	
}

void COs3Dlg::OnWait() //显示等待队列
{
	// TODO: Add your control notification handler code here
	CString str=a.menu(4);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);			
}

void COs3Dlg::OnNext() //显示下一步
{
	// TODO: Add your control notification handler code here
	CString str=a.menu(5);
	m_result+=str;
	m_ctrl_info.SetWindowText(m_result);	
}

void COs3Dlg::OnClear() //清空文本框
{
	// TODO: Add your control notification handler code here
	m_result="";
	m_ctrl_info.SetWindowText(m_result);
}

⌨️ 快捷键说明

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