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

Question:

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 processes 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:

semaphore mutex = 1, block = 0; int active = 0, waiting 1 /* share variables: semaphores, */ /* counters, and * / /* state information */ 2 = 0; 3 boolean must wait = false; 4 semwait (mutex); if (must_wait) { /* Enter the mutual exclusion */ 6 /*


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?

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question
Question Posted: