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

📄 computerosexp_03_02.cpp

📁 链表实现的银行家算法
💻 CPP
字号:
#include "stdafx.h"
#include <iostream.h> 
#include <time.h> 
#include <afx.h>
#include <conio.h> 
#include <stdio.h>
#include <stdlib.h> 
#include "Sys.h"
#include "PCB.h"
int GetRandom(int min, int max)//获取从min到max的随机数
{
	return rand() % (max + 1 -min) + min;
}
void main() 
{
	int pcbNum = 0, resourceNum = 0;
	Sys *sys = new Sys();
	sys->InitSys();
	const int resLen = sys->resLen;
	const int pcbLen = sys->pcbLen;
	int *req = new int[resLen];
	char **name = new char*[pcbLen];
	int ccc = 0;
	while(true)
	{
		PCB *tempPCB = sys->headPCB;
		srand((unsigned)time(NULL));//保证是获取到的随机数
		while(tempPCB != 0)
		{
			sys->PrintRemainRes();
			sys->PrintSys();
			cout << "进程" << tempPCB->name << "开始请求资源... ..." << endl;
			Res *tempRes = sys->headRes;
			int i = 0;
			cout << "进程" << tempPCB->name << "欲请求的资源如下:" << endl;
			while(tempRes !=0 && i < resLen)
			{
				req[i] = GetRandom(0, tempPCB->Need[i]);
				cout << tempRes->name << "  " << req[i] << "    ";
				tempRes = tempRes->nextRes; i++;
			}
			cout << endl;
			tempPCB->Run(sys->Remain, req);
			if(sys->IsSecurity(name) == true)
			{
				cout<< "进程" << tempPCB->name << "请求成功!" << "存在安全序列如下:" << endl;
				for(int j = 0; j < pcbLen; j++)
					cout << name[j] << "   ";
				cout << endl;
			}
			else
			{
				cout << "不存在安全序列!" << "进程" << tempPCB->name << "请求失败!" << endl;
				tempPCB->Resever(sys->Remain, req);
			}
			tempPCB = tempPCB->nextPCB;
			cout << endl << endl << endl;
		}
		ccc++;
		if(ccc > 0)
			break;
	}
	sys->PrintRemainRes();
	delete sys, req, name;
}

⌨️ 快捷键说明

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