为什么tail -f无法获取数据?

发布于 2024-11-02 06:28:01 字数 2649 浏览 3 评论 0原文

在我的程序中,我将子进程的输出重定向到管道,并在父进程中获取此结果并执行某些操作(这并不重要)。但是当我使用 tail -f test.txt 命令时,我的程序无法工作,并且在 tail 运行期间没有收到任何数据,并且仅在 tail 完成(或被杀死)后才获取此数据)。

起初我认为问题是 tail -f 没有刷新,这就是为什么我无法收到数据,但是当我尝试重定向 tail -f 的输出时> 对于某些文件,即使 tail 未完成,数据也位于该文件中。

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

In my program I'm redirecting output of child process to pipe and in parent getting this result and doing something (this is not important). But my program doesn't work when I'm using tail -f test.txt command and doesn't recieve any data during tail is running, and getting this data only after tail is finished (or killed).

At first I have thought that problem was that tail -f doesn't flushing and that's why no data I can recieve, but when I have tried to redirect output of tail -f to some file the data were in this file even when tail were not finished.

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

Then I'm reading from child_stderr_stream and child_stdout_stream which were passed as parameters to function the part above is from what.
For reading I'm using select() to not block program until reading from one of the streams.


Adding part of 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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤蝉 2024-11-09 06:28:01

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

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

This strange problem have been solved very strangely. I have just set buffers of files to 0 this way:

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

This is very strange, that this helps, but in any case my program is now working well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文