vm.h

来自「一个简单的虚拟机和虚拟操作系统」· C头文件 代码 · 共 162 行

H
162
字号
#pragma once
#include "all.h"
#include "CPU.h"
#include "PCB.h"
#include "file.h"
#include "Instruction.h"
#include "dictionary.h"
#include "fs.h"

class PriQue
{
public:
	PriQue()
	{
		for(int i = 0;i<MAX_PRS_NUM;i++)
			p[i] = NULL;
	}
public:
	void Put(PCB*pcb)
	{
		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] == NULL)
			{
				p[i] = pcb;
				return;
			}
		throw INDEX_OUT;
	}

	PCB* Remove()
	{
		int PRI = -1;
		int index = -1;
		int wait = -1;

		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] == NULL)continue;
			else if(p[i]->PRI>PRI)
			{
				PRI = p[i]->PRI;
				index = i;
				wait = p[i]->WAITTIME;
			}
			else if(p[i]->PRI==PRI)
			{
				if(p[i]->WAITTIME>wait)
					index = i;
			}
		if(index == -1)
			throw INDEX_OUT;

		PCB* pcb = p[index];
		p[index] = NULL;
		return pcb;
	}

	PCB* Get()
	{
		int PRI = -1;
		int index = -1;
		int wait = -1;

		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] == NULL)continue;
			else if(p[i]->PRI>PRI)
			{
				PRI = p[i]->PRI;
				index = i;
				wait = p[i]->WAITTIME;
			}
			else if(p[i]->PRI==PRI)
			{
				if(p[i]->WAITTIME>wait)
					index = i;
			}
		if(index == -1)
			throw INDEX_OUT;
		return p[index];
	}
	void Remove(PCB*pcb)
	{
		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] == pcb)
			{
				p[i] = NULL;
				return;
			}
		throw INDEX_OUT;
	}

	bool Contain(PCB*pcb)
	{
		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] == pcb)
				return true;
		return false;
	}

	int Length()
	{
		int length = 0;
		for(int i = 0;i<MAX_PRS_NUM;i++)
			if(p[i] != NULL)
				length++;
		return length;
	}

private:
	PCB* p[MAX_PRS_NUM];
};

class VM
{
friend class CPU;

public:
	VM(void);
	
public:
	void Reset();
	void Restart();
	void Step();
public:
	void MemTrim();
public:
	int getMemInUse();
	int getCPURate();
	int getAllNum();
	int getActiveNum();
	int getReadyNum();
	int getWaitANum();
	int getWaitBNum();
/*
 * operations on processes
 */
public:
	void LoadProcess(CString s,int pri);
	void StartProcess();
	void StartProcess(int ID);
	void StopProcess(int ID);
	void StopProcess();
	void PauseProcess();
private:
	int CutR(CString&s);
public:
	bool IsActive();
public:
	CPU cpu;
	FS fs;

public:
	PCB* current;
	PriQue Q_ready;
	PriQue Q_waitA;
	PriQue Q_waitB;
	PCB* pcb[MAX_PRS_NUM];
	int time_A;
	int time_B;
	//PCB* p_res[2];
	int memHead;
	volatile int run;
};

⌨️ 快捷键说明

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