Distributed System LAb FILE
Distributed System LAb FILE
Type of Algorithm:The algorithm of Lamport’s Logical Clock is a simple algorithm used to determine
the order of events in a distributed computer system.
Algorithm:
the "time" concept in distributed systems -- used to order events in a distributed system.
assumption:
o the execution of a process is characterized by a sequence of events. An event can be the
execution of one instruction or of one procedure.
o sending a message is one event, receiving a message is one event.
The events in a distributed system are not total chaos. Under some conditions, it is possible to
ascertain the order of the events. Lamport's logical clocks try to catch this.
AB if A andB are within the same process (same sequential thread of control) and A occurred
before B.
AB if A is the event of sending a message M in one process and B is the event of receiving M by
another process
if AB and BC then AC
The value of d could be 1, or it could be an approximation to the elapsed real time. For example, we could
take d1 to be the elapsed local time, and d2 to be the estimated message transmission time. The latter solves
the problem of waiting forever for a virtual time instant to pass.
Total Ordering
We can extend the partial ordering of the happened-before relation to a total ordering on ervents, by using
the logical clocks and resolving any ties by an arbitrary rule based on the processor/process ID.
Ci(a)<Cj(b) or
Ci(a)=Cj(b) and Pi<Pj
Program Code:
EXP.NO.
OBJECTIVE
Algorithm:
Some weaknesses to Huang's algorithm are that it is unable to detect termination if a message is lost
in transit or if a process fails while in an active state.
Program Code:
Experiment No. 2
Output:
Experiment No. 3
Program Code:
Client Code
Server Code
Experiment No. 3
Output:
Experiment No. 4
Program Code:
Server Code
Client Code
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 2411;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
OutputStreamos = socket.getOutputStream();
OutputStreamWriterosw = new OutputStreamWriter(os);
BufferedWriterbw = new BufferedWriter(osw);
String number = "1";
String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
InputStream is = socket.getInputStream();
ObjectInputStreaminStream = new ObjectInputStream(is);
Date time =(Date)inStream.readObject();
Date current = new Date();
long t1 = time.getTime();
long t2 = current.getTime();
long t3 = t2+t1;
t3 = t3/2;
System.out.println("time received from the server : " +t1);
System.out.println("Current time of client is : " +t2);
System.out.println("Average time in millisec is : " +t3);
time.setTime(t3);
System.out.println("So current time is : " +time);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Experiment No. 4
Output:
Experiment No. 5
RMI Concept.
Program Code:
Interface
Remote Class
Server Side:
Client Side:
Experiment No. 5
Output:
Procedure to run RMI PRogram
Experiment No. 6
Features:
Atleats 6 features
diagrams
Experiment – 7
OBJECTIVE: Write a program to simulate the Mutual Exclusion in ‘C’.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
void main()
int cs=0,pro=0;
double run=5;
char key='a';
time_t t1,t2;
clrscr();
t1 = time(NULL) - 5;
while(key!='q')
while(!kbhit())
if(cs!=0)
t2 = time(NULL);
printf("Process%d ",pro-1);
cs=0;
}
key = getch();
if(key!='q')
if(cs!=0)
else
printf("Process %d ",pro);
cs=1;
pro++;
t1 = time(NULL);
}
Experiment – 7
Output
client.java
server.java
Experiment– 9