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

Experiment 4

The document describes an experiment that aims to implement and analyze contention-based mutual exclusion algorithms like timestamp prioritized schemes and voting schemes in a distributed system. It provides theory on contention-based mutual exclusion, timestamp prioritized schemes that assign timestamps to processes requesting shared resources, and voting schemes where processes vote to decide access. Code examples are given to implement timestamp and voting mutex schemes using threads. The experiment concludes with successfully implementing both contention-based algorithms.

Uploaded by

sai nikhil
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)
9 views

Experiment 4

The document describes an experiment that aims to implement and analyze contention-based mutual exclusion algorithms like timestamp prioritized schemes and voting schemes in a distributed system. It provides theory on contention-based mutual exclusion, timestamp prioritized schemes that assign timestamps to processes requesting shared resources, and voting schemes where processes vote to decide access. Code examples are given to implement timestamp and voting mutex schemes using threads. The experiment concludes with successfully implementing both contention-based algorithms.

Uploaded by

sai nikhil
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/ 5

Experiment Number: 4

Date: 5 - 9 - 2023
Title: Contention-Based Mutual Exclusion

Aim:
The main focus of this lab exercise is to implement and analyze contention-based
mutual exclusion algorithms, specifically Timestamp Prioritized Schemes and
Voting Schemes, in a distributed system.

Theory:
• Contention-Based Mutual Exclusion
In contention-based schemes, the processes contend for the lock on the shared
resource. The lock is granted based on certain criteria, which could be a
timestamp, a voting mechanism, or some other metric.
• Timestamp Prioritized Schemes
In Timestamp Prioritized Schemes, each process requesting access to a shared
resource is assigned a timestamp. The process with the earliest timestamp is
given priority and gains access to the resource. This scheme is effective in
preventing starvation and ensuring fairness.
• Voting Schemes
In Voting Schemes, processes vote to decide which one should get access to the
shared resource. Each process sends a request to all other processes, and the one
with the majority of votes gains access.

Timestamp Prioritized Schemes:


import java.util.Scanner;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class TimestampMutex {
private int numThreads;
private int[] timestamps;
private Lock lock = new ReentrantLock();

public TimestampMutex(int numThreads) {


this.numThreads = numThreads;
timestamps = new int[numThreads];
}

public void acquire(int threadId) {


lock.lock();
timestamps[threadId] = getMaxTimestamp() + 1;
lock.unlock();

for (int i = 0; i < numThreads; i++) {


if (i != threadId) {
while (timestamps[i] != 0 && (timestamps[i] <
timestamps[threadId])) {
// Busy wait
}
}
}
}

public void release(int threadId) {


timestamps[threadId] = 0;
}

private int getMaxTimestamp() {


int max = timestamps[0];
for (int i = 1; i < numThreads; i++) {
if (timestamps[i] > max) {
max = timestamps[i];
}
}
return max;
}
}

public class TimestampMutexWithInput {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of threads: ");
int numThreads = scanner.nextInt();
scanner.close();

TimestampMutex mutex = new TimestampMutex(numThreads);


Thread[] threads = new Thread[numThreads];

for (int i = 0; i < numThreads; i++) {


final int threadId = i;
threads[i] = new Thread(() -> {
System.out.println("Thread " + threadId + " is trying to
enter.");
mutex.acquire(threadId);
System.out.println("Thread " + threadId + " has entered the
critical section.");

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Thread " + threadId + " is leaving the


critical section.");
mutex.release(threadId);
});
threads[i].start();
}

for (Thread thread : threads) {


try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Output:

Voting Mutex Schemes:


import java.util.Scanner;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class VotingMutex {
private int numThreads;
private boolean[] requests;
private int[] votes;
private Lock lock = new ReentrantLock();

public VotingMutex(int numThreads) {


this.numThreads = numThreads;
requests = new boolean[numThreads];
votes = new int[numThreads];
}
public void acquire(int threadId) {
lock.lock();
requests[threadId] = true;
votes[threadId] = 1;
lock.unlock();

for (int i = 0; i < numThreads; i++) {


if (i != threadId) {
while (requests[i]) {
// Busy wait
}
votes[threadId]++;
}
}

while (sumVotes() < numThreads) {


// Busy wait
}
}

public void release(int threadId) {


lock.lock();
requests[threadId] = false;
votes[threadId] = 0;
lock.unlock();
}

private int sumVotes() {


int sum = 0;
for (int vote : votes) {
sum += vote;
}
return sum;
}
}

public class VotingMutexWithInput {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of threads: ");
int numThreads = scanner.nextInt();
scanner.close();

VotingMutex mutex = new VotingMutex(numThreads);


Thread[] threads = new Thread[numThreads];

for (int i = 0; i < numThreads; i++) {


final int threadId = i;
threads[i] = new Thread(() -> {
System.out.println("Thread " + threadId + " is trying to
enter.");
mutex.acquire(threadId);
System.out.println("Thread " + threadId + " has entered the
critical section.");

try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("Thread " + threadId + " is leaving the


critical section.");
mutex.release(threadId);
});
threads[i].start();
}

for (Thread thread : threads) {


try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

Output:

Result/Conclusion:
Thus, we have successfully completed the implementation of contention based
mutual exclusion program for both Timestamp Prioritized Schemes and Voting
Schemes.

You might also like