当前位置:网站首页>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 )
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
边栏推荐
- Autres - - analyse de la cohérence en double écriture entre redis et MySQL
- SAP那些事-职业篇-36-从“固定资产清理”科目说开去
- Others - interface test by postman
- Reflex WMS系统,跟SAP系统的几个神似之处
- Ubuntu安装MySQL8
- 学生管理系统的架构文档
- 安洵杯2021_Crypto_复现
- 232131
- Smart face recognition 4 -- pytoch uses retinaface + facenet to build a face recognition platform
- Neural network learning small record 56 -- the principle and function of batch normalization layer
猜你喜欢

841. 字符串哈希 (字符串哈希模板)

Tongda OA system docking single sign on platform use and Development Manual

Red sun shooting range -- intranet penetration practice

飞行安全背后,不可或缺的物联网无线通信传感器设备

Module 3 operation architecture design document

培训管理系统操作说明

信息可视化大屏展板(附下载连接)

Anshui Cup 2021 Crypto Réapparition

Qt QMenu简介

学生管理系统的架构文档
随机推荐
[spark] (task5) fundamentals of sparkml (classification | clustering model)
Bailian4005 puzzle [greed]
Bailian4005 拼点游戏【贪心】
其它——Siege压力测试工具使用
Reflex WMS system has several similarities with SAP system
Others - the ultimate design scheme of sub database and sub table
Back to basics, multi-party security computing should return to the original consideration of "security"
10.2 concentration
Connection between Tongda OA and third-party app
其它——Postman做接口测试
聪明的人脸识别4——Pytorch 利用Retinaface+Facenet搭建人脸识别平台
Tongda OA form countersignature comment style
其它——Redis與Mysql雙寫一致性方案解析
Others - zerorpc and simplexmlrpserver
Qt QMenu简介
Bailian4105 save the princess [BFS]
AI + great health is the right way to open the super robot?
安洵杯2021_Crypto_复现
强化学习基础篇(一):多臂老虎机 Multi-armed Bandit
聪明的人脸识别3——Pytorch 搭建自己的Facenet人脸识别平台