
Linux:进程间通信——命名管道
一个完整的例子: 这个例子在namePipe.c中先向缓冲区中写入了hello linux!然后再read.c中读出所写的内容。 namePipe.c
另一个终端中read.c的运行结果:
发布日期:2021-05-07 02:16:06
浏览次数:19
分类:精选文章
本文共 1830 字,大约阅读时间需要 6 分钟。
进程间通信——命名管道
命名管道
和匿名管道一样,命名管道也是在内核中开辟的一段缓存区,不过和匿名管道不同的是,这段缓存区是有标识符的,这也就意味着不同的进程,不需要有亲缘关系,只需要通过标识符就能找到该缓冲区了。
命名管道的创建
命令创建
命名管道可以从命令行上创建,命令行创建是使用下面这个命令:
mkfifo filename
函数创建
相关函数:
mkfifo函数:函数原型:#includeint mkfifo(const char *filename,, mode_t mode);返回值:成功返回0,失败返回-1
代码:
#include#include #include int main() { int fifoid = mkfifo("./fifoTest",0664); if(fifoid < 0) { perror("mkfifo"); return -1; } return 0; }
运行结果:

#include#include #include #include int main() { int fifoid = mkfifo("./fifoTest",0664); if(fifoid < 0) { perror("mkfifo"); return -1; } printf("创建成功!\n"); int fd = open("./fifoTest",O_RDWR | O_CREAT); if(fd < 0) { perror("open"); return -1; } else { ssize_t w_size = write(fd,"hello linux!",12); if(w_size < 0) { perror("write"); return -1; } printf("write:%d",w_size); } while(1) { printf("wait!\n"); sleep(1); } return 0; }
read.c
#include#include #include int main() { int fd = open("./fifoTest",O_RDONLY); if(fd < 0) { perror("open"); return -1; } else { char *buf[1024] = { 0}; int ret = read(fd,buf,12); if(ret < 0) { perror("read"); return -1; } printf("buf is : %s\n",buf); } return 0; }
namePipe.c的运行结果:


特性
(1) 生命周期跟随进程
(2) 命名管道具有表示符 (3) 其它特性和匿名管道一样发表评论
最新留言
感谢大佬
[***.8.128.20]2025年04月02日 13时18分06秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
云计算之路-阿里云上:“黑色30秒”走了,“黑色1秒”来了,真相也许大白了
2021-05-09
上周热点回顾(6.9-6.15)
2021-05-09
上周热点回顾(10.20-10.26)
2021-05-09
上周热点回顾(2.16-2.22)
2021-05-09
上周热点回顾(3.2-3.8)
2021-05-09
.NET跨平台之旅:借助ASP.NET 5 Beta5的新特性显示CLR与操作系统信息
2021-05-09
上周热点回顾(7.27-8.2)
2021-05-09
上周热点回顾(5.9-5.15)
2021-05-09
上周热点回顾(1.16-1.22)
2021-05-09
上周热点回顾(1.23-1.29)
2021-05-09
上周热点回顾(3.20-3.26)
2021-05-09
上周热点回顾(6.19-6.25)
2021-05-09
云计算之路-阿里云上:docker swarm 集群故障与异常
2021-05-09
上周热点回顾(2.19-2.25)
2021-05-09
云计算之路-阿里云上:博客web服务器轮番CPU 100%
2021-05-09
云计算之路-阿里云上:服务器CPU 100%问题是memcached连接数限制引起的
2021-05-09
上周热点回顾(3.26-4.1)
2021-05-09
上周热点回顾(6.25-7.1)
2021-05-09
【故障公告】10:30-10:45 左右 docker swarm 集群节点问题引发故障
2021-05-09