Linux IPC实践(3) --具名FIFO

来源:互联网 发布:mac铁锈红是chili吗 编辑:程序博客网 时间:2024/04/30 02:27

FIFO具名/命名管道

   (匿名)管道应用的一个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。

   如果我们想在不相关的进程之间交换数据,可以使用FIFO文件来做这项工作,它经常被称为命名管道;命名管道是一种特殊类型的文件.

 

创建一个命名管道

1)命名管道可以从命令行上创建:

  $ mkfifo <filename>

2)命名管道在程序里创建:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. int mkfifo(const char *pathname, mode_t mode);  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例  
  2. int main()  
  3. {  
  4.     if (mkfifo("p2", 0644) == -1)  
  5.         err_exit("mkfifo error");  
  6. }  

FIFO与PIPE的区别:

1) 匿名管道由pipe函数创建并打开。

   命名管道由mkfifo函数创建,打开用open

2) FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的语义  (The only difference between pipes and FIFOs is the manner in which they are created and opened.  

Once these tasks have been accomplished, I/O on pipes and FIFOs has exactly  the same semantics.)

 

命名管道的打开规则

1)读打开FIFO

   O_NONBLOCK disable(阻塞):阻塞直到有相应进程为写而打开该FIFO

   O_NONBLOCK enable(非阻塞):立刻返回成功

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例1: 阻塞, 只读打开  
  2. int main()  
  3. {  
  4.     int fd = open("fifo", O_RDONLY);  
  5.     if (fd == -1)  
  6.         err_exit("FIFO open error");  
  7.     cout << "fifo O_RDONLY open success" << endl;  
  8. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例2: 只读, 非阻塞打开  
  2. int main()  
  3. {  
  4.     int fd = open("fifo", O_RDONLY|O_NONBLOCK);  
  5.     if (fd == -1)  
  6.         err_exit("FIFO open error");  
  7.     cout << "fifo O_RDONLY open success" << endl;  
  8. }  

2)写打开FIFO

   O_NONBLOCK disable(阻塞):阻塞直到有相应进程为读而打开该FIFO

   O_NONBLOCK enable(非阻塞):立刻返回失败,错误码为ENXIO

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例1: 阻塞, 只写打开  
  2. int main()  
  3. {  
  4.     int fd = open("fifo", O_WRONLY);  
  5.     if (fd == -1)  
  6.         err_exit("FIFO open error");  
  7.     cout << "FIFO O_WRONLY open success" << endl;  
  8. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //示例2: 非阻塞, 只写打开  
  2. int main()  
  3. {  
  4.     int fd = open("fifo", O_WRONLY|O_NONBLOCK);  
  5.     if (fd == -1)  
  6.         err_exit("FIFO open error");  
  7.     cout << "FIFO O_WRONLY open success" << endl;  
  8. }  

命名管道的读写规则

同匿名管道


[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. /**FIFO示例, 两个进程通过FIFO对拷数据:利用管道,两个进程间进行文件复制。 
  2. 1.进程writefifo: 
  3. (1)读文件(文件名从命令行参数中获取) 
  4. (2)写入管道myFifo(管道由该进程创建) 
  5. (3)关闭文件及管道 
  6.  
  7. 2.进程readfifo:    
  8. (1)读管道myFifo 
  9. (2)写入文件[该文件有进程创建并打开] 
  10. (3)关闭文件 
  11. (4)删除管道 
  12. */  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //1:writefifo  
  2. int main(int argc, char *argv[])  
  3. {  
  4.     if (argc < 2)  
  5.         err_quit("Usage: ./writefifo <read-file-name>");  
  6.   
  7.     // 创建管道  
  8.     if (mkfifo("myFifo", 0644) == -1)  
  9.         err_exit("mkfifo error");  
  10.     int outfd = open("myFifo", O_WRONLY);   //打开FIFO  
  11.     int infd = open(argv[1], O_RDONLY);     //打开文件  
  12.     if (outfd == -1 || infd == -1)  
  13.         err_exit("open file/fifo error");  
  14.   
  15.     char buf[BUFSIZ];  
  16.     int readBytes;  
  17.     while ((readBytes = read(infd, buf, sizeof(buf))) > 0)  
  18.     {  
  19.         write(outfd, buf, readBytes);  
  20.     }  
  21.     close(infd);  
  22.     close(outfd);  
  23. }  
[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //2:readfifo  
  2. int main(int argc, char *argv[])  
  3. {  
  4.     if (argc < 2)  
  5.         err_quit("Usage: ./writefifo <write-file-name>");  
  6.   
  7.     int outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644);  //创建并打卡文件  
  8.     int infd = open("myFifo", O_RDONLY);    //打开FIFO  
  9.     if (infd == -1 || outfd == -1)  
  10.         err_exit("open file/fifo error");  
  11.   
  12.     char buf[BUFSIZ];  
  13.     int readBytes;  
  14.     while ((readBytes = read(infd, buf, sizeof(buf))) > 0)  
  15.     {  
  16.         write(outfd, buf, readBytes);  
  17.     }  
  18.   
  19.     close(outfd);  
  20.     unlink("myFifo");   //删除FIFO  
  21. }  
0 0
原创粉丝点击