管道是一种单向通信的方式,一般用于进程间通信,在零拷贝时也会用到管道。管道本质上是一个文件,一个进程读,一个进程写。但是管道本身不占用磁盘或者其他外部存储的空间。在Linux上它占用内存空间。所以管道就是一个操作方式为文件的内存缓冲区。也正是因为管道是内存中的,所以它比使用文件作通信更快。
1.命名管道: 命名管道需要包含头文件:#include
命名管道相当于创建了一个有命的文件,那么任何进程只要知道它的文件名就可以使用该管道,因此它不受亲缘进程的影响。命名管道创建和使用方式与文件差距不大,它使用mkfifo创建并使用open,write和read来打开,写和读。但是一个进程对命名管道只可以读和写选一个,不可以即读又写。例:
#include#include #include #include #include #include #include #include const char buf[] = "hello"; const int32_t bufSize = sizeof(buf); const char str[] = "/root/namePipe"; int main() { //创建命名管道 int32_t ret = mkfifo(str, S_IFIFO | 0666); if (ret == -1) { std::cout << "Make fifo errorn"; return -1; } pid_t pid; pid = fork(); if (pid > 0) { int32_t fd = open(str, O_WRONLY); if (write(fd, buf, bufSize) < 0) { std::cout << "write errorn"; } close(fd); return 0; } sleep(1); char readBuf[bufSize]; int32_t fd = open(str, O_RDONLY); if (read(fd, readBuf, bufSize) < 0) { std::cout << "read errorn"; } else { std::cout << buf << 'n'; } close(fd); return 0; }
删除命名管道可以用linux的unlink命令
unlink 管道名2.匿名管道:
匿名管道使用pipe创建,需要包含头文件:#include
pipe会初始化一个int[2],其中一个只读,另一个只写。由于它没有文件名,只有int变量存储的文件描述符,因此只有有血缘关系的进程才能使用它通信。匿名管道用法如下:
#include#include #include #include #include #include #include int main() { int32_t pipeFd[2]; if (pipe(pipeFd) == -1) { perror("Pipe failed:"); return 0; } pid_t pid; pid = fork(); if (pid > 0) { close(pipeFd[0]); //父进程只写,所以关闭读描述符 char buf[] = "hello"; if (write(pipeFd[1], buf, sizeof(buf)) < 0) { perror("Write error:"); } close(pipeFd[1]); wait(nullptr); //防僵尸进程 return 0; } close(pipeFd[1]); //子进程只读,关闭写描述符 char buf[32]; memset(buf, 0, 32); if (read(pipeFd[0], buf, 32) < 0) { perror("Read error:"); } else { std::cout << buf << 'n'; } close(pipeFd[0]); return 0; }
需要注意的是如果所有管道的读描述符被关闭,那么写将阻塞。如果所有的写描述符已关闭,那么读将无法读到返回0。



