操作系统::银行家算法

一、银行家算法


二、用代码实现

//.h文件
#ifndef _SYS_H_
#define _SYS_H_
#define ERROR -1
#define SUCCESS 0

int init(int n_SRC,int m_PRO);
void SetVal(int *availiable,int **max,int **allocation);
int LegitimacyCheck(int PRO,int* Request);
void CopyToWork();
int PreAllocation(int PRO,int* Request);
int SafeContest();
void ReBack(int PRO,int* Request);
void DisplaySafeSeq();
void DisplayState(int process);
void RealAllocation(int process);

#endif


//.cpp(主函数)
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include "sys.h"

int num_SRC = 0;
int num_PRO = 0;
int *Availiable = NULL;
int *work = NULL;
int *finish = NULL;
int **Max = NULL;
int **Allocation = NULL;
int **Need = NULL;

int init(int n_SRC,int m_PRO)
{
	int i = 0;
	num_PRO = m_PRO;
	num_SRC = n_SRC;
	/**/
	Availiable = (int*)malloc(sizeof(int)*num_SRC);
	memset(Availiable,0,sizeof(int)*num_SRC);
	work = (int*)malloc(sizeof(int)*num_SRC);
	memset(work,0,sizeof(int)*num_SRC);
	finish = (int*)malloc(sizeof(int)*num_PRO);
	memset(finish,0,sizeof(int)*num_PRO);
	/**/
	Max = (int**)malloc(sizeof(int*)*num_PRO);
	for(i=0;i<num_PRO;i++)
	{
		Max[i] = (int*)malloc(sizeof(int)*num_SRC);
		memset(Max[i],0,sizeof(int)*num_SRC);
	}
	/**/
	Allocation = (int**)malloc(sizeof(int)*num_PRO);
	for(i=0;i<num_PRO;i++)
	{
		Allocation[i] = (int*)malloc(sizeof(int)*num_SRC);
		memset(Allocation[i],0,sizeof(int)*num_SRC);
	}
	/**/
	Need = (int**)malloc(sizeof(int)*num_PRO);
	for(i=0;i<num_PRO;i++)
	{
		Need[i] = (int*)malloc(sizeof(int)*num_SRC);
		memset(Need[i],0,sizeof(int)*num_SRC);
	}

	return SUCCESS;
}

void SetVal(int *availiable,int **max,int **allocation)
{
	int i,j;
	memcpy(Availiable,availiable,sizeof(int)*num_SRC);
	for(i=0;i<num_PRO;i++)
	{
		memcpy(Max[i],max[i],sizeof(int)*num_SRC);
	}
	for(i=0;i<num_PRO;i++)
	{
		memcpy(Allocation[i],allocation[i],sizeof(int)*num_SRC);
	}
	for(i=0;i<num_PRO;i++)
	{
		for(j=0;j<num_SRC;j++)
		{
			Need[i][j] = Max[i][j] - Allocation[i][j];
		}
	}
}

int LegitimacyCheck(int PRO,int* Request)
{
	int i = 0;
	for(i=0;i<num_SRC;i++)
		if(Request[i] > Availiable[i] || Request[i] > Need[PRO][i])
			return ERROR;
	return SUCCESS;
}

void CopyToWork()
{
	memcpy(work,Availiable,sizeof(int)*num_SRC);
	memset(finish,0,sizeof(int)*num_PRO);
}

int PreAllocation(int PRO,int* Request)
{
	int i = 0;
	/*执行预分配*/
	for (i=0;i<num_SRC;i++)
	{
		Availiable[i] -= Request[i];
		Allocation[PRO][i] += Request[i];
		Need[PRO][i] -= Request[i];
	}
	return SUCCESS;
}

void RealAllocation(int process)
{
	int i = 0;
	int flag = 0;
	/*执行预分配*/
	for (i=0;i<num_SRC;i++)
	{
		if(Need[process][i] != 0)
			flag++;
	}
	if(flag == 0)
	{
		for (i=0;i<num_SRC;i++)
		{
			Availiable[i] += Allocation[process][i];
			Allocation[process][i] = 0;
			Need[process][i] = 0;
		}
	}
}

int SafeContest()
{
	int process = 0;
	int src = 0;
	int count = 1;
	CopyToWork();
	while (count <= num_PRO)
	{
		int flag = 0;//计数器,用来记录某个进程不满足条件的资源数目,如果为0则说明全部满足条件
		for (process=0;process<num_PRO;process++)
		{
			flag = 0;
			if(finish[process] != 0)
				continue;
			for (src=0;src<num_SRC;src++)
			{
				if (Need[process][src] > work[src])
					flag++;
			}
			if(flag == 0)//当前进程满足finish = FALSE 且 对所有资源的需求量小于可分配的量
				break;   //此时退出循环,选定当前进程
		}
		if(flag != 0)//如果没有进程满足条件,则状态不安全
			break;
		else
		{
			for(src=0;src<num_SRC;src++)//如果有满足条件的进程,则进行试分配
			{
				work[src] += Allocation[process][src];
			}
			finish[process] = count;
			DisplayState(process);
			count++;
		}
	}
	if(count == num_PRO+1)
	{
		printf("当前状态安全,可分配,一个可行的安全序列为:\n");
		return SUCCESS;
	}
	else
	{
		printf("当前状态不安全,不可分配\n");
		return ERROR;
	}
}

void ReBack(int PRO,int* Request)
{
	int i = 0;
	/*执行回收*/
	for (i=0;i<num_SRC;i++)
	{
		Availiable[i] += Request[i];
		Allocation[PRO][i] -= Request[i];
		Need[PRO][i] += Request[i];
	}
}

void DisplaySafeSeq()
{
	int i = 0;
	int count = 1;
	while (count<=num_PRO)
	{
		for(i=0;i<num_PRO;i++)
			if(finish[i] == count)
				printf("%d ",i);
		count++;
	}
	printf("\n");
}

void DisplayState(int process)
{
	int i,j;
	printf("\n");
	if (process != -1)
		printf("第%d号进程执行完后状态为:\n",process);
	printf("尚需数组:\n   ");
	for(i=0;i<num_SRC;i++)
	{
		printf("%2d ",i);
	}
	printf("\n");
	for(i=0;i<num_PRO;i++)
	{
		printf("%d  ",i);
		if(finish[i] != 0 && process != -1)
			for(j=0;j<num_SRC;j++)
				printf("%d  ",j);
		else
			for(j=0;j<num_SRC;j++)
				printf("%2d ",Need[i][j]);
		printf("\n");
	}
	printf("已分配资源数组:\n   ");
	for(i=0;i<num_SRC;i++)
	{
		printf("%2d ",i);
	}
	printf("\n");
	for(i=0;i<num_PRO;i++)
	{
		printf("%d  ",i);
		if(finish[i] != 0 && process != -1)
			for(j=0;j<num_SRC;j++)
				printf(" %d ",j);
		else
				printf("%2d ",Allocation[i][j]);
		printf("\n");
	}
	printf("可用资源数组:");
	for(i=0;i<num_SRC;i++)
		if (process == -1)
			printf("%d  ",Availiable[i]);
		else
			printf("%d  ",work[i]);
	printf("\n");
}


//main.cpp
#include "sys.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int numPro = 0;
int numSrc = 0;
void struInit(int *availiable,int **max,int **allocation);

int main()
{
	int i,*availiable,**max,**allocation;
	printf("请输入进程数量:");
	scanf("%d",&numPro);
	printf("请输入资源数量:");
	scanf("%d",&numSrc);
	availiable = (int*)malloc(sizeof(int)*numSrc);
	max = (int**)malloc(sizeof(int*)*numPro);
	allocation = (int**)malloc(sizeof(int)*numPro);
	init(numSrc,numPro);
	struInit(availiable,max,allocation);
	SetVal(availiable,max,allocation);
	system("cls");
	while (1)
	{
		int cmdNum = 0;
		printf("\n\n\n");
		printf("*******************************************\n");
		printf("*           1:检查当前状态安全性          *\n");
		printf("*           2:执行请求                    *\n");
		printf("*           3:打印当前状态                *\n");
		printf("*           4:退出                        *\n");
		printf("*******************************************\n");
		printf("请输入功能选项:");
		scanf("%d",&cmdNum);
		switch(cmdNum)
		{
		case 1:
			{
				if(SafeContest() == SUCCESS)
					DisplaySafeSeq();
			}break;
		case 2:
			{
				int *Request = (int*)malloc(sizeof(int)*numSrc);
				int proNum = 0;
				printf("输入请求资源的进程号:");
				scanf("%d",&proNum);
				printf("请输入请求序列:\n");
				for (i=0;i<numSrc;i++)
				{
					scanf("%d",&Request[i]);
				}
				if(LegitimacyCheck(proNum,Request) == ERROR)
				{
					printf("请求序列不合法\n");
					continue;
				}
				else
				{
					PreAllocation(proNum,Request);
					if(SafeContest() == ERROR)
					{
						ReBack(proNum,Request);
					}
					else
					{
						RealAllocation(proNum);
						DisplaySafeSeq();
					}
				}
			}break;
		case 3:DisplayState(-1);break;
		case 4:exit(0);break;
		default:continue;
		}
	}
	return 0;
}

void struInit(int *availiable,int **max,int **allocation)
{
	int i,j;
	for(i=0;i<numPro;i++)
	{
		max[i] = (int*)malloc(sizeof(int)*numSrc);
		memset(max[i],0,sizeof(int)*numSrc);
	}
	for(i=0;i<numPro;i++)
	{
		allocation[i] = (int*)malloc(sizeof(int)*numSrc);
		memset(allocation[i],0,sizeof(int)*numSrc);
	}
	printf("请输入各种资源的剩余数量\n");
	for (i=0;i<numSrc;i++)
	{
		scanf("%d",&availiable[i]);
	}
	for (i=0;i<numPro;i++)
	{
		printf("请输入第%d号进程对各种资源的最大需求量:\n",i);
		for (j=0;j<numSrc;j++)
		{
			scanf("%d",&max[i][j]);
		}
	}
	for (i=0;i<numPro;i++)
	{
		printf("请输入第%d号进程各种资源的已分配量:\n",i);
		for (j=0;j<numSrc;j++)
		{
			scanf("%d",&allocation[i][j]);
		}
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
银行家算法是一种用于解决多个进程对多种资源的竞争和分配的法,它可以避免死锁和资源浪费的情况。下面是一个使用C++实现银行家算法的示例: ```cpp #include <iostream> using namespace std; const int MAX_PROCESS = 10; const int MAX_RESOURCE = 10; int main() { int available[MAX_RESOURCE]; int maxDemand[MAX_PROCESS][MAX_RESOURCE]; int allocation[MAX_PROCESS][MAX_RESOURCE]; int need[MAX_PROCESS][MAX_RESOURCE]; int work[MAX_RESOURCE]; bool finish[MAX_PROCESS]; int numProcesses, numResources; // 输入进程数和资源数 cout << "Enter the number of processes: "; cin >> numProcesses; cout << "Enter the number of resources: "; cin >> numResources; // 输入可用资源数 cout << "Enter the number of available resources: "; for (int i = 0; i < numResources; i++) { cin >> available[i]; } // 输入每个进程的最大需求量 cout << "Enter the maximum demand of each process: " << endl; for (int i = 0; i < numProcesses; i++) { cout << "Process " << i << ": "; for (int j = 0; j < numResources; j++) { cin >> maxDemand[i][j]; } } // 输入每个进程已分配的资源量 cout << "Enter the allocated resources of each process: " << endl; for (int i = 0; i < numProcesses; i++) { cout << "Process " << i << ": "; for (int j = 0; j < numResources; j++) { cin >> allocation[i][j]; need[i][j] = maxDemand[i][j] - allocation[i][j]; } finish[i] = false; } // 初始化work向量 for (int i = 0; i < numResources; i++) { work[i] = available[i]; } // 找到一个安全序列 int count = 0; int safeSequence[MAX_PROCESS]; while (count < numProcesses) { bool found = false; for (int i = 0; i < numProcesses; i++) { if (!finish[i]) { bool canAllocate = true; for (int j = 0; j < numResources; j++) { if (need[i][j] > work[j]) { canAllocate = false; break; } } if (canAllocate) { for (int j = 0; j < numResources; j++) { work[j] += allocation[i][j]; } safeSequence[count++] = i; finish[i] = true; found = true; } } } if (!found) { break; } } // 输出安全序列 if (count == numProcesses) { cout << "Safe sequence: "; for (int i = 0; i < numProcesses; i++) { cout << safeSequence[i] << " "; } cout << endl; } else { cout << "No safe sequence found." << endl; } return 0; } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值