结构体指针实现的银行家算法C语言版

银行家算法C语言版

结构体 作用
allocation 已分配资源
max 最大需求资源
available 可分配的资源
need 需要的资源
path 进程运行的顺序
finish 满足要求的标志
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

struct allocation {
	int value;
	struct allocation *next;
};
struct max {
	int value;
	struct max *next;
};
struct available {
	int value;
	struct available *next;
};
struct need {
	int value;
	struct need *next;
};
struct path {
	int value;
	struct path *next;
};
struct finish {
	int stat;
	struct finish *next;
};

int main() {
	int row, colum, status = 0, i, j, k, l, t, temp, processtest;
	struct allocation *allochead = (struct allocation *)malloc(sizeof(struct allocation));
	struct allocation *allocation1 = allochead;

	struct max *maxhead = (struct max *)malloc(sizeof(struct max));
	struct max *max1 = maxhead;

	struct available *avahead = (struct available *)malloc(sizeof(struct available));
	struct available *available1 = avahead;
	struct available *workhead = (struct available *)malloc(sizeof(struct available));
	struct available *work1 = workhead;

	struct need *needhead = (struct need *)malloc(sizeof(struct need));
	struct need *need1 = needhead;

	struct finish *finihead = (struct finish *)malloc(sizeof(struct finish));
	struct finish *finish1 = finihead;

	struct path *pathhead = (struct path *)malloc(sizeof(struct path));
	struct path *path1 = pathhead;

	printf("请输入系统资源的种类数:");
	scanf("%d", &colum);
	printf("请输入现在内存中的进程数:");
	scanf("%d", &row);
	printf("请输入已分配资源矩阵:\n");

	for (i = 1; i <= row; i++)
	{
		for (j = 0; j < colum; j++)
		{
			printf("请输入已分配给进程p%d的%c种系统资源:", i, 'A' + j);
			if (j == 0 && i == 1)
			{
				scanf("%d", &allochead->value);
				allochead->next = NULL;
			}
			else
			{
				struct allocation *allocation2 = (struct allocation *)malloc(sizeof(struct allocation));
				scanf("%d", &allocation2->value);
				allochead->next = allocation2;
				allochead = allocation2;
				allochead->next = NULL;
			}
		}
	}

	allochead = allocation1;

	printf("请输入最大需求矩阵:\n");

	for (i = 1; i <= row; i++)
	{
		for (j = 0; j < colum; j++)
		{
			printf("请输入进程p%d种类%c系统资源最大需求:", i, 'A' + j);
			if (j == 0 && i == 1)
			{
				scanf("%d", &maxhead->value);
				maxhead->next = NULL;
			}
			else
			{
				struct max *max2 = (struct max *)malloc(sizeof(struct max));
				scanf("%d", &max2->value);
				maxhead->next = max2;
				maxhead = max2;
				maxhead->next = NULL;
			}
		}
	}

	maxhead = max1;

	printf("请输入系统剩余的资源矩阵:\n");

	for (j = 0; j < colum; j++)
	{
		printf("种类%c的系统资源剩余:", 'A' + j);
		if (j == 0)
		{
			scanf("%d", &avahead->value);
			avahead->next = NULL;
			workhead->value = avahead->value;
			workhead->next = NULL;
		}
		else
		{
			struct available *available2 = (struct available *)malloc(sizeof(struct available));
			struct available *work2 = (struct available *)malloc(sizeof(struct available));
			scanf("%d", &available2->value);
			work2->value = available2->value;
			avahead->next = available2;
			avahead = available2;
			avahead->next = NULL;
			workhead->next = work2;
			workhead = work2;
			workhead->next = NULL;
		}
	}

	avahead = available1;
	workhead = work1;

	struct allocation *alloctemp = allochead;
	struct max *maxtemp = maxhead;
	for (i = 0; i < row; i++)
	{
		for (j = 0; j < colum; j++)
		{
			if (j == 0 && i == 0)
			{
				needhead->value = (maxtemp->value) - (alloctemp->value);
				needhead->next = NULL;
			}
			else
			{
				struct need *need2 = (struct need *)malloc(sizeof(struct need));
				need2->value = (maxtemp->value) - (alloctemp->value);
				needhead->next = need2;
				needhead = need2;
				needhead->next = NULL;
			}
			maxtemp = maxtemp->next;
			alloctemp = alloctemp->next;
		}
	}
	needhead = need1;
	for (i = 0; i < row; i++)
	{
		if (i == 0)
		{
			finihead->stat = 0;
			finihead->next = NULL;
		}
		else
		{
			struct finish *finish2 = (struct finish *)malloc(sizeof(struct finish));
			finish2->stat = 0;
			finihead->next = finish2;
			finihead = finish2;
			finihead->next = NULL;
		}
	}

	finihead = finish1;

	struct available *worktemp = workhead;
	struct need *needtemp = needhead;
	struct finish *finishtemp = finihead;
	finish1 = finishtemp;
	for (t = 0; t < row; t++)
	{
		alloctemp = allochead;
		needtemp = needhead;
		finishtemp = finihead;
		for (i = 1; i <= row; i++)
		{
			l = 0;
			if (finishtemp->stat == 0)
			{
				processtest = 0;
				for (j = 0; j < colum; j++)
				{
					if (needtemp->value <= worktemp->value)
					{
						processtest = processtest + 1;
						if (processtest == colum)
						{
							worktemp = workhead;
							for (k = 0; k < colum; k++)
							{
								worktemp->value = (worktemp->value) + (alloctemp->value);
								worktemp = worktemp->next;
								alloctemp = alloctemp->next;
							}
							if (status == 0)
							{
								pathhead->value = i;
								pathhead->next = NULL;
								status++;
							}
							else
							{
								struct path *path2 = (struct path *)malloc(sizeof(struct path));
								path2->value = i;
								pathhead->next = path2;
								pathhead = path2;
								pathhead->next = NULL;
							}
							needtemp = needtemp->next;
							finishtemp->stat = 1;
						}
						else
						{
							worktemp = worktemp->next;
							needtemp = needtemp->next;
						}
					}
					else
					{
						worktemp = worktemp->next;
						needtemp = needtemp->next;
						for (l; l < colum; l++)
						{
							alloctemp = alloctemp->next;
						}
					}
				}

			}
			finishtemp = finishtemp->next;
			worktemp = workhead;
		}
	}

	finishtemp = finish1;
	pathhead = path1;
	for (temp = 0; temp < row; temp++)
	{
		if (finishtemp->stat == 0)
		{
			printf("\n系统处于非安全状态!\n");
			break;
		}
		else if (temp == row - 1)
		{
			printf("\n系统处于安全状态!\n");
			printf("\n安全序列为:\n");
			while (pathhead)
			{
				printf("p%d", pathhead->value);
				pathhead = pathhead->next;
			}
			printf("\n");
			break;
		}
		finishtemp = finishtemp->next;
	}
	system("pause");
	return 0;
}

你可能感兴趣的:(结构体指针实现的银行家算法C语言版)