名词解释
POSIX: Portable Operating System Interface
A cross-platform specification supported by UNIX operating systems and those considered UNIX-like, such as Linux. The X in the name originally denoted that the interface was “based on UNIX.”
System V
The specification that defines the requirements for an operating system to be considered UNIX.
下表列出了System V IPC 和 POSIX IPC的区别:
SYSTEM V | POSIX |
---|---|
AT & T introduced (1983) three new forms of IPC facilities namely message queues, shared memory, and semaphores | Portable Operating System Interface standards specified by IEEE to define application programming interface (API). POSIX covers all the three forms of IPC |
SYSTEM V IPC covers all the IPC mechanisms viz., pipes, named pipes, message queues, signals, semaphores, and shared memory. It also covers socket and Unix Domain sockets. | Almost all the basic concepts are the same as System V. It only differs with the interface |
Shared Memory Interface Calls shmget(), shmat(), shmdt(), shmctl() | Shared Memory Interface Calls shm_open(), mmap(), shm_unlink() |
Message Queue Interface Calls msgget(), msgsnd(), msgrcv(), msgctl() | Message Queue Interface Calls mq_open(), mq_send(), mq_receive(), mq_unlink() |
Semaphore Interface Calls semget(), semop(), semctl() | Semaphore Interface Calls Named Semaphores sem_open(), sem_close(), sem_unlink(), sem_post(), sem_wait(), sem_trywait(), sem_timedwait(), sem_getvalue() Unnamed or Memory based semaphores sem_init(), sem_post(), sem_wait(), sem_getvalue(),sem_destroy() |
Uses keys and identifiers to identify the IPC objects. | Uses names and file descriptors to identify IPC objects |
NA | POSIX Message Queues can be monitored using select(), poll() and epoll APIs |
Offers msgctl() call | Provides functions (mq_getattr() and mq_setattr()) either to access or set attributes 11. IPC - System V & POSIX |
NA | Multi-thread safe. Covers thread synchronization functions such as mutex locks, conditional variables, read-write locks, etc. |
NA | Offers few notification features for message queues (such as mq_notify()) |
Requires system calls such as shmctl(), commands (ipcs, ipcrm) to perform status/control operations. | Shared memory objects can be examined and manipulated using system calls such as fstat(), fchmod() |
The size of a System V shared memory segment is fixed at the time of creation (via shmget()) | We can use ftruncate() to adjust the size of the underlying object, and then re-create the mapping using munmap() and mmap() (or the Linux-specific mremap()) |
实例!
话不多说,来分别看两个实例吧:
POSIX IPC
1 | //C program for Producer process illustrating POSIX shared-memory API. |
编译命令:1
gcc ipc_posix.c -o ipc_posix -lrt
System V IPC
1 |
|
编译命令:1
gcc ipc_sysv.c -o ipc_sysv
Refence
UNIX System V
System V & Posix
Linux kernel interfaces
How to use shared memory with Linux in C