What are the trade-offs between using a System V and a Posix semaphore?
相关问题
- Is shmid returned by shmget() unique across proces
- Why it isn't advised to call the release() met
- Why should we check WIFEXITED after wait in order
- Should “operator !=” always be implemented via “op
- UNIX Bash - Removing double quotes from specific s
相关文章
- How do I get characters common to two vectors in C
- Making new files automatically executable?
- Reverse four length of letters with sed in unix
- What does it take to be durable on Linux?
- Extracting columns from text file using Perl one-l
- Problem with piping commands in C
- Makefile and use of $$
-
Compare Delegates Action
I know this is old, but for the benefit of those still reading this courtesy of Google, the #1 reason I find to use System V semaphores over POSIX (system-level) semaphores is the ability to acquire the semaphore resource in a way which is automatically returned by the kernel NO MATTER HOW THE PROCESS EXITS.
I agree that the multiple (atomic) semaphore operations are rarely used (although they can be useful during staging), and that the System V interface is bizarre, but there's simply no way of reliably achieving the same clean-up semantics with POSIX semaphores.
From O'Reilly:
I wonder what makes people design bad API's like System V Semaphores! Uneless you have very strong reasons to go with System V semaphores (such as atomic operations with multiple increment-decrement in a single step), you should stick with POSIX named semaphores.
The linked article discusses what's wrong and non-intuitive with System V Semaphores.
Two major problems with POSIX shared/named semaphores used in separate processes (not threads): POSIX semaphores provide no mechanism to wake a waiting process when a different process dies while holding a semaphore lock. This lack of cleanup can lead to zombie semaphores which will cause any other or subsequent process that tries to use them to deadlock. There is also no POSIX way of listing the semaphores in the OS to attempt to identify and clean them up. The POSIX section on SysV IPC does specify the ipcs and ipcrm tools to list and manipulate global SysV IPC resources. No such tools or even mechanisms are specified for POSIX IPC, though on Linux these resources can often be found under /shm. This means that a KILL signal to the wrong process at the wrong time can deadlock an entire system of interacting processes until reboot.
Another disadvantage is the use of file semantics for POSIX semaphores. The implication is that there can be more than one shared semaphore with the same name, but in different states. For example a process calls sem_open, then sem_unlink before sem_close. This process can still use the semaphore just like unlinking an open file before closing it. Process 2 calls sem_open on the same semaphore between the sem_unlink and sem_close calls of process 1, and (according to documentation) gets a brand new semaphore with the same name, but in a different state than process 1. Two shared semaphores with the same name operating independently defeats the purpose of shared semaphores.
Limitation one above makes POSIX shared semaphores unusable in a real-world system without a guarantee that uncatchable signals can never be sent. Limitation two can be mitigated by careful coding, assuming control over all code that will use a given semaphore. Frankly, its more than a bit surprising they made it into the standard as they are.