关于银行家算法的问题

本文探讨了银行家算法中的两个关键疑问:1.为何在进程申请资源时进行安全性检查?2.在ifInSafeMode()函数中为何设置i为-1?通过分析代码和算法原理,解释了这些问题的重要性,对于操作系统学习者具有参考价值。
摘要由CSDN通过智能技术生成

关于银行家算法的问题

在校大学生,刚学习操作系统,在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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值