Philip Semanchuk大神的答案,和不同人的看法,大部分还是偏向与POSIX的,一般水平的人直接用posix,对kernel玩的溜的随便选。
Both have the same basic tools -- semaphores, shared memory and message queues. They offer a slightly different interface to those tools, but the basic concepts are the same. One notable difference is that POSIX offers some notification features for message queues that Sys V does not. (See mq_notify()
.)
Sys V IPC has been around for longer which has a couple of practical implications --
First, POSIX IPC is less widely implemented. I wrote a Python wrapper for POSIX IPC and its documentation lists what I know about POSIX IPC implementations on various platforms.
On all of the platforms listed in that documentation, Sys V IPC is completely implemented AFAIK, whereas you can see the POSIX IPC is not.
The second implication of their relative age is that POSIX IPC was designed after Sys V IPC had been used for a while. Therefore, the designers of the POSIX API were able to learn from the strengths and weaknesses of the Sys V API. As a result the POSIX API is simpler and easier to use IMO, and I recommend it over the Sys V API.
I should note that I've never run any performance tests to compare the two. I would think that the older API (Sys V) would have had more time to be performance tuned, but that's just speculation which is of course no substitute for real-world testing.
As to why there are two standards -- POSIX created their standard because they thought it was an improvement on the Sys V standard. But if everyone agreed that POSIX IPC is better, many many many programs still use Sys V IPC and it would take years to port them all to POSIX IPC. In practice, it would not be worth the effort so even if all new code used POSIX IPC as of tomorrow, Sys V IPC would stick around for many years.
We can't tell you which you should use without knowing a lot more about what you intend to do, but the answers you have here should give you enough information to decide on your own.
这里有个systemv的优点
There is one important difference that man-pages and other articles fail to highlight i.e. sysv message queues have a notion of delivering messages by mtype (posix msgq lacks this). In some cases, this could be an important design element and to quote my experience it turned out to be a show stopper. I had blogged about it. – nyrahul Apr 3 at 8:10
- I believe the major difference is that all POSIX IPC is thread-safe, while most SysV IPC is NOT [1].
- Because of Unix wars [2]. The Single UNIX specification (SUS) [3], aka POSIX, was created to standardise interfaces on Unix-based systems.
- You probably want POSIX. Depends exclusively on your requirements.
sysv是早期实践中弄出来的,posix是后来标准化之后的产物。这个导致了posix的接口设计更加严谨完善,但特殊情况下处理消息不及sysv灵活
posix ipc的那套接口做到了线程安全,sysv的就不见得了
如果你只考虑在linux上开发,就选posix那套就行了,还能和select/poll这些好东西一起用
如果还要考虑其他平台的兼容性,那就具体情况具体分析了,有些平台上有些库对posix实现的完整程度还不如sysv
-
The semaphores, queues and shared memory for Posix have Ascii string names, while under System V these are given with integer number.
-
The System V semaphores allows to be automatically released if process dies (semop SEM_UNDO flag). There is no such thing for Posix.
-
On Linux and FreeBSD there is big advantage of posix queues, as handler given by mq_open are basically file descriptor which can be polled/epolled/selected/kqueued.