Brief description: Linux inter-process communication-pipe

A small note on the communication channels between Linux processes

1. Unnamed pipe

The pipe is a basic inter-process communication mechanism, which acts between relative processes to complete data transfer. A pipe can be created by calling the pipe system function. The prototype of the pipe function is as follows:

    #include <unistd.h>
    int pipe(int pipefd[2]);     
    pipefd[0]:读端
    pipefd[1]:写端
    return: 0 成功   -1 失败

Some characteristics of unnamed pipes:

  1. The pipeline looks like an open file is inserted here in the user program, and read and write data to this file through read(filedes[0]) or write(filedes[1])
  2. In essence, the pipeline is a fixed-size buffer in the kernel space; the read-write pipeline is to read and write this kernel buffer
  3. The kernel uses the ring queue mechanism to realize the pipeline with its buffer (4K) implementation; therefore, the pipeline has a unidirectional data flow characteristic
  4. The pipe does not belong to any kind of file system, so it will not be written to disk; it only exists in memory and is kernel space
  5. In this way, you can understand why pipes can only be used between related processes; because they are only stored in memory, the pipes obtained by pipe are different each time, and only the pipes inherited from the common ancestor are the same.
  6. The written content is added to the end of the pipeline buffer every time, and data is read from the head of the buffer every time
  7. The particularity of the pipeline makes it impossible to use the lseek function to jump within the pipeline
  8. After understanding the nature of the pipeline, you can understand many characteristics of the pipeline; for example,
    once the data is read, it does not exist in the pipeline and cannot be read repeatedly.
    This is because the pipeline is a circular queue mechanism and does not exist after the data is retrieved. The queue is
    half-duplex communication.
    Isn't this also because of the circular queue? . .
  9. Conduit for communication between the genetic process, the pipe as a special file, fully embodies the child inherits the parent process open files property
  10. The descriptor pipefd[0] and pipefd[1] obtained by the pipe pipe are no different from ordinary file descriptors, and they share a descriptor queue with ordinary file descriptors.
  11. The pipe can use the IO operation at the bottom of the close system to close one port (reader/write end), or both ports can be closed
  12. The unnamed pipe continues with the process, which means that when the last process that opened the pipe exits, the pipe will die and cease to exist.
  13. The pipeline capacity is divided into PIPE_CAPACITY and PIPE_BUF. The difference between the two is that PIPE_BUF defines the size of the kernel pipeline buffer. The size of this value is set by the kernel (usually 4K); and PIPE_CAPACITY refers to the maximum value of the pipeline , Namely capacity, is a buffer in kernel memory
  14. The capacity of the pipe PIPE_CAPACITY means the maximum number of bytes that can be written to a pipe each time the pipe function is used to create it (65535 tested on this machine)
  15. The read and write characteristics of the pipe (both read and write ends are open): When the capacity of the pipe is full, writing data in it will block; when the pipe is empty, the read block
  16. When one end of the read-write end is closed, it has the following characteristics: the
    write end of the pipe is closed and there is no data in the pipe, read returns 0 (as if reading to the end of the file),
    all the read ends of the pipe are closed, and the process terminates abnormally

pipeline
pipeline

The following is a description of the implementation of the data structure of the pipeline, which may give you a feeling of sudden realization:


In Linux, the pipeline implementation does not use a special data structure, but uses the file structure of the file system and the index node inode of the VFS. This is achieved by pointing two file structures to the same temporary VFS index node, and this VFS index node points to a physical page. There are two file data structures (representing two file descriptors), but they define different file operation routine addresses. One is the routine address that writes data to the pipe, and the other is read from the pipe The routine address of the data. In this way, the system call of the user program is still the usual file operation, but the kernel uses this abstract mechanism to realize the special operation of the pipeline.


2. Famous pipes

A well-known pipe is a way that can be used to communicate between non-related processes; also known as a FIFO file, it is a special type of file that exists in the file system as a file name, but its behavior Similar to the unnamed pipe mentioned earlier.

To use a well-known pipe, you first need to create a well-known pipe file. There are two methods as follows:

  1. Create by command:
mkfifo fifo
  1. Created by function:
#include<sys/types.h>
#include<sys/stat.h>
int mkfifo(const char *pathname,mode_t mode);

mkfifo() will create a special FIFO file based on the parameter pathname, the file must not exist , and the parameter mode is the permission of the file (mode%~umask)

Another thing to note is that you cannot create a pipe file fifo in a shared folder

The read and write operations of a named pipe are the same as those of an unnamed pipe. They all use the system IO read and write operations: read/write; opening a pipe file also uses the system IO open function: open, but its parameters have some noteworthy points:

int fifo_fd = open("xxx",flags);

The optional parameters of flags are:
O_RDWR
O_RDONLY
O_WRONLY
O_NONBLOCK

The option O_NONBLOCK means non-blocking. After adding this option, it means that the open call is non-blocking. If there is no such option, it means that the open call is blocking.

  1. For a FIFO file opened in read-only mode (O_RDONLY), if the open call is blocked (that is, the second parameter is O_RDONLY), it will not return unless a process opens the same FIFO for writing; if open is called It is non-blocking (that is, the second parameter is O_RDONLY | O_NONBLOCK), even if no other process opens the same FIFO file in writing mode, the open call will succeed and return immediately.
  2. For FIFO files opened in write-only mode (O_WRONLY), if the open call is blocked (that is, the second parameter is O_WRONLY), the open call will be blocked until a process opens the same FIFO file in read-only mode; If the open call is non-blocking (that is, the second parameter is O_WRONLY | O_NONBLOCK), open will always return immediately, but if no other process opens the same FIFO file in read-only mode, the open call will return -1 and the FIFO will not be Will be opened.

Famous pipes have other characteristics:

  1. The well-known pipe file is not an ordinary file, but is actually a buffer in the kernel, so you cannot use lseek to operate the well-known pipe file
  2. After we write data into the pipeline, when we use ls -l fifo to view the pipeline file, we will find that the size of the pipeline file is always 0; this means that the data in the pipeline is not visible in the file system. It is still exchanged through the buffer of the kernel, but compared to the unnamed pipe, one more file name appears in the file system, so it is called the named pipe

Summary: Through the pipeline, you can get a glimpse of the inter-process communication and know the essential characteristics of some inter-process communication.

All inter-process communication needs to be carried out through the kernel; because the virtual memory space between different processes is independent of each other, if you want to exchange data, you must bypass the kernel space. A certain process puts the data in the kernel space first, and then Other processes then obtain data from the kernel space.


reference:

  1. https://blog.csdn.net/qq_42914528/article/details/82023408
  2. https://blog.csdn.net/rannianzhixia/article/details/72793895
  3. "UNIX Advanced Programming"

Guess you like

Origin blog.csdn.net/weixin_41629848/article/details/97523339