您的位置:

银行家算法C语言代码的详细阐述

一、银行家算法C语言代码及流程图

银行家算法,是一种避免死锁的算法,适用于操作系统、数据库、计算机网络等领域。下面是实现银行家算法的C语言代码及流程图:

#include<stdio.h>
int main()
{
    int n, m, i, j, k;
    int available[10], maximum[10][10], need[10][10], allocation[10][10];
    printf("Enter no of processes: ");
    scanf("%d", &n);
    printf("Enter no of resources: ");
    scanf("%d", &m);
    printf("Enter the available resources: ");
    for(i = 0; i < m; i++)
    {
        scanf("%d", &available[i]);
    }
    printf("Enter the maximum resource table: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &maximum[i][j]);
        }
    }
    printf("Enter the allocation matrix: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &allocation[i][j]);
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }
    printf("\n\nAvailable resources: ");
    for(i = 0; i < m; i++)
    {
        printf("%d ", available[i]);
    }
    printf("\nMaximum resource table:\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            printf("%d ", maximum[i][j]);
        }
        printf("\n");
    }
    printf("\nAllocation matrix:\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            printf("%d ", allocation[i][j]);
        }
        printf("\n");
    }
    printf("\nNeed matrix:\n");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            printf("%d ", need[i][j]);
        }
        printf("\n");
    }
    int finish[10] = {0}, ans[10], count = 0;
    for(k = 0; k < 5; k++)
    {
        for(i = 0; i < n; i++)
        {
            if(finish[i] == 0)
            {
                int flag = 0;
                for(j = 0; j < m; j++)
                {
                    if(need[i][j] > available[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                {
                    ans[count++] = i;
                    for(j = 0; j < m; j++)
                    {
                        available[j] += allocation[i][j];
                    }
                    finish[i] = 1;
                }
            }
        }
    }
    printf("\nSequence of process execution:");
    for(i = 0; i < n; i++)
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}

下面是银行家算法C语言代码的流程图:

二、银行家算法C语言代码和运行结果

下面是银行家算法C语言代码的运行结果:

Enter no of processes: 5
Enter no of resources: 3
Enter the available resources: 3 3 2
Enter the maximum resource table: 7 5 3
3 2 2
9 0 2
2 2 2
4 3 3
Enter the allocation matrix: 0 1 0
2 0 0
3 0 2
2 1 1
0 0 2


Available resources: 3 3 2
Maximum resource table:
7 5 3
3 2 2
9 0 2
2 2 2
4 3 3

Allocation matrix:
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2

Need matrix:
7 4 3
1 2 2
6 0 0
0 1 1
4 3 1

Sequence of process execution:0 2 3 1 4

三、银行家算法代码

银行家算法是在多个进程同时访问共享资源时,避免进程死锁的一种算法。下面是银行家算法的代码:

#include<stdio.h>
int main()
{
    int n, m, i, j, k;
    int available[10], maximum[10][10], need[10][10], allocation[10][10];
    printf("Enter no of processes: ");
    scanf("%d", &n);
    printf("Enter no of resources: ");
    scanf("%d", &m);
    printf("Enter the available resources: ");
    for(i = 0; i < m; i++)
    {
        scanf("%d", &available[i]);
    }
    printf("Enter the maximum resource table: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &maximum[i][j]);
        }
    }
    printf("Enter the allocation matrix: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &allocation[i][j]);
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }
    int finish[10] = {0}, ans[10], count = 0;
    for(k = 0; k < 5; k++)
    {
        for(i = 0; i < n; i++)
        {
            if(finish[i] == 0)
            {
                int flag = 0;
                for(j = 0; j < m; j++)
                {
                    if(need[i][j] > available[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                {
                    ans[count++] = i;
                    for(j = 0; j < m; j++)
                    {
                        available[j] += allocation[i][j];
                    }
                    finish[i] = 1;
                }
            }
        }
    }
    printf("\nSequence of process execution:");
    for(i = 0; i < n; i++)
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}

四、银行家算法核心代码

下面是银行家算法的核心代码:

int finish[10] = {0}, ans[10], count = 0;
for(k = 0; k < 5; k++)
{
    for(i = 0; i < n; i++)
    {
        if(finish[i] == 0)
        {
            int flag = 0;
            for(j = 0; j < m; j++)
            {
                if(need[i][j] > available[j])
                {
                    flag = 1;
                    break;
                }
            }
            if(flag == 0)
            {
                ans[count++] = i;
                for(j = 0; j < m; j++)
                {
                    available[j] += allocation[i][j];
                }
                finish[i] = 1;
            }
        }
    }
}

五、银行家算法C语言实现代码

下面是银行家算法C语言实现的完整代码:

#include<stdio.h>
int main()
{
    int n, m, i, j, k;
    int available[10], maximum[10][10], need[10][10], allocation[10][10];
    printf("Enter no of processes: ");
    scanf("%d", &n);
    printf("Enter no of resources: ");
    scanf("%d", &m);
    printf("Enter the available resources: ");
    for(i = 0; i < m; i++)
    {
        scanf("%d", &available[i]);
    }
    printf("Enter the maximum resource table: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &maximum[i][j]);
        }
    }
    printf("Enter the allocation matrix: ");
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            scanf("%d", &allocation[i][j]);
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }
    int finish[10] = {0}, ans[10], count = 0;
    for(k = 0; k < 5; k++)
    {
        for(i = 0; i < n; i++)
        {
            if(finish[i] == 0)
            {
                int flag = 0;
                for(j = 0; j < m; j++)
                {
                    if(need[i][j] > available[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                {
                    ans[count++] = i;
                    for(j = 0; j < m; j++)
                    {
                        available[j] += allocation[i][j];
                    }
                    finish[i] = 1;
                }
            }
        }
    }
    printf("\nSequence of process execution:");
    for(i = 0; i < n; i++)
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}

六、银行家算法例题及代码

以下是一个使用银行家算法的例题及代码:

题目描述:

现有3个进程和3类资源,已知它们的最大需求量和已分配量,如下表所示,请编写程序,用银行家算法判断此状态是否安全,并给出安全序列。

最大需求量:
进程1:5 4 3
进程2:2 3 1
进程3:7 5 3

已分配量:
进程1:0 1 0
进程2:2 0 0
进程3:3 0 2

可利用资源:
2 3 0

下面是使用银行家算法的C语言代码:

#include<stdio.h>
int main()
{
    int n = 3, m = 3, i, j, k;
    int available[10] = {2, 3, 0}, maximum[10][10] = {{5, 4, 3}, {2, 3, 1}, {7, 5, 3}}, need[10][10], allocation[10][10] = {{0, 1, 0}, {2, 0, 0}, {3, 0, 2}};
    for(i = 0; i < n; i++)
    {
        for(j = 0; j < m; j++)
        {
            need[i][j] = maximum[i][j] - allocation[i][j];
        }
    }
    int finish[10] = {0}, ans[10], count = 0;
    for(k = 0; k < 5; k++)
    {
        for(i = 0; i < n; i++)
        {
            if(finish[i] == 0)
            {
                int flag = 0;
                for(j = 0; j < m; j++)
                {
                    if(need[i][j] > available[j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag == 0)
                {
                    ans[count++] = i;
                    for(j = 0; j < m; j++)
                    {
                        available[j] += allocation[i][j];
                    }
                    finish[i] = 1;
                }
            }
        }
    }
    printf("\nSequence of process execution:");
    for(i = 0; i < n; i++)
    {
        printf("%d ", ans[i]);
    }
    printf("\n");
    return 0;
}

运行结果为:Sequence of process execution:1 0 2
因此,此状态是安全的,并且安全序列为,进程2->进程1->进程3。