关于银行家算法的问题
在校大学生,刚学习操作系统,在github上面参考了银行家算法的代码,有两个问题不是很了解,问题的出现都是在进程的安全性判断上.
-
问题:
-
1.为什么要在申请一个进程的时候做一遍安全性检查?不能进程申请完了再做安全性检查吗?安全性不是相对于整个进程序列来说的吗??
-
2.在ifInSafeMode()中为什么要对i赋值为-1呢?
代码如下:
/*******************************************************************
//This is a sample of banker's algorithm for OS deadlock prevention
//Built by Ling He
// 11/11/2015
//Execute the program in command line like "test.exe 5 5 5", the follow numbers are used
//to initialize the resource vector
//customer0 function will randomly request and release resources
//Now pthread library is used to run customer.
//For synchronization, I used a mutex lock for request and release method and printf.
*******************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define resourceTypeQuan 3
#define processQuan 5
int i = 0;
int j = 0;
pthread_mutex_t mutex;//实现资源申请,资源释放的互斥量
int initResourceVector [resourceTypeQuan];
//总计资源数/已分配资源数/最大资源数/还需要的资源数
int availResourceVector [resourceTypeQuan];
int allocMatrix [processQuan][resourceTypeQuan] = {
{
0,1,0},{
2,0,0},{
3,0,2},{
2,1,1},{
0,0,2}};
int maxMatrix [processQuan][resourceTypeQuan] = {
{
7,5,3},{
3,2,2},{
9,0,2},{
2,2,2},{
4,3,3}};
int needMatrix [processQuan][resourceTypeQuan];
int requestResource(int processID,int requestVector[]);
int releaseResource(int processID,int releaseVector[]);
int ifGreaterThanNeed(int processID,int requestVector[]);
int ifEnoughToRelease(int processID,int releaseVector[]);
int ifInSafeMode();
int ifEnoughToAlloc();
void printNeedMatrix();
void printAllocMatrix();
void printAvailable();
void printReqOrRelVector(int vec[]);
void *customer(void* customerID);
int main(int argc, char const *argv[])
{
if(argc != resourceTypeQuan + 1)
{
printf("Quantity of parameter is not correct.\n");
return -1;
}
for(i = 0; i < resourceTypeQuan; i++)
{
initResourceVector[i] = atoi(argv[i+1]);//argv[0] is name of program
availResourceVector[i] = initReso