一、銀行家算法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-hk/n/146125.html