當前位置:首頁 » 操作系統 » 銀行家演算法的實現

銀行家演算法的實現

發布時間: 2022-12-19 15:32:34

A. 銀行家演算法的演算法實現

在避免死鎖的方法中,所施加的限制條件較弱,有可能獲得令人滿意的系統性能。在該方法中把系統的狀態分為安全狀態和不安全狀態,只要能使系統始終都處於安全狀態,便可以避免發生死鎖。
銀行家演算法的基本思想是分配資源之前,判斷系統是否是安全的;若是,才分配。它是最具有代表性的避免死鎖的演算法。
設進程cusneed提出請求REQUEST [i],則銀行家演算法按如下規則進行判斷。
(1)如果REQUEST [cusneed] [i]<= NEED[cusneed][i],則轉(2);否則,出錯。
(2)如果REQUEST [cusneed] [i]<= AVAILABLE[i],則轉(3);否則,等待。
(3)系統試探分配資源,修改相關數據:
AVAILABLE[i]-=REQUEST[cusneed][i];
ALLOCATION[cusneed][i]+=REQUEST[cusneed][i];
NEED[cusneed][i]-=REQUEST[cusneed][i];
(4)系統執行安全性檢查,如安全,則分配成立;否則試探險性分配作廢,系統恢復原狀,進程等待。 (1)設置兩個工作向量Work=AVAILABLE;FINISH
(2)從進程集合中找到一個滿足下述條件的進程,
FINISH==false;
NEED<=Work;
如找到,執行(3);否則,執行(4)
(3)設進程獲得資源,可順利執行,直至完成,從而釋放資源。
Work=Work+ALLOCATION;
Finish=true;
GOTO 2
(4)如所有的進程Finish= true,則表示安全;否則系統不安全。
銀行家演算法流程圖
演算法(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();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()){rstordata(i);showdata();}elseshowdata();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()//在假定分配資源的情況下檢查系統的安全性{intWORK[N],FINISH[M],temp[M];//temp[]用來記錄進程安全執行的順序inti,j,m,k=0,count;for(i=0;i<M;i++)FINISH[i]=FALSE;for(j=0;j<N;j++)WORK[j]=AVAILABLE[j];//把可利用資源數賦給WORK[]for(i=0;i<M;i++){count=0;for(j=0;j<N;j++)if(FINISH[i]==FALSE&&NEED[i][j]<=WORK[j])count++;if(count==N)//當進程各類資源都滿足NEED<=WORK時{for(m=0;m<N;m++)WORK[m]=WORK[m]+ALLOCATION[i][m];FINISH[i]=TRUE;temp[k]=i;//記錄下滿足條件的進程k++;i=-1;}}for(i=0;i<M;i++)if(FINISH[i]==FALSE){printf(系統不安全!!!本次資源申請不成功!!! );return1;}printf( );printf(經安全性檢查,系統安全,本次分配成功。 );printf( );printf(本次安全序列:);for(i=0;i<M;i++)//列印安全系統的進程調用順序{printf(進程);printf(%d,temp[i]);if(i<M-1)printf(->);}printf( );return0;}

B. 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. 求java語言的銀行家演算法

銀行家演算法

一.程序說明:

本演算法有3個進程,3類資源。初始可用資源向量為Available{10,8,7},然後設置各進程的最大需求矩陣MAX以及分配矩陣Alloction,由此算出需求矩陣Need。然後判斷當前系統資源分配是否處於安全狀態,否則結束進程。最後,在當前分配資源後的系統安全時,選擇一進程,並請求各類所需資源矩陣Request,嘗試分配並修改資源分配狀況,判斷此進程請求是否該分配,進而進入安全演算法判斷是否能形成一個安全序列,如果有則分配,否則不分配。

二.程序代碼:

演算法類:

package bankerclass;

import java.util.Scanner;

public class BankerClass {

int[] Available = {10, 8, 7};

int[][] Max = new int[3][3];

int[][] Alloction = new int[3][3];

int[][] Need = new int[3][3];

int[][] Request = new int[3][3];

int[] Work = new int[3];

int num = 0;//進程編號

Scanner in = new Scanner(System.in);

public BankerClass() {

// Max={{6,3,2},{5,6,1},{2,3,2}};

}

public void setSystemVariable(){//設置各初始系統變數,並判斷是否處於安全狀態。

setMax();

setAlloction();

printSystemVariable();

SecurityAlgorithm();

}

public void setMax() {//設置Max矩陣

System.out.println("請設置各進程的最大需求矩陣Max:");

for (int i = 0; i < 3; i++) {

System.out.println("請輸入進程P" + i + "的最大資源需求量:");

for (int j = 0; j < 3; j++) {

Max[i][j] = in.nextInt();

}

}

}

public void setAlloction() {//設置已分配矩陣Alloction

System.out.println("請設置請各進程分配矩陣Alloction:");

for (int i = 0; i < 3; i++) {

System.out.println("晴輸入進程P" + i + "的分配資源量:");

for (int j = 0; j < 3; j++) {

Alloction[i][j] = in.nextInt();

}

}

System.out.println("Available=Available-Alloction.");

System.out.println("Need=Max-Alloction.");

for (int i = 0; i < 3; i++) {//設置Alloction矩陣

for (int j = 0; j < 3; j++) {

Available[i] = Available[i] - Alloction[j][i];

}

}

for (int i = 0; i < 3; i++) {//設置Need矩陣

for (int j = 0; j < 3; j++) {

Need[i][j] = Max[i][j] - Alloction[i][j];

}

}

}

public void printSystemVariable(){

System.out.println("此時資源分配量如下:");

System.out.println("進程 "+" Max "+" Alloction "+" Need "+" Available ");

for(int i=0;i<3;i++){

System.out.print("P"+i+" ");

for(int j=0;j<3;j++){

System.out.print(Max[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

if(i==0){

for(int j=0;j<3;j++){

System.out.print(Available[j]+" ");

}

}

System.out.println();

}

}

public void setRequest() {//設置請求資源量Request

System.out.println("請輸入請求資源的進程編號:");

num= in.nextInt();//設置全局變數進程編號num

System.out.println("請輸入請求各資源的數量:");

for (int j = 0; j < 3; j++) {

Request[num][j] = in.nextInt();

}

System.out.println("即進程P" + num + "對各資源請求Request:(" + Request[num][0] + "," + Request[num][1] + "," + Request[num][2] + ").");

BankerAlgorithm();

}

public void BankerAlgorithm() {//銀行家演算法

boolean T=true;

if (Request[num][0] <= Need[num][0] && Request[num][1] <= Need[num][1] && Request[num][2] <= Need[num][2]) {//判斷Request是否小於Need

if (Request[num][0] <= Available[0] && Request[num][1] <= Available[1] && Request[num][2] <= Available[2]) {//判斷Request是否小於Alloction

for (int i = 0; i < 3; i++) {

Available[i] -= Request[num][i];

Alloction[num][i] += Request[num][i];

Need[num][i] -= Request[num][i];

}

} else {

System.out.println("當前沒有足夠的資源可分配,進程P" + num + "需等待。");

T=false;

}

} else {

System.out.println("進程P" + num + "請求已經超出最大需求量Need.");

T=false;

}

if(T==true){

printSystemVariable();

System.out.println("現在進入安全演算法:");

SecurityAlgorithm();

}

}

public void SecurityAlgorithm() {//安全演算法

boolean[] Finish = {false, false, false};//初始化Finish

int count = 0;//完成進程數

int circle=0;//循環圈數

int[] S=new int[3];//安全序列

for (int i = 0; i < 3; i++) {//設置工作向量

Work[i] = Available[i];

}

System.out.println("進程 "+" Work "+" Alloction "+" Need "+"Work+Available ");

while (count < 3) {

for (int i = 0; i < 3; i++) {

if (Finish[i]==false&&Need[i][0]<=Work[0]&&Need[i][1]<=Work[1]&&Need[i][2]<=Work[2]) {//判斷條件

System.out.print("P"+i+" ");

for (int k = 0; k < 3; k++){

System.out.print(Work[k]+" ");

}

System.out.print("| ");

for (int j = 0; j<3;j++){

Work[j]+=Alloction[i][j];

}

Finish[i]=true;//當當前進程能滿足時

S[count]=i;//設置當前序列排號

count++;//滿足進程數加1

for(int j=0;j<3;j++){

System.out.print(Alloction[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Need[i][j]+" ");

}

System.out.print("| ");

for(int j=0;j<3;j++){

System.out.print(Work[j]+" ");

}

System.out.println();

}

}

circle++;//循環圈數加1

if(count==3){//判斷是否滿足所有進程需要

System.out.print("此時存在一個安全序列:");

for (int i = 0; i<3;i++){//輸出安全序列

System.out.print("P"+S[i]+" ");

}

System.out.println("故當前可分配!");

break;//跳出循環

}

if(count<circle){//判斷完成進程數是否小於循環圈數

count=5;

System.out.println("當前系統處於不安全狀態,故不存在安全序列。");

break;//跳出循環

}

}

}

}

主類:

package bankerclass;

import java.util.Scanner;

public class TestBankerClass {

public static void main(String[] args) {

// TODO code application logic here

boolean Choose = true;

String C;

Scanner in = new Scanner(System.in);

BankerClass T = new BankerClass();

System.out.println("這是一個三個進程,初始系統可用三類資源為{10,8,7}的銀行家演算法:");

T.setSystemVariable();

while (Choose == true) {

T.setRequest();

System.out.println("您是否還要進行請求:y/n?");

C = in.nextLine();

if (C.endsWith("n")) {

Choose = false;

}

}

}

}

三.隨機運行過程

1.

run:

這是一個三個進程,初始系統可用三類資源為{10,8,7}的銀行家演算法:

請設置各進程的最大需求矩陣Max:

請輸入進程P0的最大資源需求量:

8 7 5

請輸入進程P1的最大資源需求量:

5 2 5

請輸入進程P2的最大資源需求量:

6 6 2

請設置請各進程分配矩陣Alloction:

晴輸入進程P0的分配資源量:

3 2 0

晴輸入進程P1的分配資源量:

2 0 2

晴輸入進程P2的分配資源量:

1 3 2

Available=Available-Alloction.

Need=Max-Alloction.

此時資源分配量如下:

進程 Max Alloction Need Available

P0 8 7 5 | 3 2 0 | 5 5 5 | 4 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

進程 Work Alloction Need Work+Available

P1 4 3 3 | 2 0 2 | 3 2 3 | 6 3 5

P2 6 3 5 | 1 3 2 | 5 3 0 | 7 6 7

P0 7 6 7 | 3 2 0 | 5 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故當前可分配!

請輸入請求資源的進程編號:

0

請輸入請求各資源的數量:

1 0 0

即進程P0對各資源請求Request:(1,0,0).

此時資源分配量如下:

進程 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 3 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 3 2 | 5 3 0 |

現在進入安全演算法:

進程 Work Alloction Need Work+Available

P1 3 3 3 | 2 0 2 | 3 2 3 | 5 3 5

P2 5 3 5 | 1 3 2 | 5 3 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故當前可分配!

您是否還要進行請求:y/n?

y

請輸入請求資源的進程編號:

2

請輸入請求各資源的數量:

0 1 0

即進程P2對各資源請求Request:(0,1,0).

此時資源分配量如下:

進程 Max Alloction Need Available

P0 8 7 5 | 4 2 0 | 4 5 5 | 3 2 3

P1 5 2 5 | 2 0 2 | 3 2 3 |

P2 6 6 2 | 1 4 2 | 5 2 0 |

現在進入安全演算法:

進程 Work Alloction Need Work+Available

P1 3 2 3 | 2 0 2 | 3 2 3 | 5 2 5

P2 5 2 5 | 1 4 2 | 5 2 0 | 6 6 7

P0 6 6 7 | 4 2 0 | 4 5 5 | 10 8 7

此時存在一個安全序列:P1 P2 P0 故當前可分配!

您是否還要進行請求:y/n?

n

成功生成(總時間:1 分鍾 38 秒)

D. 在C++中,編寫的銀行家演算法中有以下的語句,麻煩幫忙解釋這3個語句,並用java實現,怎麼實現,謝謝先!!

在某時刻系統中所有進程可以排列一個安全序列:,剛稱此時,系統是安全的.
所謂安全序列是指對於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(在推算過程中,所有進程均可以完成),則表示(分配過後)系統是安全的,否則系統是不安全的.

#include "malloc.h"
#include "stdio.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,*availabletemp,*workhead,*work1,*work2,*worktemp,*worktemp1;
struct need *needhead,*need1,*need2,*needtemp;
struct finish *finihead,*finish1,*finish2,*finishtemp;
struct path *pathhead,*path1,*path2,*pathtemp;
char c;
printf("\nPlease enter the type of sources the system has:\n");
scanf("%d",&colum);
printf("Please enter the number of processes now in the memory:\n");
scanf("%d",&row);
printf("Please enter the allocation array:\n");
for(i=0;i<row;i++)
{
printf("The allocation for process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c system resource allocated:\n",'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("Please enter the max array:\n");
for(i=0;i<row;i++)
{
printf("The max needed from process p%d:\n",i);
for (j=0;j<colum;j++)
{
printf("The type %c maxium system resource may needed:\n",'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("Please enter the available array now exists in the system:\n");
for (j=0;j<colum;j++)
{
printf("The type %c available system resource number:\n",'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->value==0)
{
printf("\nWARNING,the system is in nonsafe status!\n");
exit(0);
}
finishtemp=finishtemp->next;
}
printf("\nThe system is in safe status!\n");
printf("\nThe safe sequence is: \n");
do
{
printf("p%d ",pathhead->value);
}
while(pathhead=pathhead->next);
}

E. 淺析銀行家演算法

作為避免死鎖的一種演算法,銀行家演算法可以說是最為出名的了。這個名字的來源是因為該演算法起初是為銀行系統設計的,以確保銀行在發放現金貸款時,不會發生不能滿足所有客戶需要的情況。在操作系統中也可以用它來實現避免死鎖。

首先我們要了解銀行家演算法的本質也即避免死鎖的原理。避免死鎖作為一種事先預防死鎖的策略,原理是在為各個進程分配資源的過程中不允許系統進去不安全狀態,以此來避免死鎖的發生。所謂安全狀態,是指系統能按某種進程推進順序為每個進程分配其所需資源,直至滿足每個進程對資源的最大需求,使每個進程都可以順利地完成。此時稱該進程推進序列為安全序列,如果無法找到這樣一個安全序列,則稱系統處於不安全狀態。

銀行家演算法中的數據結構。為了實現銀行家演算法,在系統中必須設置這樣四個數據結構,分別用來描述系統中可利用的資源,所有進程對資源的最大需求,系統中的資源分配以及所有進程還需要多少資源的情況。

(1)可利用資源向量Available。這是一個含有m個元表的數組,其中的每一個元素代表一類可利用的資源數目。其數值隨該類資源的分配和回收而動態地改變。如果 Available=K,則表示系統中現有Rj類資源K個。

    (2)最大需求矩陣Max。這是一個nxm的矩陣,它定義了系統中n個進程中的每個進程對m類資源的最大需求。如果Max[i,j]=K,則表示進程i需要Rj類資源的最大數目為K。

    (3)分配矩陣 Allocation。這也是一個nxm的矩陣,它定義了系統中每一類資源當前已分配給每一進程的資源數。如果Allocation[i,j]=K,表示進程i當前已分得Rj類資源的數目為K。

    (4)需求矩陣Need。這也是一個nxm的矩陣,用以表示每一個進程尚需的各類資源數。如果Need[i,j]=K,則表示進程i還需要Rj類資源K個才能完成。

當一個進程發出請求資源的請求後,如果它所請求的資源大於目前系統可利用資源則不予分配。如果小於可利用資源,則系統試探著把資源分配給該進程,並修改分配之後的資源數值。接著系統執行安全演算法,檢查此次資源分配後系統是否處於安全狀態。若安全,才正式將資源分配給該進程,以完成本次分配。否則,將本次的試探分配作廢,恢復原來的資源分配狀態,讓該進程等待。

F. 銀行家演算法的實現,安全性演算法中 這條語句是什麼意思Work[j]∶=Work[i]+Allocation[i,j];

work[j]表示當前系統可用的第j類資源,Allocation[i][j]表示當前已經分配給進程i使用的第j類資源數量。

Work[j]= Work[j]+ Allocation[i][j]

這句的意思是目前進程已經利用手上資源完成相關工作了,這些已分配的資源可以重新歸還系統了,所以系統可用的第j類資源work[j]就增加了,增加量就是當前進程想要歸還的資源量Allocation[i][j]

如有疑惑歡迎追問!

G. 銀行家演算法

什麼是銀行家演算法:
銀行家演算法是一種最有代表性的避免死鎖的演算法。在避免死鎖方法中允許進程動態地申請資源,但系統在進行資源分配之前,應先計算此次分配資源的安全性,若分配不會導致系統進入不安全狀態,則分配,否則等待。為實現銀行家演算法,系統必須設置若干數據結構。

要解釋銀行家演算法,必須先解釋操作系統安全狀態和不安全狀態。
安全序列是指一個進程序列{P1,…,Pn}是安全的,如果對於每一個進程Pi(1≤i≤n),它以後尚需要的資源量不超過系統當前剩餘資源量與所有進程Pj (j < i )當前佔有資源量之和。
安全狀態
如果存在一個由系統中所有進程構成的安全序列P1,…,Pn,則系統處於安全狀態。安全狀態一定是沒有死鎖發生。
不安全狀態
不存在一個安全序列。不安全狀態不一定導致死鎖。
原理:
我們可以把操作系統看作是銀行家,操作系統管理的資源相當於銀行家管理的資金,進程向操作系統請求分配資源相當於用戶向銀行家貸款。
為保證資金的安全,銀行家規定:
(1) 當一個顧客對資金的最大需求量不超過銀行家現有的資金時就可接納該顧客;
(2) 顧客可以分歧貸款,但貸款的總數不能超過最大需求量;
(3) 當銀行家現有的資金不能滿足顧客尚需的貸款數額時,對顧客的貸款可推遲支付,但總能使顧客在有限的時間里得到貸款;
(4) 當顧客得到所需的全部資金後,一定能在有限的時間里歸還所有的資金.
操作系統按照銀行家制定的規則為進程分配資源,當進程首次申請資源時,要測試該進程對資源的最大需求量,如果系統現存的資源可以滿足它的最大需求量則按當前的申請量分配資源,否則就推遲分配。當進程在執行中繼續申請資源時,先測試該進程已佔用的資源數與本次申請的資源數之和是否超過了該進程對資源的最大需求量。若超過則拒絕分配資源,若沒有超過則再測試系統現存的資源能否滿足該進程尚需的最大資源量,若能滿足則按當前的申請量分配資源,否則也要推遲分配。
程序舉例:
已知進程{P0,P1,P2,P3,P4},有三類系統資源A、B、C的數量分別為10、5、7,在T0時刻的資源
(1)若進程P1請求資源,發出請求向量Request1(1,0,2),編寫程序用銀行家演算法判斷系統能否將資源分配給它;
(2)若進程P2提出請求Request(0,1,0),用銀行家演算法程序驗證系統能否將資源分配給它。
程序代碼:
P1進程提出的請求,可以分配。
P2進程不能分配,因為請求的B類資源超過了它的最大值。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAXSIZE 50
void main()
{
unsigned int Available[MAXSIZE]; //可利用資源向量
unsigned int Max[MAXSIZE][MAXSIZE]; //最大需求矩陣
unsigned int Allocation[MAXSIZE][MAXSIZE]; //已分配矩陣
unsigned int Need[MAXSIZE][MAXSIZE]; //需求矩陣
unsigned int Request[MAXSIZE]; //請求向量
unsigned int Work[MAXSIZE]; //工作向量
bool Finish[MAXSIZE]; //是否有足夠資源分配給進程,使之運行完成
unsigned int SafeSequence[MAXSIZE]; //安全序列

int i,j;
int p; //請求資源的進程的下標
int temp = 0; //安全序列下標
int total = 0;
int N;
int M;

printf("請輸入進程數N=");
scanf("%d",&N);
printf("請輸入資源種類數M=");
scanf("%d",&M);

//用戶輸入數據,初始化Available數組
printf("初始化可用資源數組:\n");
for(i=0; i<M; i++)
{
printf("\t%c類資源:",65+i);
scanf("%d",&Available[i]);
}

//用戶輸入數據,初始化Max數組
printf("初始化最大需求數組:\n");
for(i=0; i<N; i++)
{
printf("\tP%d進程最大需要\n",i);
for(j=0; j<M; j++)
{
printf("\t\t%c類資源:",65+j);
scanf("%d",&Max[i][j]);
}
}

//用戶輸入數據,初始化Allocation數組
printf("初始化已分配資源數組:\n");
for(i=0; i<N; i++)
{
printf("\tP%d進程已分配\n",i);
for(j=0; j<M; j++)
{
printf("\t\t%c類資源:",65+j);
scanf("%d",&Allocation[i][j]);
}
}

//初始化Need數組
for(i=0; i<N; i++)
for(j=0; j<M; j++)
{
Need[i][j] = Max[i][j] - Allocation[i][j];
}

//進程發出資源請求後檢查
do
{
printf("資源請求:\n");
printf("\t輸入請求資源的進程下標:");
scanf("%d",&p);
printf("\t進程P%d請求\n",p);
//初始化請求向量
for(i=0; i<M; i++)
{
printf("\t\t%c類資源:",65+i);
scanf("%d",&Request[i]);
}
for(i=0; i<M; i++) //檢查Request <= Need ?
if(Request[i] > Need[p][i])
{
printf("\t請求的%c類資源數超過它所宣布的最大值!\n",65+i);
break;
}
if(i == M) //通過上層檢查,繼續檢查Request <= Available ?
{
for(i=0; i<M; i++)
if(Request[i] > Available[i])
{
printf("\t尚無足夠%c類資源,P%d須等待!\n",65+i,p);
break;
}
}
if(i == M) //嘗試分配
{
for(i=0; i<M; i++)
{
Available[i] -= Request[i];
Allocation[p][i] += Request[i];
Need[p][i] -= Request[i];
}

}
}while(i<M);

//初始化Work,Finish向量
for(i=0; i<M; i++)
{
Work[i] = Available[i];
}
for(i=0; i<N; i++)
{
Finish[i] = false;
}

//安全性演算法
do
{
total = temp;
for(i=0; i<N; i++)
{
if(Finish[i] == false)
{
for(j=0; j<M; j++)
if(Need[i][j] > Work[j])
{
break;
}
if(j == M) //各類資源都滿足Need <= Work
{
for(j=0; j<M; j++)
{
Work[j] += Allocation[i][j]; //釋放資源
}
Finish[i] = true;
SafeSequence[temp++] = i; //加入安全序列
}
}
}
}while(total != temp); //所有進程檢查一遍之後,如果安全序列有變化,則進行下一輪
//否則說明所有的Finish都為true,或者因沒有安全序列退出循環

if(temp == N)
{
printf("安全序列:");
for(temp=0; temp<N; temp++)
{
printf("P%d ",SafeSequence[temp]);
}
}
else
{
printf("系統處於不安全狀態!不能分配!\n");
}
getchar();
getchar();
}
這個程序還行,輸入有點麻煩,我自己編寫的是用文件輸入系統描述信息的,但是缺少說明,怕你搞不明白。希望對你有所幫助!

H. Java語言期末課程設計「操作系統中銀行家演算法的實現」

有電子版可以參考。

I. 編程實現銀行家演算法,最好可以告訴我方法,非常感謝。

#include<stdio.h>
#define N 4
#define M 5
main()
{
int maxc[M][N]={3,2,1,4,0,2,5,2,5,1,0,5,1,5,3,0,3,0,3,3};
int availc[M][N]={2,0,1,1,0,1,2,1,4,0,0,3,0,2,1,0,1,0,3,0};
int c[N]={8,5,9,7};
int avail[N]={0};
int i=0,j=0,k[M]={-1,-1,-1,-1,-1},kk=0;
int flag=0;
int flag2=0;
/*The max need table*/
printf("nt The max need table");
printf("n process ");
for(i=0;i<N;i++)
{printf("tR[%d]",i);
}
for(i=0;i<M;i++)
{printf("n p[%d]",i);
for(j=0;j<N;j++)
{printf("t%d",maxc[i][j]);
}
}
/*the rest resource*/
for(j=0;j<N;j++)
{
for(i=0;i<M;i++)
{avail[j]+=availc[i][j];}
avail[j]=c[j]-avail[j];

}
/*The alloc table*/
printf("nnt The allocation table");
printf("n process ");
for(i=0;i<N;i++)
{printf("tR[%d]",i);
}
for(i=0;i<M;i++)
{printf("n p[%d]",i);
for(j=0;j<N;j++)
{printf("t%d",availc[i][j]);
}
}
/*the avail table*/
printf("nn avail");
for(j=0;j<N;j++)
{printf("t%d",avail[j]);}

i=0,j=0;
while(i<M)
{
for(j=0;j<M;j++)
{if(k[j]==i)
{flag=1;i++;break;}
}
if(flag==1)
{flag=0;continue;}

for(j=0;j<N;j++)
{if(avail[j]<maxc[i][j]-availc[i][j])
{flag2=1;i++;j=0;break;}
}
if(flag2==1)
{flag2=0;continue;}

k[kk]=i;

for(j=0;j<N;j++)
{avail[j]+=availc[k[kk]][j];}

/*the avail table*/
printf("n after p[%d]",i);
for(j=0;j<N;j++)
{printf("t%d",avail[j]);}

if(kk==M-1)
{printf("nThis is safe!n");
for(j=0;j<M-1;j++)
{printf("Process[%d]-->",k[j]);}
printf("Process[%d]n",k[M-1]);
break;
}
kk++;
i=0;
}

if(i==M)
printf("nthis is no safe alloc!");

getchar();
}

J. 第三章 銀行家演算法

(1)各類可利用資源的數量

u向量Available:(i1,i2,…,im),含m個元素,每個元素代表一類可利用的資源數目。

u動態變化的,初始值是系統配置的該類資源的全部數目,值隨資源的分配與回收而動態的改變。

u實現:一維數組。Available【j】=K,表示系統中Rj類資源現有可用數量為K個。

(2)每個進程對每類資源的需求

最大需求、已獲得的、還需要的

v最大需求矩陣Max

vn*m,系統中n個進程中每個進程分別對m類資源的最大需求。

v取值:根據進程需求賦初始值。

v實現:二維數組。Max【i,j】=K,表示進程 i

需要Rj類資源的最大數目為K。

演算法過程:

就是對各進程的Request向量及資源數量進行一系列判斷及值操作。

進程Pi發出資源請求後,系統按下述步驟進行檢查:

首先是兩個基本判斷:

(1)IF Requesti[j]<= Need[i,j]

  THEN轉向步驟2;

  ELSE認為出錯,所需資源數超過宣布的最大值(自我矛盾)

(2)IF Requesti[j]<= Available[j]

  THEN轉向步驟3;

  ELSE表示尚無足夠資源,Pi需等待(現實不滿足)

如果上面兩步判斷都通過了,進入實質的資源分析

(3)系統試探著把資源分配給進程Pi

,並修改相應數據結構的值(假設性操作):

     Available【j】  =

     Allocation【i,j】=

     Need【i,j】=

(4)系統執行安全性演算法,判斷新的資源分配狀態是否是安全的。

  即:找一個安全序列,使這些進程按順序執行完)如果能夠找到,則將假設操作真正實施完成資源分配。

(1)需要一些記錄信息的數據結構,設置兩個向量:

v工作向量work

演算法開始時work=Available;

系統找安全序列的過程需要不斷判斷和修改當前資源數量,不能直接修改原始數據記錄Aailable。

v標志向量Finish

表示每個進程是否有足夠的資源使之運行完成。開始時所以進程都設置初值Finish[i]:=false;

找安全序列的過程相當於使所有Finish[i]:=true。

(2)找安全序列的過程

  從Finish[i] = false 的進程集合中找一個進程

  IFNeed[i,j] <= work[j]

  THEN  執行步驟 a;

  ELSE  執行步驟 b;

  a)假設Pi獲得資源順利執行完,釋放出分配給它的資源,修改相應的值:

    work【j】  = work【i】+Allocation【i,j】;

    Finish【i】= true;

    goto step (2);//返回去繼續找下一個進程。

  b)當演算法不再在(2)、a)步間循環找進程,到達本步時,若所有Finish[i]=true都滿足,則表示所有進程都按某個順序執行完了,系統處於安全狀態;否則,系統當前所處的資源分配狀態是不安全狀態。

熱點內容
搭建httpsgit伺服器搭建 發布:2025-05-14 13:09:47 瀏覽:253
新電腦拿回來我該怎麼配置 發布:2025-05-14 13:09:45 瀏覽:238
視頻伺服器新建ftp用戶 發布:2025-05-14 13:03:09 瀏覽:225
php花生 發布:2025-05-14 12:54:30 瀏覽:550
java人才 發布:2025-05-14 12:29:10 瀏覽:649
如何打開軟密碼 發布:2025-05-14 12:28:55 瀏覽:427
七牛存儲待遇 發布:2025-05-14 12:27:20 瀏覽:422
C語言a35a4a5 發布:2025-05-14 11:53:48 瀏覽:814
android隱藏item 發布:2025-05-14 11:43:56 瀏覽:328
javawebeclipse編譯 發布:2025-05-14 11:35:24 瀏覽:938