java中死锁检测算法的程序设计

java中死锁检测算法的程序设计,java,deadlock,Java,Deadlock,我一直试图用java语言编写一个死锁检测算法的程序,但是当我输入应该产生死锁的数据时,程序总是输出“没有死锁发生”。我是一个初学者,如果有人能告诉我我的代码有什么问题,我将不胜感激 这是我的密码: package org.me.deadlockdetection; import java.applet.Applet; import java.awt.*; import java.util.Scanner; public class DeadlockDetection{ private in

我一直试图用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("\n\nSystem is in Deadlock and the Deadlock process are\n");
                          for(i=0;i<processes;i++)
                          {
                              System.out.println("P"+dead[i]);
                          }
           }
             else
           {
                          System.out.println("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()
时,
进程
始终为0,因为创建新实例会丢弃您在
input()
中所做的工作。您应该改为:

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

你从哪里得到这个算法的?快速的谷歌搜索会显示它与银行家的算法相似。
DeadlockDetection dd = new DeadlockDetection();
dd.input();
dd.show();
dd.cal();