银行家算法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  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  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  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  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  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。

原创文章,作者:ZFBO,如若转载,请注明出处:https://www.506064.com/n/146125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
ZFBOZFBO
上一篇 2024-10-29 18:57
下一篇 2024-10-29 18:57

相关推荐

  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • Python字符串宽度不限制怎么打代码

    本文将为大家详细介绍Python字符串宽度不限制时如何打代码的几个方面。 一、保持代码风格的统一 在Python字符串宽度不限制的情况下,我们可以写出很长很长的一行代码。但是,为了…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python基础代码用法介绍

    本文将从多个方面对Python基础代码进行解析和详细阐述,力求让读者深刻理解Python基础代码。通过本文的学习,相信大家对Python的学习和应用会更加轻松和高效。 一、变量和数…

    编程 2025-04-29
  • Python实现爬楼梯算法

    本文介绍使用Python实现爬楼梯算法,该算法用于计算一个人爬n级楼梯有多少种不同的方法。 有一楼梯,小明可以一次走一步、两步或三步。请问小明爬上第 n 级楼梯有多少种不同的爬楼梯…

    编程 2025-04-29
  • AES加密解密算法的C语言实现

    AES(Advanced Encryption Standard)是一种对称加密算法,可用于对数据进行加密和解密。在本篇文章中,我们将介绍C语言中如何实现AES算法,并对实现过程进…

    编程 2025-04-29
  • Python满天星代码:让编程变得更加简单

    本文将从多个方面详细阐述Python满天星代码,为大家介绍它的优点以及如何在编程中使用。无论是刚刚接触编程还是资深程序员,都能从中获得一定的收获。 一、简介 Python满天星代码…

    编程 2025-04-29
  • 学习Python对学习C语言有帮助吗?

    Python和C语言是两种非常受欢迎的编程语言,在程序开发中都扮演着非常重要的角色。那么,学习Python对学习C语言有帮助吗?答案是肯定的。在本文中,我们将从多个角度探讨Pyth…

    编程 2025-04-29
  • 仓库管理系统代码设计Python

    这篇文章将详细探讨如何设计一个基于Python的仓库管理系统。 一、基本需求 在着手设计之前,我们首先需要确定仓库管理系统的基本需求。 我们可以将需求分为以下几个方面: 1、库存管…

    编程 2025-04-29
  • 写代码新手教程

    本文将从语言选择、学习方法、编码规范以及常见问题解答等多个方面,为编程新手提供实用、简明的教程。 一、语言选择 作为编程新手,选择一门编程语言是很关键的一步。以下是几个有代表性的编…

    编程 2025-04-29

发表回复

登录后才能评论