【问题标题】:Why can't get data from tail -f?为什么无法从tail -f获取数据?
【发布时间】:2011-08-06 10:42:18
【问题描述】:

在我的程序中,我将子进程的输出重定向到管道,并且在父进程中得到这个结果并做一些事情(这并不重要)。但是当我使用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_streamchild_stdout_stream 读取它们作为参数传递给函数上面的部分来自什么。 对于阅读,我使用 select() 来阻止程序,直到从其中一个流中读取。


添加部分选择和读取

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;
}

【问题讨论】:

  • 问题可能在 select() 中。添加一些调试以查看当您附加到标准输出时 FD 是否准备就绪。也许您是标准输出上的 select()?
  • 是的 select() 并没有告诉我我可以从流中读取。它挂了,但为什么?我该如何解决这个问题?
  • 你在选择()什么?这里似乎缺少一些重要的部分。
  • 不要分叉一个进程来做这件事,而是找到tail的源代码,了解它的工作原理并自己做。一旦你剥离了所有的论点处理,它就没什么了。很多年前我做了同样的事情来写相当于写cat,然后写tail -f,这并不是一个很大的努力。
  • @Blrfl 可能程序不是tail的替代品,它只是可以运行它。

标签: c++ c unix posix ipc


【解决方案1】:

这个奇怪的问题已经很奇怪地解决了。我刚刚以这种方式将文件缓冲区设置为 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;
}

这很奇怪,这很有帮助,但无论如何我的程序现在运行良好。

【讨论】:

  • 这并不奇怪,这只是一个缓冲问题。如果您的程序在流中打印了更多数据,那么您会看到每 X 个字节(其中 X 通常为 4096,但并非总是如此)获得输出,这对性能和电池寿命有好处(您的程序睡眠时间更长)。但是,如果您想在行到达时查看行,则使用行缓冲比不使用缓冲要好。
  • 这很奇怪,因为问题只有尾巴。其他尾巴类型的程序运行良好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 2019-02-13
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-26
相关资源
最近更新 更多