0% found this document useful (0 votes)
606 views

Lamport's Algorithm For Logical Clock

This document contains the source code for a program that implements Lamport's algorithm for logical clock synchronization between a server and client. The server runs on port 100 and accepts a connection from the client. Both the client and server have their own logical clocks that are incremented at different rates. When the client sends its clock value to the server, the server adopts the larger of the two values if its own is smaller to maintain causality.

Uploaded by

Shree Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
606 views

Lamport's Algorithm For Logical Clock

This document contains the source code for a program that implements Lamport's algorithm for logical clock synchronization between a server and client. The server runs on port 100 and accepts a connection from the client. Both the client and server have their own logical clocks that are incremented at different rates. When the client sends its clock value to the server, the server adopts the larger of the two values if its own is smaller to maintain causality.

Uploaded by

Shree Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

==================================================

===================================
Name: Nikita L Shinde
Subject: Computer
Laboratory-II
RollNo: B4262
College: MIT College And
Engineering
==================================================
===================================
Aim: Write program for synchronization of logical clock using Lamports Algorithm.
import java.io.*;
import java.net.*;
class Clock implements Runnable
{
Thread t;
int clk;
int inc;
public Clock(int c,int i)
{
clk=c; inc=i;
t=new Thread(this,"Clock");
t.start();
}
public void run()
{
for(int i=0;i<10000;i++)
{
try
{
clk=clk+inc;
Thread.sleep(1000);
}
catch(Exception e){}
}
}
int getClk()
{
return clk;
}
void setClk(int c)
{
clk=c;
}
}
class LServer implements Runnable

{
Thread t;
Clock c;
public LServer()
{
c=new Clock(0,1);
t=new Thread(this,"Server");
t.start();
}
public void run()
{
ServerSocket svr;
DataInputStream din;
DataOutputStream dout;
int clk=999;
try
{
svr=new ServerSocket(100);
System.out.println("Listening on port 100");
Socket cl=svr.accept();
System.out.println("\nConnected to >> " +
cl.getInetAddress().toString());
din=new DataInputStream(cl.getInputStream());
dout=new DataOutputStream(cl.getOutputStream());
String msg=null;
while(true)
{
try
{
msg=din.readUTF();
clk=Integer.parseInt(msg);
System.out.println("Client Clock >> " + clk);
System.out.println("Server Clock >> " +
c.getClk());
if(c.getClk()<clk)
{
System.out.println("Lamports in picture...");
c.setClk(clk+1);
System.out.println("New Server Clock >> " +
c.getClk());
}}
catch(Exception e)
{
if(msg.equalsIgnoreCase("sc time"))
{System.out.println("Server Clock >> " + c.getClk()); }
}
}
}

catch(Exception e)
{}
}
}
class LClient implements Runnable
{
Thread t;
Clock c;
public LClient()
{
c=new Clock(0,2);
t=new Thread(this,"Client");
t.start();
}
public void run()
{
int port;
String rip;
Socket s;
DataInputStream din;
DataOutputStream dout;
try
{
BufferedReader bin=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter Remote IP >> ");
rip=bin.readLine();
System.out.println("Enter Remote Port >> ");
port=Integer.parseInt(bin.readLine());
s=new Socket(rip,port);
System.out.println("Client -> Connection Made ");
din=new DataInputStream(s.getInputStream());
dout=new DataOutputStream(s.getOutputStream());
String ch;
while(true)
{
System.out.println("Enter Your Choice");
ch=bin.readLine();
if(ch.equalsIgnoreCase("send clock"))
dout.writeUTF(Integer.toString(c.getClk()));
else if(ch.equalsIgnoreCase("quit"))
System.exit(-1);
else if(ch.equalsIgnoreCase("sc time"))
dout.writeUTF(ch);
}
}

catch(Exception e){}
}
}
public class Lamport
{
public static void main(String a[])throws Exception
{
LClient cl=new LClient();
LServer sv=new LServer();
try
{
sv.t.join();
cl.t.join();
}
catch(Exception e)
{}
}
}
=====================================OUTPUT========
=================================
C:\Program Files\Java\jdk1.7.0\bin >javac Lamport.java
C:\Program Files\Java\jdk1.7.0\bin >java Lamport
Enter Remote IP >>
Listening on port 100
127.0.0.1
Enter Remote Port >>
100
Client -> Connection Made
Connected to >> /127.0.0.1
Enter Your Choice
sc time
Enter Your Choice
Server Clock >> 14
send clock
Enter Your Choice
Client Clock >> 62
Server Clock >> 31
Lamports in picture...
New Server Clock >> 63
Enter Your Choice
sc time
Enter Your Choice
Server Clock >> 72
quit

==================================================
===================================

You might also like