银行家算法

来源:互联网 发布:手机搭建php 编辑:程序博客网 时间:2024/05/16 10:33

*banker.h*

///#include < stdio.h >

//系统中进程的数量
//#define PRO_NUM 5

typedef struct{
int A;
int B;
int C;
int D;
}RESOURCE;

//最大需求矩阵
RESOURCE max_need[PRO_NUM] =
{
{0,1,4,4},
{2,6,4,1},
{3,5,9,9},
{0,7,6,5},
{1,5,5,9}
};

//已分配资源矩阵
RESOURCE allocation[PRO_NUM] =
{
{0,1,3,1},
{1,0,1,1},
{1,2,4,3},
{0,2,3,2},
{1,0,2,3}
};

//需求矩阵
RESOURCE need[PRO_NUM] =
{
{0,0,1,3},
{1,6,3,0},
{2,3,5,6},
{0,5,3,3},
{0,5,3,6}
};

RESOURCE available = {2,7,3,3};

*banker.c*

// #include “banker.h”

int check_safe()
{
RESOURCE temp = available;
int checked[PRO_NUM] = {0,0,0,0,0};//0为检查未完成,1为检查完成
int i = 0;

while(i != PRO_NUM){    if(checked[i] == 0)    {        if((need[i].A <= temp.A) && (need[i].B <= temp.B) && (need[i].C <= temp.C) && (need[i].D <= temp.D))        {            temp.A += allocation[i].A;            temp.B += allocation[i].B;            temp.C += allocation[i].C;            temp.D += allocation[i].D;            checked[i] = 1;            printf("process %d is checked\n",i + 1);            i = -1;        }    }    i++;}for(i = 0; i < PRO_NUM; i++){    if(checked[i] == 0)    {        return 0;    }}return 1;

}

void print_all()
{
int i;

printf("**************资源分配情况**************\n");printf("进程     最大需求     已分配     还需要 \n");printf("        A  B  C  D  A  B  C  D  A  B  C  D \n");for(i = 0; i < PRO_NUM; i++){    printf("P%d      %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d  %d\n",i,max_need[i].A,max_need[i].B,max_need[i].C,max_need[i].D,allocation[i].A,allocation[i].B,allocation[i].C,allocation[i].D,need[i].A,need[i].B,need[i].C,need[i].D);}printf("目前系统可用资源: A:%d B:%d C:%d D:%d\n",available.A,available.B,available.C,available.D);printf("****************************************\n");

}

void allocate(int n_pro,RESOURCE *n_res)
{
need[n_pro].A = need[n_pro].A - n_res->A;
need[n_pro].B = need[n_pro].B - n_res->B;
need[n_pro].C = need[n_pro].C - n_res->C;
need[n_pro].D = need[n_pro].D - n_res->D;

available.A = available.A - n_res->A;available.B = available.B - n_res->B;available.C = available.C - n_res->C;available.D = available.D - n_res->D;allocation[n_pro].A = allocation[n_pro].A + n_res->A;allocation[n_pro].B = allocation[n_pro].B + n_res->B;allocation[n_pro].C = allocation[n_pro].C + n_res->C;allocation[n_pro].D = allocation[n_pro].D + n_res->D;//printf("目前系统可用资源: A:%d B:%d C:%d \n",available.A,available.B,available.C);//sleep(1);

}

void ret(int n_pro,RESOURCE *n_res)
{
need[n_pro].A = need[n_pro].A + n_res->A;
need[n_pro].B = need[n_pro].B + n_res->B;
need[n_pro].C = need[n_pro].C + n_res->C;
need[n_pro].D = need[n_pro].D + n_res->D;

available.A = available.A + n_res->A;available.B = available.B + n_res->B;available.C = available.C + n_res->C;available.D = available.D + n_res->D;allocation[n_pro].A = allocation[n_pro].A - n_res->A;allocation[n_pro].B = allocation[n_pro].B - n_res->B;allocation[n_pro].C = allocation[n_pro].C - n_res->C;allocation[n_pro].D = allocation[n_pro].D - n_res->D;

}

int request(int n_pro,RESOURCE *n_res)
{
if((n_res->A <= need[n_pro].A) && (n_res->B <= need[n_pro].B) && (n_res->C <= need[n_pro].C) && (n_res->D <= need[n_pro].D))
{
if((n_res->A <= available.A) && (n_res->B <= available.B) && (n_res->C <= available.C) && (n_res->D <= available.D))
{
allocate(n_pro,n_res);

        if(check_safe())        {            if((need[n_pro].A == 0) && (need[n_pro].B == 0) && (need[n_pro].C == 0) && (need[n_pro].D == 0))            {                available.A = available.A + max_need[n_pro].A;                available.B = available.B + max_need[n_pro].B;                available.C = available.C + max_need[n_pro].C;                available.D = available.D + max_need[n_pro].D;            }            return 1;        }        else        {            printf("当前系统状态不安全!\n");            printf("正在返回安全状态...\n");            ret(n_pro,n_res);            printf("返回安全状态成功!\n");            return 0;        }    }    else    {        printf("出错啦:请求资源大于系统可使用资源!\n");        return 0;    }}else{    printf("出错啦:请求资源大于需求资源!\n");    return 0;}

}
/*
void safe_1(int finished)
{
int i;
int j;

static int a = 0;static num = 0;static seq[PRO_NUM];if(finished == PRO_NUM){    num++;    for(i = 0; i < PRO_NUM; i++)    {        printf("%d ",seq[i]);    }    printf("\n");    return;}for(i = 0; i < PRO_NUM; i++){    if(finished)}

}*/

void safe()
{
int tmp = PRO_NUM;
int n = 1;

while(tmp != 0){    n = n * tmp;    tmp--;}int max[n][PRO_NUM];int a,b,c,d,e;int m = 0;int count = 0;RESOURCE temp = available;for(a = 0; a < 5; a++){    for(b = 0; b < 5; b++)    {        for(c = 0; c < 5; c++)        {            for(d = 0; d < 5; d++)            {                for(e = 0; e < 5; e++)                {                    if((a != b) && (a != c) && (a != d) && (a !=e) && (b != c) && (b != d) && (b != e) && (c != d) && (c != e) && (d != e))                    {                        max[m][0] = a;                        max[m][1] = b;                        max[m][2] = c;                        max[m][3] = d;                        max[m][4] = e;                        m++;                    }                }            }        }    }}for(a = 0; a < m; a++){    temp = available;    for(b = 0; b < 5; b++)    {    if((need[max[a][b]].A <= temp.A) && (need[max[a][b]].B <= temp.B) && (need[max[a][b]].C <= temp.C) && (need[max[a][b]].D <= temp.D))        {            temp.A += allocation[max[a][b]].A;            temp.B += allocation[max[a][b]].B;            temp.C += allocation[max[a][b]].C;            temp.D += allocation[max[a][b]].D;        }        else        {            break;        }    }    if(b == 5)    {          count++;          printf("第%d种安全序列为:",count);          for(c = 0; c < 5; c++)          {              if((need[max[a][c]].A != 0) || (need[max[a][c]].B != 0) || (need[max[a][c]].C != 0) && (need[max[a][c]].D != 0))              {                        printf("%d ",max[a][c]);                           }          }          printf("\n");    }}

}

int main()
{
int i;
int n_pro;
char choose = ‘y’;
char choose_1;
RESOURCE n_res;

system("clear");printf("************************************\n");printf("***********银行家算法演示***********\n");printf("************************************\n");printf("首先检查初始状态是否安全...\n");if(check_safe() == 1){    printf("系统处于安全状态!^-^\n");}else{    printf("系统处于不安全状态!T-T\n演示结束!\n");    return -1;}sleep(2);printf("你想要输出所有的安全序列吗?(y/n):\n");getchar();scanf("%c",choose_1);if(choose == 'y'){    safe();}sleep(2);while(choose != 'n'){    system("clear");    print_all();    printf("请输入请求分配的进程和对ABC三类资源的请求:\n");    scanf("%d %d %d %d %d",&n_pro,&n_res.A,&n_res.B,&n_res.C,&n_res.D);    if(request(n_pro,&n_res))    {        printf("分配成功!^-^\n");        safe();    }    else    {        printf("分配失败!T-T!\n");    }    for(i = 0; i < PRO_NUM; i++)    {        if((need[i].A != 0) || (need[i].B != 0) || (need[i].C != 0) || (need[i].D != 0))        {            break;        }    }    if(i == 5)    {        break;    }   // safe();    printf("是否继续分配?(任意键继续,n退出):\n");    getchar();    scanf("%c",&choose);}system("clear");print_all();printf("***********银行家算法演示完成***********\n");printf("****************************************\n");return 0;

}

0 0