Java中的死锁检测算法程序



我一直在尝试用java语言编写一个用于死锁检测算法的程序,但是当我输入应该产生死锁的数据时,程序总是输出"没有死锁发生"。我是初学者,如果有人能告诉我我的代码出了什么问题,我将不胜感激。

这是我的代码:

package org.me.deadlockdetection;
import java.applet.Applet;
import java.awt.*;
import java.util.Scanner;
public class DeadlockDetection{
private int processes, resources, max[][], allocate[][],need[][], available[];
public static void main(String[] args) {
System.out.println("******* Deadlock detection algorithm *******");
new DeadlockDetection().input();
new DeadlockDetection().show();
new DeadlockDetection().cal();
}
public void input(){
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of processes: ");
processes=sc.nextInt();  //no. of process
System.out.print("Enter no. of resources: ");
resources=sc.nextInt();  //no. of resources
need=new int[processes][resources];  //initializing arrays
max=new int[processes][resources];
allocate=new int[processes][resources];
available=new int[resources];
System.out.println("Enter allocation matrix -->");
for(int i=0;i<processes;i++)
for(int j=0;j<resources;j++)
allocate[i][j]=sc.nextInt();  //allocation matrix
System.out.println("Enter max matrix -->");
for(int i=0;i<processes;i++)
for(int j=0;j<resources;j++)
max[i][j]=sc.nextInt();  //max matrix
System.out.println("Enter available matrix -->");
for(int j=0;j<resources;j++)
available[j]=sc.nextInt();  //available matrix
sc.close();          
}
public void show(){
//int i,j;
System.out.println("Processes     Allocation     Max     Available");
for(int i=0;i<processes;i++){
System.out.println("P"+(i+1));
System.out.println("       ");
for(int j=0;j<resources;j++){
System.out.println("  "+allocate[i][j]);
}
System.out.println("       ");
for(int j=0;j<resources;j++){
System.out.println("  "+max[i][j]);
}
System.out.println("       ");
if(i==0){
for(int j=0;j<resources;j++){
System.out.println("  "+available[j]); 
}
}
}
}
public void cal(){
int finish[],temp,flag=1, k,c1=0;
int dead[];
int safe[];
int i,j;
finish=new int[100];
dead=new int[100];
safe=new int[100];
for(i=0;i<processes;i++)
{
finish[i]=0;
}
//find need matrix
for(i=0;i<processes;i++)
{
for(j=0;j<resources;j++)
{
need[i][j]=max[i][j]-allocate[i][j];
}
}
while(flag==1)
{
flag=0;
for(i=0;i<processes;i++)
{
int c=0;
for(j=0;j<resources;j++)
{
if((finish[i]==0)&&(need[i][j]<=available[j]))
{
 c++;
 if(c==resources)
 {
                for(k=0;k<resources;k++)
                {
                               available[k]+=allocate[i][j];
                               finish[i]=1;
                               flag=1;
                }
                 if(finish[i]==1)
                {
                               i=processes;
                  }
}
}
}
}
}
j=0;
flag=0;
for(i=0;i<processes;i++)
{
if(finish[i]==0)
{
dead[j]=i;
j++;
flag=1;
}
}
if(flag==1)
{
System.out.println("nnSystem is in Deadlock and the Deadlock process aren");
for(i=0;i<processes;i++)
{
System.out.println("P"+dead[i]);
}
}
else
{
System.out.println("No deadlock occure");
}  
}
}

运行:

******* Deadlock detection algorithm *******
Enter no. of processes: 3
Enter no. of resources: 3
Enter allocation matrix -->
3 3 3
2 0 3
1 2 4
Enter max matrix -->
3 6 8
4 3 3
3 4 4
Enter available matrix -->
1 2 0
Processes     Allocation     Max     Available
No deadlock occure

结果应该是:

System is in Deadlock and the Deadlock process are
p0
p1
p2

main中,您创建了 3 个单独的实例:

new DeadlockDetection().input();
new DeadlockDetection().show();
new DeadlockDetection().cal();

这意味着当您调用show()cal()时,processes始终为 0,因为创建新实例会丢弃您在input()中所做的操作。你应该有这个:

DeadlockDetection dd = new DeadlockDetection();
dd.input();
dd.show();
dd.cal();

最新更新