当前位置:网站首页>Communication between parent and child processes (II) -- four cases of anonymous pipeline communication

Communication between parent and child processes (II) -- four cases of anonymous pipeline communication

2022-04-21 21:25:00 abs(ln(1+NaN))

To gain insight into the characteristics of anonymous pipes , We need to understand four situations of pipeline communication

(1) The speed at which the child process writes Far greater than The speed at which the parent process reads

(2) The speed at which the parent process reads Far greater than The speed at which the child process writes

(3) Subprocess is writing , But the parent process closed the file descriptor

(4) The subprocess closes the file descriptor after writing a batch of data , Parent process reading


Catalog

One 、 The subprocess writes very fast , The parent process reads very slowly and does not even read

1、 Subprocess write fast , The parent process does not read ( The purpose is to test the pipe size )

2、 The subprocess writes very fast , The parent process reads only one at a time ( When the test parent process fetches data , Whether the child process will continue to write )

 3、 The subprocess writes very fast , The parent process reads at least... At a time 4KB(4096 bytes)

Two 、 The parent process reads faster than the child process writes

3、 ... and 、 Subprocess is writing , But the parent process closed the file descriptor

Four 、 The parent process keeps reading , But the subprocess writes a string of characters and exits

5、 ... and 、 Five features of anonymous pipes


One 、 The subprocess writes very fast , The parent process reads very slowly and does not even read

1、 Subprocess write fast , The parent process does not read ( The purpose is to test the pipe size )

Such a result can be imagined , because Pipes are of different sizes !! So after the pipe is filled, it will not continue to write into it , This is the time The child process will wait for the parent process to fetch data

So how big is the pipe ? Now we can test

We found that 65536 After a character , The child process no longer writes , Convert it ,65536 That's right. 64KB, So we know Linux The size of the pipes in the system is 64KB

2、 The subprocess writes very fast , The parent process reads only one at a time ( When the test parent process fetches data , Whether the child process will continue to write )

When the pipe is full , This means that the child process has to wait for the parent process to fetch data , So is it to say , The parent process fetched data , The subprocess will continue to write ??

  According to the results, we found that , The parent process reads several times , But the subprocesses count Still nothing has changed , How much does the parent process need to read before it can write ? The answer is 4KB

We can do it in pipe Find the answer in the definition of the function

 

 3、 The subprocess writes very fast , The parent process reads at least... At a time 4KB(4096 bytes)

Now set the number of characters read by the parent process to 4096, Let's continue to test

We found that the child process after the parent process fetches the data , Started writing again ! So this verifies , When the pipe is full , The parent process must read at least 4KB, namely 4096 Characters , The child process will continue to write

Two 、 The parent process reads faster than the child process writes

Now let's set it as a child process every 10s Write data to the pipeline , Then the parent process passes while The loop keeps reading , And the number of characters printed and read

  We can see , The number of characters is not always printed , Instead, wait until the parent process reads the data before printing , That means When there is no data readable in the pipeline , The parent process will wait for the child process to write data

3、 ... and 、 Subprocess is writing , But the parent process closed the file descriptor

Subprocess every 1s Write data once , Parent process sleep 3s After the descriptor is read, the file is closed pipefd[0], Then exit the process , At this time, we can detect whether the process will be highlighted normally by the exit code and exit signal of the sub process

Will the child process continue to write all the time at this time ??

Based on the test results, we found that , At first, both processes were running , But the last two processes quit , This means that After the parent process exits , The child process will not continue to write

The parent process has exited , But the child process keeps writing ,OS Think this is a waste of resources , Then it will send a message to the child process SIGPIPE The signal To kill child processes

 

Four 、 The parent process keeps reading , But the subprocess writes a string of characters and exits

After the subprocess writes a string of characters , Withdraw from the process ; The parent process runs every 1s Read a character

  Let's first look at the printed results , After the parent process reads the contents written by the child process , It will also exit automatically , Same as above , This is also OS Intolerable behavior , Because no data is readable , And keep reading , It's a waste of resources

  Let's continue to look at the running process of the process , It is basically consistent with the above results

 

5、 ... and 、 Five features of anonymous pipes

(1) Pipeline is a communication channel that can only communicate in one direction .

(2) Pipes are byte stream oriented ! This can be seen from the size of the pipe

(3) Anonymous pipes are limited to parent-child communication ( Communication between blood related processes )

(4) The pipeline has its own synchronization mechanism , When the parent process reads enough data , The subprocess will automatically continue to write

(5) The declaration period of the pipeline is changed with the process ( After the relevant process exits , The pipe will also be closed )

版权声明
本文为[abs(ln(1+NaN))]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212052436870.html