銀行家算法是一種最有代表性的避免死鎖的算法。在避免死鎖方法中允許進程動態地申請資源,但系統在進行資源分配之前,應先計算此次分配資源的安全性,若分配不會導致系統進入不安全狀態,則分配,否則等待。
package com.zc.manythread;
import javax.swing.JOptionPane;
/*一共有5個進程需要請求資源,有3類資源*/
public class Bank {
// 每個進程所需要的最大資源數
public static int MAX[][] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 },
{ 2, 2, 2 }, { 4, 3, 3 } };
// 系統擁有的初始資源數
public static int AVAILABLE[] = { 10, 5, 7 };
// 系統已給每個進程分配的資源數
public static int ALLOCATION[][] = { { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 } };
// 每個進程還需要的資源數
public static int NEED[][] = { { 7, 5, 3 }, { 3, 2, 2 }, { 9, 0, 2 },
{ 2, 2, 2 }, { 4, 3, 3 } };
// 每次申請的資源數
public static int Request[] = { 0, 0, 0 };
// 進程數與資源數
public static int M = 5, N = 3;
int FALSE = 0;
int TRUE = 1;
public void showdata() {
int i, j;
System.out.print("系統可用的資源數爲:/n");
for (j = 0; j < N; j++) {
System.out.print("資源" + j + ":" + AVAILABLE[j] + " ");
}
System.out.println();
System.out.println("各進程還需要的資源量:");
for (i = 0; i < M; i++) {
System.out.print("進程" + i + ":");
for (j = 0; j < N; j++) {
System.out.print("資源" + j + ":" + NEED[i][j] + " ");
}
System.out.print("/n");
}
System.out.print("各進程已經得到的資源量: /n");
for (i = 0; i < M; i++) {
System.out.print("進程");
System.out.print(i);
for (j = 0; j < N; j++) {
System.out.print("資源" + j + ":" + ALLOCATION[i][j] + " ");
}
System.out.print("/n");
}
}
// 分配資源,並重新更新各種狀態
public void changdata(int k) {
int j;
for (j = 0; j < N; j++) {
AVAILABLE[j] = AVAILABLE[j] - Request[j];
ALLOCATION[k][j] = ALLOCATION[k][j] + Request[j];
NEED[k][j] = NEED[k][j] - Request[j];
}
};
// 回收資源,並重新更新各種狀態
public void rstordata(int k) {
int j;
for (j = 0; j < N; j++) {
AVAILABLE[j] = AVAILABLE[j] + Request[j];
ALLOCATION[k][j] = ALLOCATION[k][j] - Request[j];
NEED[k][j] = NEED[k][j] + Request[j];
}
};
// 釋放資源
public void free(int k) {
for (int j = 0; j < N; j++) {
AVAILABLE[j] = AVAILABLE[j] + ALLOCATION[k][j];
System.out.print("釋放" + k + "號進程的" + j + "資源!/n");
}
}
public int check0(int k) {
int j, n = 0;
for (j = 0; j < N; j++) {
if (NEED[k][j] == 0)
n++;
}
if (n == 3)
return 1;
else
return 0;
}
// 檢查安全性函數
public int chkerr(int s) {
int WORK;
int FINISH[] = new int[M], temp[] = new int[M];// 保存臨時的安全進程序列
int i, j, k = 0;
for (i = 0; i < M; i++)
FINISH[i] = FALSE;
for (j = 0; j < N; j++) {
WORK = AVAILABLE[j]; // 第j個資源可用數
i = s;
// 判斷第i個進程是否滿足條件
while (i < M) {
if (FINISH[i] == FALSE && NEED[i][j] <= WORK) {
WORK = WORK + ALLOCATION[i][j];
FINISH[i] = TRUE;
temp[k] = i;
k++;
i = 0;
} else {
i++;
}
}
for (i = 0; i < M; i++)
if (FINISH[i] == FALSE) {
System.out.print("/n系統不安全!!! 本次資源申請不成功!/n");
return 1;
}
}
System.out.print("/n經安全性檢查,系統安全,本次分配成功。/n");
System.out.print("本次安全序列:");
for (i = 0; i < M - 1; i++) {
System.out.print("進程" + temp[i] + "->");
}
System.out.print("進程" + temp[M - 1]);
System.out.println("/n");
return 0;
}
/**
* @param args
*/
public static void main(String[] args) {
int i = 0, j = 0;
int flag = 1;
Bank bank = new Bank();
bank.showdata();
while (flag == 1) {
i = -1;
while (i < 0 || i >= M) {
String str = JOptionPane.showInputDialog("請輸入需申請資源的進程號(從0到"
+ (M - 1) + ",否則重輸入!)");
i = Integer.parseInt(str);
if (i < 0 || i >= M)
System.out.println("輸入的進程號不存在,重新輸入!/n");
}
System.out.print("請輸入進程" + i + "申請的資源數/n");
for (j = 0; j < N; j++) {
String str = JOptionPane.showInputDialog("資源" + j + ":");
Request[j] = Integer.parseInt(str);
if (Request[j] > NEED[i][j]) {
System.out.print("進程" + i + "申請的資源數大於進程" + i + "還需要" + j
+ "類資源的資源量!申請不合理,出錯!請重新選擇!/n");
flag = 0;
break;
} else {
if (Request[j] > AVAILABLE[j]) {
System.out.print("進程" + i + "申請的資源數大於系統可用" + j
+ "類資源的資源量!申請不合理,出錯!請重新選擇!/n");
flag = 0;
break;
}
}
}
if (flag == 1) {
bank.changdata(i);
int chkerr = bank.chkerr(i);
if (chkerr == 1) {
bank.rstordata(i);
bank.showdata();
} else {
bank.showdata();
int check = bank.check0(i);
if (check == 1) {
System.out.print("進程" + i + "已經完成,系統將其所佔用資源釋放/n");
bank.free(i);
}
}
} else {
bank.showdata();
}
System.out.println("/n");
String str = JOptionPane
.showInputDialog("是否繼續銀行家算法演示,按'1'鍵繼續,按'0'鍵退出演示");
flag = Integer.parseInt(str);
}
}
}
博客地址: | http://www.cnblogs.com/oumyye/ |
博客版權: | 本文以學習、研究和分享爲主,歡迎轉載,轉載請務必註明出處,謝謝合作。 如果文中有不妥或者錯誤的地方請指出。如果覺得本文對你有所幫助不如【推薦】一下!如果你有更好的建議,不如留言一起討論,共同進步! |