Assignment 3
Assignment 3
Assignment: 3
SP22-BCS-136
#include <mutex>
#include <thread>
#include <iostream>
mutex mtx;
lock_guard<mutex> lock(mtx);
cout << "Thread " << threadId << " acquired the lock." << endl;
this_thread::sleep_for(chrono::milliseconds(100));
cout << "Thread " << threadId << " releasing the lock." << endl;
}
int main()
t1.join();
t2.join();
t3.join();
return 0;
output:
1. Semaphore Implementation:
#include <condition_variable>
#include <atomic>
#include <mutex>
#include <thread>
#include <iostream>
mutex mtx;
condition_variable cv;
atomic<int> count;
unique_lock<mutex> lock(mtx);
cv.wait(lock, []
cout << "Thread " << threadId << " acquired the semaphore." << endl;
this_thread::sleep_for(chrono::milliseconds(100));
cout << "Thread " << threadId << " releasing the semaphore." << endl;
lock.unlock();
cv.notify_one();
int main()
count.store(0);
t1.join();
t2.join();
t3.join();
t4.join();
t5.join();
return 0;
output:
#include <iostream>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <mutex>
#include <thread>
#include <sys/wait.h>
struct SharedMemory
{
std::mutex mtx;
int turn;
bool flag[PROCESS_COUNT];
munmap(shm, SHARED_MEMORY_SIZE);
}
int main()
{
pid_t pids[PROCESS_COUNT];
if (pids[i] == 0)
{
process(i);
return 0;
}
else if (pids[i] < 0)
{
std::cerr << "Error in fork." << std::endl;
return 1;
}
}
return 0;
}
output:
3. Producer-Consumer Problem with Mutex:
#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <chrono>
queue<int> sharedBuffer;
mutex mtx;
unique_lock<mutex> lock(mtx);
cvProducer.wait(lock, []
{ return sharedBuffer.size() < BUFFER_SIZE; });
int item = i;
sharedBuffer.push(item);
cout << "Producer " << id << " produced item " << item << endl;
cvConsumer.notify_one();
lock.unlock();
this_thread::sleep_for(chrono::milliseconds(100));
finishedProducing = true;
unique_lock<mutex> lock(mtx);
cvConsumer.wait(lock, []
cout << "Consumer " << id << " consumed item " << item << endl;
cvProducer.notify_one();
lock.unlock();
this_thread::sleep_for(chrono::milliseconds(100));
int main()
producer1.join();
producer2.join();
consumer1.join();
consumer2.join();
return 0;
output:
4. Reader-Writer Problem with Semaphores:
#include <iostream>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <semaphore.h>
sem_t readers_mutex;
int readers = 0;
{
sem_wait(&readers_mutex);
readers++;
if (readers == 1)
sem_wait(&wr_mutex);
sem_post(&readers_mutex);
std::cout << "Reader " << id << " started reading." << std::endl;
// simulate reading
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "Reader " << id << " finished reading." << std::endl;
sem_wait(&readers_mutex);
readers--;
if (readers == 0)
{
sem_post(&wr_mutex);
sem_post(&readers_mutex);
sem_wait(&wr_mutex);
std::cout << "Writer " << id << " started writing." << std::endl;
// simulate writing
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "Writer " << id << " finished writing." << std::endl;
sem_post(&wr_mutex);
}
int main()
sem_init(&rw_mutex, 0, 1);
sem_init(&wr_mutex, 0, 1);
sem_init(&readers_mutex, 0, 1);
reader1.join();
reader2.join();
reader3.join();
reader4.join();
reader5.join();
writer1.join();
writer2.join();
sem_destroy(&rw_mutex);
sem_destroy(&wr_mutex);
sem_destroy(&readers_mutex);
return 0;
output: