当前位置:网站首页>Unrelated interprocess communication -- creation and use of named pipes
Unrelated interprocess communication -- creation and use of named pipes
2022-04-23 01:24:00 【abs(ln(1+NaN))】
Due to the independence between processes , Let two unrelated processes communicate , The first problem to be solved is to let different processes see the same resource ( Memory files 、 Memory 、 queue ).
Anonymous pipeline It's through “ The child process will inherit the resources of the parent process ” This feature realizes the communication between parent and child processes , In this way, the parent-child process can see the same resource

name pipes It's through route + file name See the same resource . The most typical is like this , After knowing the path , A process writes data to the disk file first , Another process reads data from the disk file , But such reading and writing efficiency is relatively low

Let's follow this idea to understand how the named pipeline acts as a bridge between two processes
Catalog
Two 、 Named pipe creation function mkfifo
3、 ... and 、 Pipeline file creation and use
1、 Create a pipeline in the server process
2、 The server reads data from the pipeline
3、 The client writes data into the pipeline
One 、 How named pipes work
A file can be opened by multiple processes , The most typical is ours stdout . After the file is opened , Will be loaded into memory , We let the process A Open in write only mode , process A When writing , The disk cannot be refreshed ; process B Open as read-only

The process then A Write data to this memory , process B Read data from this memory , This enables basic communication between unrelated processes

Two 、 Named pipe creation function mkfifo
mkfifo After being called , Will create a pipeline file ( The essence of pipeline is file ), Equivalent to the disk file in the figure above
The first parameter : Indicates the creation path of the pipeline file , Where to create this file
The second parameter : Indicates the initial permission of the pipeline file , When a general file is created , Permissions include readable / Can write / Executable
Return value : When the pipeline is created successfully , return 0; Otherwise return to -1


3、 ... and 、 Pipeline file creation and use
Next, we will try to let the two processes communicate through the pipeline , A process is the server , A process is a client . The server is responsible for reading , The client is responsible for writing
1、 Create a pipeline in the server process
Generally speaking , The server creates a pipeline , Then the client can communicate directly with the pipeline created by the server
You can write one here Makefile Test whether it can be created successfully

Makefile The contents and test results are as follows , If there is a pipeline file like this , That means the creation is successful

![]()
2、 The server reads data from the pipeline
After creating the pipeline file , The server can open the file , Then read the data from it

Be careful :
Because the client wants to write data to the pipeline file , If the client closes the file , The server cannot read the data , That is, the number of bytes of the file read bytes = 0, There is no need to continue reading at this time , So you need to be in while Loop plus one close(fd)

3、 The client writes data into the pipeline
After the pipe is created , That is, the disk file has , A process ( Client side ) With Just write To open a file , Write data to this file
But how does the client get the pipeline path ?? We can move the macro definition of the pipeline path in the server to a header file common.h in , Let the client and server include this header file at the same time
======================== common.h ========================
because read Functions and write The following header files are required for all functions , So we put the following header files into common.h in , And then let server.c and client.c Both contain common.h
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h> //close function

======================== server.c ========================

======================== client.c ========================
Let's read the content from the keyboard first , Then write the read content to the pipeline file , The specific operation steps are shown in the figure below

4、 Process communication test



版权声明
本文为[abs(ln(1+NaN))]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230123336174.html
边栏推荐
- Get in the car, the era of intelligent database autonomy has come, and Tencent cloud database x AI has made a new breakthrough
- 再谈被动安全 教你看懂中保研碰撞测试的评级报告
- How to introduce SPI into a project
- Examples of branch and loop statements
- "Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
- [server data recovery] data recovery case of server crash after the hard disk of the server is flooded
- Earth day collection: Microsoft and Intel invite you to get the green Ambassador badge and give you negative carbon emission!
- Interview eight part essay (disorderly order, no classification)
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- GBase 8s查询处理和优化
猜你喜欢

Soatest preliminary understanding

Research and application of Acrel-5000 building energy consumption monitoring system in Xixian Airport Garden Project
再谈被动安全 教你看懂中保研碰撞测试的评级报告

清研环境深交所上市:年营收1.8亿 市值41亿

计蒜客:数独(DFS)
![[HCTF 2018]admin](/img/08/8111f2f87796dda8dd6b0dcf72c42b.png)
[HCTF 2018]admin

“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负

CVPR | 2022 | expressed by transformer learning multiple hypotheses! A new framework for 3D human pose estimation!

代码实现发邮件---sendemails

世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
随机推荐
修改数组(并查集)
京东一面:子线程如何获取父线程 ThreadLocal 的值?我蒙了。。。
稳定币是让公链加速死亡或者取代BTC的超级机会?
清研环境深交所上市:年营收1.8亿 市值41亿
Gbase 8s检查点简介
engine.POST()处理POST请求
JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...
Blocking type of gbase 8s concurrency control
Redis forgets the password and resets the password
引爆炸弹(DFS)
Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)
代码实现发邮件---sendemails
Introduction to granularity locking of gbase 8s concurrency control
Practice and exploration of knowledge map visualization technology in meituan
Generating class diagram with EA reverse engineering code
Get rid of the "small workshop" of AI production: how to build a cloud native AI platform based on kubernetes
Open WebRTC Toolkit(OWT) Server User Guide
Design and application of intelligent lighting control system in hospital
DFS奇偶性剪枝
光猫超级帐号密码,重置光猫获取超级帐号密码