2
Table Of Content
S. No. Experiment Pg No. Sign
1 rogram to implement non token
P 3
based algorithm for Mutual
Exclusion
2 rogram to implement Lamport’s
P 5
Logical Clock
3 rogram to implement edge chasing 7
P
distributed deadlock detection
algorithm.
4 rogram to implement locking
P 8
algorithm for mutual exclusion
5 rogram to implement Remote
P 9
Method Invocation.
6 rogram to implement Remote
P 11
Procedure Call.
7 Program to implement Chat Server. 12
8 rogram to implement termination
P 14
Detection
7
ab 3. Program to implement edge chasing distributed
L
deadlock detection algorithm
Code:
include <bits/stdc++.h>
#
using namespace std;
bool deadlock(vector<vector<int>> sites, vector<vector<int>> edges){
int n = edges.size();
sort(edges.begin(), edges.end());
int a = sites[edges[0][0]][sites[edges[0][0]].size()-1];
int b = sites[edges[0][1]][0];
vector<int> probe = {1, a,b};
for(int i = 1; i<n; i++){
int a = sites[edges[i][0]][sites[edges[i][0]].size()-1];
int b = sites[edges[i][1]][0];
probe[1] = a;
probe[2] = b;
if(probe[0]==probe[2]){
return true;
}
}
return false;
}
int main(){
vector<vector<int>> sites = {{1,2,3}, {4,5}, {6,7,8}, {9,10,11,12}};
vector<vector<int>> edges = {{0,1}, {1,2}, {2,3},{3,0}};
if(deadlock(sites, edges)){
cout<<"Deadlock Detected";
}
else{
cout<<"No Deadlock Detected";
}
return 0;
}
Output:
8
ab 4. Program to implement locking algorithm for mutual
L
exclusion
Code:
include <iostream>
#
#include <thread>
#include <mutex>
using namespace std;
//locking algorithm using mutal exlusion
mutex mtx; // Declare a mutex
void critical_section(int thread_id) {
// Simulate the critical section
mtx.lock(); // Lock the mutex to enter the critical section
cout << "Thread " << thread_id << " is in the critical section." << endl;
mtx.unlock(); // Unlock the mutex to exit the critical section
}
//this_thread::sleep_for(chrono::milliseconds(3000));
int main() {
const int num_threads = 4;
thread threads[num_threads];
for (int i = 0; i < num_threads; ++i) {
threads[i] = thread(critical_section, i);
}
for (int i = 0; i < num_threads; ++i) {
threads[i].join();
}
return 0;
}
Output:
14
ab 8. Program to implement termination detection
L
Code:
include <iostream>
#
#include <vector>
#include <thread>
#include <atomic>
#include <chrono>
using namespace std;
const int NUM_PROCESSES = 5;
atomic<int> completedProcesses(0);
// Simulate a process
void process(int id) {
// Simulate some work
this_thread::sleep_for(chrono::milliseconds(2000));
// Signal that this process has completed
completedProcesses.fetch_add(1, memory_order_relaxed);}
int main() {
vector<thread> processes;
// Create and start the processes
for (int i = 0; i < NUM_PROCESSES; ++i) {
processes.push_back(thread(process, i));
cout<<"Process "<<i+1<<" created"<<endl;}
// Wait for all processes to complete
for (thread& t : processes) {
t.join();
while (completedProcesses.load(memory_order_relaxed) <
NUM_PROCESSES) {
cout << "Waiting for processes to complete..." << endl;
this_thread::sleep_for(chrono::milliseconds(500));}}
// Termination detection
cout<<completedProcesses.load(memory_order_relaxed)<<endl;
cout << "All processes have completed." << endl;
return 0;}
Output: