当前位置: 首页 > news >正文

Linux--命名管道

命名管道

• 管道应⽤的⼀个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。
• 如果我们想在不相关的进程之间交换数据,可以使⽤FIFO⽂件来做这项⼯作,它经常被称为命名
管道。
• 命名管道是⼀种特殊类型的⽂件

1 创建⼀个命名管道

在这里插入图片描述

• 命名管道可以从命令⾏上创建,命令⾏⽅法是使⽤下⾯这个命令:
1 $ mkfifo filename
• 命名管道也可以从程序⾥创建,相关函数有:

1 int mkfifo(const char *filename,mode_t mode);创建命名管道:
int main(int argc, char *argv[])
{
mkfifo("p2", 0644);
return 0;
}

2 匿名管道与命名管道的区别

在这里插入图片描述

• 匿名管道由pipe函数创建并打开。
• 命名管道由mkfifo函数创建,打开⽤open
• FIFO(命名管道)与pipe(匿名管道)之间唯⼀的区别在它们创建与打开的⽅式不同,⼀但这些
⼯作完成之后,它们具有相同的语义。

3 命名管道的打开规则

在这里插入图片描述

• 如果当前打开操作是为读⽽打开FIFO时
◦ O_NONBLOCK disable:阻塞直到有相应进程为写⽽打开该FIFO
◦ O_NONBLOCK enable:⽴刻返回成功
• 如果当前打开操作是为写⽽打开FIFO时
◦ O_NONBLOCK disable:阻塞直到有相应进程为读⽽打开该FIFO
◦ O_NONBLOCK enable:⽴刻返回失败,错误码为ENXIO
实例1. ⽤命名管道实现⽂件拷⻉
读取⽂件,写⼊命名管道:

#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)int main(int argc, char *argv[])
{
mkfifo("tp", 0644);
int infd;
infd = open("abc", O_RDONLY);
if (infd == -1) ERR_EXIT("open");
int outfd;
outfd = open("tp", O_WRONLY);
if (outfd == -1) ERR_EXIT("open");
char buf[1024];
int n;
while ((n=read(infd, buf, 1024))>0)
{
write(outfd, buf, n);
}
close(infd);
close(outfd);
return 0;
}读取管道,写⼊⽬标⽂件:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(int argc, char *argv[])
{
int outfd;
outfd = open("abc.bak", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (outfd == -1) ERR_EXIT("open");
int infd;
infd = open("tp", O_RDONLY);
if (outfd == -1)ERR_EXIT("open");
char buf[1024];
int n;
while ((n=read(infd, buf, 1024))>0)
{
write(outfd, buf, n);
}
close(infd);
close(outfd);
unlink("tp");
return 0;
}

实例2. ⽤命名管道实现server&client通信

total 12
-rw-r--r--. 1 root root 46 Sep 18 22:37 clientPipe.c
-rw-r--r--. 1 root root 164 Sep 18 22:37 Makefile
-rw-r--r--. 1 root root 46 Sep 18 22:38 serverPipe.c
cat Makefile
.PHONY:all
all:clientPipe serverPipe
clientPipe:clientPipe.c
gcc -o $@ $^
serverPipe:serverPipe.c
gcc -o $@ $^
.PHONY:clean
clean:
rm -f clientPipe serverPipeserverPipe.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>#define ERR_EXIT(m) \
do{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main()
{
umask(0);
if(mkfifo("mypipe", 0644) < 0){
ERR_EXIT("mkfifo");
}
int rfd = open("mypipe", O_RDONLY);
if(rfd < 0){
ERR_EXIT("open");
}
char buf[1024];
while(1){
buf[0] = 0;
printf("Please wait...\n");
ssize_t s = read(rfd, buf, sizeof(buf)-1);
if(s > 0 ){
buf[s-1] = 0;
printf("client say# %s\n", buf);
}else if(s == 0){
printf("client quit, exit now!\n");
exit(EXIT_SUCCESS);
}else{
ERR_EXIT("read");
}
}
close(rfd);
return 0;
}clientPipe.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>#include <string.h>
#define ERR_EXIT(m) \
do{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
int main()
{
int wfd = open("mypipe", O_WRONLY);
if(wfd < 0){
ERR_EXIT("open");
}
char buf[1024];
while(1){
buf[0] = 0;
printf("Please Enter# ");
fflush(stdout);
ssize_t s = read(0, buf, sizeof(buf)-1);
if(s > 0 ){
buf[s] = 0;
write(wfd, buf, strlen(buf));
}else if(s <= 0){
ERR_EXIT("read");
}
}
close(wfd);
return 0;
}
http://www.xdnf.cn/news/1481869.html

相关文章:

  • 【大语言模型 44】创造力评估:开放域生成质量测试
  • 【C++/STL】优先级队列,仿函数和反向迭代器
  • 阿喀琉斯之踵:从神话传说到现代隐喻的致命弱点
  • 【Kubernetes】知识点总结6
  • 2025高教社国赛数学建模竞赛B题完整参考论文(含模型和代码)
  • MQTT 与 Java 框架集成:Spring Boot 实战(二)
  • 自注意力机制解析
  • 我用Claude Code 开发了一个浏览器插件
  • Storybook:多框架兼容的前端组件开发工具,高效解决组件隔离开发与文档管理问题
  • ElasticSearch 基础内容深度解析
  • 网站管理后台
  • cifar10下载太慢,解决使用第三方链接或迅雷下载
  • VSCode下载安装与汉化
  • NAND Flash块擦除与数据状态解析
  • 【视网膜分割】一种基于结构自适应模型的空洞残差网络
  • 基于大数据+python的肾脏疾病风险教育与数据可视化系统源码 基于数据挖掘的肾脏疾病风险分析与决策支持系统(调试、开题、LW、PPT)
  • 芯片ATE测试PAT(Part Average Testing)学习总结-20250916
  • 提示词工程知识积累及分析
  • C++ 并发编程指南 实现无锁队列
  • Sentinel服务治理:服务降级、熔断与线程隔离
  • 新后端漏洞(上)- Weblogic SSRF漏洞
  • 「数据获取」《中国服务业统计与服务业发展(2014)》
  • 详解flink性能优化
  • docker使用nginxWebUI配置
  • OSG工具集
  • Python错误测试与调试——文档测试
  • ElemenetUI之常用小组件
  • Elasticsearch面试精讲 Day 10:搜索建议与自动补全
  • GEE:基于自定义的年度时序数据集进行LandTrendr变化检测
  • Qt UDP通信学习