银行家算法

被元元抓来当苦力

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define PROCESS_COUNT (5)
#define RESOURCES_TYPE (3)
typedef struct {
	int allocated[RESOURCES_TYPE];
	int max[RESOURCES_TYPE];
	int need[RESOURCES_TYPE];
	int finish;
	int exclude;
}Process;
Process process[PROCESS_COUNT];
int resources[RESOURCES_TYPE];

typedef struct {
	int index;
	int data[RESOURCES_TYPE];
}Event;
Event empty = {
	.index = -1
};
typedef struct stack {
	int top;
	Event array[100];
	void (*push)(struct stack* stack, Event event);
	Event (*pop)(struct stack* stack);
}Stack;
void _push_back_Stack(Stack* stack, Event node) {
	if (stack->top == 100)
		return;
	stack->array[stack->top++] = node;
}

Event _pop_back_Stack(Stack* stack) {
	return stack->top == 0 ? empty : stack->array[--stack->top];
}

Stack* __initializer_stack(Stack* stack) {
	if (!stack)return NULL;
	memset((void*)stack, 0, sizeof(Stack));
	stack->push = _push_back_Stack;
	stack->pop = _pop_back_Stack;
	return stack;
}

Stack* newStack(void) {
	Stack* stack = (Stack*)malloc(sizeof(Stack));
	return !stack ? stack : __initializer_stack(stack);
}


int main() {
	printf("Input resource count");
	for (int i = 0; i < RESOURCES_TYPE; i++) {
		scanf("%d", &resources[i]);
		if (resources[i] < 0) {
			printf("Error data\n");
			exit(-1);
		}
	}
	for (int i = 0; i < PROCESS_COUNT; i++) {
		printf("Input max and allocated for Process%d \n", i);
		for (int j = 0; j < RESOURCES_TYPE; j++) {
			scanf("%d", &process[i].max[j]);
		}
		for (int j = 0; j < RESOURCES_TYPE; j++) {
			scanf("%d", &process[i].allocated[j]);
		}
		for (int j = 0; j < RESOURCES_TYPE; j++) {
			if (process[i].max[j] < 0 || process[i].allocated[j] < 0 || process[i].allocated>process[i].max) {
				printf("Error data\n");
				exit(-1);
			}
			process[i].need[j] = process[i].max[j] - process[i].allocated[j];

		}
		process[i].finish = 0;
	}
	for (int i = 0; i < PROCESS_COUNT; i++) {
		for (int j = 0; j < RESOURCES_TYPE; j++) {
			resources[j] -= process[i].allocated[j];
		}
	}
	Stack* stack = newStack();
	int pos = 0;
	int sequence[PROCESS_COUNT] = { 0 };

	int finish = 0;
	int finishCount = 0;
	while (!finish) {
		int find = 0;
		for (int i = 0; i < PROCESS_COUNT; i++) {
			if (!process[i].finish && !process[i].exclude) {
				int satisfied = 1;
				for (int j = 0; j < RESOURCES_TYPE; j++) {
					if (process[i].need[j] > resources[j]) {
						satisfied = 0;
					}
				}
				if (satisfied) {
					find = 1;
					Event event = {
						.index = i,
					};
					for (int j = 0; j < RESOURCES_TYPE; j++) {
						event.data[j] = process[i].allocated[j];
						resources[j] += process[i].allocated[j];
					}
					sequence[pos++] = i;
					process[i].finish = 1;
					stack->push(stack, event);
					finishCount++;
				}
			}
		}
		if (finishCount == PROCESS_COUNT) {
			finish = 1;
			break;
		}
		if (find) {
			for (int j = 0; j < PROCESS_COUNT; j++) {
				process[j].exclude = 0;
			}
		}
		else {
			Event lastEvent = stack->pop(stack);
			if (lastEvent.index == -1) {
				printf("No security sequence\n");
				exit(1);
			}
			pos--;
			process[lastEvent.index].finish = 0;
			process[lastEvent.index].exclude = 1;
			for (int j = 0; j < RESOURCES_TYPE; j++) {
				resources[j] -= lastEvent.data[j];
			}
			finishCount--;
		}
	}
	if (pos == PROCESS_COUNT) {
		for (int i = 0; i < pos; i++) {
			printf("%d ", sequence[i]);
		}
		printf("\n");
	}
	else {
		printf("No security sequence\n");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值