银行家算法 C语言实现

关于银行家算法就不赘述了,首先是结果截图:
请添加图片描述在这里插入图片描述

下面是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define TRUE 1
#define FALSE 0

int N_resource, N_process;	// 资源数和进程数
// 银行家算法用到的数据结构
int *Avaliable = NULL;
int **Max = NULL;
int **Allocation = NULL;
int **Need = NULL;
int requestID = 0;	// 申请资源的进程ID
int *Request = NULL;

// 输入向量和矩阵
void input(FILE *fp) {
	char buf[100];
	// 输入N_resource and N_process
	fgets(buf, 100, fp);
	N_resource = atoi(strtok(buf, " "));
	N_process = atoi(strtok(NULL, " "));
	printf("%d resource %d process\n", N_resource, N_process);
	// 输入Avaliable向量
	Avaliable = (int *)malloc(sizeof(int) * N_resource);
	fgets(buf, 100, fp);
	Avaliable[0] = atoi(strtok(buf, " "));
	for (int i = 1; i < N_resource; i++) {
		Avaliable[i] = atoi(strtok(NULL, " "));
	}
	// 输入Max矩阵
	Max = (int **)malloc(sizeof(int *) * N_process);
	fgets(buf, 100, fp);	// 跳过空行
	for (int i = 0; i < N_process; i++) {
		Max[i] = (int *)malloc(sizeof(int) * N_resource);
		
		fgets(buf, 100, fp);
		Max[i][0] = atoi(strtok(buf, " "));
		for (int j = 1; j < N_resource; j++) {
			Max[i][j] = atoi(strtok(NULL, " "));
		}
	}
	// 输入Allocation矩阵
	Allocation = (int **)malloc(sizeof(int *) * N_process);
	fgets(buf, 100, fp);	// 跳过空行
	for (int i = 0; i < N_process; i++) {
		Allocation[i] = (int *)malloc(sizeof(int) * N_resource);
	
		fgets(buf, 100, fp);
		Allocation[i][0] = atoi(strtok(buf, " "));
		for (int j = 1; j < N_resource; j++) {
			Allocation[i][j] = atoi(strtok(NULL, " "));
		}
	}
	// 初始化Need矩阵
	Need = (int **)malloc(sizeof(int *) * N_process);
	for (int i = 0; i < N_process; i++) {
		Need[i] = (int *)malloc(sizeof(int) * N_resource);
		for (int j = 0; j < N_resource; j++)
			Need[i][j] = Max[i][j] - Allocation[i][j];
	}
	// 输入requestID
	fgets(buf, 100, fp);	// 跳过空行
	fgets(buf, 100, fp);
	requestID = atoi(buf);
	// 输入Request向量
	Request = (int *)malloc(sizeof(int) * N_resource);
	fgets(buf, 100, fp);
	Request[0] = atoi(strtok(buf, " "));
	for (int i = 1; i < N_resource; i++) {
		Request[i] = atoi(strtok(NULL, " "));
	}
}

// 打印当前状态
void print_state() {
	printf("Avaliable: ");
	for (int i = 0; i < N_resource; i++)
		printf("%d ", Avaliable[i]);
	printf("\n");
	printf("processId\tMax\t\tAllocation\tNeed\n");
	for (int i = 0; i < N_process; i++) {
		printf("    %d\t\t", i);
		for (int j = 0; j < N_resource; j++)
			printf("%d ", Max[i][j]);
		printf("\t");
		for (int j = 0; j < N_resource; j++)
			printf("%d ", Allocation[i][j]);
		printf("\t");
		for (int j = 0; j < N_resource; j++)
			printf("%d ", Need[i][j]);
		printf("\n");
	}
}

// 判断是否所有的arrayA[i] <= arrayB[i]
int ALessthanB(int *arrayA, int *arrayB, int N) {
	for (int i = 0; i < N; i++)
		if (arrayA[i] > arrayB[i])
			return FALSE;
	return TRUE;
}

// 判断当前是否是安全状态
int isSafety() {
	int *Work = malloc(sizeof(int) * N_resource);
	int *Finish = malloc(sizeof(int) * N_process);
	for (int i = 0; i < N_resource; i++)
		Work[i] = Avaliable[i];
	for (int i = 0; i < N_process; i++)
		Finish[i] = FALSE;
	
	printf("The allocation sequence is:\n");
	printf("allocate to\tNow Avaliable\n");
	for (int i = 0; i < N_process; i++) {
		if (Finish[i] == TRUE)
			continue;
		int findit = FALSE;
		for (int j = 0; j < N_resource; j++) {
			// 对于该进程i,判断是否所有资源Need[i][j] <= Work[j]
			if (ALessthanB(Need[i], Work, N_resource)) {
				findit = TRUE;
				break;
			}
		}
		if (findit) {
			for (int j = 0; j < N_resource; j++)
				Work[j] += Allocation[i][j];
			Finish[i] = TRUE;
			// 打印安全序列的判断路径
			printf("process %d\t", i);
			for (int j = 0; j < N_resource; j++)
				printf("%d ", Work[j]);
			printf("\n");
			// 重新搜索
			i = -1;
		}
	}
	free(Work);
	// 判断是否所有的Finish[i]都是TRUE
	for (int i = 0; i < N_process; i++) {
		if (Finish[i] == FALSE) {
			free(Finish);
			return FALSE;
		}
	}
	free(Finish);
	return TRUE;
}

// int ID,是申请资源的进程ID
// 判断是否允许该进程的申请
int allowRequest(int ID) {
	// print the Request array
	printf("RequestID: %d\tRequest: ", requestID);
	for (int i = 0; i < N_resource; i++)
		printf("%d ", Request[i]);
	printf("\n");
	
	if (!ALessthanB(Request, Need[ID], N_resource)) {
		printf("Error: Request <= Need\n");
		return FALSE;
	}
	if (!ALessthanB(Request, Avaliable, N_resource)) {
		printf("Error: Request <= Avaliable\n");
		return FALSE;
	}
	
	// 尝试分配申请的资源
	for (int i = 0; i < N_resource; i++) {
		Avaliable[i] -= Request[i];
		Allocation[ID][i] += Request[i];
		Need[ID][i] -= Request[i];
	}
	printf("now try to allocate the Requested resource\n");
	print_state();
	
	if (isSafety())
		return TRUE;
	else {
		// 不能允许该申请,恢复原先的数据结构
		for (int i = 0; i < N_resource; i++) {
			Avaliable[i] += Request[i];
			Allocation[ID][i] -= Request[i];
			Need[ID][i] += Request[i];
		}
		return FALSE;
	}
}

/*
 * 测试文件的格式为:
 * 资源数 进程数
 * Avaliable向量
 * 
 * Max矩阵
 * 
 * Allocation矩阵
 * 
 * 申请资源的进程ID
 * Request向量
 */
int main(int argc, char *argv[]) {
	FILE *fp = NULL;
	if ((fp = fopen(argv[1], "r")) == NULL) {
		printf("%s: No such file or directory\n", argv[1]);
		return 0;
	}
	input(fp);
	printf("The inisialized state is:\n");
	print_state();
	
	printf("Judge whether the inisialized state is safety:\n");
	if (isSafety())
		printf("The inisialized state is safety\n\n");
	else
		printf("The inisialized state is unsafety\n\n");
	
	printf("Judge should we allocate resource to process%d\n", requestID);
	if (allowRequest(requestID))
		printf("This request is safety\n");
	else
		printf("This request is unsafety\n");
	
	// 释放内存
	for (int i = 0; i < N_process; i++) {
		free(Max[i]);	free(Allocation[i]);	free(Need[i]);
	}
	free(Avaliable);	free(Max);
	free(Allocation);	free(Need);
	return 0;
}

测试用例是:

4 5
1 6 2 2

0 0 4 4
2 7 5 0
3 6 10 10
0 9 8 4
0 6 6 10

0 0 3 2
1 0 0 0
1 3 5 4
0 3 3 2
0 0 1 4

2
1 3 1 0
  • 5
    点赞
  • 83
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值