I. Inter-process communication
Each process has a different user address space, the global variables for any one process are not visible in another process, so the data must be exchanged between processes through the kernel, a buffer is created in the kernel, process 1 copies the data from the user space to the kernel buffer, and the process 2 reads the data from the kernel buffer. This mechanism provided by the kernel is called interprocess communication (ipc,interprocess communication). As shown in the following figure.
Second, the pipeline is one of the most basic IPC mechanism, created by the pipe function:
#include <unistd.h>
int pipe (int filedes[2]);
When the pipe function is invoked, a buffer (called a pipe) is used in the kernel to communicate, it has a read end, a write end, and then the Filedes parameter is passed out to the user program two file descriptors, Filedes[0] to the end of the pipe, filedes[1 to the end of the pipe (well remember, Just like 0 is standard input 1 is the same as standard output. So the pipe in the user program looks like an open file, through Read (Filedes[0]), or write (Filedes[1]), and reading and writing to this file is actually read and write to the kernel buffer. The pipe function call returned 0 successfully, and the call failed to return-1.
How do you implement communication between two processes after opening the pipeline? For example, you can follow the steps below to communicate.