Question
a) Implement a semaphore class that has a private int and three public methods: init, wait and signal. The wait and signal methods should behave
a) Implement a semaphore class that has a private int and three public methods: init, wait and signal. The wait and signal methods should behave as expected from a semaphore and must use Peterson's N process algorithm in their implementation. (50 points) b) Write a program that creates 5 threads that concurrently update the value of a shared integer and use an object of semaphore class created in part a) to ensure the correctness of the concurrent updates. (10 points). The code should be in C or C++. If you use C, the semaphore could be a struct (and not a class) and the int inside wont be private. Hint: One way to do this would be to use Peterson's algorithm to enforce mutual exclusion among threads trying to access the semaphore's value (the private int) concurrently. Accessing the semaphore's value would be the critical section inside the wait/signal methods. A thread would need to play the tournament every time it wants to enter the critical section. After a thread is done accessing the semaphore's value, a thread should indicate that it is no longer playing the tournament to allow other threads to enter the critical section. Inside the wait method, a thread can execute a loop in each iteration of which the thread plays the tournament and then checks the value of the semaphore. If the semaphore value is more than zero, the thread decrements the value, declares that it is not playing the tournament now and exits the loop (and the wait() method). On the other hand, if the semaphore value is zero, the thread declares that it is not playing the tournament now and starts the next iteration of the loop. In the signal method, the thread plays the tournament, increments the semaphore value and finally declares that it is no longer playing the tournament.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started