Monday 8 January 2018

C Program for leftrecursion

#include<stdio.h> 
#include<string.h> 
#define SIZE 10 

int main () { 
char non_terminal; 
char beta,alpha; 
int num; 
char production[10][SIZE]; 
int index=3; /* starting of the string following "->" */ 
printf("Enter Number of Production : "); 
scanf("%d",&num); 
printf("Enter the grammar as E->E-A :\n"); 
for(int i=0;i<num;i++){ 
scanf("%s",production[i]); 

for(int i=0;i<num;i++){ 
printf("\nGRAMMAR : : : %s",production[i]); 
non_terminal=production[i][0]; 
if(non_terminal==production[i][index]) { 
alpha=production[i][index+1]; 
printf(" is left recursive.\n"); 
while(production[i][index]!=0 && production[i][index]!='|') 
index++; 
if(production[i][index]!=0) { 
beta=production[i][index+1]; 
printf("Grammar without left recursion:\n"); 
printf("%c->%c%c\'",non_terminal,beta,non_terminal);     
printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal); 

else 
printf(" can't be reduced\n"); 

else 
printf(" is not left recursive.\n"); 
index=3; 



/*
Output------------------------------------------
aiktc@aiktc11 ~ $ gcc -std=c99 leftrecursion.c
aiktc@aiktc11 ~ $ ./a.out
Enter Number of Production : 2
Enter the grammar as E->E-A :
E->EA|A
A->AT|a

GRAMMAR : : : E->EA|A is left recursive.
Grammar without left recursion:
E->AE'
E'->AE'|E

GRAMMAR : : : A->AT|a is left recursive.
Grammar without left recursion:
A->aA'
A'->TA'|E
aiktc@aiktc11 ~ $
*/

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

STUDY AND IMPLEMENT TWO PHASE LOCKING PROTOCOL (DD)

import java.io.*;
import java.util.*;
class Account
{
String name;
int amount,lock;
Account(String name,int amount,int lock)
{
this.name=name;
this.amount=amount;
this.lock=lock;
}
public void deposit(int amt)
{
amount +=amt;
}
public void withdraw(int amt)
{
if((amount-amt)<0)
{
System.out.println("Transaction Cannot proceed : Not Enough Balance in Account"+name);
System.exit(0);
}
amount -=amt;

}
public int balance()
{
return amount;
}
}

class Transaction implements Runnable
{
private Thread t=null;
String id;
Account from,to;
int amount;
Transaction(String id,Account from,Account to,int amount)
{
this.id=id;
this.from=from;
this.to=to;
this.amount=amount;
}

public void acquireLock(Account x,int type)
{
x.lock=type;
}

public void releaseLock(Account x)
{
x.lock=0;
}

public void  run() {
System.out.println("Running Transaction" +  id );
try {
acquireLock(from,2);
acquireLock(to,2);
from.withdraw(amount);
to.deposit(amount);
System.out.println("Transaction "+id+" is completed");
releaseLock(from);
releaseLock(to);
System.out.println("Balance of Account "+from.name+" is "+from.balance());
System.out.println("Balance of Account "+to.name+" is "+to.balance());
// Let the thread sleep for a while.
Thread.sleep(50);

}
catch (InterruptedException e) {
System.out.println("Transaction " +  id + " interrupted.");
}
System.out.println("Transaction " +  id + " exiting.");
}


public void start ()
{
System.out.println("Starting Transaction " +  id );

while(!(from.lock==0 && to.lock==0));

if(t==null)
{
t = new Thread(this,this.id);
t.start ();

}

}
public class twophase
{
public static void main(String args[])
{
Account A=new Account("A",500,0);
Account B=new Account("B",800,0);
Account C=new Account("C",900,0);
Transaction T1 = new Transaction( "T-1",A,B,200);
T1.start();

Transaction T2 = new Transaction( "T-2",C,B,500);
T2.start();

Transaction T3 = new Transaction( "T-3",A,B,400);
T3.start();

}

*******************OUTPUT*********************
aiktc@aiktc17 ~ $ javac twophase.java
aiktc@aiktc17 ~ $ java twophase
Starting Transaction T-1
Starting Transaction T-2
Running TransactionT-1
Starting Transaction T-3
Transaction T-1 is completed
Running TransactionT-2
Balance of Account A is 300
Transaction T-2 is completed
Balance of Account C is 400
Balance of Account B is 1500
Balance of Account B is 1500
Running TransactionT-3
Transaction Cannot proceed : Not Enough Balance in AccountA