Detecting Requests Using Posix Semaphores
You k开发者_JAVA技巧now we can use message queues with the function mq_receive(); what is a good way to implement that functionality (you know, waiting until the shared data is changed) with semaphores?
The standard way:
- The consumer waits on the semaphore for the producer to indicate that there is something ready for the consumer to consume.
- The produce signals the semaphore when there is something ready for the consumer to consume.
If you have multiple consumers and multiple producers, you ensure that the semaphore has enough range to allow for multiple requests to be queued by the producers and you ensure that the consumers know how to handle perhaps several of them being active at once. All of this is standard multi-processing (multi-threading) theory, though.
If you need a run-down on the operations required, then you need to look at the POSIX manual pages for:
System V IPC
- semctl()
- semget()
- semop()
POSIX IPC
- sem_close()
- sem_destroy()
- sem_getvalue()
- sem_init()
- sem_open()
- sem_post()
- sem_timedwait()
- sem_trywait()
- sem_unlink()
- sem_wait()
精彩评论