检查 unix 管道是否关闭而不写入任何内容?

nik*_*tiu 5 c unix pipe process

基本上,我有一个父进程,它分叉一个子进程并通过管道将它的 stdin 提供给它。子进程可以在以下两种情况之一终止:

  • 管道的写端被父进程关闭,这意味着它到达了 stdin 的末尾,从而接收到一个EOF
  • 或者它通过管道接收某个输入(在这种情况下为-1)并退出

我的父代码大致如下所示:

close(pi[0]); // close input end
signal(SIGPIPE, SIG_IGN); // do not handle SIGPIPE
char buffer;
int ok = 1;
while(ok && read(STDIN_FILENO, &buffer, 1) > 0)  {
    int b_written = write(pi[1], &buffer, 1);
    if(b_written == -1) {
        if(errno == EPIPE) ok = 0;
        else perror("pipe write"); // some other error
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我通过检查errno == EPIPE. 然而,这意味着读取循环在关闭之前会进行一次额外的迭代。我怎么可能轮询以查看管道是否已关闭而不必对其进行写入?

Tav*_*nes 6

此代码段将检查可写管道的另一端是否使用poll(2). 这适用于 Linux——我不确定其他操作系统或 POSIX 所说的内容。

#include <poll.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>

bool is_pipe_closed(int fd) {
    struct pollfd pfd = {
        .fd = fd,
        .events = POLLOUT,
    };

    if (poll(&pfd, 1, 1) < 0) {
        return false;
    }

    return pfd.revents & POLLERR;
}
Run Code Online (Sandbox Code Playgroud)


jda*_*nay 1

孩子可以发送信号,例如SIGUSR1当它检测到它已经完成时。父级可以在接收信号时设置一个标志SIGUSR1,并在尝试读取输入之前检查该标志。但我不确定SIGUSR1在读取来自 ) 的输入之前检查标志 ans 后是否无法收到stdin。所以我更喜欢使用控制管道,每次子进程知道它能够再读取一个数据时,它都会在该控制管道中写入 1。结果可能是这样的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>

#define STOP_VALUE 100
#define SIZE_STDIN_BUFFER 1024

static char can_read_more = 1;

static int handle_child(int *p_child_input_stream, int *p_control_stream)
{
    int pipefd[2][2];
    pid_t fk;

    if (pipe(pipefd[0]) < 0) // Pipe to read input from 
    {
        perror("pipe");
        return -1;
    }

    if (pipe(pipefd[1]) < 0) // Pipe to notifiate parent input can be processed
    {
        perror("pipe");
        close(pipefd[0][0]);
        close(pipefd[0][1]);
        return -1;
    }

    if ((fk = fork()) < 0)
    {
        perror("fork");
        close(pipefd[0][0]);
        close(pipefd[0][1]);
        close(pipefd[1][0]);
        close(pipefd[1][1]);
        return -1;
    }

    if (fk == 0)
    {
        close(pipefd[0][1]);
        close(pipefd[1][0]);
        write(pipefd[1][1], &can_read_more, sizeof(char)); // sizeof(char) == 1

        ssize_t nb_read = 0;
        char buffer;
        while (nb_read >= 0)
        {
            nb_read = read(pipefd[0][0], &buffer, sizeof(char));
            if (nb_read > 0)
            {
                printf("0x%02x\n", (unsigned int) buffer);
                if (buffer == STOP_VALUE)
                {
                    nb_read = -1;
                }
                else
                {
                    write(pipefd[1][1], &can_read_more, sizeof(char));
                }
            }
        }
        close(pipefd[0][0]);
        close(pipefd[1][1]);
        exit(0);
    }

    close(pipefd[0][0]);
    close(pipefd[1][1]);

    *p_child_input_stream = pipefd[0][1];
    *p_control_stream = pipefd[1][0];

    return 0;
}

int main()
{
    int child_input_stream;
    int control_stream;

    if (handle_child(&child_input_stream, &control_stream) < 0)
    {
        return 1;
    }

    char stdin_buffer[SIZE_STDIN_BUFFER];
    char buffer;
    int ok = 1;
    int child_available_input = 0;

    while(ok)
    {
        while (child_available_input <= 0 && ok)
        {
            ssize_t nb_control = read(control_stream, &buffer, sizeof(char));
            if (nb_control > 0)
            {
                child_available_input += buffer;
            }
            else
            {
                fprintf(stderr, "End of child reading its input detected.\n");
                ok = 0;
            }
        }

        if (ok)
        {
            if (fgets(stdin_buffer, SIZE_STDIN_BUFFER, stdin) == NULL)
            {
                ok = 0;
            }
            else
            {
                if (stdin_buffer[strlen(stdin_buffer) - 1] == '\n')
                {
                    stdin_buffer[strlen(stdin_buffer) - 1] = '\0';
                }

                char dummy;
                int input;
                if (sscanf(stdin_buffer, "%d%c", &input, &dummy) == 1)
                {
                    buffer = (char) input;
                    write(child_input_stream, &buffer, sizeof(char));
                    child_available_input--;
                }
            }
        }
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)