Technology Sharing

Inter-process communication - pipes

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

insert image description here

1. Introduction to process communication

1.1 Why do processes need to communicate?

There needs to be coordination between processes. For example, the various management levels in a school are interconnected, and cannot be managed vertically. It is precisely because processes need to collaborate, and the prerequisite for collaboration is that processes need to communicate. Data is classified, some data is ready for notification, some data is simply transmitted data, and some is control-related data.

Fact: The process is independent, the process = kernel data structure process code and data

Purpose of process communication:

  • Data transfer: A process needs to send its data to another process
  • Resource sharing: Multiple processes share the same resources.
  • Notification event: A process needs to send a message to another process or a group of processes to notify it (them) that a certain event has occurred (such as notifying the parent process when the process terminates).
  • Process control: Some processes want to fully control the execution of another process (such as the Debug process). At this time, the controlling process hopes to intercept all traps and exceptions of the other process and be able to know its state changes in time.

1.2 How processes communicate

The cost of communication between processes may be slightly higher: processes are independent, and resources opened by any process cannot be seen by another process. When we talked about parent-child processes before, the data of the parent process was inherited by the child process, which does not belong to communication. It can be inherited but not always inherited. There is a difference between being able to transmit information and being able to transmit information all the time.

The premise of inter-process communication is to let different processes see the same (operating system) resource ("a piece of memory"). The two processes are independent. To achieve communication, a tool, namely the operating system, is needed to make the two processes have the same piece of memory. The reason why the operating system does this is determined by the user.
How to let the operating system create resources:

  1. It must be that a process needs to communicate first, so the OS creates a shared resource.
  2. The OS must provide many system calls to allow processes to apply for system resources through system calls.
    The different shared resources created by the OS and the different system call interfaces determine that there will be different types of inter-process communication.

insert image description here

2. Pipeline

2.1 Anonymous Pipes

2.1.1 File Descriptor Understanding Pipes

insert image description here

A pipeline is essentially a memory-level file that does not need to be flushed to disk.
First, the parent process opens a file twice in read-write mode. The reason for opening it twice is to obtain two struct file objects. In this way, there are two read and write pointers for a file, so that the read and write operations use their own independent pointers, so that the read and write operations will not affect each other. The read and write pointers record the current file reading or writing position.struct file There is only one read-write pointer in the file. When writing (or reading) to the file, the read-write pointer will move, and then read (write) again. At this time, the read-write pointer is no longer in the original position, and the content just written cannot be read. Therefore, it is necessary to open the same file twice in different ways. Then create a child process. The child process will inherit the file opened in the parent process, that is, inherit the file descriptor table of the parent process. At this time, the parent and child processes will share the same file resources. The child process can write to the file through file descriptor No. 4, and the parent process can read from the file through file descriptor No. 3. At this time, the parent and child processes have realized data transmission, that is, communication. The parent and child processes see the same memory buffer, which we call a pipe file here. Pipes only allow one-way communication because it is simple.

Why do the parent and child processes print data to the same display terminal?
Because the corresponding child process will inherit the file descriptor table corresponding to the parent process, and then point to the same file, which means that the parent process writes to a file, the child process will also write to a file, and both will be written to the same buffer, and the operating system will refresh to the same display.


The process will open three standard input and standard output by default: 0, 1, 2... How to open 0, 1, 2 by default?
All the commands arebashAs long as the child processbashOnce it is turned on, all default subprocesses are turned on.


Why does the child process take the initiative to clos(0/1/2) without affecting the parent process's continued use of the display file?
Memory-level reference counting--, when the memory-level reference count drops to 0, the file resources are released.


The parent and child processes close unneeded file descriptors, so why did they need to be opened before?
In order to allow the child process to inherit, it is not necessary to close it, but it is recommended to close it to prevent accidental writing.


Why are pipes one-way communication?
The method is simple and reduces development costs. It only allows one-way communication. Any file is refreshed into the buffer, and then the data is refreshed into the file. This process itself is one-way.
The simple pipes we see in life are all one-way, such as water pipes, which have one inlet and one outlet, which conforms to the characteristics of pipes.

2.1.2 Interface Usage

can usepipeTo create an unnamed pipe, the parameters do not require a file path and file name

int pipe(int pipefd[2]);