进程间通信2命名管道

命名管道

1、与管道的区别:提供了一个路径名与之关联,以FIFO文件的形式存储于文件系统中,能够实现任何两个进程之间通信。而匿名管道对于文件系统是不可见的,它仅限于在父子进程之间的通信。 
2、FIFO是一个设备文件,在文件系统中以文件名的形式存在,因此即使进程与创建FIFO的进程不存在血缘关系也依然可以通信,前提是可以访问该路径。 

3、FIFO(first input first output)总是遵循先进先出的原则,即第一个进来的数据会第一个被读走。

相比于匿名管道他可以在无关的两个进程间进行通信,并且他提供了open函数,对弈上一章对匿名管道的介绍,匿名管道并没有提供open函数。

具体在使用上也有区别 首先必须先要创建管道文件。

mkfifo 路径 文件名对于命名管道而言只能是一个读另一个写所以open时他的权限是有所限制。它也属于管道文件系统管理。他并不是真正磁盘上的文件他是一个设备文件。所以一般意义上而言管道都在内存上。

摘抄man手册的一句话        

A  FIFO special file is similar to a pipe, except that it is created in a different way.  Instead of being an anonymous communications channel,  a  FIFO  special file is entered into the file system by calling mkfifo().   

  1.  #include <stdio.h>  
  2.   2 #include <stdlib.h>  
  3.   3 #include <fcntl.h>  
  4.   4 #include <string.h>  
  5.   5 #include <assert.h>  
  6.   6 int main()  
  7.   7 {  
  8.   8     int fd=open("fifo",O_RDONLY);  
  9.   9     assert(fd!=-1);  
  10.  10     printf("fd=%d",fd);  
  11.  11     char buff[128]={0};  
  12.  12     gets(buff);  
  13.  13     read(fd,buff,128);  
  14.  14     printf("%s",buff);  
  15.  15     return 0;  
  16.  16 }  
  17. ~                                                                              
  18. ~    命名管道的用法(两个进程间的通信)通过管道文件                                                                         
  19. ~   #include <stdlib.h>  
  20.   3 #include <fcntl.h>  
  21.   4 #include <string.h>  
  22.   5 #include <assert.h>  
  23.   6 int main()  
  24.   7 {  
  25.   8     char buff[128]={0};  
  26.   9     int fd=open("fifo",O_WRONLY);  
  27.  10     assert(fd!=-1);  
  28.  11     printf("fd=%d\n",fd);  
  29.  12     while(1)  
  30.  13     {  
  31.  14         memset(buff,0,127);  
  32.  15         gets(buff);  
  33.  16         if(strcmp(buff,"end")==0)  
  34.  17         {  
  35.  18             break;  
  36.  19         }  
  37.  20         write(fd,buff,127);  
  38.  21     }  
  39.  22     close(fd);  
  40.  23     return 0;  
  41.  24 }  
  42.   
对于命名管道而言也是有大小的和匿名管道的大小都是65536;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值