Linux IPC实践(3) --具名FIFO
来源:互联网 发布:mac铁锈红是chili吗 编辑:程序博客网 时间:2024/04/30 02:27
FIFO具名/命名管道
(匿名)管道应用的一个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。
如果我们想在不相关的进程之间交换数据,可以使用FIFO文件来做这项工作,它经常被称为命名管道;命名管道是一种特殊类型的文件.
创建一个命名管道
1)命名管道可以从命令行上创建:
$ mkfifo <filename>
2)命名管道在程序里创建:
- #include <sys/types.h>
- #include <sys/stat.h>
- int mkfifo(const char *pathname, mode_t mode);
- //示例
- int main()
- {
- if (mkfifo("p2", 0644) == -1)
- err_exit("mkfifo error");
- }
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(非阻塞):立刻返回成功
- //示例1: 阻塞, 只读打开
- int main()
- {
- int fd = open("fifo", O_RDONLY);
- if (fd == -1)
- err_exit("FIFO open error");
- cout << "fifo O_RDONLY open success" << endl;
- }
- //示例2: 只读, 非阻塞打开
- int main()
- {
- int fd = open("fifo", O_RDONLY|O_NONBLOCK);
- if (fd == -1)
- err_exit("FIFO open error");
- cout << "fifo O_RDONLY open success" << endl;
- }
2)写打开FIFO
O_NONBLOCK disable(阻塞):阻塞直到有相应进程为读而打开该FIFO
O_NONBLOCK enable(非阻塞):立刻返回失败,错误码为ENXIO
- //示例1: 阻塞, 只写打开
- int main()
- {
- int fd = open("fifo", O_WRONLY);
- if (fd == -1)
- err_exit("FIFO open error");
- cout << "FIFO O_WRONLY open success" << endl;
- }
- //示例2: 非阻塞, 只写打开
- int main()
- {
- int fd = open("fifo", O_WRONLY|O_NONBLOCK);
- if (fd == -1)
- err_exit("FIFO open error");
- cout << "FIFO O_WRONLY open success" << endl;
- }
命名管道的读写规则
同匿名管道
- /**FIFO示例, 两个进程通过FIFO对拷数据:利用管道,两个进程间进行文件复制。
- 1.进程writefifo:
- (1)读文件(文件名从命令行参数中获取)
- (2)写入管道myFifo(管道由该进程创建)
- (3)关闭文件及管道
- 2.进程readfifo:
- (1)读管道myFifo
- (2)写入文件[该文件有进程创建并打开]
- (3)关闭文件
- (4)删除管道
- */
- //1:writefifo
- int main(int argc, char *argv[])
- {
- if (argc < 2)
- err_quit("Usage: ./writefifo <read-file-name>");
- // 创建管道
- if (mkfifo("myFifo", 0644) == -1)
- err_exit("mkfifo error");
- int outfd = open("myFifo", O_WRONLY); //打开FIFO
- int infd = open(argv[1], O_RDONLY); //打开文件
- if (outfd == -1 || infd == -1)
- err_exit("open file/fifo error");
- char buf[BUFSIZ];
- int readBytes;
- while ((readBytes = read(infd, buf, sizeof(buf))) > 0)
- {
- write(outfd, buf, readBytes);
- }
- close(infd);
- close(outfd);
- }
- //2:readfifo
- int main(int argc, char *argv[])
- {
- if (argc < 2)
- err_quit("Usage: ./writefifo <write-file-name>");
- int outfd = open(argv[1], O_WRONLY|O_CREAT|O_TRUNC, 0644); //创建并打卡文件
- int infd = open("myFifo", O_RDONLY); //打开FIFO
- if (infd == -1 || outfd == -1)
- err_exit("open file/fifo error");
- char buf[BUFSIZ];
- int readBytes;
- while ((readBytes = read(infd, buf, sizeof(buf))) > 0)
- {
- write(outfd, buf, readBytes);
- }
- close(outfd);
- unlink("myFifo"); //删除FIFO
- }
- Linux IPC实践(3) --具名FIFO
- Linux IPC实践(3) --具名FIFO
- Linux IPC实践(3) --具名FIFO
- Linux IPC - FIFO
- linux ipc &pipe fifo
- linux ipc—FIFO
- Linux IPC,FIFO和shm
- Linux进程间通信(IPC)编程实践(二) FIFO命名管道
- IPC FIFO
- Linux——IPC 管道、FIFO
- Linux IPC通讯之-管道(PIPE、FIFO)
- Linux中IPC机制:管道和FIFO
- Linux进程间通讯(IPC)------FIFO
- Linux IPC之管道和FIFO
- Linux IPC实践(1) -- 概述
- Linux IPC实践(1) -- 概述
- Linux进程间通信IPC(二)-- FIFO
- UNIX/Linux进程间通信IPC系列(三)FIFO
- Linux下ls命令实现
- web测试方法总结(三)
- LeetCode---TwoSum
- oracel update
- OpenCV之处理视频帧
- Linux IPC实践(3) --具名FIFO
- clustered index和secondary indexes
- java基础学习笔记——LinkedList
- CMake生成VS2010工程相对路径和绝对路径问题说明
- TCP建立连接为什么是三次握手?
- String
- Oracle 编译、调用或调试Procedure、Function或Package时卡死问题
- DotNet中FTP操作
- HDFS的架构及原理