Consider this variation of the Readers-Writer Problem. There is an intersection of two roads: along one road, automobiles (processes) drive North-or-South; along the other road, automobiles drive East-or-West. At the intersection, there is no traffic light. Instead, the following rules apply to automobile A approaching the intersection:
a) if the intersection is empty (available), then it is OK for A to proceed
b) if an automobile B is already in the intersection then
i) if automobile A is going along the same road (e.g., A and B are both going North-or-South, independent of direction) then it is OK to proceed
ii) if automobile B is going along the other road (e.g., A is going North-or-South but B is going East-or-West), then it is NOT OK to proceed.
Assume the existence of these global variables:
int NS: number of North-or-South bound automobiles in the intersection, initially 0.
int EW: number of East-or-West bound automobiles in the intersection, initially 0.
Semaphore NSmutex: to protect NS variable, initially available.
Semaphore EWmutex: to protect EW variable, initially available.
Semaphore intersectionAvailable: initially available;
Write the code for the North-or-South bound automobile process.
Be sure to include this comment at the appropriate place in the code:
/*driving in the intersection */