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

Lab Da 2

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

Lab Da 2

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

CSI3012 - DISTRIBUTED SYSTEMS

LAB DIGITAL ASSESSMENT – 2

1. Ricart-Agrawala Algorithm:

CODE:
import java.util.concurrent.atomic.AtomicInteger;
import java.util.ArrayList;

class RicartAgrawala {
AtomicInteger requestTimestamp;
boolean[] replies;
int numRepliesNeeded;
int processId;
ArrayList<Integer> deferredQueue;

public RicartAgrawala(int numProcesses, int processId) {


requestTimestamp = new AtomicInteger(0);
replies = new boolean[numProcesses];
this.processId = processId;
deferredQueue = new ArrayList<>();
}

public synchronized void requestCriticalSection() {


requestTimestamp.getAndIncrement();
numRepliesNeeded = replies.length - 1;
for (int i = 0; i < replies.length; i++) {
if (i != processId) {
sendRequest(i, processId, requestTimestamp.get());
}
}

while (numRepliesNeeded > 0) {


try {
wait(); // Wait for replies
} catch (InterruptedException e) {
e.printStackTrace();
}
}

// Enter critical section


System.out.println("Process " + processId + " enters critical section.");
}

public synchronized void receiveRequest(int timestamp, int senderId) {


if (timestamp > requestTimestamp.get() || (timestamp == requestTimestamp.get() && senderId >
processId)) {
sendReply(senderId);
} else {
deferredQueue.add(senderId);
}
}

public synchronized void receiveReply(int senderId) {


numRepliesNeeded--;
if (numRepliesNeeded == 0) {
notify(); // Notify waiting threads
}
}
public synchronized void receiveRelease(int senderId) {
// Process release message
}

public synchronized void releaseCriticalSection() {


// Release critical section
for (int i = 0; i < replies.length; i++) {
if (i != processId) {
sendRelease(i);
}
}
System.out.println("Process " + processId + " exits critical section.");
}

private void sendRequest(int receiverId, int senderId, int timestamp) {


System.out.println("Process " + senderId + " sends request to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveRequest(timestamp, senderId);
}

private void sendReply(int receiverId) {


System.out.println("Process " + processId + " sends reply to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveReply(processId);
}

private void sendRelease(int receiverId) {


System.out.println("Process " + processId + " sends release to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveRelease(processId);
}
}

public class Main {


static RicartAgrawala[] processes;

public static void main(String[] args) {


final int numProcesses = 5;

processes = new RicartAgrawala[numProcesses];


for (int i = 0; i < numProcesses; i++) {
processes[i] = new RicartAgrawala(numProcesses, i);
}

processes[0].requestCriticalSection();
processes[0].releaseCriticalSection();
}
}
IMPLEMENTATION:
OUTPUT:

2. Maekawa's Algorithm:

CODE:
import java.util.ArrayList;
import java.util.List;
class Maekawa {
List<Integer> groups[];
List<Integer> deferredQueue;
int processId;

public Maekawa(int numProcesses, int processId) {


this.processId = processId;
groups = new ArrayList[numProcesses];
deferredQueue = new ArrayList<>();
for (int i = 0; i < numProcesses; i++) {
groups[i] = new ArrayList<>();
groups[i].add(i); // Each process belongs to its own group initially
}
}
public synchronized void requestCriticalSection() {
List<Integer> group = groups[processId];
for (int pid : group) {
if (pid != processId) {
sendRequest(pid, processId);
}
}
while (deferredQueue.size() < group.size() - 1) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("Process " + processId + " enters critical section.");
}
public synchronized void receiveRequest(int requesterId) {
List<Integer> requesterGroup = groups[requesterId];
boolean intersect = false;
for (int pid : requesterGroup) {
if (groups[pid].contains(processId)) {
intersect = true;
break;
}
}
if (intersect) {
deferredQueue.add(requesterId);
} else {
sendReply(requesterId);
}
}
public synchronized void receiveReply(int senderId) {
// Process reply message
System.out.println("Process " + processId + " receives reply from process " + senderId);
// Implement your logic here
}

public synchronized void receiveRelease(int senderId) {


// Process release message
System.out.println("Process " + processId + " receives release from process " + senderId);
// Implement your logic here
}

public synchronized void releaseCriticalSection() {


System.out.println("Process " + processId + " exits critical section.");
for (int pid : deferredQueue) {
sendRelease(pid);
}
deferredQueue.clear();
notifyAll();
}

private void sendRequest(int receiverId, int senderId) {


System.out.println("Process " + senderId + " sends request to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveRequest(senderId);
}

private void sendReply(int receiverId) {


System.out.println("Process " + processId + " sends reply to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveReply(processId);
}

private void sendRelease(int receiverId) {


System.out.println("Process " + processId + " sends release to process " + receiverId);
// Simulated message sending
Main.processes[receiverId].receiveRelease(processId);
}
}

public class Main {


static Maekawa[] processes;
public static void main(String[] args) {
final int numProcesses = 5;
processes = new Maekawa[numProcesses];
for (int i = 0; i < numProcesses; i++) {
processes[i] = new Maekawa(numProcesses, i);
}
// Request critical section for each process
for (int i = 0; i < numProcesses; i++) {
final int processId = i;
new Thread(() -> processes[processId].requestCriticalSection()).start();
}
// Delay before releasing critical section
try {
Thread.sleep(5000); // Adjust the delay as needed
} catch (InterruptedException e) {
e.printStackTrace();
}
// Release critical section for each process
for (int i = 0; i < numProcesses; i++) {
final int processId = i;
new Thread(() -> processes[processId].releaseCriticalSection()).start();
}
}
}
IMPLEMENTATION:
OUTPUT:

You might also like