📄 banker.cpp
字号:
// banker.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "windows.h"
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#define MAX_PROCESS 32 //The max number of processes
#define MAX_SOURCE 32 // The maximum of resource types
int MAX_REAL_PROCESS; //The real number of processes
int MAX_REAL_SOURCE; //The real number of resource types
int Available[MAX_SOURCE]; //Availble resource.
int Max[MAX_PROCESS][MAX_SOURCE]; //The matrix of maximum of need
int Allocation[MAX_PROCESS][MAX_SOURCE]; //The matrix of allocated resource
int Need[MAX_PROCESS][MAX_SOURCE]; // The need matrix
int Request_PROCESS; //The request process
int Request_SOURCE; //The request resource type
int Request_SOURCE_NEMBER; //The request number of resource.
typedef struct NODE
{
int value;
int num;
int next;
} Node;
//input read function.
void InitRead(void)
{
cout<<"Input the number of processes: ";
cin>>MAX_REAL_PROCESS;
cout<<"Input the number of source types: ";
cin>>MAX_REAL_SOURCE;
cout<<"Input the number of Available "<< MAX_REAL_SOURCE<<" kinds of resource seperately :"<<endl;
for (int j=0; j<MAX_REAL_SOURCE; j++)
{
cin>>Available[j];
}
cout<<"\nInput the max request number of various resource of processes["<<MAX_REAL_PROCESS<<"]["<<MAX_REAL_SOURCE<<"]: "<<endl;
cout<<"\n("<<MAX_REAL_PROCESS<<"X"<<MAX_REAL_SOURCE<<")Matrix:"<<endl;
for (int i=0; i<MAX_REAL_PROCESS; i++)
{
cout <<"Process"<<i<<":"<<endl;
for (int j=0; j<MAX_REAL_SOURCE; j++)
{
cout<<"the number of Allocated Resource "<< j<<" :"<<endl;
cin>>Max[i][j];
}
}
}
//Initialize the allocated matrix.
void InitAllocated(void)
{
cout<<"\nInput the allocated number of various resource seperately ["<<MAX_REAL_PROCESS<<"]["<<MAX_REAL_SOURCE<<"]: "<<endl;
cout<<"\n("<<MAX_REAL_PROCESS<<"×"<<MAX_REAL_SOURCE<<") Matrix:"<<endl;
for (int i=0; i<MAX_REAL_PROCESS; i++)
{
cout <<"Process"<<i<<":"<<endl;
for (int j=0; j<MAX_REAL_SOURCE; j++)
{
cout<<"the number of Allocated Resource "<< j<<" :"<<endl;
cin>>Allocation[i][j];
}
}
}
//Set the need matrix.
void SetNeed(void)
{
cout<<endl;
cout<<"Set request matrix"<<'\n';
for(int i=0;i<MAX_REAL_PROCESS;i++)
for(int j=0;j<MAX_REAL_SOURCE;j++)
Need[i][j]=Max[i][j]-Allocation[i][j];
}
//input the request.
void ReadRequest(void)
{
cout<<" Input the request process:"<<endl;
cin>>Request_PROCESS;
cout<<" Input the request kind of resource:"<<endl;
cin>>Request_SOURCE;
cout<<" Input the request number of resource:"<<endl;
cin>>Request_SOURCE_NEMBER;
}
//output the reallocated resource.
void ReAllocateResource(void)
{
cout<<endl;
cout<<"Start process "<<Request_PROCESS<<"to allocate resource "<<Request_SOURCE<<" , number is "<<Request_SOURCE_NEMBER<<" "<<endl;
cout<<endl;
cout<<"Congratulation!"<<endl;
}
//safety test.
void SafetyTest()
{
cout<<'\n'<<"Enter safety test"<<endl;
int Run[MAX_SOURCE];
for(int i=0;i<MAX_REAL_SOURCE;i++)
{
Run[i]=Available[i];
}
bool Finish[MAX_PROCESS][MAX_SOURCE];
for(i=0;i<MAX_REAL_PROCESS;i++)
for(int j=0;j<MAX_REAL_SOURCE;j++)
Finish[i][j]=false;
Node Array[32];
for(i=0;i<MAX_REAL_PROCESS;i++)
{
Array[i].value=Need[i][Request_SOURCE-1];
Array[i].num=i;
}
for(i=0;i<MAX_REAL_PROCESS;i++)
for(int j=i+1;j<MAX_REAL_PROCESS;j++)
{
if(Array[i].value>=Array[j].value)
{
int t;
t=Array[j].value;
Array[j].value=Array[i].value;
Array[i].value=t;
t=Array[j].num;
Array[j].num=Array[i].num;
Array[i].num=t;
}
else
continue;
}
if(Finish[Request_PROCESS][Request_SOURCE]==false
&&Need[Request_PROCESS][Request_SOURCE]<=Run[Request_SOURCE])
{
Run[Request_SOURCE]=Run[Request_SOURCE]+Allocation[Request_PROCESS][Request_SOURCE];
Finish[Request_PROCESS][Request_SOURCE]=true;
}
else
{
cout<<"Not safe, failed!"<<endl;
return;
}
for(i=0;i<MAX_REAL_PROCESS;i++)
{
if(Array[i].num==Request_PROCESS)
continue;
if(Array[i].num!=Request_PROCESS
&&Finish[Array[i].num][Request_SOURCE]==false
&&Need[Array[i].num][Request_SOURCE]<=Run[Request_SOURCE])
{
Run[Request_SOURCE]=Run[Request_SOURCE]+Allocation[Array[i].num][Request_SOURCE];
Finish[Array[i].num][Request_SOURCE]=true;
}
}
for(i=0;i<MAX_REAL_PROCESS;i++)
{
if(Finish[i][Request_SOURCE]==true)
continue;
else
{
cout<<"Not safe, failed!"<<endl;
return;
}
}
cout<<'\n'<<"Find out safe sequence:"<<"P"<<Request_PROCESS<<"--->";
for(i=0;i<MAX_REAL_PROCESS;i++)
{
if(Array[i].num==Request_PROCESS)
continue;
else
cout<<"P"<<Array[i].num<<"--->";
}
cout<<'\n'<<"passed safe test!"<<endl;
ReAllocateResource();
}
//Handle the condition process.
void HandleFunc(void)
{
cout<<"Start process request..."<<endl;
if(Request_SOURCE_NEMBER>Need[Request_PROCESS][Request_SOURCE])
{
cout<<'\n'<<"Process No. "<<Request_PROCESS<<" request the resource "<<Request_SOURCE<<", number is "<<Request_SOURCE_NEMBER<<" "<<endl;
cout<<"exceed the maximum of resource."<<endl;
Sleep(2);
return;
}
if(Request_SOURCE_NEMBER>Available[Request_SOURCE])
{
cout<<'\n'<<"Process No."<<Request_PROCESS<<" request the resource "<<Request_SOURCE<<", number is"<<Request_SOURCE_NEMBER<<" "<<endl;
cout<<"No enough resources. Request is dennied. So the program can be STOPPED if you are willing to do so."<<endl;
Sleep(5);
return;
}
else
{
Available[Request_SOURCE]=Available[Request_SOURCE]-Request_SOURCE_NEMBER;
Allocation[Request_PROCESS][Request_SOURCE]=Allocation[Request_PROCESS][Request_SOURCE]+Request_SOURCE_NEMBER;
Need[Request_PROCESS][Request_SOURCE]=Need[Request_PROCESS][Request_SOURCE]-Request_SOURCE_NEMBER;
cout<<"search Ok! "<<endl;
SafetyTest();
}
}
//main function.
void main(void)
{
InitRead();
cout<<endl;
cout<<MAX_REAL_SOURCE<<'\t';
for(int i=0;i<MAX_REAL_SOURCE;i++)
cout<<" "<<Available[i]<<'\t';
cout<<endl<<MAX_REAL_PROCESS<<endl;
for(i=0;i<MAX_REAL_PROCESS;i++)
{
for(int j=0;j<MAX_REAL_SOURCE;j++)
cout<<" "<<Max[i][j]<<'\t';
cout<<endl;
}
InitAllocated();
for(i=0;i<MAX_REAL_PROCESS;i++)
{
for(int j=0;j<MAX_REAL_SOURCE;j++)
cout<<" "<<Allocation[i][j]<<'\t';
cout<<endl;
}
SetNeed();
for(i=0;i<MAX_REAL_PROCESS;i++)
{
for(int j=0;j<MAX_REAL_SOURCE;j++)
cout<<" "<<Need[i][j]<<'\t';
cout<<endl;
}
char stop = 0;
do{
ReadRequest();
cout<<'\n'<<"Process No."<<Request_PROCESS<<" request the resource "<<Request_SOURCE<<", the number is "<<Request_SOURCE_NEMBER<<" "<<endl;
cout<<'\n'<<"read ok."<<'\n';
HandleFunc();
cout<< " Press q to quit or press any other key to continue"<<endl;
cin>>stop;
}
while( stop !='q' );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -