被元元抓来当苦力
#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");
}
}