I'm trying to solve following quiz:
The confluence of 3 two-way roads is managed by a French-style roundabout so:
- the right of way is for those who are already traveling at the roundabout;
- the direction of travel is counterclockwise.
Each sector i of the roundabout (section between two consecutive accesses i and i+1 mod3 ) can contain a maximum MAX_i number of cars.
Represent the the behavior of cars when crossing the roundabout.
Each car is represented by by a process that can recall three functions:
- INPUT(i): used to access from outside to the roundabout from access i;
- NEXT(i): used to move from sector i of the roundabout to the next one;
- EXIT(i): used to exit the roundabout at the end of sector i.
For simplicity, set MAX_i = 1 for each sector and simplify the priority rule. for vehicles coming from outside, establishing that a car can enter the roundabout access only if the sector is not currently occupied and the sector is not previous. The issue of deadlock is being discussed.
This is my solution, what do you think?
I think that the deadlock can be caused only if all the process try to access to the same sector, right?class Example { semaphore mutex = 1; semaphore sectors[3] = {0, 0, 0}; boolean isFree = true; public void input (int i) { P(mutex); while (sectors[i]==0) { //wait for the sector } isFree = true; P(sectors[i]); V(mutex); } public void next (int i) { P(mutex); while (sectors[i+1]==0) { //wait for the next sector } isFree = true; V(mutex); } public void exit (int i) { P(mutex); //only 1 process can enter in the rondabout if (isFree) { //check the rondabout isFree = false; V(sectors[i]); } V(mutex); }