样例:
资源类型的个数:3
每种资源类型的实例:10 5 7
进程的个数:5
每个进程的分配与最大需求:
0 1 0 7 5 3
2 0 0 3 2 2
3 0 2 9 0 2
2 1 1 2 2 2
0 0 2 4 3 3
请求进程编号: 1
请求分配的大小: 1 0 2
样例虽然都过了,可能代码有些不足之处,望指正。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
int request_num;//进程请求的编号
int r[10];//请求的资源个数
int capacity[10];//每种资源类型实例的最大数量
int n;//资源类型的个数
int N;//进程数量
int available[10];
int need[10][10];
int finish[10];
int safe_access;//安全算法是否通过
int mirror_available[10];
int mirror_allocation[10][10];
int mirror_need[10][10];
vector<int>list_safe;//安全序列
struct node
{
int all_val[10];
int max_val[10];
int allocation[10];
int id;
} process[10];
bool judge(int finish[])
{
for(int i=0; i<N; i++)
{
if(finish[i]==0)
return false;
}
return true;
}
void Add(int work[],int (*mirror_allocation)[10],int k)
{
for(int j=0; j<n; j++)
{
work[j]+=mirror_allocation[k][j];
}
}
bool isSafe()//安全性检查
{
int work[10];
int flag,safe;
memset(finish,0,sizeof(finish));
for(int i=0; i<n; i++)
{
work[i] = mirror_available[i];
}
int m=N;
int pos=0;
int cnt=0;
while(m--)//最多有m次循环来寻找安全序列,如果m次之后还没有找到安全序列,则返回false
{
//在找安全序列时,像循环扫描调度一样,从0到n-1,再返回0,再从头开始找
safe=0;
for(int i=pos; i<N; i++)//检查N个进程中是否有可以分配的
{
//如果N个进程里面没有一个进程的 Need<work,就返回false
flag=1;
if(!finish[i])//
{
for(int j=0; j<n; j++)
{
if(mirror_need[i][j]>work[j])
{
flag=0;
break;
}
}
if(flag)// need<work,该进程可以分配
{
finish[i]=1;
safe=1;
Add(work,mirror_allocation,i);
}
}
pos++;
if(safe==1)//如果找到一个可以分配的就进行下一次分配
{
list_safe.push_back(i);
safe=0;
cnt++;
if(cnt==N)//安全序列已找到
{
return true;
}
if(pos==N)//如果判断完最后一个进程,就返回第一个进程重新判断
{
pos=0;
break;
}
}
if(pos==N)
{
pos=0;
break;
}
}
}
if(judge(finish))//判断是否finish都等于 1
return true;
else
return false;
}
bool Request()
{
int flag_one = 1;
int flag_two = 1;
for(int i=0; i<n; i++)//判断每个请求是否小于need
{
if(r[i]>need[request_num][i])
{
flag_one = 0;
break;
}
}
if(!flag_one)
return false;
for(int i=0; i<n; i++)//判断每个请求是否小于available
{
if(r[i]>available[i])
{
flag_two = 0;
break;
}
}
if(!flag_two)
return false;
for(int i=0; i<n; i++)
{
mirror_available[i] = available[i];
}
//只定义一个availiable数组
for(int i=0; i<N; i++)
{
for(int j=0; j<n; j++)
{
mirror_allocation[i][j] = process[i].allocation[j];
mirror_need[i][j] = need[i][j];
}
}
for(int i=0; i<n; i++)
{
mirror_allocation[request_num][i]+=r[i];
mirror_need[request_num][i] -= r[i];
mirror_available[i]-=r[i];
}
if(isSafe())//分配之后检查是否安全
{
for(int i=0; i<n; i++)
{
process[request_num].allocation[i]+=r[i];
need[request_num][i]-=r[i];
available[i]-=r[i];
}
return true;
}
else
{
safe_access=0;
return false;
}
}
int main()
{
//输入初始数据
printf("Please enter the number of resource types: ");
scanf("%d",&n);
cout<<endl;
printf("Declare the maximum number of instances of each resource: ");
for(int i=0; i<n; i++)
{
scanf("%d",&capacity[i]);
}
cout<<endl;
printf("Please enter the number of processes: ");
scanf("%d",&N);
cout<<endl;
printf("Enter the allocation of each resource for each process and the maximum requirements for each process: \n");
for(int i=0; i<N; i++)
{
for(int j=0; j<n; j++)
{
scanf("%d",&process[i].allocation[j]);
}
for(int j=0; j<n; j++)
{
scanf("%d",&process[i].max_val[j]);
need[i][j] = process[i].max_val[j]-process[i].allocation[j];
}
}
cout<<endl;
//get the array of available
for(int i=0; i<n; i++)
{
int sum=0;
for(int j=0; j<N; j++)
{
sum+=process[j].allocation[i];
}
available[i] = capacity[i] - sum;
}
char if_continue='Y';
while(if_continue=='Y')
{
if(if_continue!='Y')
break;
//资源请求
list_safe.clear();
safe_access=1;
printf("Please enter the requests: Request ");
scanf("%d",&request_num);
cout<<endl;
printf("Please enter the number of allocation: ");
for(int i=0; i<n; i++)
{
scanf("%d",&r[i]);
}
getchar();
if(Request())
{
printf("The request succeeds!\n");
printf("The security sequence is: ");
vector<int>::iterator it;
for(it = list_safe.begin(); it!=list_safe.end(); it++)
cout<<*it<<' ';
cout<<endl;
}
else
{
if(!safe_access)
{
printf("The security algorithm failed!\n");
}
else
{
printf("The resource is insufficient!\n");
}
}
printf("Whether or not continue(Y/N): ");
if_continue = getchar();
}
return 0;
}
// 3
// 10 5 7
// 5
// 0 1 0 7 5 3
// 2 0 0 3 2 2
// 3 0 2 9 0 2
// 2 1 1 2 2 2
// 0 0 2 4 3 3
// 1
// 1 0 2