当前位置:网站首页>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!
边栏推荐
猜你喜欢
随机推荐
微信公众号后台管理
FPGA学习专栏-串口通信(xinlinx)
数据库数据采集利器FlinkCDC
13.cuBLAS开发指南中文版--cuBLAS中的Level-1函数copy()和dot()
两日总结十一
How to determine the size of the version number
dump_stack ()
Deep Learning [Chapter 2]
MySQL进阶查询
软件测试面试题:验收测试包括哪三种类型?
Update chromedriver driver programming skills │ selenium
报考PMP需要做些什么准备?
22. Inventory service
Deep Learning【第二章】
Exception: try catch finally throws throw
年薪30W,BAT抢着要,懂面试技巧的测试人究竟多吃香?
SQL statement--get database table information, table name, column name, description comment, etc.
Tomca启动闪退问题如何解决
loop word
导入数据包上传宝贝提示“类目不能为空”是什么原因,怎么解决?