Consider the following solution to the producer-consumer synchronization problem. The shared buffer size is . Three semaphores , and are defined with respective initial values of and . Semaphore denotes the number of available slots in the buffer, for the consumer to read from. Semaphore denotes the number of available slots in the buffer, for the producer to write to. The placeholder variables, denoted by , , and , in the code below can be assigned either or . The valid semaphore operations are: and .
Which one of the following assignments tp , , and will yield the correct solution?
View worked solution
Answer: C
To solve this problem, we must carefully read the non-standard definitions of the semaphores given in the question:
empty: Initialized to . It denotes the number of available slots in the buffer for the consumer to read from (i.e., the number of filled slots containing items).full: Initialized to . It denotes the number of available slots in the buffer for the producer to write to (i.e., the number of empty slots).
1. Producer Analysis
- Before adding an item to the buffer, the producer must check if there is an empty slot available to write. This corresponds to waiting on the semaphore that tracks write slots:
- After adding the item, the producer must signal that a new item is now available for the consumer to read:
2. Consumer Analysis
- Before consuming an item, the consumer must check if there is at least one filled slot available to read. This corresponds to waiting on the semaphore that tracks read slots:
- After consuming the item, the consumer must signal that a slot has been freed up, making it available for the producer to write:
Conclusion
Combining these assignments, we get:
This matches Option C.
Free practice · Your answers on this page are not saved.
