Deadlock Detection > Java Program

Deadlock Detection > Java Program

Distributed Databases


Program:
import java.util.Scanner;

/**
 *
 * @author PRATHAMESH PANDIT
 */

public class DeadlockDD {
   
    public static void main(String[] args) {

        // TODO code application logic here

        Scanner sc = new Scanner(System.in);


       

        System.out.print("Enter total no of processes: ");

        int n = sc.nextInt();

        int process[][]= new int[n+1][n+1];

        System.out.println("Dependency graph");

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if(i==j){

                    process[i][j]= 0;

                    continue;

                }

                System.out.print("P["+i+"]["+j+"]: ");

                process[i][j]=sc.nextInt();

            }

        }

       

        System.out.print("Origin of the probe: ");

        int probeOrigin = sc.nextInt();
        int probe[] = new int[3];

        probe[0] = probeOrigin;

        int count = n;

        for (int i = probeOrigin; ; ) {

            if(count == 0){

                break;

            }

            int next = 0;

            for (int j = 1; j <= n; j++) {

                if(process[i][j] == 1){

                    next = j;

                }

            }

            probe[1] = i;

            probe[2] = next;

           

            i = next;

            count--;

        }

       

        if(probe[0] == probe[2]){

            System.out.println("Deadlock");

        }else{

            System.out.println("No deadlock");

        }

        sc.close();

      
    }
}


Output:

Enter total no of processes: 5
Dependency graph
P[1][2]: 1
P[1][3]: 0
P[1][4]: 0
P[1][5]: 0
P[2][1]: 0
P[2][3]: 1
P[2][4]: 0
P[2][5]: 0
P[3][1]: 0
P[3][2]: 0
P[3][4]: 1
P[3][5]: 0
P[4][1]: 0
P[4][2]: 0
P[4][3]: 0
P[4][5]: 1
P[5][1]: 1
P[5][2]: 0
P[5][3]: 0
P[5][4]: 0
Origin of the probe: 1
Deadlock
BUILD SUCCESSFUL (total time: 43 seconds)

    

Comments

Popular posts from this blog

Intermediate Code Generation > C Program