One way to do this, with POSIX semaphores, is to initialize a semaphore to a value equal to the maximum buffer-depth you want. A process wishing to add an entry must first sem_wait() on a semaphore which contains this count. It then is entitled to add an entry to the queue, which action is controlled by another (this time, binary) semaphore. The consumer process that removes entries from the buffer then issues a sem_post() against the count semaphore to allow processes that are waiting for permission to post a new entry to do so.
Notice there are two semaphores. One, with a count, controls the size of the buffer. The second, binary, secures the shared data-structure itself.
|