Example: Producer/consumer Problem
In the Producer-consumer problem, one process (the producer) generates data items and another process (the consumer) receives and uses them. They communicate using a queue of maximum size N and are subject to the following conditions:
- The consumer must wait for the producer to produce something if the queue is empty.
- The producer must wait for the consumer to consume something if the queue is full.
The semaphore solution to the Producer-consumer problem tracks the state of the queue with two semaphores: emptyCount
, the number of empty places in the queue, and fullCount
, the number of elements in the queue. To maintain integrity, emptyCount
may be lower (but never higher) than the actual number of empty places in the queue, and fullCount
may be lower (but never higher) than the actual number of items in the queue. Empty places and items represent two kinds of resources, empty boxes and full boxes, and the semaphores emptyCount
and fullCount
maintain control over these resources.
The binary semaphore useQueue
ensures that the integrity of the state of the queue itself is not compromised, for example by two producers attempting to add items to an empty queue simultaneously, thereby corrupting its internal state. Alternatively a Mutex could be used in place of the binary semaphore.
The emptyCount
is initially N, fullCount
is initially 0, and useQueue
is initially 1. The producer does the following repeatedly:
The consumer does the following repeatedly:
consume: P(fullCount) P(useQueue) item ← getItemFromQueue V(useQueue) V(emptyCount)Example.
- A single consumer enters its critical section. Since
fullCount
is 0, the consumer blocks. - Several producers enter the producer critical section. No more than N producers may enter their critical section due to
emptyCount
constraining their entry. - The producers, one at a time, gain access to the queue through
useQueue
and deposit items in the queue. - Once the first producer exits its critical section,
fullCount
is incremented, allowing one consumer to enter its critical section.
Note that emptyCount
may be much lower than the actual number of empty places in the queue, for example in the case where many producers have decremented it but are waiting their turn on useQueue
before filling empty places. Note that emptyCount + fullCount ≤ N
always holds, with equality if and only if no producers or consumers are executing their critical sections.
Read more about this topic: Semaphore (programming)
Famous quotes containing the words consumer and/or problem:
“Vice is its own reward. It is virtue which, if it is to be marketed with consumer appeal, must carry Green Shield stamps.”
—Quentin Crisp (b. 1908)
“Great speeches have always had great soundbites. The problem now is that the young technicians who put together speeches are paying attention only to the soundbite, not to the text as a whole, not realizing that all great soundbites happen by accident, which is to say, all great soundbites are yielded up inevitably, as part of the natural expression of the text. They are part of the tapestry, they arent a little flower somebody sewed on.”
—Peggy Noonan (b. 1950)