C語言實現銀行家算法
這幾天老師要求使用C語言實現銀行家算法,數據可以自定義。想來想去還是照着書現成的數據來模擬一下。
教材使用的是西安電子科大出版社的《計算機操作系統》湯小丹 第四版。模擬數據使用的是P121頁第4題的數據。
聽到老師佈置題目的第一時間我還是有點懵,看了下書更懵了,這條條框框的判斷條件怎麼這麼多。。沉下心來慢慢看,其實還是挺簡單的算法。
/*Author:Cnkizy
數據參考 P121 4.銀行家算法之例
*/
#include<stdio.h>
#define Pcount 5 //5個進程
#define Scount 3 //3類資源
int Available[Scount];//可利用資源向量
int Max[Pcount][Scount];//最大需求矩陣 可以通過Need+Allocation算出來
int Allocation[Pcount][Scount];//分配矩陣
int Need[Pcount][Scount];//需求矩陣
//int SouresMax[Scount] = { 10,5,7 };//這裏給ABC三類資源的數量爲10,5,7
/*資源分配表,必要的一些數據如下
Max Allocation Need Available
P0 0 1 0 7 4 3 3 3 2
P1 2 0 0 1 2 2
P2 3 0 2 6 0 0
P3 2 1 1 0 1 1
P4 0 0 2 4 3 1
*/
//初始化數據
void InitializeData();
//查看當前資源分配表
void ShowData(int line);
//計算最大需求數量
void CalcMaxMatrix();
//資源比較 a<=b 返回1 a>b 返回0
int Equals(int a[Scount], int b[Scount]);
//安全性算法,當前是否處於安全狀態
int CheckSafe();
//檢查標誌所有都爲True,是返回1 不是返回0
int CheckFinish(int Finish[Scount]);
//向量相加 a = a+b
void Add(int* a, int b[Scount]);
//向量相減 a = a-b
void Minus(int* a, int b[Scount]);
//進程資源請求 P:進程i,r申請資源數{1,1,1} 返回1成功 0失敗
int Request(int P, int Request[Scount]);
//帶命令提示符提示的請求
void RequestShowMsg(int P, int R[Scount]);
int main() {
//初始化銀行家算法的數據,詳見上表
InitializeData();
printf("=============初始數據如下=============\n");
ShowData(0);
//安全性檢查
CheckSafe();
//進程P1 申請資源{1,0,2}
int apply[Scount] = { 1,0,2 };
RequestShowMsg(1, apply);
//進程P4 申請資源{1,0,2}
int apply2[Scount] = { 3,3,0 };
RequestShowMsg(4, apply2);
//進程P0 申請資源{0,2,0}
int apply3[Scount] = { 0,2,0 };
RequestShowMsg(0, apply3);
return 0;
}
//初始化數據,資源分配表
void InitializeData() {
Allocation[0][0] = 0, Allocation[0][1] = 1, Allocation[0][2] = 0;
Allocation[1][0] = 2, Allocation[1][1] = 0, Allocation[1][2] = 0;
Allocation[2][0] = 3, Allocation[2][1] = 0, Allocation[2][2] = 2;
Allocation[3][0] = 2, Allocation[3][1] = 1, Allocation[3][2] = 1;
Allocation[4][0] = 0, Allocation[4][1] = 0, Allocation[4][2] = 2;
Need[0][0] = 7, Need[0][1] = 4, Need[0][2] = 3;
Need[1][0] = 1, Need[1][1] = 2, Need[1][2] = 2;
Need[2][0] = 6, Need[2][1] = 0, Need[2][2] = 0;
Need[3][0] = 0, Need[3][1] = 1, Need[3][2] = 1;
Need[4][0] = 4, Need[4][1] = 3, Need[4][2] = 1;
Available[0] = 3, Available[1] = 3, Available[2] = 2;
CalcMaxMatrix();
}
//進程資源請求 P:進程i,r申請資源數{1,1,1} 返回1成功 0失敗
int Request(int P,int Request[Scount]) {
printf("進程P%d申請資源%d %d %d:\n",P, Request[0], Request[1], Request[2]);
//步驟1 進行資源檢查Request <= Need才能執行步驟2
if (!Equals(Request, Need[P])) {
printf("進程P%d,Request:%d %d %d > Need:%d %d %d 申請失敗,所需資源數超過宣佈最大值!\n", P, Request[0], Request[1], Request[2], Need[P][0], Need[P][1], Need[P][2]);
return 0;
}
//步驟2 進行資源檢查Request <= Available才能執行步驟3
if (!Equals(Request, Available)) {
printf("進程P%d,Request:%d %d %d > Available:%d %d %d 申請失敗,尚無足夠資源,該進程需要等待!\n", P, Request[0], Request[1], Request[2], Available[0], Available[1], Available[2]);
return 0;
}
printf("進程P%d,Request:%d %d %d <= Need:%d %d %d\n", P, Request[0], Request[1], Request[2], Need[P][0], Need[P][1], Need[P][2]);
printf("進程P%d,Request:%d %d %d <= Available:%d %d %d \n", P, Request[0], Request[1], Request[2], Available[0], Available[1], Available[2]);
//步驟3 試分配資源給進程P
Minus(Available, Request);//Available -= Request
Add(Allocation[P],Request);//Allocation += Request
Minus(Need[P], Request);//Need -= Request
//步驟4 安全性檢查
int Safestate = CheckSafe();
if (Safestate) {
return Safestate;//分配後處於安全狀態 分配成功
}
//分配後處於不安全狀態 分配失敗,本次分配作廢,回覆原來的資源分配狀態
Add(Available, Request);//Available += Request
Minus(Allocation[P], Request);//Allocation -= Request
Add(Need[P], Request);//Need += Request
return Safestate;
}
//帶命令提示符提示的請求
void RequestShowMsg(int P, int R[Scount]) {
//進程P 申請資源Request{1,0,2}
printf("\n模擬分配資源:P%d申請資源 %d %d %d\n======================\n",P, R[0], R[1], R[2]);
int State = Request(P, R);
if (State) {
printf("本次資源分配成功!\n");
ShowData(0);
}else {
printf("本次資源分配失敗!進程P%d需要等待\n",P);
}
}
//安全性算法,當前是否處於安全狀態
int CheckSafe() {
printf("開始安全性檢查:\n");
//步驟1 設置兩個向量
int Finish[Pcount] = { 0 };//是否被處理過,初始值全爲False,被檢查過才置爲True
int Work[Scount] = { 0 };//工作向量
Add(Work, Available);//首先讓Work = Available
//步驟2 從進程集合尋找符合下列條件的進程
//Finish[i] = false;
//Need[i,j] <= Work[j];
for (int i = 0; i < Pcount; i++) {
if (Finish[i])continue;//已經標記爲True就跳過
if (!Equals(Need[i], Work))continue;//Need[i,j] > Work[j] 就跳過。
//上述條件成立,執行步驟3
Add(Work, Allocation[i]);//Work += Allocation;
Finish[i] = 1;//Finish[i]=True;
printf("P%d進程,Work=%d %d %d,Finish=true,安全狀態\n", i, Work[0], Work[1], Work[2]);
i = -1;//返回步驟2
}
//步驟4 判斷Finish
if (CheckFinish(Finish)) {
printf("安全狀態檢查完畢:【Finish全爲true,系統處於安全狀態】\n");
return 1;//全爲True
}
printf("安全狀態檢查完畢:【Finish存在False,系統處於不安全狀態】\n");
return 0;//存在False
}
//檢查標誌所有都爲True,是返回1 不是返回0
int CheckFinish(int Finish[Scount]) {
for (int i = 0; i < Scount; i++) {
if (Finish[i] == 0) return 0;
}
return 1;
}
//查看當前資源分配表
void ShowData(int line) {
printf(" Max Alloca Need Available\n");
for (int i = 0; i < Pcount; i++) {
printf("p%d:\t", i);
for (int j = 0; j < Scount; j++) {
printf("%d ", Max[i][j]);
}
printf("\t");
for (int j = 0; j < Scount; j++) {
printf("%d ", Allocation[i][j]);
}
printf("\t");
for (int j = 0; j < Scount; j++) {
printf("%d ", Need[i][j]);
}
if (line == i) {
printf("\t");
for (int j = 0; j < Scount; j++) {
printf("%d ", Available[j]);
}
}
printf("\n");
}
}
//計算最大需求數量
void CalcMaxMatrix() {
for (int i = 0; i < Pcount; i++) {
for (int j = 0; j < Scount; j++) {
Max[i][j] = Need[i][j] + Allocation[i][j];
}
}
}
//向量相加 a = a+b
void Add(int* a, int b[Scount]) {
for (int i = 0; i < Scount; i++) {
a[i] = a[i] + b[i];
}
}
//向量相減 a = a-b
void Minus(int* a, int b[Scount]) {
for (int i = 0; i < Scount; i++) {
a[i] = a[i] - b[i];
}
}
//資源比較 a<=b 返回1 a>b 返回0
int Equals(int a[Scount], int b[Scount]) {
for (int i = 0; i < Scount; i++) {
if (a[i] > b[i]) return 0;
}
return 1;
}
偷懶for循環所以使用了C++編譯器。