操作系统银行家算法 Java实现
package bankerTest;
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 ( ) {
}
public void setSystemVariable ( ) {
setMax ( ) ;
setAlloction ( ) ;
printSystemVariable ( ) ;
SecurityAlgorithm ( ) ;
}
public void setMax ( ) {
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 ( ) {
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++ ) {
for ( int j = 0 ; j < 3 ; j++ ) {
Available[ i] = Available[ i] - Alloction[ j] [ i] ;
}
}
for ( int i = 0 ; i < 3 ; i++ ) {
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 ( ) {
System. out. println ( "请输入请求资源的进程编号:" ) ;
num= in. nextInt ( ) ;
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 ] ) {
if ( Request[ num] [ 0 ] <= Available[ 0 ] && Request[ num] [ 1 ] <= Available[ 1 ] && Request[ num] [ 2 ] <= Available[ 2 ] ) {
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 } ;
int count = 0 ;
int circle= 0 ;
int [ ] S= new int [ 3 ] ;
for ( int i = 0 ; i < 3 ; i++ ) {
Work[ i] = Available[ i] ;
}
boolean flag = true ;
while ( count < 3 ) {
if ( flag) {
System. out. println ( "进程 " + " Work " + " Alloction " + " Need " + " Work+Alloction " ) ;
flag = false ;
}
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++ ;
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++ ;
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 ;
}
}
}
}
public class TestBankerClass {
public static void main ( String[ ] args) {
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 ;
}
}
}
}