Distributed Systems Lab Record
Distributed Systems Lab Record
FOR
Distributed Systems
(CDS3001)
Submitted by:
Aditya Sachin Khamitkar
Registration Number: 22MIP10006
Slot: F11+F12
Submitted to:
Dr. Velmurugan L
School of Computing Science and Artificial Intelligence (SCAI)
4 Chat Server 11
5 Termination Detection 13
1
Chapter 1
Aim
To implement a non-token based algorithm for mutual exclusion using Lamport’s algo-
rithm, ensuring that only one process can enter its critical section at a time without using
any physical token.
Requirements
Theory
In a distributed system, mutual exclusion ensures that no two processes execute their
critical sections simultaneously. Lamport’s algorithm uses logical timestamps and mes-
sage passing to achieve mutual exclusion without using a physical token. Each request
is timestamped, and a process enters the critical section only when it has the earliest
request and all other processes have acknowledged its request.
2
Lab Manual - Distributed Systems
• Safety: At most one process can execute in the critical section at any time.
Procedure
1. Each process maintains a logical clock which increments with each event.
2. When a process wants to enter its critical section, it sends a timestamped request
to all other processes.
3. Upon receiving a request, each process compares the timestamp with its own.
4. A process enters its critical section only after receiving replies from all other pro-
cesses.
5. After exiting the critical section, the process sends a release message to all others.
Code
1 import java . util .*;
2
3 class LamportMutex {
4 int pid ;
5 int clock ;
6 PriorityQueue < Request > requestQueue = new PriorityQueue < >(( a ,
b ) -> a . timestamp - b . timestamp ) ;
7
21 void requestCS () {
3
Lab Manual - Distributed Systems
22 clock ++;
23 Request req = new Request ( pid , clock ) ;
24 requestQueue . add ( req ) ;
25 broadcast ( " REQUEST " , req ) ;
26 }
27
38 void releaseCS () {
39 requestQueue . removeIf ( r -> r . pid == pid ) ;
40 broadcast ( " RELEASE " , new Request ( pid , clock ) ) ;
41 }
42
Sample Output
REQUEST sent by P1 at 1
REPLY sent to P1
P1 enters critical section
RELEASE sent by P1 at 2
P1 exits critical section
Result/Outcome
4
Lab Manual - Distributed Systems
section.
Conclusion
5
Chapter 2
Aim
Requirements
• Java IDE
Theory
6
Lab Manual - Distributed Systems
Procedure
3. When a message is sent, the current clock value is sent with it.
Code
1 class Logi c a lC l o ck P r oc e s s {
2 int clock = 0;
3 int pid ;
4
9 void internalEvent () {
10 clock ++;
11 System . out . println ( " Internal event at P " + pid + " with
clock " + clock ) ;
12 }
13
14 void sendEvent () {
15 clock ++;
16 System . out . println ( " Send event from P " + pid + " with
clock " + clock ) ;
17 }
18
Sample Output
7
Lab Manual - Distributed Systems
Result/Outcome
The logical clock implementation maintains a consistent order of events across distributed
processes.
Conclusion
Lamport2̆019s Logical Clock provides a simple mechanism for ordering events in a dis-
tributed system without relying on physical time synchronization.
8
Chapter 3
Aim
To implement Remote Procedure Call (RPC) in Java to transfer a file from client to
server.
Requirements
• Java SDK
• Java RMI or Socket Programming (for simulation of RPC)
• Basic file handling knowledge
Theory
9
Lab Manual - Distributed Systems
20 // Client . java
21 import java . io .*;
22 import java . net .*;
23
Conclusion
RPC allows invoking remote services. File transfer demonstrates RPC’s ability to abstract
remote interaction.
10
Chapter 4
Chat Server
Aim
Requirements
• Java SDK
• Socket programming knowledge
Theory
A chat server allows multiple clients to send and receive messages. It uses multithreading
to manage each client.
Code Overview
11
Lab Manual - Distributed Systems
7 private static Set < Socket > clientSockets = new HashSet < >() ;
8
Conclusion
The chat server showcases client-server architecture with concurrent client handling using
multithreading.
12
Chapter 5
Termination Detection
Aim
Theory
Termination detection ensures that a distributed computation has ended, and all pro-
cesses are idle with no messages in transit.
5 void performTask () {
6 active = true ;
7 System . out . println ( " Node " + id + " working ... " ) ;
8 try { Thread . sleep (1000) ; } catch ( Exception e ) {}
9 active = false ;
10 System . out . println ( " Node " + id + " finished . " ) ;
11 }
12
13 boolean isIdle () {
14 return ! active ;
15 }
16 }
13
Lab Manual - Distributed Systems
Conclusion
14
Chapter 6
Aim
To implement CORBA with one end in C++ and the other in Java for distributed object
communication.
Requirements
• IDL compiler
Theory
Steps
15
Lab Manual - Distributed Systems
IDL Example
1 interface Hello {
2 string sayHello () ;
3 };
Conclusion
16
Chapter 7
Aim
To implement a distributed locking mechanism that allows safe access to shared resources.
Requirements
Theory
Locking algorithms ensure only one process accesses a resource at a time. In a distributed
environment, locks are managed using either centralized, distributed, or quorum-based
approaches. Here, we implement a basic centralized lock server.
Procedure
17
Lab Manual - Distributed Systems
Code
1 class LockManager {
2 boolean locked = false ;
3 Queue < Integer > queue = new LinkedList < >() ;
4
Sample Output
P1 requesting lock
Lock granted to P1
P2 requesting lock
P2 added to queue
P1 releasing lock
Lock granted to P2
18
Lab Manual - Distributed Systems
Result/Outcome
The lock manager successfully controls access to a shared resource, ensuring mutual
exclusion.
Conclusion
Centralized locking is a simple yet effective method for managing concurrent access in
distributed systems.
19
Chapter 8
Aim
Requirements
Theory
Java Remote Method Invocation (RMI) is an API that allows objects to communicate
remotely. It enables an object running in one Java Virtual Machine (JVM) to invoke
methods on an object in another JVM.
20
Lab Manual - Distributed Systems
Procedure
import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
21
Lab Manual - Distributed Systems
} catch (Exception e) {
System.out.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
javac *.java
java Server
java Client
22
Lab Manual - Distributed Systems
Result
The client successfully invoked the remote method from the server and received the
message: "Hello from the RMI server!".
Conclusion
23