【发布时间】:2017-08-18 08:58:33
【问题描述】:
我想将一个先进先出重定向到标准输出和 我读了文档http://man7.org/linux/man-pages/man2/tee.2.html
上面写着tee(int fd_in, int fd_out,...)
但是当我向第一个参数抛出一个 fifo fd 时,它会显示无效错误。
#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <limits.h>
int main() {
int num = 0, fd;
char fifo[] = "/tmp/tmpfifo";
fd = open(fifo, O_RDONLY, 0644);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
num = tee(fd, STDOUT_FILENO, INT_MAX, SPLICE_F_NONBLOCK);
if (num < 0) {
perror("tee");
exit(EXIT_FAILURE);
}
fprintf(stderr,"%d\n", num);
return 0;
}
控制台显示:tee:invalid arguments。 第一个参数应该是标准输入?
【问题讨论】:
-
我想你想要
dup2函数。 -
顺便问一下,您确定传递给
open的标志吗?不应该只有O_RDONLY或O_WRONLY吗?您的程序是否应该与自身通信?你是如何创建 FIFO 的? -
如果我仍然使用 tee() 系统调用,这是否有效?
-
通过您的编辑,目的是自动将您从 FIFO 中读取的所有内容通过管道传输到
stdout?您要解决的实际问题是什么? -
您不应该为读写打开一个fifo,您必须将调用分开......
tee旨在与fifos一起使用,来自文档EINVAL fd_in or fd_out does not refer to a pipe; or fd_in and fd_out refer to the same pipe.。标准输出是先进先出吗?