当前位置:网站首页>Famous pipeline principle and detailed explanation (very practical)
Famous pipeline principle and detailed explanation (very practical)
2022-07-16 08:57:00 【One meter nine hundred little fat man】
1、 Definition
Famous pipeline (FIFO) What's different from the anonymous pipeline is that it Provides a pathname associated with it , With FIFO In the form of documents ( Special document form ) Exists in the file system , And its opening method is the same as opening an ordinary file , Even though And FIFO The creation process of does not have a kinship process , As long as you can access the path, you can pass through each other FIFO Mutual communication , therefore , adopt FIFO Unrelated processes can also exchange data .
Once opened FIFO, You can use the same system calls on it that operate on anonymous pipes and other files I/O The system calls ( Such as read ( ). write() and close()). Like pipes , FIFO There is also a write side and a read side , And the order of reading data from the pipeline is the same as that of writing .FIFO And that's the name of : First in, first out .
2、 The difference between famous pipelines and anonymous pipelines
1. FIFO Exists as a special file in the file system , but FIFO The contents of are stored in memory .
2. When using FIFO After the process exits ,FIFO The file will continue to be saved in the file system for later use .
3. FIFO Have a name , Unrelated processes can communicate by opening a named pipeline ( No kinship can also ).
3、 Use of famous pipes
establish fifo file
1. Through the command : mkfifo name
2. By function :int mkfifo(const char *pathname, mode_t mode);
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
Parameters :
- pathname: The path of the pipe name
- mode: File permissions and open Of mode It's the same , Is an octal number
Return value : Successfully returns 0, Failure to return -1, And set the error number 4、 Famous pipeline communication cases
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
// Write data into the famous pipeline
int main() {
// 1. Judge whether the file exists
int ret = access("test", F_OK);
if(ret == -1) {
printf(" The pipe does not exist , Create pipes \n");
// 2. Create pipeline file
ret = mkfifo("test", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 3. Open the pipe in a write only manner
int fd = open("test", O_WRONLY);
if(fd == -1) {
perror("open");
exit(0);
}
// Writing data
for(int i = 0; i < 100; i++) {
char buf[1024];
sprintf(buf, "hello, %d\n", i);
printf("write data : %s\n", buf);
write(fd, buf, strlen(buf));
sleep(1);
}
close(fd);
return 0;
}#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
// Read data from the pipeline
int main() {
// 1. Open the pipeline file
int fd = open("test", O_RDONLY);
if(fd == -1) {
perror("open");
exit(0);
}
// Reading data
while(1) {
char buf[1024] = {0};
int len = read(fd, buf, sizeof(buf));
if(len == 0) {
printf(" The writer is disconnected ...\n");
break;
}
printf("recv buf : %s\n", buf);
}
close(fd);
return 0;
}explain : Only when both the read end and the write segment are opened can the communication be normal ; At both terminals ( Two processes run two functions respectively );
5、 matters needing attention
Read pipeline :
There's data in the pipeline ,read Returns the actual number of bytes read
No data in the pipeline :
All write ends of the pipeline are closed ,read return 0,( Equivalent to reading to the end of the file )
Not all of the writing ends have been turned off ,read Block waiting
Write the pipeline :
The reading end of the pipeline is completely closed , Perform abnormal termination ( Receive a SIGPIPE The signal )
The reading end is not all closed :
The pipe is full ,write It will block
The pipe is not full ,write Write data to , And return the actual number of bytes written .
6、 Famous pipeline cases

(1) process A:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1. Determine whether the named pipeline file exists
int ret = access("fifo1", F_OK);
if(ret == -1) {
// file does not exist
printf(" The pipe does not exist , Create the corresponding named pipeline \n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// file does not exist
printf(" The pipe does not exist , Create the corresponding named pipeline \n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2. Open the pipe in a write only manner fifo1
int fdw = open("fifo1", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf(" Open the pipe fifo1 success , Waiting to write ...\n");
// 3. Open the pipe as read-only fifo2
int fdr = open("fifo2", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf(" Open the pipe fifo2 success , Waiting to read ...\n");
char buf[128];
// 4. Cyclic write and read data
while(1) {
memset(buf, 0, 128);
// Get standard input data
fgets(buf, 128, stdin);
// Writing data
ret = write(fdw, buf, strlen(buf));
if(ret == -1) {
perror("write");
exit(0);
}
// 5. Read pipeline data
memset(buf, 0, 128);
ret = read(fdr, buf, 128);
if(ret <= 0) {
perror("read");
break;
}
printf("buf: %s\n", buf);
}
// 6. Close the file descriptor
close(fdr);
close(fdw);
return 0;
}(2) process B:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
int main() {
// 1. Determine whether the named pipeline file exists
int ret = access("fifo1", F_OK);
if(ret == -1) {
// file does not exist
printf(" The pipe does not exist , Create the corresponding named pipeline \n");
ret = mkfifo("fifo1", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
ret = access("fifo2", F_OK);
if(ret == -1) {
// file does not exist
printf(" The pipe does not exist , Create the corresponding named pipeline \n");
ret = mkfifo("fifo2", 0664);
if(ret == -1) {
perror("mkfifo");
exit(0);
}
}
// 2. Open the pipe as read-only fifo1
int fdr = open("fifo1", O_RDONLY);
if(fdr == -1) {
perror("open");
exit(0);
}
printf(" Open the pipe fifo1 success , Waiting to read ...\n");
// 3. Open the pipe in a write only manner fifo2
int fdw = open("fifo2", O_WRONLY);
if(fdw == -1) {
perror("open");
exit(0);
}
printf(" Open the pipe fifo2 success , Waiting to write ...\n");
char buf[128];
// 4. Cyclic read / write data
while(1) {
// 5. Read pipeline data
memset(buf, 0, 128);
ret = read(fdr, buf, 128);
if(ret <= 0) {
perror("read");
break;
}
printf("buf: %s\n", buf);
memset(buf, 0, 128);
// Get standard input data
fgets(buf, 128, stdin);
// Writing data
ret = write(fdw, buf, strlen(buf));
if(ret == -1) {
perror("write");
exit(0);
}
}
// 6. Close the file descriptor
close(fdr);
close(fdw);
return 0;
}explain : Only when both the read end and the write segment are opened can the communication be normal ; At both terminals ( Two processes run two functions respectively );
边栏推荐
- Where is the win11 uninstaller? Two methods of uninstalling software in win11
- HCIP第四天笔记
- Hj9 extract non duplicate integer hj09
- 面试诈骗:竟然还有靠面试挣钱的公司
- 有名管道原理及详解(非常实用)
- 基于STM32F405的硬件IIC+DMA操作分享
- 创原会丨携手50+云原生企业,共探跨越数字化鸿沟新模式
- 【每日一题】735. 行星碰撞
- Hj3 explicit random number hj03
- Hongliulin, data gold mine has been dug up in the coal mine!
猜你喜欢
map集合总结
![[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API](/img/fd/8ae3d6a4c0d0c973ce81672c1c529c.png)
[go] Ⅱ. Introduction à l'API reposante et au processus et à la structure du Code de l'API

EMQX Cloud 更新:新增 Redis 和 JWT 外部认证授权

判断两棵二叉树是否同构,三种实现方式(递归、队列、堆栈)

Hcip day 5 experiment

Preorder and inorder traversal sequences determine a binary tree (restore binary tree)

【虹科技术】网络万用表在数据中心的应用

How to solve the problem that the computer shared file cannot be opened

HCIP静态路由

The computer regularly clears wechat data
随机推荐
How to apply for PMP project management certification examination?
Persistence mechanism, expiration strategy and elimination strategy of redis
【无标题】
Is there a completely independent localization database technology
Hcip second day notes
Jerry's VM will cause the nixie tube to flash when sorting [chapter]
babylon.js高度图
Preorder and inorder traversal sequences determine a binary tree (restore binary tree)
Burpsite v2.1 Chinese version
HCIP第一天笔记
QT+VS 工程在 Release/Debug 文件夹下导入相关 DLL (非常实用)
HCIP静态路由
Web security - DOS regular expression denial of service attack
Orphan process, zombie process and process exit (for interview)
Set series opening: why learn set?
创原会丨携手50+云原生企业,共探跨越数字化鸿沟新模式
LeetCode 1584. Minimum cost of connecting all points
Hcip static routing
有名管道原理及详解(非常实用)
Hj8 consolidated statement record hj08