前文:
Linux–进程间的通信-匿名管道
Linux–进程间的通信–进程池
命名管道的概念
命名管道是一种进程间通信(IPC)机制,运行不同进程之间进行可靠的、单向或双向的数据通信。
特点和作用:
- 跨平台性:在WIndows和Linux系统中都有实现,因此具有良好的跨平台性。
- 简单易用: 使用命名管道进行通信相对简单。
- 安全性:命名管道充分利用了操作系统提供的安全特性,如WIndows的访问控制列表(ACL),从而确保通信的安全性。
命名管道的模拟实现
mkfifo是用于创建命名管道的命令,它的作用是在文件系统中创建一个特殊类型的文件,该文件可被多个进程用于进程间的通信。
这是mkfifo函数。
代码
Comm.hpp: 这里面包含了一个有关命名管道的类,包括管道的创建和销毁。
#ifndef __COMM_HPP__ #define __COMM_HPP__ #include #include #include #include #include #include #include #include using namespace std; #define Mode 0666 #define Path "./fifo" class fifo { public: fifo(const string & path) :_path(path) { umask(0); int n=mkfifo(_path.c_str(),Mode); if(n==0) { cout cerr int n=unlink(_path.c_str()); if (n == 0) { cout cerr int wfd=open(Path,O_WRONLY); if(wfd cerr cout cerr fifo ff(Path); int rfd=open(Path,O_RDONLY); if(rfd cerr ssize_t n=read(rfd,buffer,sizeof(buffer)-1); if(n0) { buffer[n]=0; cout cout cerr
文章版权声明:除非注明,否则均为VPS857原创文章,转载或复制请以超链接形式并注明出处。
还没有评论,来说两句吧...