前文中笔者介绍了管道,本文接着介绍命名管道。文中演示所用环境为 Ubuntu 18.04 desktop。html
命名管道(named pipe)又被称为先进先出队列(FIFO),是一种特殊的管道,存在于文件系统中。命名管道与管道很是相似,可是又有自身的显著特征:编程
和管道同样,命名管道也只能用于数据的单向传输,若是要用命名管道实现两个进程间数据的双向传输,建议使用两个单向的命名管道。函数
在命令行上建立命名管道
能够经过命令行命令 mkfifo 或 mknod 建立命名管道:spa
$ mkfifo /tmp/testp $ mknod /tmp/testp p
能够经过 ls 命令查看命名管道的文件属性:命令行
输出中的第一个字符为 p,表示这个文件的类型为管道。最后的 | 符号是有 ls 命令的 -F 选项添加的,也表示这个一个管道。设计
在程序中建立命名管道
在程序中建立命名管道,能够使用 mkfifo 函数,其签名以下:3d
#include <sys/types.h> #include <sys/stat.h> int mkfifo(const char *pathname, mode_t mode);
参数 pathname 是一个字符串指针,用于存放命名管道的文件路径。参数 mode 用于表示指定所建立文件的权限。该函数调用成功时返回 0;调用失败时返回 -1。
mkfifo 函数是一个专门用来建立命名管道的函数,而另一个函数 mknod 却能够兼职建立命名文件,其函数签名以下:指针
#include <sys/types.h> #include <sys/stat.h> int mknod(char *pathname, mode_t mode, dev_t dev);
建立命名管道只是 mknod 函数的功能之一,它的前两个参数和 mkfifo 函数相同。在建立命名管道时,为第三个参数 dev 传递 0 就能够了。该函数调用成功时返回 0;调用失败时返回 -1。code
下面的 demo 模拟一个生产者进程和消费者进程,两者经过命名管道传输数据。生产者的代码以下:htm
#include <limits.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/testp" #define BUFFER_SIZE 4096 #define TEN_MEG (1024 * 1024 * 10) int main(void) { int pipe_fd; int res; int open_mode = O_WRONLY; int bytes_sent = 0; char buffer[BUFFER_SIZE + 1]; if(access(FIFO_NAME, F_OK) == -1) { res = mkfifo(FIFO_NAME, 0777); if(res != 0) { fprintf(stderr, "Could not create fifo %s\n", FIFO_NAME); exit(EXIT_FAILURE); } } printf("Process %d opening FIFO O_WRONLY\n", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d opened fd %d\n", getpid(), pipe_fd); if(pipe_fd != -1) { while(bytes_sent < TEN_MEG) { res = write(pipe_fd, buffer, BUFFER_SIZE); if(res == -1) { fprintf(stderr, "Write error on pipe\n"); exit(EXIT_FAILURE); } bytes_sent += res; } (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished\n", getpid()); exit(EXIT_SUCCESS); }
把上面的代码保存到文件 namedpipedemo.c 中。
消费者的代码以下:
#include <limits.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #define FIFO_NAME "/tmp/testp" #define BUFFER_SIZE 4096 int main(void) { int pipe_fd; int res; int open_mode = O_RDONLY; int bytes_read = 0; char buffer[BUFFER_SIZE + 1]; memset(buffer, '\0', sizeof(buffer)); printf("Process %d opening FIFO O_RDONLY\n", getpid()); pipe_fd = open(FIFO_NAME, open_mode); printf("Process %d opened fd %d\n", getpid(), pipe_fd); if(pipe_fd != -1) { do { res = read(pipe_fd, buffer, BUFFER_SIZE); bytes_read += res; } while (res > 0); (void)close(pipe_fd); } else { exit(EXIT_FAILURE); } printf("Process %d finished, %d bytes read\n", getpid(), bytes_read); exit(EXIT_SUCCESS); }
把上面的代码保存到文件 namedpipedemo2.c 中。并分别编译这两个程序:
$ gcc -Wall namedpipedemo.c -o pipe1 $ gcc -Wall namedpipedemo2.c -o pipe2
先在一个终端中执行生产者:
而后在另外一个终端中执行消费者:
结果是两者完成数据传输后都返回了:
删除命名管道和删除一个普通文件没有什么区别:
$ rm /tmp/testp
这就能够了!
参考:
《Linux 程序设计》
《Linux 环境下 C 编程指南》