銀行家演算法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/zh-tw/n/146125.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
ZFBO的頭像ZFBO
上一篇 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

發表回復

登錄後才能評論