0% found this document useful (0 votes)
99 views16 pages

Distributed System LAb FILE

Here is a sample Java program to implement threads: // ThreadDemo.java class ThreadDemo { public static void main(String args[]) { // Create a runnable thread Runnable hello = new Hello(); // Create a thread from the runnable thread Thread thread = new Thread(hello); // Start the thread. thread.start(); // Print prime numbers till 50 for(int i=2;i<=50;i++){ if(isPrime(i)){ System.out.print(i+" "); } } } public static boolean isPrime(int n){ for(int i=2;i<n;i++){

Uploaded by

Nandini Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views16 pages

Distributed System LAb FILE

Here is a sample Java program to implement threads: // ThreadDemo.java class ThreadDemo { public static void main(String args[]) { // Create a runnable thread Runnable hello = new Hello(); // Create a thread from the runnable thread Thread thread = new Thread(hello); // Start the thread. thread.start(); // Print prime numbers till 50 for(int i=2;i<=50;i++){ if(isPrime(i)){ System.out.print(i+" "); } } } public static boolean isPrime(int n){ for(int i=2;i<n;i++){

Uploaded by

Nandini Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Experiment No.

Objective:Write a program to implement Lamport’s logical clock.

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:

Lamport's logical clocks

 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.

Lamport's ``happened before'' relation

The ``happened before'' relation () is defined as follows:

 AB if A andB are within the same process (same sequential thread of control) and A occurred
before B.
 AB 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 AB and BC then AC

Event Acausally affects event BiffAB.

Distinct events A and B are concurrent (A | | B) if we do not have AB or BA.

Lamport Logical Clocks

 are local to each process (processor?)


 do not measure real time
 only measure ``events''
 are consistent with the happened-before relation
 are useful for totally ordering transactions, by using logical clock values as timestamps

Logical Clock Conditions

Ci is the local clock for process Pi

 if a and b are two successive events in Pi, then


Ci(b) = Ci(a) + d1, where d1> 0
 if a is the sending of message m by Pi, then m is assigned timestamp tm = Ci(a)
 if b is the receipt of m by Pj, then
Cj(b) = max{Cj(b), tm + d2}, where d2> 0

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.

If a is an event in Pi and b is in Pj, abiff

 Ci(a)<Cj(b) or
 Ci(a)=Cj(b) and Pi<Pj

where< is an arbitrary total ordering of the processes

Program Code:

EXP.NO.

OBJECTIVE

OUTPUT on blank page


Experiment No. 2

Objective:Write a program to implement Huang’s Algorithm.

Type of Algorithm:Huang's algorithm is an algorithm for detecting termination in a distributed


system.

Algorithm:

Huang's algorithm can be described by the following:

 Initially all processes are idle.


 A distributed task is started by a process sending a computational message to another
process. This initial process to send the message is the "controlling agent".
o The initial weight of the controlling agent is (usually 1).
 The following rules are applied throughout the computation:
o A process sending a message splits its current weight between itself and the message.
o A process receiving a message adds the weight of the message to itself.
o Upon becoming idle, a process sends a message containing its entire weight back to
the controlling agent and it goes idle.
o Termination occurs when the controlling agent has a weight of and is in the idle
state.

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

Objective:Write a program to implement Huang’s Algorithm.

Output:
Experiment No. 3

Objective:Implement data transfer between client-server using socket programming(RPC).

Program Code:

Client Code

Server Code
Experiment No. 3

Objective:Implement data transfer between client-server using socket programming.

Output:
Experiment No. 4

Objective:Implementation of Interactive convergence algorithm.

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

Objective:Implementation of Interactive convergence algorithm.

Output:
Experiment No. 5

Objective:Implement Remote Method Invocation (RMI).

RMI Concept.

RMI Layered Diagram

Program Code:

Interface

Remote Class

Server Side:

Client Side:
Experiment No. 5

Objective:Implement Remote Method Invocation (RMI).

Output:
Procedure to run RMI PRogram
Experiment No. 6

Objective:Study of CORBA Architecture (CASE STUDY)


Theory:

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();

printf("Press a key(except q) to enter a process into critical section.");

printf(" \nPress q at any time to exit.");

t1 = time(NULL) - 5;

while(key!='q')

while(!kbhit())

if(cs!=0)

t2 = time(NULL);

if(t2-t1 > run)

printf("Process%d ",pro-1);

printf(" exits critical section.\n");

cs=0;

}
key = getch();

if(key!='q')

if(cs!=0)

printf("Error: Another process is currently executing


critical section Please wait till its execution is over.\n");

else

printf("Process %d ",pro);

printf(" entered critical section\n");

cs=1;

pro++;

t1 = time(NULL);

}
Experiment – 7

Output

Press a key(except q) to enter a process into critical section.

Press q at any time to exit.

Process 0 entered critical section.

Error: Another process is currently executing critical section.

Please wait till its execution is over.

Process 0 exits critical section.

Process 1 entered critical section.

Process 1 exits critical section.

Process 2 entered critical section.

Error: Another process is currently executing critical section.

Please wait till its execution is over.

Process 2 exits critical section.


Experiment– 8

OBJECTIVE: Implement RPC mechanism for a file transfer across a network in


‘java’.

client.java

server.java
Experiment– 9

OBJECTIVE: Implement Threads in java.

You might also like