Search code examples
javadeadlock

Program for deadlock detection algorithm in java


I have been trying to write a program for deadlock detection algorithm in java language but when I input data that should produce deadlock, the program always outputs that "No deadlock occurs". I am a beginner and would appreciate it if anyone can tell me what's wrong with my code.

here is my code:

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");
           }  
}
}

The Run:

******* 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

The result should be:

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

Solution

  • In main, you create 3 separate instances:

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

    This means that when you call show() and cal(), processes is always 0 because creating a new instance throws away the stuff you did in input(). You should have this instead:

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