当前位置:网站首页>Inter-process communication method (2) Named pipe
Inter-process communication method (2) Named pipe
2022-08-11 01:55:00 【smooth ccc】
The book continues to introduce the introduction of anonymous pipelines. This article will continue to introduce the second type of pipelines - named pipelines. The biggest difference between named pipelines and anonymous pipelines is that there is no relationship between parent and child processes.Communication, then this article will introduce the way of the famous pipeline in detail!
Framework:Create a named pipe =="Open a named pipe =="Read-write pipe=="Close a pipe =="Uninstall a named pipe
Similar to an anonymous pipe, the framework of a named pipe is basically the same, but the difference from an anonymous pipe is that the operation of mkfifo of a named pipe is to create a pipe, not the return value of pipe like an anonymous pipe is already a pipe, mkfifoIt is to create a pipeline, and then we need to open one end of the pipeline created by open to perform read and write operations on the input or output port to realize data communication and information exchange between different processes without affinities.
1. Create a well-known pipe mkfifo
int mkfifo(const char *pathname, mode_t mode);
- Function: Create a named pipeline file with the permission of mode under the specified pathname path + name
- Parameters:
- pathname the named pipe path to create
- mode--->8 binary file permissions (usually 0777, 0666, 0664)
- Return value:
- Success 0
- Failure-1
2. Open a well-known pipe open
- When opened with O_RDONLR, the pipe is blocked at open
- When opened with O_WRONLY, the pipe is blocked at open
- Unblocks only when both ends are open at the same time.
3. Read and write pipeline read, write
Read and write the read-write end opened by open by means of file IO, here the read-write end is still regarded as a file descriptor to operate on it
4. Close the port close
Close the read-write two ports pointed to by the open file descriptor
5. Uninstall the famous pipeline remove
int remove(const char *pathname);
- Function: Uninstall the specified pathname pipeline file and delete it from the file system at the same time.
- Parameters: ptahtname Named pipe to uninstall
- Return value:
- Success 0
- Failure -1
Code example:
//The read end of read receives the information sent from another processint main(int argc, const char *argv[]){int ret = mkfifo("./fifo", 0777);if(ret < 0 && errno != EEXIST){perror("fail to mkfifo");return -1;}int fd_r = open("fifo", O_RDONLY);if(-1 == fd_r){perror("fail to open");return -1;}while(1){char buff[1024] = {0};read(fd_r, buff, sizeof(buff));if(0 == strcmp(buff, "quit\n")){break;}printf("w->r:%s", buff);}close(fd_r);remove("fifo");return 0;}//Get a piece of information from the terminal and input it into the fifo pipe to write to another portint main(int argc, const char *argv[]){int fifo = mkfifo("./fifo", 0777);if(-1 == fifo && errno != EEXIST){perror("fail to mkfifo");return -1;}int fd_w = open("fifo", O_WRONLY);if(-1 == fd_w){perror("fail to open");return -1;}while(1){char buff[1024] = {0};fgets(buff, sizeof(buff), stdin);write(fd_w, buff, strlen(buff)+1);if(0 == strcmp(buff, "quit\n")){break;}}close(fd_w);remove("fifo");return 0;}
The above code realizes that two processes without parent-child relationship can establish a pipeline through mkfifo, so as to realize the sending and receiving of data between two unrelated processes.way of communication.
Through the above code, we can see that the simple use of mkfifo further strengthens our knowledge and understanding. Interested students can think about the two processes of full duplexThe communication method can be realized by using the knowledge of concurrency learned before, and the specific code can be obtained by private chat with me.
The above are the two methods of pipeline communication between processes. Next, I will explain the communication method of shared memory for everyone. Thank you!
边栏推荐
- 软件测试面试题:性能测试工作?
- Successfully resolved TypeError: can't multiply sequence by non-int of type 'float'
- Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
- QT+VTK+PCL拟合圆柱并计算起始点、中止点
- 软件测试面试题:软件测试的过程的V模型,说出它的缺点?
- FPGA learning column (xinlinx) serial communication -
- Oops Framework模板项目新手引导
- C # - delegate detailed usage
- Lianshengde W801 series 6-Analyze the Bluetooth communication source code of W801 from the perspective of WeChat applet (indicate method)
- Please talk about for...in and for...of in JS (below)
猜你喜欢

Two-dimensional array combat project -------- "Minesweeper Game"

js原型和原型链及原型继承
![划分字母区间[贪心->空间换时间->数组hash优化]](/img/bb/e750c7cd4a80e767bd64d96ffc2ce3.png)
划分字母区间[贪心->空间换时间->数组hash优化]

从键入网址到网页显示的详细过程

MySQL基础篇【第一篇】| 数据库概述及数据准备、常用命令、查看表结构步骤

Engineering Design of Single-sided PCB Routing Impedance

络达开发---自定义BLE服务(二):功能实现

Lianshengde W801 series 5-WeChat applet and W801 Bluetooth communication routine (read notes)

2022年PMP报考指南

单面PCB布线阻抗的工程设计
随机推荐
winform下的富文本编辑器
2022年PMP报考指南
Summary of DDL routine operations in MySQL
Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
Construction inspection, no rules and no square
报考PMP需要做些什么准备?
MySQL八股文背诵版(续)
最新国产电源厂家及具体型号pin-to-pin替代手册发布
Successfully resolved raise TypeError('Unexpected feature_names type')TypeError: Unexpected feature_names type
The iterator and generator
Sigma开发注意细节
21、阿里云oss
21. Aliyun oss
The statistical data analysis, interview manual"
深度解析:什么是太爱速M抢单模式?
wincc如何实现远程监控1200PLC
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
leetcode 739. Daily Temperatures 每日温度(中等)
惨遭面试官吊打高并发系统设计,回来学习 2400 小时后成功复仇
