銀行家演算法c代碼
Ⅰ 關於銀行家演算法中部分代碼不理解,請高手幫忙指點下謝謝!!!
lass ThreadTest {
static int type = 4, num = 10; //定義資源數目和線程數目
static int[] resource = new int[type]; //系統資源總數
//static int[] Resource = new int[type]; //副本
static Random rand = new Random();
static Bank[] bank = new Bank[num]; //線程組
Bank temp = new Bank();
public void init() {
//初始化組中每個線程,隨機填充系統資源總數
for(int i = 0; i < type; i++)
resource[i] = rand.nextInt(10) + 80;
System.out.print("Resource:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
for(int i = 0; i < bank.length; i++)
bank[i] = new Bank("#" + i);
}
public ThreadTest4() {
init();
}
class Bank extends Thread {
//銀行家演算法避免死鎖
public int[]
max = new int[type], //總共需求量
need = new int[type], //尚需資源量
allocation = new int[type]; //已分配量
private int[]
request = new int[type], //申請資源量
Resource = new int[type]; //資源副本
private boolean isFinish = false; //線程是否完成
int[][] table = new int[bank.length][type*4]; //二維資源分配表
private void init() {
// 隨機填充總共、尚需、已分配量
synchronized(resource) {
for(int i = 0; i < type; i++) {
max[i] = rand.nextInt(5) + 10;
need[i] = rand.nextInt(10);
allocation[i] = max[i] - need[i];
resource[i] -= allocation[i]; //從系統資源中減去已分配的
}
printer();
for(int i = 0; i < type; i++) {
if(resource[i] < 0) {
//若出現已分配量超出系統資源總數的錯誤則退出
System.out.println("The summation of Threads' allocations is out of range!");
System.exit(1);
}
}
}
}
public Bank(String s) {
setName(s);
init();
start();
}
public Bank() {
//none
}
public void run() {
try {
sleep(rand.nextInt(2000));
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
while(true) {
//程序沒有完成時一直不斷申請資源
if(askFor() == false) {
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
}
else
tryRequest();
if(noNeed() == true)
break;
}
//休眠一段時間模擬程序運行
try {
sleep(1000);
}
catch(InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println(getName() + " finish!");
synchronized(resource) {
//運行結束釋放佔有資源
for(int i = 0; i < type; i++) {
resource[i] += allocation[i];
need[i] = allocation[i] = max[i] = 0;
}
}
}
private void printer() {
//列印當前資源信息
System.out.print(getName() + " Max:");
for(int i = 0; i < type; i++)
System.out.print(" " + max[i]);
System.out.print(" Allocation:");
for(int i = 0; i < type; i++)
System.out.print(" " + allocation[i]);
System.out.print(" Need:");
for(int i = 0; i < type; i++)
System.out.print(" " + need[i]);
System.out.print(" Available:");
for(int i = 0; i < type; i++)
System.out.print(" " + resource[i]);
System.out.println("");
}
private boolean askFor() {
//隨機產生申請資源量並檢測是否超標
boolean canAsk = false;
for(int i = 0; i < type; i++) {
request[i] = rand.nextInt(20);
//防止申請量超過所需量
if(request[i] > need[i])
request[i] = need[i];
}
for(int i = 0; i < type; i++) //防止隨機申請資源全為0
if(request[i] > 0)
canAsk = true;
synchronized(resource) {
//鎖住可供資源檢查是否超標
for(int i = 0; i < type; i++) {
if(request[i] > resource[i])
//如果申請資源超過可供資源則等待一段時間後重新申請
return false;
}
}
return canAsk;
}
private void tryRequest() {
//創建副本嘗試分配請求
synchronized(resource) {
for(int i = 0; i < type; i++)
//依然要防止請求量超出范圍
if(request[i] > resource[i])
return;
for(int i = 0; i < type; i++) {
//復制資源量並減去需求量到一個副本上
Resource[i] = resource[i];
Resource[i] -= request[i];
}
System.out.print(getName() + " ask for:");
for(int i = 0; i < type; i++)
System.out.print(" " + request[i]);
System.out.println("");
if(checkSafe() == true) {
//如果檢查安全則將副本值賦給資源量並修改佔有量和需求量
for(int i = 0; i < type; i++) {
resource[i] = Resource[i];
allocation[i] += request[i];
need[i] -= request[i];
}
System.out.println(getName() + " request succeed!");
}
else
System.out.println(getName() + " request fail!");
}
}
private boolean checkSafe() {
//銀行家演算法檢查安全性
synchronized(bank) {
//將線程資源信息放入二維資源分配表檢查安全性,0~type可用資源/type~type*2所需資源/type*2~type*3佔有資源/type*3~-1可用+佔用資源
for(int i = 0; i < bank.length; i++) {
for(int j = type; j < type*2; j++) {
table[i][j] = bank[i].need[j%type];
}
for(int j = type*2; j < type*3; j++) {
table[i][j] = bank[i].allocation[j%type];
}
}
//冒泡排序按需求資源從小到大排
for(int i = 0; i < bank.length; i++) {
for(int j = i; j < bank.length-1; j++) {
sort(j, 4);
}
}
//進行此時刻的安全性檢查
for(int i = 0; i < type; i++) {
table[0][i] = Resource[i];
table[0][i+type*3] = table[0][i] + table[0][i+type*2];
if(table[0][i+type*3] < table[1][i+type])
return false;
}
for(int j = 1; j < bank.length-1; j++) {
for(int k = 0; k < type; k++) {
table[j][k] = table[j-1][k+type*3];
table[j][k+type*3] = table[j][k] + table[j][k+type*2];
if(table[j][k+type*3] < table[j+1][k+type])
return false;
}
}
}
return true;
}
private void sort(int j, int k) {
//遞歸冒泡排序
int tempNum;
if(table[j][k] > table[j+1][k]) {
for(int i = type; i < type*2; i++) {
tempNum = table[j][i];
table[j][i] = table[j+1][i];
table[j+1][i] = tempNum;
}
/*temp = bank[j];
bank[j] = bank[j+1];
bank[j+1] = temp;*/
}
else if(table[j][k] == table[j+1][k] && k < type*2) //此資源量相同時遞歸下一個資源量排序並且防止超出范圍
sort(j, k+1);
}
private boolean noNeed() {
//是否還需要資源
boolean finish = true;
for(int i = 0; i < type; i++) {
if(need[i] != 0) {
finish = false;
break;
}
}
return finish;
}
}
public static void main(String[] args) {
ThreadTest t = new ThreadTest();
//後台線程,設定程序運行多長時間後自動結束
new Timeout(30000, "---Stop!!!---");
}
}
Ⅱ 銀行家演算法
#include<stdio.h>
#include "stdlib.h"
#define M 5
#define N 3
int max[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
int need[M][N]={{7,4,3},{1,2,2},{6,0,0},{0,1,1},{4,3,1}};
int allocation[M][N]={{0,1,0},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
int request[N]={0};
int finish[M]={0};
int available[N]={3,3,2};
int work[N]={3,3,2};
int safe[M]={0};
int n=1,m=1;
void showdata()
{
int i,j;
printf("系統可用的資源數為:\n");
for (j=0;j<N;j++)
printf("資源%c: %d \n",j+65, work[j]);
printf("\n");
printf("進程還需要的資源量: \n");
printf(" A B C\n" );
for(i=0;i<M;i++)
{
printf("進程p%d:",i);
for(j=0;j<N;j++)
printf(" %d ", need[i][j]);
printf("\n");
}
printf("進程已經得到的資源量:\n");
printf(" A B C\n" );
for(i=0;i<M;i++)
{
printf("進程%d:",i);
for(j=0;j<N;j++)
printf(" %d ",allocation[i][j]);
printf("\n"坦源);
}
}
void fp(int i){
for(int j=0;j<N;j++){
work[j]=work[j]+allocation[i][j];
finish[i]=1;
}
}
void secure(){
showdata();
int y=0,flag=6,h,i,j;
while(flag){
for(i=0;i<M;i++){
for(j=0;j<N;j++){
if((finish[i]==0)&&(need[i][j]<=work[j]))
continue;
else
j=15;
}
if(j==N){
fp(i);
safe[y++]=i;
i=15;
}
else
continue;
}
flag--;
}
for( h=0;h<M;h++){
if(finish[h]==1)
continue;
else
h=M+1;
}
if(h==M){
printf("該狀態安全!安全序列鏈碧是:\n");
for(int l=0;l<M;l++){
if(l==M-1)
printf("p%d",safe[l]);
else
printf("p%d-->",safe[l]);
}
n=1;
}
else
{
n=0;
printf("該狀態不安全!!");
}
}
void sfp(int k){
for(int i=0;i<N;i++){
work[i]=work[i]-request[i];
allocation[k][i]+=request[i];
need[k][i]-=request[i];
}
secure();
if(n==1){
printf("給與分配!\n");
}
else if(n==0){
printf("不給與分配!\n");
}
}
int choice(int k){
int e=1;
printf("輸入選擇的量:\n"讓喚態);
while(e){
for(int i=0;i<N;i++){
printf("資源%c:",65+i);
scanf("%d",&request[i]);
}
for(int j=0;j<N;j++){
if((request[j]<=need[k][j])&&(request[j]<=work[j]))
continue;
else{
printf("資源%c申請超量!不與分配!",65+j);
return 0;
e=0;
}
}
if(j==N){
printf("可以試申請!\n");
return 1;
e=0;
}
}
}
void recover(int k){
for(int i=0;i<N;i++){
work[i]=available[i];
need[k][i]+=request[i];
allocation[k][i]-=request[i];
}
}
void main(){
int p=1,k=1,i=0,c,f;
while(k){
printf("請選擇:1:顯示進程情況;2:判斷此時狀態是否安全;3:選擇進程進行申請資源;4:結束程序!");
scanf("%d",&c);
switch(c){
case 1:showdata();break;
case 2:secure();work[0]=3;for(i=0;i<N;i++) work[i]=available[i];
break;
case 3: for(i=0;i<M;i++)
finish[i]=0;
printf("請選擇進程(0--4):\n");
scanf("%d",&f);
if(choice(f)){
sfp(f);
recover(f);
}
else{
printf("不與分配");
}
break;
case 4:k=0;
break;
default:k=0;break;
}
printf("\n");
}
}
Ⅲ C編程完成銀行家演算法的進程調度及同步模擬的實現
#include<stdio.h>
voidrukou(intjinchenggeshu,intziyuangeshu)
{
inti,j,k=0;
intzanyong[100][100];
intxuqiuzuida[100][100];
intxuyaoziyuangeshu[100][100];
intmeizhongziyuangeshu[100];
intmeizhongzuyuanshengyugeshu[100];
intjieguo[100];
intshifouanquan=0;
intkongshu;
intgaibianshuju;
intyongyouziyuanjishu;
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
{
printf(" 請輸入第%d進程現在佔用第%d種資源的個數:",i+1,j+1);
scanf("%d",&zanyong[i][j]);
}
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
{
printf(" 請輸入第%d進程現在所需最大第%d種資源的個數:",i+1,j+1);
scanf("%d",&xuqiuzuida[i][j]);
}
for(i=0;i<ziyuangeshu;i++)
{
printf(" 請輸入第%d種資源的個數:",i+1);
scanf("%d",&meizhongziyuangeshu[i]);
}
printf("如果有輸入錯誤想改變 ");
gaibian:printf("想改變佔用的數據請輸入1 ");
printf("想改變最大資源需求量的數據請輸入2 ");
printf("想改變資源中的數據請輸入3 ");
printf("退出改變數據請輸入4 ");
scanf("%d",&gaibianshuju);
if(gaibianshuju==1)
{
printf("請輸入改變哪個進程中的數據:");
scanf("%d",&i);
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&zanyong[i][j]);
gotogaibian;
}
elseif(gaibianshuju==2)
{
printf("請輸入改變哪個進程中的數據:");
scanf("%d",&i);
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&xuyaoziyuangeshu[i][j]);
gotogaibian;
}
elseif(gaibianshuju==3)
{
printf("請輸入改變哪種資源中的數據:");
scanf("%d",&j);
printf("請輸入新數據:");
scanf("%d",&meizhongziyuangeshu[j]);
gotogaibian;
}
elseif(gaibianshuju==4);
else{
printf("您選擇的無效,請從新選擇 :");
gotogaibian;
}
for(i=0;i<jinchenggeshu;i++)
for(j=0;j<ziyuangeshu;j++)
xuyaoziyuangeshu[i][j]=xuqiuzuida[i][j]-zanyong[i][j];
for(j=0;j<ziyuangeshu;j++)
{
yongyouziyuanjishu=0;
for(i=0;i<jinchenggeshu;i++)
yongyouziyuanjishu+=zanyong[i][j];
meizhongzuyuanshengyugeshu[j]=meizhongziyuangeshu[j]-yongyouziyuanjishu;
}
while(1)
{
shifouanquan++;
for(i=0;i<jinchenggeshu;i++)
{
if(zanyong[i][0]!=-1)
{
kongshu=0;
for(j=0;j<ziyuangeshu;j++)
if(meizhongzuyuanshengyugeshu[j]>=xuyaoziyuangeshu[i][j])
kongshu+=1;
if(kongshu==ziyuangeshu)
{
for(j=0;j<ziyuangeshu;j++)
meizhongzuyuanshengyugeshu[j]+=zanyong[i][j];
zanyong[i][0]=-1;
jieguo[k]=i+1;
k++;
}
}
}
if(k==jinchenggeshu)
{
printf("T0時刻是安全狀態,安全序列為:");
for(i=0;i<jinchenggeshu;i++)
printf("%d",jieguo[i]);
break;
}
if(shifouanquan>k)
{
printf("T0時刻是不安全狀態!");
break;
}
}
}
intmain()
{
intjinchenggeshu,ziyuangeshu;
printf("請輸入進程個數:");
scanf("%d",&jinchenggeshu);
printf("請輸入資源種類數:");
scanf("%d",&ziyuangeshu);
rukou(jinchenggeshu,ziyuangeshu);
return0;
}
Ⅳ c語言銀行家演算法安全性判別
把1作為參數傳給yanzheng() yanzheng(int m)
然後驗證函數里修改:
work=Avaliable;
i=m;
while(i<m)
{
if(Finish[i]==false&&Need[i]<=work)
{
work=work+Allocation[i];
Finish[i]=true;
anquan[k]=i;
k++;
i=0;
}
else
i++;
}
Ⅳ 銀行家演算法是如何實現的
銀行家演算法是從當前狀態出發,逐個按安全序列檢查各客戶中誰能完成其工作,然後假定其完成工作且歸還全部貸款,再進而檢查下一個能完成工作的客戶。如果所有客戶都能完成工作,則找到一個安全序列,銀行家才是安全的。
�7�4 與預防死鎖的幾種方法相比較,限制條件少,資源利用程度提高了。
�7�4 缺點:該演算法要求客戶數保持固定不變,這在多道程序系統中是難以做到的;該演算法保證所有客戶在有限的時間內得到滿足,但實時客戶要求快速響應,所以要考慮這個因素;由於要尋找一個安全序列,實際上增加了系統的開銷.
Banker algorithm 最重要的一點是:保證操作系統的安全狀態!這也是操作系統判斷是否分配給一個進程資源的標准!那什麼是安全狀態?舉個小例子,進程 P 需要申請 8 個資源(假設都是一樣的),已經申請了 5 個資源,還差 3 個資源。若這個時候操作系統還剩下 2 個資源。很顯然,這個時候操作系統無論如何都不能再分配資源給進程 P 了,因為即使全部給了他也不夠,還很可能會造成死鎖。若這個時候操作系統還有 3 個資源,無論 P 這一次申請幾個資源,操作系統都可以滿足他,因為操作系統可以保證 P 不死鎖,只要他不把剩餘的資源分配給別人,進程 P 就一定能順利完成任務。 為什麼銀行家演算法是可行的呢?這里需要嚴格的證明一下。不管任何時候,操作系統分配資源的時候都可以保證當前接受資源的進程不會陷入死鎖,因為操作系統總是可以滿足該進程需要的資源的。假設有 n 個進程 {p1, p2, p3, … pn} ,最後一個分配到資源的是 pi , pi 還需要 mi 個資源,假設此時操作系統還有 m 個資源剩餘。那麼很顯然 m>=mi !而且如果之後操作系統又把資源分配給其他進程了,假設是 pj , pj 還需要 mj 個資源,同理可知 m>=mj !也就是說在所有的進程中,還需要的資源數總是有小於 m 的!這樣就可以保證資源數永遠不會為 0 ,即使可能暫時性為 0 。另外,還需要保證資源數不會減少!而且,所有已經分配到資源的進程總有一天會歸還它所擁有的資源!根據操作系統再分配的時候的狀態即可判定。
Ⅵ 銀行家演算法
銀行家演算法是一種預防死鎖的演算法。具體演算法步驟可以參考網路: 銀行家演算法
例子 :某系統有A、B、C、D , 4類資源共5個進程(P0、P1、P2、P3、P4)共享,各進程對資源的需求和分配情況如下表所示。
輸入進程的數目:5
輸入資源的種類:4
輸入每個進程最多所需的各類資源數:
P0 : 0 0 1 2
P1 : 1 7 5 0
P2 : 2 3 5 6
P3 : 0 6 5 2
P4 : 0 6 5 6
輸入每個進程已經分配的各類資源數:
P0 : 0 0 1 2
P1 : 1 0 0 0
P2 : 1 3 5 4
P3 : 0 6 3 2
P4 : 0 0 1 4
請輸入各個資源現有的數目:
1 5 2 0
當前系統安全!
系統安全序列是:
P0->P2->P1->P3->P4
輸入要申請資源的進程號(0-4):1
輸入進程所請求的各資源的數量:0 4 2 0
系統安全!
系統安全序列是:
P0->P2->P1->P3->P4
同意分配請求!
系統可用的資源數為 : 1 1 0 0
各進程還需要的資源量:
進程 P0 : 0 0 0 0
進程 P1 : 0 3 3 0
進程 P2 : 1 0 0 2
進程 P3 : 0 0 2 0
進程 P4 : 0 6 4 2
各進程已經得到的資源量:
進程 P0 : 0 0 1 2
進程 P1 : 1 4 2 0
進程 P2 : 1 3 5 4
進程 P3 : 0 6 3 2
進程 P4 : 0 0 1 4
是否再次請求分配?是請按Y/y,否請按N/n:
N
Ⅶ 銀行家演算法是什麼
銀行家演算法=-- -
1. 安全狀態: 在某時刻系統中所有進程可以排列一個安全序列:{P1,P2,`````Pn},剛稱此時,系統是安全的.
所謂安全序列{P1,P2,`````Pn}是指對於P2,都有它所需要剩餘資源數量不大於系統掌握的剩餘的空間資源與所有Pi(j<i)所佔的資源之和.
2.不安全狀態可能產生死鎖.
目前狀態 最大需求 尚需
P1 3 9 6
P2 5 10 5
P3 2 4 2
在每一次進程中申請的資源,判定一下,若實際分配的話,之後系統是否安全.
3.銀行家演算法的思路:
1),進程一開始向系統提出最大需求量.
2),進程每次提出新的需求(分期貸款)都統計是否超出它事先提出的最大需求量.
3),若正常,則判斷該進程所需剩餘剩餘量(包括本次申請)是否超出系統所掌握的
剩餘資源量,若不超出,則分配,否則等待.
4.銀行家演算法的數據結構.
1),系統剩餘資源量A[n],其中A[n]表示第I類資源剩餘量.
2),各進程最大需求量,B[m][n],其中B[j][i]表示進程j對i
類資源最大需求.
3),已分配資源量C[m][n],其中C[j][i]表示系統j程已得到的第i資源的數量.
4),剩餘需求量.D[m][n],其中D[j][i]對第i資源尚需的數目.
5.銀行家演算法流程:當某時刻,某進程時,提出新的資源申請,系統作以下操作:
1),判定E[n]是否大於D[j][n],若大於,表示出錯.
2),判定E[n]是否大於系統剩餘量A[n],若大於,則該進程等待.
3),若以上兩步沒有問題,嘗試分配,即各變數作調整.
4),按照安全性推測演算法,判斷,分配過後,系統是否安全,若安全,則實際分配,否則,撤消分配,讓進程等待.
6."安全性檢測"演算法
1),先定義兩個變數,用來表示推算過程的數據.
F[n]=A[n],表示推算過程中,系統中剩餘資源量的變化.
J[n]=False表示推算過程中各進程是否假設"已完成"
2),流程:
在"剩餘"的進程中(在推算)過程中,一些進程假設已完成,查找D[j][n]<=F[n]的進程,找到後令J[j]=True
(假設該進程完成),F[n]+D[j][n](該進程所佔資源釋放),如此循環執行.
若最後,所有的F[n]=True(在推算過程中,所有進程均可以完成),則表示(分配過後)系統是安全的,否則系統是不安全的.
參考資料:http://huangqiyu.blogchina.com/419807.html
Ⅷ 求一個沒錯誤的銀行家演算法程序,c語言或C++的都行!!!!
#include<STRING.H>
#include<stdio.h>
#include<stdlib.h>
#include<CONIO.H>/*用到了getch()*/
#defineM5/*進程數*/
#defineN3/*資源數*/
#defineFALSE0
#defineTRUE1
/*M個進程對N類資源最大資源需求量*/
intMAX[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*系統可用資源數*/
intAVAILABLE[N]={10,5,7};
/*M個進程已分配到的N類數量*/
intALLOCATION[M][N]={{0,0,0},{0,0,0},{0,0,0},{0,0,0},{0,0,0}};
/*M個進程已經得到N類資源的資源量*/
intNEED[M][N]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
/*M個進程還需要N類資源的資源量*/
intRequest[N]={0,0,0};
voidmain()
{
inti=0,j=0;
charflag;
voidshowdata();
voidchangdata(int);
voidrstordata(int);
intchkerr(int);
showdata();
enter:
{
printf("請輸入需申請資源的進程號(從0到");
printf("%d",M-1);
printf("):");
scanf("%d",&i);
}
if(i<0||i>=M)
{
printf("輸入的進程號不存在,重新輸入! ");
gotoenter;
}
err:
{
printf("請輸入進程");
printf("%d",i);
printf("申請的資源數 ");
printf("類別:ABC ");
printf("");
for(j=0;j<N;j++)
{
scanf("%d",&Request[j]);
if(Request[j]>NEED[i][j])
{
printf("%d",i);
printf("號進程");
printf("申請的資源數>進程");
printf("%d",i);
printf("還需要");
printf("%d",j);
printf("類資源的資源量!申請不合理,出錯!請重新選擇! ");
gotoerr;
}
else
{
if(Request[j]>AVAILABLE[j])
{
printf("進程");
printf("%d",i);
printf("申請的資源數大於汪核態系統可用");
printf("%d",j);
printf("類資源的資源量!申請不合理,出錯!請重新選擇! ");
gotoerr;
}
}
}
}
changdata(i);
if(chkerr(i))
{
rstordata(i);
showdata();
}
else
showdata();
printf(" ");
printf("按'y'或'Y'鍵繼續,否則退出 ");
flag=getch();
if(flag=='y'||flag=='Y')
{
gotoenter;
}
else
{
exit(0);
}
}
/*顯示數組*/
voidshowdata()
{
inti,j;
printf("系統可用資源向量: ");
printf("***Available*** ");
printf("資源類別:ABC ");
printf("資源數目:");
for(j=0;j<N;j++)
{
printf("%d",AVAILABLE[j]);
}
printf(" ");
printf(" ");
printf("各進程還需要的資源量: ");
printf("******Need****** ");
printf("資源類別:ABC ");
for(i=0;i<M;i++)
{
printf(""氏行);
printf("%d",i);
printf("號進程:");
for(j=0;j<N;j++)
{
printf("%d",NEED[i][j]);
}
printf(" ");
}
printf(" ");
printf("各進程已經得到的資源量: ");
printf("***Allocation*** ");
printf("資源類別:ABC ");
for(i=0;i<困源M;i++)
{
printf("");
printf("%d",i);
printf("號進程:");
/*printf(": ");*/
for(j=0;j<N;j++)
{
printf("%d",ALLOCATION[i][j]);
}
printf(" ");
}
printf(" ");
}
/*系統對進程請求響應,資源向量改變*/
voidchangdata(intk)
{
intj;
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];
}
}
/*資源向量改變*/
voidrstordata(intk)
{
intj;
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];
}
}
/*安全性檢查函數*/
intchkerr(ints)
{
intWORK,FINISH[M],temp[M];
inti,j,k=0;
for(i=0;i<M;i++)FINISH[i]=FALSE;
for(j=0;j<N;j++)
{
WORK=AVAILABLE[j];
i=s;
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)
{
printf(" ");
printf("系統不安全!本次資源申請不成功! ");
printf(" ");
return1;
}
}
printf(" ");
printf("經安全性檢查,系統安全,本次分配成功。 ");
printf(" ");
printf("本次安全序列: ");
printf("進程依次為");
for(i=0;i<M;i++)
{
printf("%d",temp[i]);
printf("->");
}
printf(" ");
return0;
}
Ⅸ 急!銀行家演算法用C語言編寫.全部程序.
銀行家演算法
銀行家演算法是一種最有代表性的避免死鎖的演算法。
要解釋銀行家演算法,必須先解釋操作系統安全狀態和不安全狀態。
安全狀態:如果存在一個由系統中所有進程構成的安全序列P1,…,Pn,則系統處於安全狀態。安全狀態一定是沒有死鎖發生。
不安全狀態:不存在一個安全序列。不安全狀態不一定導致死鎖。
那麼什麼是安全序列呢?
安全序列:一個進程序列{P1,…,Pn}是安全的,如果對於每一個進程Pi(1≤i≤n),它以後尚需要的資源量不超過系統當前剩餘資源量與所有進程Pj (j < i )當前佔有資源量之和。
銀行家演算法:
我們可以把操作系統看作是銀行家,操作系統管理的資源相當於銀行家管理的資金,進程向操作系統請求分配資源相當於用戶向銀行家貸款。操作系統按照銀行家制定的規則為進程分配資源,當進程首次申請資源時,要測試該進程對資源的最大需求量,如果系統現存的資源可以滿足它的最大需求量則按當前的申請量分配資源,否則就推遲分配。當進程在執行中繼續申請資源時,先測試該進程已佔用的資源數與本次申請的資源數之和是否超過了該進程對資源的最大需求量。若超過則拒絕分配資源,若沒有超過則再測試系統現存的資源能否滿足該進程尚需的最大資源量,若能滿足則按當前的申請量分配資源,否則也要推遲分配。
演算法:
n:系統中進程的總數
m:資源類總數
Available: ARRAY[1..m] of integer;
Max: ARRAY[1..n,1..m] of integer;
Allocation: ARRAY[1..n,1..m] of integer;
Need: ARRAY[1..n,1..m] of integer;
Request: ARRAY[1..n,1..m] of integer;
符號說明:
Available 可用剩餘資源
Max 最大需求
Allocation 已分配資源
Need 需求資源
Request 請求資源
當進程pi提出資源申請時,系統執行下列
步驟:(「=」為賦值符號,「==」為等號)
step(1)若Request<=Need, goto step(2);否則錯誤返回
step(2)若Request<=Available, goto step(3);否則進程等待
step(3)假設系統分配了資源,則有:
Available=Available-Request;
Allocation=Allocation+Request;
Need=Need-Request
若系統新狀態是安全的,則分配完成
若系統新狀態是不安全的,則恢復原狀態,進程等待
為進行安全性檢查,定義數據結構:
Work:ARRAY[1..m] of integer;
Finish:ARRAY[1..n] of Boolean;
安全性檢查的步驟:
step (1):
Work=Available;
Finish=false;
step (2) 尋找滿足條件的i:
a.Finish==false;
b.Need<=Work;
如果不存在,goto step(4)
step(3)
Work=Work+Allocation;
Finish=true;
goto step(2)
step (4) 若對所有i,Finish=true,則系統處於安全狀態,否則處於不安全狀態
/* 銀行家演算法,操作系統概念(OS concepts Six Edition)
reedit by Johnny hagen,SCAU,run at vc6.0
*/
#include "malloc.h"
#include "stdio.h"
#include "stdlib.h"
#define alloclen sizeof(struct allocation)
#define maxlen sizeof(struct max)
#define avalen sizeof(struct available)
#define needlen sizeof(struct need)
#define finilen sizeof(struct finish)
#define pathlen sizeof(struct path)
struct allocation
{
int value;
struct allocation *next;
};
struct max
{
int value;
struct max *next;
};
struct available /*可用資源數*/
{
int value;
struct available *next;
};
struct need /*需求資源數*/
{
int value;
struct need *next;
};
struct path
{
int value;
struct path *next;
};
struct finish
{
int stat;
struct finish *next;
};
int main()
{
int row,colum,status=0,i,j,t,temp,processtest;
struct allocation *allochead,*alloc1,*alloc2,*alloctemp;
struct max *maxhead,*maxium1,*maxium2,*maxtemp;
struct available *avahead,*available1,*available2,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2;
printf("\n請輸入系統資源的種類數:");
scanf("%d",&colum);
printf("請輸入現時內存中的進程數:");
scanf("%d",&row);
printf("請輸入已分配資源矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("請輸入已分配給進程 p%d 的 %c 種系統資源:",i,'A'+j);
if(status==0)
{
allochead=alloc1=alloc2=(struct allocation*)malloc(alloclen);
alloc1->next=alloc2->next=NULL;
scanf("%d",&allochead->value);
status++;
}
else
{
alloc2=(struct allocation *)malloc(alloclen);
scanf("%d,%d",&alloc2->value);
if(status==1)
{
allochead->next=alloc2;
status++;
}
alloc1->next=alloc2;
alloc1=alloc2;
}
}
}
alloc2->next=NULL;
status=0;
printf("請輸入最大需求矩陣:\n");
for(i=0;i<row;i++)
{
for (j=0;j<colum;j++)
{
printf("請輸入進程 p%d 種類 %c 系統資源最大需求:",i,'A'+j);
if(status==0)
{
maxhead=maxium1=maxium2=(struct max*)malloc(maxlen);
maxium1->next=maxium2->next=NULL;
scanf("%d",&maxium1->value);
status++;
}
else
{
maxium2=(struct max *)malloc(maxlen);
scanf("%d,%d",&maxium2->value);
if(status==1)
{
maxhead->next=maxium2;
status++;
}
maxium1->next=maxium2;
maxium1=maxium2;
}
}
}
maxium2->next=NULL;
status=0;
printf("請輸入現時系統剩餘的資源矩陣:\n");
for (j=0;j<colum;j++)
{
printf("種類 %c 的系統資源剩餘:",'A'+j);
if(status==0)
{
avahead=available1=available2=(struct available*)malloc(avalen);
workhead=work1=work2=(struct available*)malloc(avalen);
available1->next=available2->next=NULL;
work1->next=work2->next=NULL;
scanf("%d",&available1->value);
work1->value=available1->value;
status++;
}
else
{
available2=(struct available*)malloc(avalen);
work2=(struct available*)malloc(avalen);
scanf("%d,%d",&available2->value);
work2->value=available2->value;
if(status==1)
{
avahead->next=available2;
workhead->next=work2;
status++;
}
available1->next=available2;
available1=available2;
work1->next=work2;
work1=work2;
}
}
available2->next=NULL;
work2->next=NULL;
status=0;
alloctemp=allochead;
maxtemp=maxhead;
for(i=0;i<row;i++)
for (j=0;j<colum;j++)
{
if(status==0)
{
needhead=need1=need2=(struct need*)malloc(needlen);
need1->next=need2->next=NULL;
need1->value=maxtemp->value-alloctemp->value;
status++;
}
else
{
need2=(struct need *)malloc(needlen);
need2->value=(maxtemp->value)-(alloctemp->value);
if(status==1)
{
needhead->next=need2;
status++;
}
need1->next=need2;
need1=need2;
}
maxtemp=maxtemp->next;
alloctemp=alloctemp->next;
}
need2->next=NULL;
status=0;
for(i=0;i<row;i++)
{
if(status==0)
{
finihead=finish1=finish2=(struct finish*)malloc(finilen);
finish1->next=finish2->next=NULL;
finish1->stat=0;
status++;
}
else
{
finish2=(struct finish*)malloc(finilen);
finish2->stat=0;
if(status==1)
{
finihead->next=finish2;
status++;
}
finish1->next=finish2;
finish1=finish2;
}
}
finish2->next=NULL; /*Initialization compleated*/
status=0;
processtest=0;
for(temp=0;temp<row;temp++)
{
alloctemp=allochead;
needtemp=needhead;
finishtemp=finihead;
worktemp=workhead;
for(i=0;i<row;i++)
{
worktemp1=worktemp;
if(finishtemp->stat==0)
{
for(j=0;j<colum;j++,needtemp=needtemp->next,worktemp=worktemp->next)
if(needtemp->value<=worktemp->value)
processtest++;
if(processtest==colum)
{
for(j=0;j<colum;j++)
{
worktemp1->value+=alloctemp->value;
worktemp1=worktemp1->next;
alloctemp=alloctemp->next;
}
if(status==0)
{
pathhead=path1=path2=(struct path*)malloc(pathlen);
path1->next=path2->next=NULL;
path1->value=i;
status++;
}
else
{
path2=(struct path*)malloc(pathlen);
path2->value=i;
if(status==1)
{
pathhead->next=path2;
status++;
}
path1->next=path2;
path1=path2;
}
finishtemp->stat=1;
}
else
{
for(t=0;t<colum;t++)
alloctemp=alloctemp->next;
finishtemp->stat=0;
}
}
else
for(t=0;t<colum;t++)
{
needtemp=needtemp->next;
alloctemp=alloctemp->next;
}
processtest=0;
worktemp=workhead;
finishtemp=finishtemp->next;
}
}
path2->next=NULL;
finishtemp=finihead;
for(temp=0;temp<row;temp++)
{
if(finishtemp->stat==0)
{
printf("\n系統處於非安全狀態!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\n系統處於安全狀態.\n");
printf("\n安全序列為: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
printf("\n");
return 0;
}
Ⅹ 銀行家演算法C++描述
#include <iostream>
#include <string>
#define M 3 //資源的種類數
#define N 5 //進程的個數
void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]); //統一的輸出格式
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N]);
int main()
{
int i,j;
//當前可用每類資源的資源數
int iAvailable[M]={3,3,2};
//系統中N個進程中的每一個進程對M類資源的最大需求
int iMax[N][M]={{7,5,3},{3,2,2},{9,0,2},{2,2,2},{4,3,3}};
//iNeed[N][M]每一個進程尚需的各類資源數
//iAllocation[N][M]為系統中每一類資源當前已分配給每一進程的資源數
int iNeed[N][M],iAllocation[N][M]={{0,1,1},{2,0,0},{3,0,2},{2,1,1},{0,0,2}};
//進程名
char cName[N]={'a','b','c','d','e'};
bool bExitFlag=true; //退出標記
char ch; //接收選擇是否繼續提出申請時傳進來的值
bool bSafe; //存放安全與否的標志
//計算iNeed[N][M]的值
for(i=0;i<N;i++)
for(j=0;j<M;j++)
iNeed[i][j]=iMax[i][j]-iAllocation[i][j];
//輸出初始值
output(iMax,iAllocation,iNeed,iAvailable,cName);
//判斷當前狀態是否安全
bSafe=safety(iAllocation,iNeed,iAvailable,cName);
//是否繼續提出申請
while(bExitFlag)
{
cout<<"\n"<<"繼續提出申請?\ny為是;n為否。\n";
cin>>ch;
switch(ch)
{
case 'y':
//cout<<"調用銀行家演算法";
bSafe=banker(iAllocation,iNeed,iAvailable,cName);
if (bSafe) //安全,則輸出變化後的數據
output(iMax,iAllocation,iNeed,iAvailable,cName);
break;
case 'n':
cout<<"退出。\n";
bExitFlag=false;
break;
default:
cout<<"輸入有誤,請重新輸入:\n";
}
}
}
//輸出
void output(int iMax[N][M],int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
int i,j;
cout<<"\n\t Max \tAllocation\t Need \t Available"<<endl;
cout<<"\tA B C\tA B C\tA B C\t A B C"<<endl;
for(i=0;i<N;i++)
{
cout<<cName[i]<<"\t";
for(j=0;j<M;j++)
cout<<iMax[i][j]<<" ";
cout<<"\t";
for(j=0;j<M;j++)
cout<<iAllocation[i][j]<<" ";
cout<<"\t";
for(j=0;j<M;j++)
cout<<iNeed[i][j]<<" ";
cout<<"\t";
cout<<" ";
//Available只需要輸出一次
if (i==0)
for(j=0;j<M;j++)
cout<<iAvailable[j]<<" ";
cout<<endl;
}
}
//安全性演算法,進行安全性檢查;安全返回true,並且輸出安全序列,不安全返回false,並輸出不安全的提示;
bool safety(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}
//定位ch對應的進程名在數組中的位置
//沒找見返回-1,否則返回數組下標
int locate(char cName[N],char ch)
{
int i;
for(i=0;i<N;i++)
if (cName[i]==ch) //找到
return i;
//未找到
return -1;
}
//提出申請,返回提出申請的進程名對應的下標
int request(char cName[N],int iRequest[M])
{
int i,loc;
char ch;
bool bFlag=true;
//判斷輸入的進程名是否有誤
while(bFlag)
{
//輸出進程名
for(i=0;i<N;i++)
cout<<cName[i]<<"\t";
//輸入提出申請的進程名
cout<<"\n輸入提出資源申請的進程名:\n";
cin>>ch;
//定位ch對應的進程名在進程名數組中的位置
loc=locate(cName,ch);
//沒找到,重新輸入
if (loc==-1)
cout<<"\n您輸入的進程名有誤!請重新輸入";
//找到,退出循環
else
bFlag=false;
}
//輸入提出申請的資源數
cout<<"輸入申請各類資源的數量:\n";
for(i=0;i<M;i++)
cin>>iRequest[i];
//返回提出申請的進程名對應的下標
return loc;
}
bool banker(int iAllocation[N][M],int iNeed[N][M],int iAvailable[M],char cName[N])
{
}
這個是c++的 我的報告