DC Exp 7 Bhavesh Kumar Jha RollNo 06

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Universal College of Engineering, Kaman

Department of Computer Engineering


Subject: Distributed Computing Laboratory

Experiment No: 07

Roll No:06 Name: Bhavesh_Kumar_jha_roll_no_06 Div: A Batch: A1

Aim: Program to demonstrate Token Ring Algorithm for Distributed Mutual Exclusion.

Code & Output:


Step1: Write and compile server program MutualServer.java

import java.io.*;
import java.net.*;
public class MutualServer implements Runnable
{
Socket socket=null;
static ServerSocket ss;
MutualServer (Socket newSocket)
{
this.socket=newSocket;
}
public static void main (String args []) throws IOException
{
ss=new ServerSocket(7000);
System.out.println("Server Started");
while (true)
{
Socket s = ss.accept();
MutualServer es = new MutualServer(s);
Thread t = new Thread(es);
t.start();
}
}
public void run()
{
try
{
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true)
{
Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: Distributed Computing Laboratory

System.out.println(in.readLine());
}
}
catch(Exception e){}
}
}

Step2: Write and compile first client program ClientOne.java

import java.io.*;
import java.net.*;
public class ClientOne
{
public static void main (String args []) throws IOException
{
Socket s=new Socket("localhost",7000);
PrintStream out = new PrintStream(s.getOutputStream());
ServerSocket ss = new ServerSocket(7001);
Socket s1 = ss.accept();
BufferedReader in1 = new BufferedReader(new InputStreamReader(s1.getInputStream()));
PrintStream out1 = new PrintStream(s1.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str="Token";
while(true)
{
if(str.equalsIgnoreCase("Token"))

{
System.out.println("Do you want to send some data");
System.out.println("Enter Yes or No");
str=br.readLine();
if(str.equalsIgnoreCase("Yes"))
{
System.out.println("Enter the data");
str=br.readLine();
out.println(str);
}
out1.println("Token");
Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: Distributed Computing Laboratory
}
System.out.println("Waiting for Token");
str=in1.readLine();
}
}
}

Step3: Write and compile second client program ClientTwo.java

import java.io.*;
import java.net.*;
public class ClientTwo

{
public static void main (String args []) throws IOException
{
Socket s=new Socket("localhost", 7000);
PrintStream out = new PrintStream(s.getOutputStream());
Socket s2=new Socket("localhost", 7001);
BufferedReader in2 = new BufferedReader(new InputStreamReader(s2.getInputStream()));
PrintStream out2 = new PrintStream(s2.getOutputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;
while(true)
{
System.out.println("Waiting for Token");
str=in2.readLine();
if(str.equalsIgnoreCase("Token"))
{
System.out.println("Do you want to send some data");
System.out.println("Enter Yes or No");
str=br.readLine();
if(str.equalsIgnoreCase("Yes"))
Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: Distributed Computing Laboratory
{
System.out.println("Enter the data");
str=br.readLine();
out.println(str);
}
out2.println("Token");
}
}
}
}

Step4: Compile MutualServer,clientOne ,ClientTwo

Step5: Run MutualServer

Step6: run the ClientOne program.


Universal College of Engineering, Kaman
Department of Computer Engineering
Subject: Distributed Computing Laboratory

Conclusion: Hence ,we successfully performed an experiment on a Program to Demonstrate


Token Ring Algorithm for Distributed Mutual Exclusion.

You might also like