import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class DeadLockTest extends JFrame {
private String choose;
private int current;
private String[][] input = new String[3][3];
private int num[][] = new int[3][3];
private String[] resinput = new String[3];
private String[] srequest = new String[3];
private String[][] firstinput = new String[3][3];
private int first[][] = new int[3][3];
private int resource[] = new int[3];
private int request[][] = new int[3][3];
private int thread[] = new int[3];
private int need[][] = new int[3][3];
private int available[] = new int[3];
public DeadLockTest()
{
for( int k = 0 ; k < 3 ; k++ )
{
resinput[k] =
JOptionPane.showInputDialog("请输入第"+(k+1)+"类资源的总数量:");
resource[k] = Integer.parseInt( resinput[k] ) ;
}
for( int i = 0 ; i < 3 ; i++ )
for( int j = 0 ; j < 3 ; j++ )
{
input[i][j] =
JOptionPane.showInputDialog("请输入第个"+ (i+1) +"进程的所需要第"+ (j+1) +"类资源个数:");
num[i][j] = Integer.parseInt( input[i][j] ) ;
firstinput[i][j] =
JOptionPane.showInputDialog("请输入第个"+ (i+1) +"进程的开始有"+ (j+1) +"类的资源数:");
first[i][j] = Integer.parseInt( firstinput[i][j] ) ;
}
choose = new String();
choose =
JOptionPane.showInputDialog("输入申请资源的进程:");
current = Integer.parseInt( choose ) - 1 ;
for( int r = 0 ; r < 3 ; r++ )
{
srequest[r] =
JOptionPane.showInputDialog("请输入首次申请第"+(r+1)+"类资源的数量:");
request[current][r] = Integer.parseInt( srequest[r] ) ;
}
for ( int n = 0 ; n < 3 ; n++ )
for( int m = 0; m< 3 ; m++ )
need[n][m] = num[n][m] - first[n][m];
available[0] = resource[0] - ( first[0][0] + first[1][0] + first[2][0]);
available[1] = resource[1] - ( first[0][1] + first[1][1] + first[2][1]);
available[2] = resource[2] - ( first[0][2] + first[1][2] + first[2][2]);
}
public void run()
{
if( request[current][0] <= need[current][0]&&request[current][1] <= need[current][1]&&request[current][2] <= need[current][2])
{
if( request[current][0] <= available[0]&&request[current][1] <= available[1]&&request[current][2] <= available[2])
{
for(int i = 0 ; i < 3 ;i++ )
{
available[i] = available[i] - request[current][i];
first[current][i] = first[current][i] + request[current][i];
need[current][i] = need[current][i] - request[current][i];
}
if( !issafe() )
{
JOptionPane.showMessageDialog( null , "安全序列为;" + thread[current] + thread[1]+ thread[2]);
}
}
else
JOptionPane.showMessageDialog( null , " 无法分配等待:");
}
else
JOptionPane.showMessageDialog( null , "wrong" );
}
public boolean issafe()
{
int work[] = new int[3];
boolean finish[] = new boolean[3];
for( int i = 0 ; i < 3 ; i++ )
{
work[i] = available[i];
finish[i] = 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])
{
work[0] = work[0] + first[i][0];
work[1] = work[1] + first[i][1];
work[2] = work[2] + first[i][2];
finish[i] = true;
thread[i+1] = i;
}
else break;
}
if( finish[0] == true && finish[0] == true && finish[0] == true )
return true;
else
return false;
}
public static void main( String args[] )
{
DeadLockTest app = new DeadLockTest();
app.run();
}
}