Distributed System and Cloud Computing Lab Manual
ISBN 9788119221868

Highlights

Notes

  

Chapter 5: Program on Mutual Exclusion

Implementation of Mutual Exclusion using Token Ring technique. Token Ring

Token Ring algorithm achieves mutual exclusion in a distributed system by creating a bus network of processes. A logical ring is constructed with these processes and each process is assigned a position in the ring. Each process knows who is next in line after itself. When the ring is initialized, process 0 is given a token. The token circulates around the ring. When a process acquires the token from its neighbor, it checks to see if it is attempting to enter a critical region. If so, the process enters the region, does all the work it needs to, and leaves the region. After it has exited, it passes the token to the next process in the ring. It is not allowed to enter the critical region again using the same token. If a process is handed the token by its neighbor and is not interested in entering a critical region, it just passes the token along to the next process.

Advantages:

The correctness of this algorithm is evident. Only one process has the token at any instant, so only one process can be in a CS

Since the token circulates among processes in a well-defined order, starvation cannot occur.

Disadvantages:

Once a process decides it wants to enter a CS, at worst it will have to wait for every other process to enter and leave one critical region.

If the token is ever lost, it must be regenerated. In fact, detecting that it is lost is difficult, since the amount of time between successive appearances of the token on the network is not a constant. The fact that the token has not been spotted for an hour does not mean that it has been lost; some process may still be using it.

The algorithm also runs into trouble if a process crashes, but recovery is easier than in the other cases. If we require a process receiving the token to acknowledge receipt, a dead process will be detected when its neighbor tries to give it the token and fails. At that point the dead process can be removed from the group, and the token holder can pass the token to the next member down the line

Source Code:

File Name: TokenRingServer.java

import java.net.*;

public class TokenRingServer

{

public static DatagramSocket ds; public static DatagramPacket dp; public static void main(String[] args)

{

try{

ds=new DatagramSocket(2000); while(true)

{

byte[] buffer=new byte[1024];

dp= new DatagramPacket(buffer,buffer.length); ds.receive(dp);

String msg=new String(dp.getData(),0,dp.getLength()); System.out.println(“+ +”+ “\n”+“| Message From

Client”+msg+“\n”+“+ +”);

}

}

catch(Exception e){}

}

}

File Name: TokenRingClient1.java

import java.net.*; import java.io.*;

  • public class TokenClient1
  • {

public static DatagramSocket ds; public static DatagramPacket dp; public static BufferedReader br;

public static void main(String[] args)throws Exception

{

boolean hasToken = true;

ds = new DatagramSocket(2001); while(true)

{

if(hasToken==true)

{

System.out.println(“do you want to enter CS..(yes/no)”);

br=new BufferedReader(new InputStreamReader(System.in)); String choice =br.readLine(); if(choice.equalsIgnoreCase(“yes”))

{

System.out.println(“the client or process is ready to write”); System.out.println(“enter the message”);

  • br=new BufferedReader(new InputStreamReader(System.in));
  • String msg =“: 1 | ”+br.readLine();

dp=new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getLocalHost(),2000); ds.send(dp);

System.out.println(“message sent”);

}

else if(choice.equalsIgnoreCase(“no”))

{

System.out.println(“i am not ready to enter the critical section”);

String msg1=“Token”;

dp=new DatagramPacket(msg1.getBytes(),msg1.length(),InetAddress.getLocalHost(),2002); ds.send(dp);

hasToken=false;

}

}

else

{

System.out.println(“Waiting for token”); byte[] buffer = new byte[2048];

dp=new DatagramPacket(buffer,buffer.length);

ds.receive(dp);

String prevProcessMsg=new String(dp.getData(),0,dp.getLength()); System.out.println(“PreviousProcessMsg is”+prevProcessMsg); if(prevProcessMsg.equals(“Token”))

{

hasToken=true;

System.out.println(“I have token now”);

}

}

}

}

}

File Name: TokenRingClient2.java

import java.net.*;

import java.io.*;

public class TokenClient2

{

public static DatagramSocket ds; public static DatagramPacket dp; public static BufferedReader br;

public static void main(String[] args)throws Exception

{

boolean hasToken = true;

ds = new DatagramSocket(2002); while(true)

{

if(hasToken==true)

{

System.out.println(“do you want to enter CS..(yes/no)”);

br=new BufferedReader(new InputStreamReader(System.in)); String choice =br.readLine(); if(choice.equalsIgnoreCase(“yes”))

{

System.out.println(“the client or process is ready to write”); System.out.println(“enter the message”);

br=new BufferedReader(new InputStreamReader(System.in)); String msg = “: 2 | ”+br.readLine(); dp=new

DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getLocalHost(),2000); ds.send(dp);

System.out.println(“message sent”);

}

else if(choice.equalsIgnoreCase(“no”))

{

  • System.out.println(“i am not ready to enter the critical section”);

String msg1=“Token”;

dp=new DatagramPacket(msg1.getBytes(),msg1.length(),InetAddress.getLocalHost(),2003); ds.send(dp);

hasToken=false;

}

}

else

{

System.out.println(“Waiting for token”); byte[] buffer = new byte[2048];

dp=new DatagramPacket(buffer,buffer.length);

ds.receive(dp);

String prevProcessMsg=new String(dp.getData(),0,dp.getLength()); System.out.println(“PreviousProcessMsg is”+prevProcessMsg); if(prevProcessMsg.equals(“Token”))

{

hasToken=true;

System.out.println(“I have token now”);

}

}

}

}

}

File Name: TokenRingClient3.java

import java.net.*; import java.io.*;

public class TokenClient3

{

public static DatagramSocket ds; public static DatagramPacket dp; public static BufferedReader br;

public static void main(String[] args)throws Exception

{

boolean hasToken = true;

ds = new DatagramSocket(2003); while(true)

{

if(hasToken==true)

{

System.out.println(“do you want to enter CS..(yes/no)”);

br=new BufferedReader(new InputStreamReader(System.in)); String choice =br.readLine(); if(choice.equalsIgnoreCase(“yes”))

{

System.out.println(“the client or process is ready to write”); System.out.println(“enter the message”);

br=new BufferedReader(new InputStreamReader(System.in)); String msg =“: 3 | ”+br.readLine();

dp=new DatagramPacket(msg.getBytes(),msg.length(),InetAddress.getLocalHost(),2000); ds.send(dp);

System.out.println(“message sent”);

}

else if(choice.equalsIgnoreCase(“no”))

{

System.out.println(“i am not ready to enter the critical section”);

String msg1=“Token”;

dp=new DatagramPacket(msg1.getBytes(),msg1.length(),InetAddress.getLocalHost(),2001); ds.send(dp);

hasToken=false;

}

}

else

{

System.out.println(“Waiting for token”); byte[] buffer = new byte[2048];

dp=new DatagramPacket(buffer,buffer.length);

ds.receive(dp);

String prevProcessMsg=new String(dp.getData(),0,dp.getLength()); System.out.println(“PreviousProcessMsg is”+prevProcessMsg); if(prevProcessMsg.equals(“Token”))

{

hasToken=true;

System.out.println(“I have token now”);

}

}

}

}

}

Output: