Note: Please indicate the source when reprinting, all rights reserved.Note: This is based on my own understanding,if it conflicts with your principles and ideas, please forgive me and do not criticize.
Environment Description
Linux 4.4.0-31-generic #50-Ubuntu SMP Wed Jul 13 00:07:12 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Introduction
- 1. IPC – Inter-Process Communication
- 2. UNIX and LINUX do not have a direct lineage; it is a newly created operating system. LINUX can be said to be a system written based on MINIX (a UNIX-like system, the reasons for which can be searched online).
- 3. System V is a branch of the UNIX system. POSIX is a standard customized by IEEE (Portable Operating System Interface, abbreviated as POSIX).
- 4. There are many types of IPC, including the following:
- • A Original UNIX IPC
- • B System V IPC evolved from A
- • C Socket IPC evolved from A
- • D IPC defined in the POSIX standard by the IEEE
- • E Linux IPC developed from A, B, C, and D
IPC
Linux IPC
Due to some historical reasons, the common IPC in the current Linux kernel is System V and POSIX communication. As for Socket IPC, it has been separated into Linux network communication, which is essentially still IPC, just transformed from single-machine IPC to multi-machine IPC (mainly supported by the TCP/IP protocol).
System V IPC
- 1. System V IPC mainly includes: System V message queues, System V semaphores, and System V shared memory segments.
- 2. A notable feature of System V IPC is that there is a key that represents this IPC, and this key must be unique. There are various methods to obtain this key, but I have only used one, which is to use ftok() to get it.
- 3. API Collection
- • A System V Message Queue
- • msgget() accesses or creates IPC objects based on key and flag.
- • msgctl() controls and destroys IPC objects.
- • msgsnd/msgrcv() sends/receives messages.
- • B System V Semaphore
- • semget() accesses or creates IPC objects based on key and flag.
- • semctl() controls and destroys IPC objects.
- • semop() operates on IPC objects, implementing the PV operation.
- • C System V Shared Memory Segment
- • shmget() accesses or creates IPC objects based on key and flag.
- • shmctl() controls and destroys IPC objects.
- • shmat() attaches shared memory.
- • shmdt() detaches from shared memory.
POSIX IPC
The characteristics of POSIX IPC are also very clear compared to System V. First, the API naming has changed. It is composed of IPC name_IPC operation. Secondly, it uses a name as an identifier to represent an IPC object. For example, semaphore family: sem_open()/sem_close()/sem_trywait()/sem_post()
- • A POSIX Semaphore (named semaphore, unnamed semaphore for related process communication)
- • sem_open()/sem_close() opens or creates a semaphore / closes the semaphore.
- • sem_wait()/sem_trywait() V operation.
- • sem_post() P operation.
- • sem_unlink() deletes the semaphore.
- • sem_getvalue() gets the value.
- • sem_init() initializes unnamed semaphore.
- • sem_destroy() destroys unnamed semaphore.
- • B POSIX Shared Memory
- • shm_open()/shm_unlink() opens the memory area / deletes the memory area.
- • ftruncate() clears the memory area.
- • fstat() gets the attributes of the file corresponding to the file descriptor after the file is opened.
- • mmap() maps data (file) to the current process space.
- • munmap() is the opposite of mmap.
- • C POSIX Message Queue
- • mq_open()/mq_close() can be obtained similarly.
- • mq_unlink() can be obtained similarly.
- • mq_getattr()/mq_setattr() sets or gets the attributes of the message queue.
- • mq_send() has the same meaning as in English.
- • mq_receive() has the same meaning as in English.
- • mq_notify() has the same meaning as in English.
IPC – Pipes
Pipes are divided into named pipes and unnamed pipes, which can be compared to POSIX semaphores.
- • unnamed pipe API:
- • pipe() creates an unnamed pipe.
- • named pipe/fifo API:
- • mkfifo() creates a named pipe.
- • unlink() deletes a named pipe.
- • Pipe operations:
- • read()/write()
Conclusion
From the above, we can see that POSIX IPC is simpler to use than System V. However, it also has some shortcomings. We usually use these things in combination to complete our work well. Of course, there is an increasing tendency to write POSIX IPC because it is easier to port.
Friendly reminder: Before using, we should read the help documentation more, as it can help you avoid many pitfalls.
References
None
Donations, subscriptions, favorites, throwing bananas, coins, please follow the public account.
Note: Please respect original work, if you dislike it, please do not criticize.Note: Please indicate the source when reprinting, all rights reserved.Note: If you have questions, please leave a message, I will reply as soon as I see it.