为什么无法从尾部 -f 获取数据?
Why can't get data from tail -f?
在我的程序中,我将子进程的输出重定向到管道,并在父进程中获得此结果并做一些事情(这并不重要)。但是当我使用tail -f test.txt
命令并且在tail运行期间不接收任何数据时,我的程序不工作,并且只有在tail完成(或杀死)之后才获得此数据。
起初我认为问题是tail -f
不刷新,这就是为什么没有数据我可以接收,但是当我试图将tail -f
的输出重定向到一些文件时,数据在这个文件中,即使尾巴没有完成。
//the code of creating child and redirecting data (important part)
//only core is here so please don't tell me that maybe pipe() or fork() is failed
pid_t pid;
int outpipe[2]; //pipe for reading from stdout
int errpipe[2]; //pipe for reading from stderr
// Createing pipes for childs stdout and stderr streams
pipe(outpipe);
pipe(errpipe);
pid = fork();
if(pid == 0)
{
// This is the child process. Closing read end of pipes and duplicating stdout and stderr streams
close(outpipe[0]);
dup2(outpipe[1], STDOUT_FILENO);
close(errpipe[0]);
dup2(errpipe[1], STDERR_FILENO);
if(execvp(argv[0], (char * const *)argv) == -1)
{
fprintf(stderr, "Failed to execute command %s: %s", argv[0], strerror(errno));
_exit(EXIT_FAILURE);
}
_exit(EXIT_SUCCESS);
}
else if (pid != -1)
{
// This is the parent process, Closing write end of pipes and opening fds as FILE
close(outpipe[1]);
*child_stdout_stream=fdopen(outpipe[0], "rt");
close(errpipe[1]);
*child_stderr_stream=fdopen(errpipe[0], "rt");
*child_pid=pid;
}
然后我从child_stderr_stream
和child_stdout_stream
中读取,它们作为参数传递给功能,上面的部分来自什么。对于读取,我使用select()来不阻塞程序,直到从其中一个流读取。
添加select and read部分
int select_and_read(FILE **files, bool *is_eof, char *chars, int *mask, int nfiles, int timeout, pid_t child_pid)
{
int max_fd_plus_1 = 0;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
for(int i = 0; i < nfiles; ++i)
{
if(is_eof[i]==false)
{
FD_SET(fileno(files[i]), &rfds);
max_fd_plus_1 = (max_fd_plus_1 > fileno(files[i])) ? max_fd_plus_1 : fileno(files[i]);
}
}
++max_fd_plus_1;
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
int retval = select(max_fd_plus_1, &rfds, NULL, NULL, &tv);
if(retval > 0)
{
*mask = 0;
for(int i = 0; i < nfiles; ++i)
{
if(is_eof[i]==false)
{
if(FD_ISSET(fileno(files[i]), &rfds))
{
*mask |= 1 << i;
chars[i] = fgetc(files[i]);
}
}
}
}
else
{
kill(child_pid, SIGKILL);
}
return retval;
}
这个奇怪的问题被奇怪地解决了。我刚刚用这种方式将文件缓冲区设置为0:
else if (pid != -1)
{
// This is the parent process, Closing write end of pipes and opening fds as FILE
close(outpipe[1]);
*child_stdout_stream=fdopen(outpipe[0], "rt");
setbuf(*child_stdout_stream, NULL);
close(errpipe[1]);
*child_stderr_stream=fdopen(errpipe[0], "rt");
setbuf(*child_stderr_stream, NULL);
*child_pid=pid;
}
这是非常奇怪的,这有帮助,但无论如何,我的程序现在运行良好。
相关文章:
- C++ - 忽略并从其他文件获取数据
- 如何从网站获取数据并将其传输到数据库?
- 与多个 for 循环与单个 for 循环 wrt 相关的性能从多映射获取数据
- 如何从 CSV 获取数据并将其存储在 C++ 中的表对象中
- 获取 R 数据帧的内存地址
- C++ 如何从虚拟类的模板化子类中获取数据?
- 如何在C++中使用带有SFML的http reqest从节点.js服务器获取数据?
- 连接到远程存储库并获取数据
- 使用WiFi.h从WiFiClient ESP32获取数据
- CSV文件未从缓冲区获取数据
- C++从双指针获取数据
- C++ 从文件中获取数据时使用 strcpy 和 strcmp 按字母顺序对数组进行排序?
- 使用 Recvfrom 获取数据
- C 编程:在循环时运行 2 并从 loop1 获取数据的随机结果
- 复制构造函数并从方法中获取数据
- 如何在运行时从平面缓冲区获取数据值和数据类型
- 在C J2ME中读取Excel文件,并从ITQ获取数据
- 如何通过NodeMCU从Web获取数据
- 获取数据QFUTUREWATCHER
- 从文件中获取数据时如何放置换行符?