Tutsol5

.pdf
School
Nanyang Technological University**We aren't endorsed by this school
Course
CS SC2005
Subject
Computer Science
Date
Dec 18, 2024
Pages
8
Uploaded by MegaBoarMaster1218
Multiplayer Game1. Alice is playing a game that involves pairing apples with oranges. The game produces apples and oranges in separate baskets at random instants. For Alice to get points, she must pair exactly 1 apple with 2 orangesby picking them from the respective baskets. Each basket is protected by a semaphore; A for the apple basket and O for the orange basket. To access a basket, Alice must first acquire the corresponding semaphore.Complete the code in the below figure to help Alice to play this game. You should only use the operations Wait(A), Wait(O), Signal(A) and Signal(O) in the boxes provided. You may use any number of these operations (including zero) in each of the boxes. Your solution must be deadlock-free even in the presence of other players who may be executing a different code.1// Pick 1 appleIf (atleast 2 oranges in basket) {** Pick 2 oranges and pair with apple. **} else {** Drop 1 apple back in basket **}12345
Background image
Multiplayer Game1.First acquire an apple and release semaphore A.2.If there are at least 2 oranges available, then acquire them and release semaphore O.3.Else release semaphore O, drop the apple back and release semaphore A.There is no deadlock, because the two semaphores are never acquired together (no nesting).2// Pick 1 appleIf (atleast 2 oranges in basket) {** Pick 2 oranges and pair with apple. **} else {** Drop 1 apple back in basket **}1: Wait(A)2: Signal(A)Wait(O)3: Signal(O)4: Signal(O)Wait(A)5: Signal(A)
Background image
Semaphore System Calls2. Consider the following implementations of the system calls Wait() and Signal().Suppose these system calls are guaranteed to be atomic and a Semaphore S in this system is used to protect critical sections as shown below. Assume S is initialized to 0. Explain if this solution ensures mutual exclusion and progress with justifications?3Wait (Semaphore S) {S.value--; If (S.value < -1) { block(); }}Signal (Semaphore S) { S.value++; If (S.value <= 0) { wakeup(); }}Process Pi: While(1){Wait(S);Critical-section for PiSignal(S);Remainder-section for Pi}
Background image
Semaphore System CallsIt satisfies mutual exclusion. The first process to execute Wait() will get access to the critical section and the value of S becomes 1 (since the condition check in Wait() is < -1). Thereafter, no other process can enter its critical section until the first process executes Signal(). Also, since Wait() and Signal() are executed in pairs in that order and the Semaphore is initialized to 0, its value can never be greater than 0.It satisfies progress as well. When the value of S increments from -1 to 0, Signal() will execute an unnecessary wakeup() although no process is waiting for the Semaphore. If the value of S is less than -1, then processes are blocked, and any execution of Signal() will wakeup() a blocked process. So, no process is blocked while the Semaphore is available, i.e., its value is 0.4
Background image
Semaphore Implementation3. Describe how Wait(S) and Signal(S) of a semaphore can be implemented using a TestAndSetinstruction, given the below semaphore structure definition. Hint: The semaphore value and the process queue L are shared variables among different processes when those processes access the semaphore.5
Background image
Where is the Critical Section?6//Current implementation of Wait(S)//Current implementation of Signal(S)Put current process to waiting state;Add current process to S.L;Add process P to ready queue; Remove a process P from S.L;Critical sectionRemainder sectionNotentire block() and wakeup() functions are in the critical section.
Background image
Semaphore Implementation7wait(S) Shared variable: Boolean lock=false;Put current process to waiting state;
Background image
Semaphore Implementation (Cont’)8Signal(S) Add process P to ready queue;
Background image