c语言实现银行家算法避免死锁,C实现银行家算法(避免死锁)

#include

#include

#include

#define SourceType 3

#define ProcessNum 5

//第SourceNum类可用资源数:

typedefstructAvailable{

int Available[SourceType];

}Ava;

//进程ProcessNum对资源SourceNum类资源的最大需求:

typedefstructMax{

int Max[ProcessNum][SourceType];

}Max;

//进程ProcessNum已占有资源SourceNum类资源的数量:

typedefstructAllocation{

int Allocation[ProcessNum][SourceType];

}Alloc;

//进程ProcessNum还需要多少SourceNum资源:

typedefstructNeed{

int Need[ProcessNum][SourceType];

}Need;

int check(Ava * ava,Need * need,Alloc * allo){

//初始化Finish:

int Finish[ProcessNum];

for (int i=0; iFinish[i] = 0;

}

//初始化Work:

int Work[SourceType];

for (int j=0; jWork[j] = ava->Available[j];

}

//生成p数组(即安全序列):

int p[ProcessNum];

for (int pp = 0; ppp[pp] = 0;

}

//复制need:

Need * needCp = (Need*)malloc(sizeof(Need));

for (int i=0; ifor (int j=0; jneedCp->Need[i][j] = need->Need[i][j];

}

}

for (int ii=0; iiint outCheckSum = 0;

//从0号进程开始查找:

for (int i=0; iint checkSum = 0;

for (int j=0; jif (needCp->Need[i][j] <= Work[j]) {

checkSum+=1;

}

}

if (checkSum == SourceType) {

printf("work = ");

for (int j=0; j//使这个进程的need不会再被遍历:

needCp->Need[i][j] += 10;

printf(" %d ",Work[j]);

Work[j] = Work[j] + allo->Allocation[i][j];

Finish[i] = 1;

p[ii]=i;

}

//跳出循环:

i=ProcessNum;

}

outCheckSum = checkSum;

}

if (outCheckSum < SourceType) {

free(needCp);

return 0;

}

printf("p[ii] = %d \n",p[ii]);

}

free(needCp);

return 1;

}

int main(int argc, const char * argv[]) {

/************************** 初始化 ****************************/

/*已知有3个并发进程共享10个系统资源(因此SourceNum=1,ProcessNum=3)*/

//初始化系统资源数Available:

printf("输入系统原有资源数:\n");

Ava * ava = (Ava*)malloc(sizeof(Ava));

for (int i=0; iscanf("%d",&ava->Available[i]);

}

//Allocation数组存放各进程申请的各SourceType类的资源数:

Alloc * allo = (Alloc*)malloc(sizeof(Alloc));

int sum[SourceType];

for (int i=0; i1) {

sum[i] = 0;

}

printf("输入各进程动态申请资源数组:\n");

for (int i=0; iprintf("进程 %d : \n",i);

for (int j=0; jscanf("%d",&allo->Allocation[i][j]);

sum[j] = sum[j] + allo->Allocation[i][j];

}

}

//初始化Max数组,并生成Need数组:

Max * max = (Max*)malloc(sizeof(Max));

Need * need = (Need*)malloc(sizeof(Need));

printf("输入各进程对于各资源的最大需求:\n");

for (int i=0; iprintf("进程 %d : \n",i);

for (int j=0; jscanf("%d",&max->Max[i][j]);

if (max->Max[i][j] > ava->Available[j]) {

printf("申请最大资源数大于系统现有资源数!");

return -1;

}else{

need->Need[i][j] = max->Max[i][j] - allo->Allocation[i][j];

}

}

}

printf("计算出现有的SourceType类的资源数:\n");

for (int i=0; iava->Available[i] = ava->Available[i] - sum[i];

printf(" %d ",ava->Available[i]);

}

printf("\n");

/************************** 初始安全检测 ****************************/

int checkResult = check(ava,need,allo);

if (checkResult) {

printf("Safe!\n");

}else{

printf("Unsafe!\n");

return -1;

}

/************************** 开始运行 ****************************/

//输入第i号进程的Request向量:

int Request[SourceType];

int RequestNum = 0;

while (RequestNum != -1) {

//输入第RequestNum号进程申请资源:

printf("输入第RequestNum号进程申请资源\n");

scanf("%d",&RequestNum);

if (RequestNum == -1) break;

//对第j类资源的申请量:

printf("输入对第j类资源的申请量:\n");

for (int jj=0; jjscanf("%d",&Request[jj]);

}

//判断申请量是否合格:

intcheckAll =0;//如果等于SourceType,说明下面的条件每一类资源都成立:

for (int jj = 0; jjif ((Request[jj] <= need->Need[RequestNum][jj]) && (Request[jj] <= ava->Available[jj])) {

checkAll+= 1;

}

}

//对资源进行试探性分配:

if (checkAll == SourceType) {

for (int jj=0; jjallo->Allocation[RequestNum][jj] = allo->Allocation[RequestNum][jj] + Request[jj];

need->Need[RequestNum][jj] = need->Need[RequestNum][jj] - Request[jj];

ava->Available[jj] = ava->Available[jj] - Request[jj];

}

}else{

printf("不安全!\n");

continue;

}

//安全检查:

if (check(ava, need, allo)) {

printf("安全!\n");

}else{

printf("不安全!\n");

//撤销刚才的试探性分配:

for (int jj=0; jjallo->Allocation[RequestNum][jj] = allo->Allocation[RequestNum][jj] - Request[jj];

need->Need[RequestNum][jj] = need->Need[RequestNum][jj] + Request[jj];

ava->Available[jj] = ava->Available[jj] + Request[jj];

}

}

}

return 0;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值