Centralized Mutual Exclusion
Centralized Mutual Exclusion
import java.io.*;
class pc
{
public int flag=0;
public int front=0;
public int rear=-1;
public boolean grant;
int[] queue=new int[10];
public void request(int p)
{
System.out.println("process "+p+ " entered");
if(flag==0)
{
System.out.println("reply to process = "+p);
flag=1;
}
else
{
if(flag==1)
{
System.out.println("wait process = "+p);
rear++;
queue[rear]=p;
}
}
}
public void release(int p)
{
flag=0;
if(queue[front]!=0)
{
System.out.println("released process = "+p);
System.out.println("delete from queue process = "+queue[front]);
request(queue[front]);
front=front+1;
}
}
}
public class cendist extends pc
{
public static void main(String args[]) throws IOException
{
cendist p1=new cendist();
p1.request(10);
p1.request(11);
p1.request(12);
p1.release(10);
p1.release(11);
p1.release(12);
}
}
Output
import java.io.*;
class pc
{
public int flag=0;
public int front=0;
public int rear=-1;
public boolean grant;
int[] queue=new int[10];
public void request(int p)
{
System.out.println("process "+p+ " entered");
if(flag==0)
{
System.out.println("reply to process = "+p);
flag=1;
}
else
{
if(flag==1)
{
System.out.println("wait process = "+p);
rear++;
queue[rear]=p;
}
}
}
public void release(int p)
{
flag=0;
if(queue[front]!=0)
{
System.out.println("released process = "+p);
System.out.println("delete from queue process = "+queue[front]);
request(queue[front]);
front=front+1;
}
}
}
public class cendist extends pc
{
public static void main(String args[]) throws IOException
{
cendist p1=new cendist();
p1.request(10);
p1.request(11);
p1.request(12);
p1.release(10);
p1.release(11);
p1.release(12);
}
}
Output
Comments
Post a Comment