Problem

Now consider another correct solution to the preceding problem:1 semaphore mutex = 1, bloc...

Now consider another correct solution to the preceding problem:

1 semaphore mutex = 1, block = 0;  /* share variables: semaphores, */

2 int active = 0, waiting = 0; /* counters, and */

3 boolean must_wait = false; /* state information */

4 

5 semWait (mutex); /* Enter the mutual exclusion */

6 if(must_wait) { /* If there are (or were) 3, then */

7  ++waiting; /* we must wait, but we must leave */

8  semSignal (mutex); /* the mutual exclusion first */

9  semWait (block); /*wait for all current users to depart */

10    –waiting;                          /* We’ve got the mutual exclusion; update count */

11    }

12    ++active; /* update active count, and remember */

13    must_wait = active == 3; /* remember if the count reached 3 */

14 if(waiting > 0 && !must_wait) /* if there are others waiting */

15       semSignal (block);; /* and we don’t yet have 3 active, */

16          /* unblock a waiting process */

17       Else semSignal (mutex); /* otherwise open the mutual exclusion */

18        

19       /* critical section */

20        

21       semWait (mutex); /* Enter mutual exclusion */

22       - -active; /* and update the active count */

23       If (active == 0) /* If last one to leave? */

24       must_wait = false; /* set up to let new processes enter */

25       if (waiting = = 0) && !must_wait) /* if there are others waiting */

26       semSignal (block); ; /* and we don’t have 3 active, */

27           /* unblock a waiting process */

28        else semSignal (mutex); /* otherwise open the mutual exclusion */

a.      Explain how this program works and why it is correct.

b.      Does this solution differ from the preceding one in terms of the number of processes that can be unblocked at a time? Explain.

c.       This program is an example of a general design pattern that is a uniform way to implement solutions to many concurrency problems using semaphores. It has been referred to as the Pass The Baton pattern. Describe the pattern.

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 5