[Linux] - Anonymous pipe communication between processes

Inter-process communication

A room, the concept of communication process

  • Process is the concept of the operating system, whenever we execute a program, in terms of the operating system creates a process, in this process, along with the allocation and release of resources . It can be considered a process of execution of a program.
  • Address space of a process is independent of each other, it is not generally accessible to each other , if readers do not understand the process address space concepts, refer to the blog process address space . But in many cases required inter-process communication with each other, to accomplish a function of the system. Process to coordinate their behavior with each other through communication between the kernel and other processes.
  • We also mentioned before between the independence process is , so to achieve communication between the two processes it is difficult, so the precondition for communication before you want two processes are: to allow different processes to see with a resource (generally it refers to a piece of memory).
    Inter-process communication

The purpose between two, interprocess communication

  • Data transmission : it needs to be a process of data transmission to another process
  • Resource sharing : multiple processes share the same resources, you want to operate multiple processes to share data, a process to modify the shared data, other process should be seen immediately.
  • Notification events : a need to send a message to another process or group of processes, informing it (them) some event occurs (such as when to notify the parent process terminates).
  • Resource sharing : sharing the same resources among multiple processes. In order to do this, we need to provide the kernel locking and synchronization mechanisms.
  • Process Control : Some want to completely control the process of execution of another process (such as Debug process), and the control will process all want to be able to intercept and exceptions into another process, and be able to timely know its status changed.

Pipe communication between the three processes

pipeline

First, what is the pipeline

  • Pipeline is the communication between the oldest Unix process
  • We have a data connection from one process to another process stream known as the "pipeline."
    pipeline

Second, the pipeline Introduction

Pipeline includes three types:

  • Ordinary pipeline pipe: There are usually two restrictions, one simplex, only one-way transmission; the second is only with a process of genetic relationship between father and son or between brothers commonly used in the process to use.
  • Flow pipe s_pipe: removes the first restriction, half-duplex, only between parent and child, brother or process use, can be two-way transmission.
  • Named Pipes: name_pipe (fifo): The second limitation is removed, can communicate between many unrelated processes.

Third, anonymous pipes

  • Anonymous nature of the pipeline : the kernel provides some memory (queue), this memory by means of memory, complete inter-process communication. The pipeline then abstracted into this memory file. In the form of access to the file descriptor to read and write data in this memory, it can only be applied with a genetic relationship between processes, commonly used in parent and child and sibling processes .
  • Create anonymous pipe function prototype :int pipe(int fd[2]);
  • Parameters : fd [2]: an array of file descriptor, where f [0] indicating a read side, f [1] represents a writing end
  • Return value : the successful return 0, else return an error code

Anonymous pipe pipe

Fourth, the fork to the principle of shared pipeline

  • We speak before the control process when details the fork function, we referred to the parent process through fork to create a child process, the child process is the parent process and has the same information at this time that his son would be able to see the process piping, and the pipeline has read and write functions because anonymous pipe is half-duplex, and therefore do not require the parent and child to close their file descriptor, then the process can change each read or write operation for communication.
    fork

Fifth, the file descriptor - in-depth understanding of anonymous pipes

Understand pipe file descriptor

Six, standing angle of the kernel - Essence conduit

  • 在 Linux 中,管道的实现并没有使用专门的数据结构,而是借助了文件系统的file结构和VFS的索引节点inode。通过将两个 file 结构指向同一个临时的 VFS 索引节点,而这个 VFS 索引节点又指向一个物理页面而实现的。如下图: Kernel understand pipeline
  • 有两个 file 数据结构,但它们定义文件操作例程地址是不同的,其中一个是向管道中写入数据的例程地址,而另一个是从管道中读出数据的例程地址。这样,用户程序的系统调用仍然是通常的文件操作,而内核却利用这种抽象机制实现了管道这一特殊操作。
  • 所以,看待管道,就如同看待文件一样,管道的使用和文件一致,迎合了我们之前在 深入理解Linux文件系统中提到过的"Linux下一切皆文件的思想"

接下来我们看看实例代码

  • 子进程向管道中写数据,父进程读数据
    Create an anonymous pipe

  • 父子进程通信成功父进程读取到子进程在管道中写入的数据,运行结果如下:
    Inter-process communication

七、管道读写规则

我们在讲管道读写规则之前我们先来了解一下几个概念:

  • 临界资源:进程间通信的本质是让两个进程看到同一份资源,多进程间共享的资源称为临界资源
  • 临界区:进程访问临界资源的那部分代码称为临界区
  • 互斥:任何一个时刻只允许一个进程进入临界区访问临界资源的情况称为互斥
  • 同步:在保证数据安全的前提下(通常是互斥),让多进程访问临界资源具有一定的顺序性,称为同步(目标:协同进程步调,避免饥饿问题)
  • 原子性:通常访问临界资源时只有两种状态:不访问和访问完,我们将此称为原子性

在了解完上面几个概念之后我们再来看看管道的读写规则

  • 当没有数据可读时
    O_NONBLOCK disable:read调用阻塞,即进程暂停执行,一直等到有数据来到为止。
    O_NONBLOCK enable:read调用返回-1,errno值为EAGAIN。
  • 当管道满的时候
    O_NONBLOCK disable: write调用阻塞,直到有进程读走数据
    O_NONBLOCK enable:调用返回-1,errno值为EAGAIN
  • 如果所有管道写端对应的文件描述符被关闭,则read返回0
  • 如果所有管道读端对应的文件描述符被关闭,则write操作会产生信号SIGPIPE,进而可能导致write进程退出
  • 当要写入的数据量不大于PIPE_BUF时,linux将保证写入的原子性。
  • 当要写入的数据量大于PIPE_BUF时,linux将不再保证写入的原子性。

我们分别举例说明一下上面提到的几种规则

情况1

  • 管道被写满的情况,子进程一直在往管道中写数据,但父进程不从管道中读取数据,而是仅仅只在wait子进程,这就会出现阻塞的情况,直到有进程读走数据。
    Case 1
    情况2
  • 子进程一直写,父进程不读且关闭其对应的读操作符,这是write操作就会常数信号SIGPIPE,进而可能导致write的进程也就是子进程退出
    Here Insert Picture Description

情况3

  • 子进程写了一条数据之后,直接退出,并且关闭对应的写的文件操作符,此时父进程读取了一条数据之后就无数据可读,此时read调用返回-1,errno的值为ESGAIN。Case 3

情况4

  • 子进程写了一条数据后退出,但不关闭对应的写文件操作符,此时父进程就会一直阻塞等待,一直到有数据来为止。
    Case 4

八、管道特点

  • Can only be used for processes with a common ancestor to communicate (the process has genetic relationship) between; usually, a pipe is created by a process, then the process calls fork, after which a parent, you can use the pipe between the child process.
  • Pipes provide streaming service
  • In general, the process of withdrawal, the release of the pipeline, so the life cycle of the pipeline with the process
  • Generally, the core will pipe operation synchronization and mutual exclusion
  • Pipe is half-duplex , data can only flow in one direction; communicating parties when required, need to establish two pipes
Published 167 original articles · won praise 175 · views 10000 +

Guess you like

Origin blog.csdn.net/chenxiyuehh/article/details/90759128