打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
linux 进程间通信 之fifo

上一篇博客已经介绍了一种进程间通信的方式,但是那只是针对于有血缘关系的进程,即父子进程间的通信,那对于没有血缘关系的进程,那要怎么通信呢?

这就要创建一个有名管道,来解决无血缘关系的进程通信, fifo:

book@ubuntu:~$ mkfifo xwpbook@ubuntu:~$ ls -l myfifo prw-rw-r-- 1 book book 0 Feb 6 2016 myfifo

mkfifo 既有命令也有函数

#include #include int mkfifo(const char *pathname, mode_t mode);
1 /* fifo_write.c */ 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 10 void sys_err(char *str, int exitno)11 {12 perror(str);13 exit(exitno);14 }15 16 int main(int argc,char *argv[])17 {18 int fd, len;19 char buf[1024] = 'hello world\n';20 if(argc < 2)21="" {22="" printf('./app="" myfifo\n');23="" exit(1);24="" }25="" 26="" fd="open(argv[1],O_WRONLY);27" if(fd="">< 0)28="" sys_err('open',1);29="" 30="" write(fd,="" buf,="" strlen(buf));31="" close(fd);32="" 33="" return="" 0;34="" }35="" 36="" 37="" 38="" 39="" fifo_read="" */40="" #include="">41 #include 42 #include 43 #include 44 #include 45 #include 46 #include 47 48 void sys_err(char *str, int exitno)49 {50 perror(str);51 exit(exitno);52 }53 54 int main(int argc,char *argv[])55 {56 int fd, len;57 char buf[1024] = {0};58 if(argc < 2)59="" {60="" printf('./app="" myfifo\n');61="" exit(1);62="" }63="" 64="" fd="open(argv[1],O_RDONLY);65" if(fd="">< 0)66="" sys_err('open',1);67="" 68="" read(fd,="" buf,="" sizeof(buf));69="" write(stdout_fileno,="" buf,="" strlen(buf));70="" close(fd);71="" 72="" return="" 0;73="" }74="" 75="" 76="" 操作方法:77="" 分别编译成可执行程序:fifo_write="" 和="" fifo_read="" 78="" 在一个终端下输入="" ./fifo_write="" myfifo79="" 在另一个终端下输入="" ./fifo_read="" myfifo80="">

注:

  • 当只写打开FIFO管道时,该FIFO没有读端打开,则open写打开会

    阻塞

  • FIFO内核实现时可以

    支持双向通信

  • FIFO可以一个读端,多个写端;也可以一个写端,多个读端。

其实fifo指向的还是内核中的缓冲区,只不过他指向所有的内核缓冲区,因此他不是阻塞的,不像pipe只是指向两端,造成阻塞现象

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Linux系统编程之进程间通信方式:命名管道(二)
有名管道和无名管道
C语言进程间通信(二)
管道通信
多进程编程通信——管道
有名管道
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服