Experiment 4
Experiment 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.
class TimestampMutex {
private int numThreads;
private int[] timestamps;
private Lock lock = new ReentrantLock();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
class VotingMutex {
private int numThreads;
private boolean[] requests;
private int[] votes;
private Lock lock = new ReentrantLock();
try {
Thread.sleep(2000);
} 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.