Monday, 8 January 2018

STUDY AND IMPLEMENT WAIT FOR GRAPH IN DEADLOCK (DD)

import java.io.*;
import java.util.*;

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

int n;

Scanner scr=new Scanner(System.in);
System.out.println("Enter the number of transcation : ");
n=scr.nextInt();

int t[][]=new int[n][n];

System.out.println("Enter the Adjency Matrix of ["+n+"]["+n+"]");
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
System.out.print("\nT["+i+"]["+j+"]");
t[i][j]=scr.nextInt();
}
System.out.println("You Have Entered the Adjency Matrix of ["+n+"]["+n+"]");
for(int i=0;i<n;i++)
{ for(int j=0;j<n;j++)
System.out.print(" "+t[i][j]);

System.out.println("");
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{ if(t[i][j]==1)
{ for(int k=0;k<n;k++)
{ if(t[j][k]==1)
{
if(n==2 && i==k && !(i==j))
{
System.out.println("DeadLock Cycle Detected T"+(i+1)+"-->T"+(j+1)+"-->T"+(i+1));
System.exit(0);
}
if(t[k][i]==1 && n!=2)
{
System.out.println("DeadLock Cycle Detected T"+(i+1)+"-->T"+(j+1)+"-->T"+(k+1)+"-->T"+(i+1));
System.exit(0);
}
}
}
}
}
}

System.out.println("No Deadlock Cycle Detected");
}
}

/* OUTPUT :

aiktc@aiktc17 ~ $ javac WaitForGraph.java
aiktc@aiktc17 ~ $ java WaitForGraph
Enter the number of transcation :
2
Enter the Adjency Matrix of [2][2]

T[0][0]1

T[0][1]2

T[1][0]1

T[1][1]1
You Have Entered the Adjency Matrix of [2][2]
1 2
1 1
No Deadlock Cycle Detected
aiktc@aiktc17 ~ $ java WaitForGraph
Enter the number of transcation :
3
Enter the Adjency Matrix of [3][3]

T[0][0]1

T[0][1]2

T[0][2]3

T[1][0]4

T[1][1]5

T[1][2]6

T[2][0]7

T[2][1]8

T[2][2]9
You Have Entered the Adjency Matrix of [3][3]
1 2 3
4 5 6
7 8 9
DeadLock Cycle Detected T1-->T1-->T1-->T1
*/

No comments:

Post a Comment