现在的位置: 首页 > 综合 > 正文

模拟操作系统优先级调度进程

2013年10月16日 ⁄ 综合 ⁄ 共 3958字 ⁄ 字号 评论关闭

自学C++,也写过一些小程序,就拿最近写的一个出来与大家分享一下,VC6编译通过且运行无误;有问题还请大家斧正!

操作系统模拟演示实验程序

这是我们操作系统课程结业时老师布置的编程题,是模拟操作一些工作方式编的程序,以更进一步了解操作系统工作方式。
这里只列出一个:按优先级调度进程运行的小程序;
 
//////////////////////////////////////////////////////////////////////////
//Run.h

#ifndef C_H_H
#define C_H_H
#include <conio.h>
#include <iostream>
#include <windows.h>
#include "PCB.h"

#endif

int FindMax(PCB*pcb);

bool Decide (PCB*pcb);

PCB* Input();

void Deal(PCB*);

bool m_IsNum(char );

bool m_IsNum(char *  );

int GetPcbNum(PCB * pcb);

void show(PCB * pcb);

void showRunning(PCB*pcb,int );
////Run.cpp

#include "Run.h"


int FindMax(PCB*pcb)//找最大的优先级。
{   

	
	int num=pcb->CountPcb;
	int max=0;
	for(int i=0;i<num;i++){
		if(pcb[i].priority>=max)
			if(pcb[i].time>0 ){
				max=pcb[i].priority;		//优先级最大而且还没有运行完毕,就将其优先级赋给max.
			}	
	}
	for(int j=0;j<num;j++){
		if(max==pcb[j].priority){
			if(pcb[j].time>0){
				return j;				//优先级最大而且没有运行完毕,就返回下标。
			}
		}
	}
	return 0;
	
}

bool Decide (PCB *pcb)
{
	int s=0;
	for(int i=0;i<pcb->CountPcb;i++){
		s+=pcb[i].time;
	}
	if(s==0)return false;
	else
		return true;  //没有运行完。
}

bool m_IsNum(char c)//输入合法性。
{
	return ( c<= '9' && c>='0');
	return false;
}

bool m_IsNum(char *  lpc)//重载函数,输入合法性。
{
	int i=0;
	while (lpc[i]!=0)
	{
		if (lpc[i]>'9' || lpc[i] <'0') return false;
		i++;
	}
	return true;
	
}

int GetPcbNum(PCB * pcb)
{
	return pcb->CountPcb;
}

void show(PCB * pcb)
{
	int totaltime=0;
	for (int ii=0;ii<pcb->CountPcb;ii++)
	{
		totaltime+=pcb[ii].time;
	}
	system("cls");
	int num=pcb->CountPcb;
		
		cout<<"	"<<"PCBname		"<<"Priority	"<<"Time		"<<"State"<<endl;
		cout<<"																			 "<<totaltime<<endl;
	for(int i=0;i<num;i++)
	{
		cout<<""<<endl;
		cout<<"	"<<pcb[i].name<<"		"<<pcb[i].priority<<"		"<<pcb[i].time<<"		"<<pcb[i].state<<endl;
		cout<<""<<endl;
	}
	
}

void showRunning(PCB * pcb,int maxmark)
{
	int totaltime=0;
	for (int ii=0;ii<pcb->CountPcb;ii++)
	{
		totaltime+=pcb[ii].time;
	}
	system("cls");
	int num=pcb->CountPcb;
	cout<<"	"<<"PCBname		"<<"Priority	"<<"Time		"<<"State"<<endl;
	cout<<"																			 "<<totaltime<<endl;
	for(int i=0;i<num;i++)
	{
		if(i==maxmark)
		{
				cout<<""<<endl;
				cout<<"	"<<pcb[i].name<<"		"<<pcb[i].priority<<"		"<<pcb[i].time<<"		"<<pcb[i].state<<"     √"<<endl;
				cout<<""<<endl;
		}
		else
		{
				cout<<""<<endl;
				cout<<"	"<<pcb[i].name<<"		"<<pcb[i].priority<<"		"<<pcb[i].time<<"		"<<pcb[i].state<<endl;
				cout<<""<<endl;
		}
	}
}


PCB* Input()
{
	PCB*PCB_lINK;
	char*  temp="INput";
	int num;
	system("cls");
	cout<<endl<<"			=====>>>>>>>>>INPUT<<<<<<<<<=====			"<<endl<<endl;
	while(!m_IsNum(temp))//直到输入的为数字。
	{
		cout<<endl<<"		Input The number of PCB you want to get in:  ";
		cin>>temp;
	}
	num =atoi(temp);
	PCB_lINK=new PCB[num];
	int count=0;
	char * numtemp="cc";
	char * tiemtemp="dd";
	while(count<num)
	{	
		
		cout<<""<<endl;
		cout<<endl<<"	The PCB "<<++count<<endl;
		--count;
		cout<<endl<<"	PCBname:            ";
		cin>>PCB_lINK[count].name;
		while (!m_IsNum(numtemp))
		{
			cout<<endl<<"	Priority:           ";
			cin>>numtemp;
		}
		PCB_lINK[count].priority=atoi(numtemp);
		while (!m_IsNum(tiemtemp))
		{
			cout<<endl<<"	Time                ";
			cin>>tiemtemp;
		}

		PCB_lINK[count].time = atoi(tiemtemp);
		PCB_lINK[count].CountPcb=num;
		PCB_lINK[count].state=">_<";
		count++;
		numtemp=new char[];
		tiemtemp=new char[]; 
	}
	show(PCB_lINK);
	return PCB_lINK;
}





void Deal(PCB* pcb)//PCB一次运行的处理。
{
	Sleep(2000);
	for (int i=0;i<pcb->CountPcb;i++)
	{
		if (pcb[i].time==0)
		{
			pcb[i].state="Done";
		}
		else
			pcb[i].state="Enable";//都置为Enable;
	}
	
	int maxmark=FindMax(pcb);
	if (pcb[maxmark].time>0)
	{
		pcb[maxmark].state="Running";
		
		if (pcb[maxmark].priority>0)
		{	
			pcb[maxmark].priority--;
		}
		
		pcb[maxmark].time--;
		showRunning(pcb,maxmark);
	}
	
	if(pcb[maxmark].time==0)
	{
		pcb[maxmark].state="Done";
		
		pcb[maxmark].priority=0;//如果一个进程运行完毕,其优先级就置为0;
		show(pcb);
		
	}
	
	
}

// PCB.h: interface for the PCB class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_PCB_H__D437D36E_79E5_40FA_B1C3_5903CA35AA07__INCLUDED_)
#define AFX_PCB_H__D437D36E_79E5_40FA_B1C3_5903CA35AA07__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <string>
using namespace std;
class PCB  
{
	
	public:
			int priority;
			int time;
			string name;
			string state;
			int CountPcb;

	public:
			PCB();
			virtual ~PCB();

};

#endif // !defined(AFX_PCB_H__D437D36E_79E5_40FA_B1C3_5903CA35AA07__INCLUDED_)

//Deal.cpp 

#include <iostream>
#include <cstring>
#include <conio.h>
#ifndef C_H_H
#define C_H_H
#include "PCB.H"
#include "Run.h"
#endif

PCB *PCB_lINK;

int main()
{
	while(1)
	{
		PCB_lINK=Input();
		while (Decide(PCB_lINK))
		{
			Deal(PCB_lINK);
		}
		cout<<endl<<endl<<"	按任意键开始_/"<<endl;
		getch();
		
	}
	return 0;
}


抱歉!评论已关闭.