138-Interprocess communication (pipeline)

Insert picture description here
The pipe is the inter-process communication on the local machine.
Insert picture description here
If you don’t use the pipe you learned today for inter-process communication,
use file.txt, a write data to a file, b read data from the file
Insert picture description here
this way: this file.txt is on the disk Storage, access speed is very slow, slow
disk, faster memory, faster cache, faster registers, but smaller

For pipeline files, after opening, a space is created in memory

Pipes can be used to transfer data between two processes, such as: ps -ef | grep "bash", where'|' is the pipe, and its role is to write the result of the ps command to the pipe file, and then grep from the pipe file Read out the data to filter.

Famous pipe

A well-known pipe can communicate between any two processes.
The creation of a well-known pipe:
Command creation: mkfifo FIFO
system call creation

 #include <sys/types.h>
 #include <sys/stat.h>
//filename 是管道名 mode 是创建的文件访问权限
int mkfifo(const char *filename, mode_t mode);

The following is the command to create a well-known pipe
Insert picture description here
ls -l view.
Here p means that a file of pipe type
Insert picture description here
must have a read end and a write end to communicate. The
two processes must be opened at the same time,
otherwise it will be blocked. Do not open
open write read close

Although you can see the pipeline file, the
size of the pipeline file is always 0 and
the data will not be written to the disk.

Write ac
Insert picture description here

Write bc
Insert picture description here

gcc -oa ac
dcc -ob bc
./b
in the same position of another terminal: ./a
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
If the other party closes the pipe, directly return
read==0 as the condition for judging that the other party closes the pipe.
This condition is required for cyclic writing and reading
vi ac
Insert picture description here

vi bc
Insert picture description here

Run these two programs in two terminals to
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
allow multiple reading and writing.
Changed to read only one byte at a time.
Insert picture description here
Insert picture description here
Insert picture description here
After we open the pipeline, we will allocate space for memory, dividing one byte by one byte. The
head pointer and the tail pointer control the
writing Data, the head pointer moves backwards.
Read data, and the tail pointer moves backwards,
similar to the sequential circular queue being
full. Write will block. If it is
empty, read will block
the characteristics of the pipeline.
1. Regardless of whether it is named or unnamed, the data written to the pipeline is in memory.
2. The pipeline is a half-duplex communication method (communication methods include simplex, half-duplex, Full-duplex)
3. The difference between a named and an unnamed pipe: a named pipe can be used between any process, while an unnamed pipe is mainly used between parent and child processes

Ctrl+c, the process ends, all open file descriptors are automatically closed, a is closed, the write end is closed, and the read end of b returns 0.
If you close b and the read end is closed, a writes data and exits. Write data to the closed pipe, no one is reading, the kernel thinks that an exception has occurred, and sends a signal to notify the end of the process

Add a signal function to the write end
Insert picture description here

Insert picture description here
If there is another reading end
Insert picture description here

Unnamed pipe

Unnamed pipes are mainly used for communication between parent and child processes

The creation of an unnamed pipe:
Insert picture description here
this is fixed. After
Insert picture description here
Insert picture description here
Insert picture description here
running it is as follows.
Insert picture description here
Print fd[0], fd[1]
Insert picture description here
Insert picture description here
why are 3
, 4 is actually the subscript value of the file table
Insert picture description here

Guess you like

Origin blog.csdn.net/LINZEYU666/article/details/112789545