Problem

Now consider this correct solution to the preceding problem:1 semaphore mutex = 1, block =...

Now consider this 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       } else {

11        ++active; /* update active count, and */

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

13       semSignal (mutex); /* Leave mutual exclusion */

14       }

15        

16       /* critical section */

17        

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

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

20       If (active == 0) { /* Last one to leave? */

21       int n;

22       if (waiting <3) n = waiting;

23       else n = 3;                               /* if so, see how many processes to unblock */

24       waiting -= n; /* Deduct this number from waiting count */

25       active = n; /* and set active to this number */

26       while (n > 0) { /* Now unblock the processes */

27       semSignal (block); /* one by one */

28       - - n;

29       }

30       must_wait = active == 3; /* Remember if the count is 3 */

31       }

32       semSignal (mutex); /* Leave the mutual exclusion */

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

b. This solution does not completely prevent newly arriving processes from cuttingin line but it does make it less likely. Give an example of cutting in line.

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 I'll Do It For You 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