A concurrent system consists of 3 processes using a shared resource R in a non-preemptible and mutually exclusive manner. The processes have unique priorities in the range 1...3, 3 being the highest priority. It is required to synchronize the processes such that the resource is always allocated to the highest priority requester. Shared data: mutex semaphore = 1 /* initialized to 1 */ process[i] semaphore = 0 /* initialized to 0 */ R_requested[i] boolean = false /* initialized to false */ busy boolean = false /* initialized to false */ Code for processes: begin process my_priority_integer; my_priority := /* in the range 1..3 */ repeat request_R(my_priority); /* use shared resource R */ release_R(my_priority); forever end process Procedures: procedure request_R(priority); begin P(mutex); if busy = true then begin R_requested[priority] := true; V(mutex); P(process[priority]); end else begin busy := true; V(mutex); end end; procedure release_R(priority); begin /* TO BE COMPLETED */ end; Give the pseudo-code for the procedure release_R. A. Set busy=false and signal the highest priority waiting process B. Simply call V(mutex) to release mutual exclusion C. Set R_requested[priority]=false and V(process[priority]) D. Busy-wait until no process is requesting R, then set busy=false
GATE 1997 · Operating System · Process Synchronization · medium
Answer: release_R scans R_requested[] from priority 3 to 1; if a waiter exists it clears the flag and signals that process's semaphore; otherwise it sets busy=false. This always grants R to the highest-priority requester.
- Acquire mutex to protect shared state: Before reading or modifying R_requested[] or busy, call P(mutex) to ensure mutual exclusion over the critical data.
- Scan for highest-priority waiter: Loop from j=3 down to j=1. The first j with R_requested[j]=true is the highest-priority waiter. Set R_requested[j]:=false, call V(mutex) to release the lock, then call V(process[j]) to wake up that process. R remains busy (busy stays true). Return immediately.
- No waiter found — mark resource free: If the loop completes without finding any R_requested[j]=true, set busy:=false and call V(mutex). The resource is now free for the next requester.