当前位置:网站首页>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!
边栏推荐
- Two-dimensional array combat project -------- "Minesweeper Game"
- 研发项目流程规范
- 软件测试面试题:谈谈你对 cmm 和 is9000 的认识?
- Summarize the acquisition of commonly used file information QFileInfo in Qt: suffix, name, path, link
- 进程间通信方式(2)有名管道
- 英伟达 GPU 架构简史
- 【备战“金九银十”】2022年软件测试面试题最新汇总
- WinForm (5) control and its members
- 数据的存储(下)——浮点型在内存中的存储
- How to determine the size of the version number
猜你喜欢
测试3年,开口就要25k?面试完最多给15k...
Dual machine thermal for comprehensive experiment (VRRP + OSPF + + NAT + DHCP + VTP PVSTP + single-arm routing)
SQL statement--get database table information, table name, column name, description comment, etc.
Engineering Design of Single-sided PCB Routing Impedance
MySQL索引与事务
88Q2110 通过C22方式访问C45 phy地址
[ASM] The relationship between the role of the bytecode operation ClassWriter COMPUTE_FRAMES and visitMaxs
wincc如何实现远程监控1200PLC
Exception: try catch finally throws throw
进程间通信方式(2)有名管道
随机推荐
Use mysql statement to operate data table (table)
软件测试面试题:什么是α测试,β测试?
微信公众号后台管理
SystemVerilog: Verifying knowledge bits and pieces
MySQL基础篇【第一篇】| 数据库概述及数据准备、常用命令、查看表结构步骤
MySQL中的DDL常规操作总结
nvidia-smi:控制你的 GPU
13.cuBLAS开发指南中文版--cuBLAS中的Level-1函数copy()和dot()
WinForm (5) control and its members
《QA离业务代码能有多近?》轻量级单元测试方案
paddle2.3和torch1.8在SentenceBert上的性能对比
sql 使用到where和groupby时到底怎么建立索引?
wincc如何实现远程监控1200PLC
Construction inspection, no rules and no square
一次简单的 JVM 调优,拿去写到简历里
软件测试面试题:性能测试工作?
Sigma development pays attention to details
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?
划分字母区间[贪心->空间换时间->数组hash优化]
Oops Framework模板项目新手引导