sys.cpp

来自「链表实现的银行家算法」· C++ 代码 · 共 237 行

CPP
237
字号
#include "stdafx.h"
#include "Sys.h"
#include <iostream.h>
#include <afx.h>
#include <conio.h> 
#include <stdio.h>
#include <stdlib.h> 
Sys::Sys()
{
	pcbLen = 0;
	resLen = 0;
	Flag = 0;
	Work = 0;
	Remain = 0;
	headRes = 0;
	tailRes = 0;
	headPCB = 0;
	tailPCB = 0;
}
Sys::~Sys()
{
	delete Work;
	delete Flag;
	delete Remain;
	FreeRes();
	FreePCB();
}
void Sys::FreeRes()
{
	Res *tempRes = tailRes;
	while(tempRes != 0 && tailRes->preRes != 0)
	{
		tempRes = tailRes->preRes;
		delete tailRes;
		tailRes = tempRes;
	}
	if(tailRes->preRes == 0)
		delete tailRes;
}
void Sys::FreePCB()
{
	PCB *tempPCB = tailPCB;
	while(tempPCB != 0 && tailPCB->prePCB != 0)
	{
		tempPCB = tailPCB->prePCB;
		delete tailPCB;
		tailPCB = tempPCB;
	}
	if(tailPCB->prePCB == 0)
		delete tailPCB;
}
bool Sys::InsertRes(Res *res)
{
	bool result = false;
	if(resLen == 0)
	{
		result = true;
		headRes = res;
		tailRes = res;
	}
	else
	{
		result = true;
		tailRes->nextRes = res;
		res->preRes = tailRes;
		tailRes = res;
	}
	resLen++;
	return result;
}
void Sys::CompleteRes()
{
	Work = new int[resLen];
	Remain = new int[resLen];
	for(int i = 0; i < resLen; i++)
	{
		Work[i] = 0;
		Remain[i] = 0;
	}
}
void Sys::CompletePCB()
{
	Flag = new bool[pcbLen];
	for(int i = 0; i < pcbLen; i++)
		Flag[i] = false;
}
bool Sys::InsertPCB(PCB *pcb)
{
	bool result = false;
	if(pcbLen == 0)
	{
		result = true;
		headPCB = pcb;
		tailPCB = pcb;
	}
	else
	{
		result = true;
		tailPCB->nextPCB = pcb;
		pcb->prePCB = tailPCB;
		tailPCB = pcb;
	}
	pcbLen++;
	return result;
}
void Sys::InitSys()
{
	ReadRes();
	CompleteRes();
	ReadPCB();
	CompletePCB();
	ConstructRemain();
}
void Sys::ReadRes()
{
	CStdioFile csf;
	csf.Open("res.txt", CFile::modeRead);
	CString str; 
	while(csf.ReadString(str))
	{
		Res *res = new Res();
		sscanf(str, "%[^,],%d", res->name, &res->total);
		InsertRes(res);
	}
	csf.Close();
}
void Sys::ReadPCB()
{
	CStdioFile csf;
	csf.Open("pcb.txt", CFile::modeRead);
	CString str; 
	while(csf.ReadString(str))
	{
		PCB *pcb = new PCB();
		pcb->resLen = resLen;
		pcb->InitPCB();
		const int len = pcb->resLen;
		int *max = new int[len];
		int a, b, c, d, e;
		sscanf(str, "%[^,],%d,%d,%d,%d,%d", pcb->name, &a, &b, &c, &d, &e);
		max[0] = a; max[1] = b; max[2] = c; max[3] = d; max[4] = e;
		pcb->InitPCB(max);
		InsertPCB(pcb);
		delete max;
	}
	csf.Close();
}
void Sys::ConstructRemain()
{
	int i = 0;
	Res *tempRes = headRes;
	while(i < resLen && tempRes != 0)
	{
		Remain[i] = tempRes->total;
		i++;
		tempRes = tempRes->nextRes;
	}
}
void Sys::ConstructWork()
{
	for(int i = 0; i < resLen; i++)
		Work[i] = Remain[i];
}
bool Sys::IsSecurity(char *name[])
{
	for(int i = 0; i < pcbLen; i++)
		Flag[i] = false;
	ConstructWork();
	bool result = true;
	int iPCB = 0, iRes = 0, iName = 0, count = 0;
	PCB *tempPCB = headPCB;
	Res *tempRes = headRes;
	for(count = 0; count < pcbLen; count++)
	{
		for(iPCB = 0, tempPCB = headPCB; iPCB < pcbLen && tempPCB != 0; iPCB++, tempPCB = tempPCB->nextPCB)
		{
			result = true;
			if(Flag[iPCB] == false)
			{
				for(iRes = 0, tempRes = headRes; iRes < resLen && tempRes != 0; iRes++, tempRes = tempRes->nextRes)
				{
					if(tempPCB->Need[iRes] > Work[iRes])
						result = false;
					else
						Work[iRes] = tempRes->total + tempPCB->Alloc[iRes];
				}
				if(result == true)
				{
					name[iName] = tempPCB->name;
					iName++;
					Flag[iPCB] = true;
				}
			}
		}
	}
	for(i = 0; i < pcbLen; i++)
		if(Flag[i] == false)
			return false;
	return true;
}
void Sys::PrintPCB()
{
	PCB *tempPCB = headPCB;
	while(tempPCB != 0)
	{
		cout << tempPCB->name << "   "  << tempPCB->Max[0] << endl;
		tempPCB = tempPCB->nextPCB;
	}
}

void Sys::PrintRemainRes()
{
	cout << "系统剩余的资源如下:" << endl;
	Res *tempRes = headRes; 
	int i = 0;
	for(tempRes = headRes; tempRes!= 0 && i < resLen; tempRes = tempRes->nextRes, i++)
		cout << tempRes->name << "   " << Remain[i] << "   ";
	cout << endl;
}
void Sys::PrintSys()
{
	cout << "当前系统中各进程对各资源的最大资源需求,分配到的资源,还需要的资源如下:" << endl;
	cout << "     ";
	for(Res *tempRes = headRes; tempRes != 0; tempRes = tempRes->nextRes)
		cout << tempRes->name << "      ";
	cout << endl;
	for(PCB *tempPCB = headPCB; tempPCB != 0; tempPCB = tempPCB->nextPCB)
	{
		cout << tempPCB->name << "   ";
		for(int i = 0; i < resLen; i++)
		{
			cout << tempPCB->Max[i] << "/" << tempPCB->Alloc[i] << "/" << tempPCB->Need[i] << "   "; 
		}
		cout << endl;
	}
}

⌨️ 快捷键说明

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