Problem

Consider a sharable resource with the following characteristics: (1) As long as there are...

Consider a sharable resource with the following characteristics: (1) As long as there are fewer than three processes using the resource, new processes can start using it right away. (2) Once there are three process using the resource, all three must leave before any new processes can begin using it. We realize that counters are needed to keep track of how many processes are waiting and active, and that these counters are themselves shared resources that must be protected with mutual exclusion. So we might create the following solution:

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       semWait(mutex); /*Reenter the mutual exclusion */

11       - - waiting; /* and update the waiting count */

12       }

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

14       must_wait = active == 3; /* if the count reached 3 */

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

16        

17       /* critical section */

18        

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

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

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

22       int n;

23       if (waiting<3) n = waiting;

24       else n = 3;  if so, unblock up to 3 */

25       while (n > 0) { /* waiting processes */

26       semSignal (block);

27       - - n;

28       }

29       must_wait = false; /* All active processes have left */

30       }

31        semsignal (mutex); /* Leave the mutual exclusion */

The solution appears to do everything right: All accesses to the shared variables are protected by mutual exclusion, processes do not block themselves while in the mutual exclusion, new processes are prevented from using the resource if there are (or were) three active users, and the last process to depart unblocks up to three waiting processes.

a. The program is nevertheless incorrect. Explain why.

b. Suppose we change the if in line 6 to a while. Does this solve any problem in the program? Do any difficulties remain?

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