什么是命名管道
匿名管道只能用于父子关系的进程之间进行通信。命名管道是一种实际存在的FIFO文件,称作“管道文件”,用于不同的进程间,命名管道进程间打开同一个FIFO文件,进行通信。
创建命名管道
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char* pathname, mode_t mode);
参数
- pathname: FIFO文件的路径
- mode: 文件的权限
返回值
- 成功:0
- 失败:-1,errno
打开命名管道
#include <sys/types.h>
#include <sys/stat.h>
int open(const char* pathname, int flags);
参数
- pathname: FIFO文件的路径
- flags:
- O_RDONLY
- O_WRONLY
- O_NONBLOCK
返回值
- 成功:文件描述符
- 失败:-1,errno
注意: 命名管道只有在两个进程中同时打开,并且是以一端只读,另一端只写的方式打开时,才能完成打开操作,不然会发生阻塞,直到两端同时使用打开操作为止。
向命名管道写
#include <unistd.h>
ssize_t write(int fd, void* buf, size_t count);
参数
- fd: 文件描述符
- buf: 缓冲区
- count: 写入的字节数
返回值
- 成功:写入的字节数
- 失败:-1,errno
从命名管道读
#include <unistd.h>
ssize_t read(int fd, void* buf, size_t count);
参数
- fd: 文件描述符
- buf: 缓冲区
- count: 读取的字节数上限
返回值
- 成功:读取的字节数
- 失败:-1,errno
示例
//fifowrite.c
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
const char* fifo_name = "my_fifo";
const char* fifo_name2 = "your_fifo";
char buffer[PIPE_BUF + 1];
char buffer2[PIPE_BUF + 1];
if (access(fifo_name, F_OK) == -1)
{
int res = mkfifo(fifo_name, 0774);
if (res != 0)
{
perror("Could not create fifo");
exit(-1);
}
}
if (access(fifo_name2, F_OK) == -1)
{
int res = mkfifo(fifo_name2, 0774);
if (res != 0)
{
perror("Could not create fifo2");
exit(-1);
}
}
int pipe_fd = open(fifo_name, O_WRONLY);
int pipe_fd2 = open(fifo_name2, O_RDONLY);
if (pipe_fd == -1)
{
perror("Open pipe failed.");
exit(-1);
}
if (pipe_fd2 == -1)
{
perror("Open pipe failed.");
exit(-1);
}
int bytes_read = 0;
while(1)
{
printf("$ ");fflush(stdout);
bytes_read = read(0, buffer, PIPE_BUF);
if (bytes_read > 0)
{
buffer[bytes_read] = '\0';
int bytes_write = write(pipe_fd, buffer, bytes_read);
if (bytes_write <= 0)
{
perror("Write to pipe failed.");
close(pipe_fd);
exit(-1);
}
}
else
break;
bytes_read = read(pipe_fd2, buffer2, PIPE_BUF);
if (bytes_read > 0)
{
buffer2[bytes_read] = '\0';
write(1, buffer2, bytes_read);
}
}
close(pipe_fd);
close(pipe_fd2);
return 0;
}
//fiforead.c
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
const char* fifo_name = "my_fifo";
const char* fifo_name2 = "your_fifo";
char buffer[PIPE_BUF + 1];
int pipe_fd = open(fifo_name, O_RDONLY);
int pipe_fd2 = open(fifo_name2, O_WRONLY);
if (pipe_fd == -1)
{
perror("Open pipe failed");
exit(-1);
}
if (pipe_fd2 == -1)
{
perror("Open pipe failed");
exit(-1);
}
dup2(pipe_fd, 0);
dup2(pipe_fd2, 1);
dup2(pipe_fd2, 2);
execl("/bin/sh", (char*)NULL);
close(pipe_fd);
close(pipe_fd2);
return 0;
}