銀行家演算法的核心思想是「捨大取小」:先設法滿足需求小的,最後才處理需求大的,藉此判斷一次資源分配是否仍處於安全狀態。
/**************************************************
銀行家算法:
主要思想是 捨大取小,先滿足小的,最後才滿足大的。
author: lyb
date: 2014/10/15
***************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
// 進程運行狀態標誌
#define TRUE 1
#define FALSE 0
#define WAIT -1
// version 2:採用動態分配陣列,為了函式呼叫方便,使用全域指標
int *Resource = NULL; // 各類資源的總量
int *Max = NULL; // 各資源的最大需求量
int *Need = NULL; // 各進程需求的資源量
int *Allocation= NULL; // 已分配的資源量
int *Available = NULL; // 剩餘可用的資源量
// 檢測此時的系統分配狀態是否安全(核心函式)
int testStatus(const int P, const int R)
{
int finish = 0; // 運行完成的進程數
int wait = 0; // 等待的進程數
int minR = 0; // 最小的資源數
int minP = 0; // 最小需求資源的進程
int i = 0, j = 0, k = 0, l = 0;
int *status = (int*)malloc(P*sizeof(int)); // 進程的狀態
int *Available_tmp = (int*)malloc(R*sizeof(int)); // Available 的一份拷貝
if (status != NULL && Available_tmp != NULL)
{
memset(status, FALSE, P*sizeof(int));
memcpy(Available_tmp, Available, R*sizeof(int));
}
else
{
printf("pointer NULL\n");
return FALSE;
}
while( finish != P && wait != P)
{
// 以第一類資源為基準,選取該資源需求量最小的進程
minR = Resource[0];
minP = 0;
for (i=0; i<P; ++i)
{
if (status[i] == FALSE && Need[i*R + 0] < minR)
{
minR = Need[i*R + 0];
minP = i;
}
}
// 驗證挑選出來的進程能否滿足
for (j=0; j<R; ++j)
{
if (Need[minP*R + j] > Available_tmp[j])
break;
}
if (j == R) // 能夠滿足
{
status[minP] = TRUE;
finish++;
// 進程能運行結束後釋放資源,這裡回收
for (l=0; l<R; ++l)
Available_tmp[l] += Allocation[minP*R + l];
// 喚醒等待的所有進程
for(k=0; k<P; ++k)
{
if (status[k] == WAIT)
{
status[k] = FALSE;
wait--;
}
}
}
else
{
// 不能滿足時,該進程等待
status[minP] = WAIT;
wait++;
}
}
free(status);
free(Available_tmp);
return (finish == P) ? TRUE : FALSE;
}
// 從文件中讀取數據
int readData(int *p, int *r)
{
int i = 0;
int pCount = 0;
int rCount = 0;
// 為方便操作,這裡僅使用重定向處理
freopen("in.txt", "r", stdin);
scanf("%d", p);
scanf("%d", r);
pCount = *p;
rCount = *r;
Resource = (int*)malloc( rCount * sizeof(int));
Max = (int*)malloc( pCount * rCount * sizeof(int));
Need = (int*)malloc( pCount * rCount * sizeof(int));
Allocation = (int*)malloc( pCount * rCount * sizeof(int));
Available = (int*)malloc( rCount * sizeof(int));
if (Resource == NULL || Max == NULL || Need == NULL
|| Allocation == NULL || Available == NULL )
{
return FALSE;
}
for (i=0; i<rCount; ++i) // 各資源的總量
scanf("%d", Resource + i);
for (i=0; i<pCount*rCount; ++i) // 最大需求量
scanf("%d", Max+i);
for (i=0; i<pCount*rCount; ++i) // 已分配的資源量
scanf("%d", Allocation+i);
for (i=0; i<rCount; ++i) // 剩餘可分配的資源量
scanf("%d", Available+i);
for (i=0; i<pCount*rCount; ++i) // 計算各資源的需求量
*(Need+i) = *(Max+i) - *(Allocation+i);
return 0;
}
// 某進程申請資源的請求
int request(const int PCount, const int RCount, const int pId, const int *reqSource)
{
int i=0;
int *testAllocate = (int*)malloc(PCount*RCount*sizeof(int)); // 預儲存嘗試的分配情況
if (testAllocate != NULL)
memcpy(testAllocate, Allocation, PCount*RCount*sizeof(int));
else
return FALSE;
// 進行資源預分配
for (i=0; i<RCount; ++i)
{
if (reqSource[i] > Available[i]) // 申請的資源比剩餘的還多
return FALSE;
else
testAllocate[pId*RCount + i] += reqSource[i];
}
if (testStatus(PCount, RCount) == TRUE) // 是一個安全狀態
{
memcpy(Allocation, testAllocate, PCount*RCount*sizeof(int));
free(testAllocate);
return TRUE;
}
else
{
free(testAllocate);
return FALSE;
}
}
// 釋放所有的記憶體空間
int destroy()
{
if (Resource == NULL || Max == NULL || Need == NULL
|| Allocation == NULL || Available == NULL)
{
return FALSE;
}
else
{
free(Resource); Resource = NULL;
free(Max); Max = NULL;
free(Need); Need = NULL;
free(Allocation); Allocation = NULL;
free(Available); Available = NULL;
printf("Destroy\n");
return TRUE;
}
}
int main()
{
int p = 0; // 進程數
int r = 0; // 資源分類數
int reqSource[3] = {0, 3, 4};
readData(&p, &r);
if (testStatus(p, r) == TRUE)
printf("Saft\n");
else
printf("nonSaft\n");
if (request(p, r, 1, reqSource) == TRUE)
printf("Allocate\n");
else
printf("Non-Allocate\n");
destroy();
return 0;
}
5 3 // 進程數 資源種類數 17 5 20 // 各類資源總數 // 最大需求量 5 5 9 5 3 6 4 0 11 4 2 5 4 2 4 // 已分配資源數 2 1 2 4 0 2 4 0 5 2 0 4 3 1 4 // 剩餘的資源數 2 3 3
實作重點:testStatus 每次都複製一份 Available 來模擬分配,避免污染真實狀態;request 先「預分配」再跑一次安全性檢查,只有在安全時才正式提交,這正是銀行家演算法避免死結的關鍵。