?? yyyy.txt
字號:
/* Note:Your choice is C IDE */
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include "windows.h"
#define MAX_PROCESS 32 //最大進程數
#define MAX_COURCE 64 //最大資源類別
int MAX_FACT_PROCESS; //實際總進程數
int MAX_FACT_COURCE; //實際資源類別數
int Available[MAX_COURCE]; //可利用資源向量
int Max[MAX_PROCESS][MAX_COURCE]; //最大需求矩陣
int Allocation[MAX_PROCESS][MAX_COURCE]; //分配矩陣
int Need[MAX_PROCESS][MAX_COURCE]; //需求矩陣
int Request_PROCESS; //發出請求的進程
int Request_COURCE; //被請求資源類別
int Request_COURCE_NEMBER; //請求資源數
struct COMP{
int value;
int num;
int next;
};
int flag=0;
void Read_Initiate(void){ //讀入初始化文檔
ifstream infile("Initiate.txt");
if(!infile){
cout<<"不能打開輸入文件:"<<"Initiate.txt"<<'\n';
exit(1);
}
cout<<"開始讀入初始化文檔"<<'\n';
int ch;
int Array[MAX_PROCESS*MAX_COURCE*2];
int num=0;
while(infile>>ch)
Array[num++]=ch;
num=0;
MAX_FACT_COURCE=Array[num++];
for(int j=0;j<MAX_FACT_COURCE;j++)
Available[j]=Array[num++];
MAX_FACT_PROCESS=Array[num++];
for(int i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
Max[i][j]=Array[num++];
}
infile.close();
}
void Write_Initiate(void){ //寫入初始化文檔(分配資源
ofstream outfile("Initiate.txt");
if(!outfile){
cout<<"不能打開初始化文檔:"<<'\n';
exit(1);
}
int Array[MAX_PROCESS*MAX_COURCE*2];
int num=0;
Array[num++]=MAX_FACT_COURCE;
for(int i=0;i<MAX_FACT_COURCE;i++)
Array[num++]=Available[i];
Array[num++]=MAX_FACT_PROCESS;
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Array[num++]=Max[i][j];
num=0;
outfile<<Array[num++]<<" ";
for(i=0;i<MAX_FACT_COURCE;i++)
outfile<<Array[num++]<<" ";
outfile<<'\n'<<Array[num++]<<endl;
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
outfile<<Array[num++]<<" ";
outfile<<endl;
}
DWORD m_delay=3000;
Sleep(m_delay);
outfile.close();
cout<<"修改初始化文檔成功!"<<endl;
}
void Allocated_list(void){ //讀入已分配資源列表
ifstream infile("Allocated_list.txt");
if(!infile){
cout<<"不能打開輸入文件:"<<"Allocated_list.txt"<<'\n';
exit(1);
}
cout<<"開始讀入已分配資源列表"<<'\n';
int ch,num=0;
int Array[MAX_PROCESS*MAX_COURCE];
while(infile>>ch)
Array[num++]=ch;
num=0;
for(int i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Allocation[i][j]=Array[num++];
infile.close();
}
void Set_Need(void){ //設置需求矩陣
cout<<"設置需求矩陣"<<'\n';
for(int i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Need[i][j]=Max[i][j]-Allocation[i][j];
}
void Read_Request(void){ //讀入請求向量
ifstream infile("Request_list.txt");
if(!infile){
cout<<"不能打開輸入文件:"<<"Request_list.txt"<<'\n';
exit(1);
}
cout<<"開始讀入請求向量"<<'\n';
int Array[3];
int num=0,ch;
while(infile>>ch)
Array[num++]=ch;
Request_PROCESS=Array[0];
Request_COURCE=Array[1];
Request_COURCE_NEMBER=Array[2];
infile.close();
}
void Write_Allocation(void){ //修改資源分配列表(資源分配)
ofstream outfile("Allocated_list.txt");
if(!outfile){
cout<<"不能打開資源分配列表:"<<'\n';
exit(1);
}
for(int i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
outfile<<Allocation[i][j]<<" ";
outfile<<endl;
}
DWORD m_delay=3000;
Sleep(m_delay);
cout<<"修改資源分配列表成功!"<<endl;
outfile.close();
}
void Allocate_Source(void){ //開始分配(已通過掃描和安全性檢測)
cout<<'\n'<<"開始給第"<<Request_PROCESS<<"個進程分配第"<<Request_COURCE
<<"類資源"<<Request_COURCE_NEMBER<<"個"<<endl;
Write_Initiate();
Write_Allocation();
DWORD m_delay=3000;
Sleep(m_delay);
cout<<'\n'<<"祝賀您,資源分配已成功!"<<endl;
}
void Test_Safty(){ //安全性檢測
cout<<'\n'<<"進入安全性檢測!"<<endl;
int Work[MAX_COURCE];
for(int i=0;i<MAX_FACT_COURCE;i++){
Work[i]=Available[i];
}
bool Finish[MAX_PROCESS][MAX_COURCE];
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=0;j<MAX_FACT_COURCE;j++)
Finish[i][j]=false;
COMP Array[32];
for(i=0;i<MAX_FACT_PROCESS;i++)
{
Array[i].value=Need[i][Request_COURCE-1];
Array[i].num=i;
}
for(i=0;i<MAX_FACT_PROCESS;i++)
for(int j=i+1;j<MAX_FACT_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;
}
DWORD m_delay=3000;
Sleep(m_delay);
/*for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Need[i][j]<<'\t';
cout<<endl;
}
*/
if(Finish[Request_PROCESS-1][Request_COURCE-1]==false&&Need[Request_PROCESS-1][Request_COURCE-1]<=Work[Request_COURCE-1])
{
Work[Request_COURCE-1]=Work[Request_COURCE-1]+Allocation[Request_PROCESS-1][Request_COURCE-1];
Finish[Request_PROCESS-1][Request_COURCE-1]=true;
}
else
{
cout<<"未通過安全性測試,不與以分配"<<endl;
exit(0);
}
for(i=0;i<MAX_FACT_PROCESS;i++){
if(Array[i].num==Request_PROCESS-1)
continue;
if(Array[i].num!=Request_PROCESS-1&&Finish[Array[i].num][Request_COURCE-1]==false&&Need[Array[i].num][Request_COURCE-1]<=Work[Request_COURCE-1]){
Work[Request_COURCE-1]=Work[Request_COURCE-1]+Allocation[Array[i].num][Request_COURCE-1];
Finish[Array[i].num][Request_COURCE-1]=true;
}
}
for(i=0;i<MAX_FACT_PROCESS;i++)
{
if(Finish[i][Request_COURCE-1]==true)
continue;
else
{
cout<<"未通過安全性測試,不與以分配"<<endl;
exit(0);
}
}
cout<<'\n'<<"找到一個安全序列:"<<"P"<<Request_PROCESS<<"--->";
for(i=0;i<MAX_FACT_PROCESS;i++){
if(Array[i].num==Request_PROCESS)
continue;
else
cout<<"P"<<Array[i].num<<"--->";
}
cout<<'\n'<<"已通過安全性測試!"<<endl;
Allocate_Source();
}
void RUN(void){ //執行銀行家算法
cout<<"*************************************************"<<'\n'<<"點擊1執行!"
<<'\n'<<"點擊2退出!"
<<'\n'<<"*************************************************"<<endl;
cin>>flag;
if(flag==2)
exit(0);
if(flag==1)
{
cout<<"開始掃描請求信息!"<<endl;
DWORD m_delay=3000;
Sleep(m_delay);
if(Request_COURCE_NEMBER>Need[Request_PROCESS-1][Request_COURCE-1])
{
cout<<'\n'<<"第"<<Request_PROCESS<<"個進程請求第"<<Request_COURCE<<"類資源"<<Request_COURCE_NEMBER<<"個"<<endl;
cout<<"可是已超出該進程尚需的該類資源的最大數量,所以不予以分配!!"<<endl;
exit(0);
}
if(Request_COURCE_NEMBER>Available[Request_COURCE-1])
{
cout<<'\n'<<"第"<<Request_PROCESS<<"個進程請求第"<<Request_COURCE<<"類資源"<<Request_COURCE_NEMBER<<"個"<<endl;
cout<<"可是系統中尚無足夠的資源,所以進入等待隊列!!"<<endl;
exit(0);
}
else{
Available[Request_COURCE-1]=Available[Request_COURCE-1]-Request_COURCE_NEMBER;
Allocation[Request_PROCESS-1][Request_COURCE-1]=Allocation[Request_PROCESS-1][Request_COURCE-1]+Request_COURCE_NEMBER;
Need[Request_PROCESS-1][Request_COURCE-1]=Need[Request_PROCESS-1][Request_COURCE-1]-Request_COURCE_NEMBER;
cout<<"掃描通過"<<endl;
Sleep(m_delay);
Test_Safty();
}
}
else {
cout<<"輸入錯誤,請重新輸入!"<<'\n';
RUN();
}
}
void main(void){
Read_Initiate();
cout<<MAX_FACT_COURCE<<'\t';
for(int i=0;i<MAX_FACT_COURCE;i++)
cout<<Available[i]<<'\t';
cout<<endl<<MAX_FACT_PROCESS<<endl;
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Max[i][j]<<'\t';
cout<<endl;
}
DWORD m_delay=3000;
Sleep(m_delay);
cout<<"讀入成功"<<'\n';
Allocated_list();
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Allocation[i][j]<<'\t';
cout<<endl;
}
Sleep(m_delay);
cout<<"讀入成功"<<'\n';
Set_Need();
for(i=0;i<MAX_FACT_PROCESS;i++){
for(int j=0;j<MAX_FACT_COURCE;j++)
cout<<Need[i][j]<<'\t';
cout<<endl;
}
Sleep(m_delay);
cout<<"設置成功"<<'\n';
Read_Request();
cout<<'\n'<<"第"<<Request_PROCESS<<"個進程請求第"<<Request_COURCE<<"類資源"<<Request_COURCE_NEMBER<<"個"<<endl;
cout<<'\n'<<"讀入成功"<<'\n';
RUN();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -