interprocess communication (interprocess communication, IPC), Classic IPC: Pipelines, FIFO, Message Queuing, semaphores, and shared storage and sockets.
First, the pipeline
Pipelines are the oldest form of the IPC for UNIX systems, and all UNIX systems provide such communication mechanisms.
1 ·, Two limitations:
(1) Half-duplex, the data can only flow in one direction, now some systems can support full-duplex pipeline, but for the best portability, should be considered that the system does not support full-duplex pipeline;
(2) Pipelines can only be used between two processes that have a common ancestor;
2, the creation of pipelines:
It can be regarded as a special kind of file, for its read and write can also use ordinary read, write and other functions. However, it is not an ordinary file, it does not belong to any other file system, and it only exists in memory. Pipelines are created by calling the pipe function.
1 #include <unistd.h>23int pipe (int fd[2]); 4 5 // return value: If successful, return 0, if error, return -1.
The two file descriptor returned by the parameter FD: fd[0] is open for reading, fd[1] is open for write, fd[1] output is fd[0] input. Typically, the process calls the pipe first, then the fork, creating the IPC channel between the parent and child processes. What to do after fork depends on the direction of the data stream we want, for the parent process to close the read end of the pipeline from the parent process to the child process fd[0], and the child process to close the write end fd[1].
Inter-process communication of multi-process programming