如果子进程在阅读时无法关闭写入管道,会发生什么情况? [英] What happens if a child process won't close the pipe from writing, while reading?

查看:152
本文介绍了如果子进程在阅读时无法关闭写入管道,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

int main(int argc, char *argv[])
{
    int pipefd[2];
    pid_t cpid;
    char buf;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s \n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (pipe(pipefd) == -1) {
        perror("pipe");
        exit(EXIT_FAILURE);
    }

    cpid = fork();
    if (cpid == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    }

    if (cpid == 0) {    /* Child reads from pipe */
        close(pipefd[1]);          /* Close unused write end */

        while (read(pipefd[0], &buf, 1) > 0)
            write(STDOUT_FILENO, &buf, 1);

        write(STDOUT_FILENO, "\n", 1);
        close(pipefd[0]);
        _exit(EXIT_SUCCESS);

    } else {            /* Parent writes argv[1] to pipe */
        close(pipefd[0]);          /* Close unused read end */
        write(pipefd[1], argv[1], strlen(argv[1]));
        close(pipefd[1]);          /* Reader will see EOF */
        wait(NULL);                /* Wait for child */
        exit(EXIT_SUCCESS);
    }
return 0;

}

无论何时子进程想要从管道读取,它都必须首先关闭管道一侧以防止写入.当我从子进程的if中删除该行close(pipefd[1]);时, 我基本上是说好,孩子可以从管道中读取内容,但是我允许父母同时向管道中写入内容"?

Whenever the child process wants to read from the pipe, it must first close the pipe's side from writing. When I remove that line close(pipefd[1]); from the child process's if, I'm basically saying that "okay, the child can read from the pipe, but I'm allowing the parent to write to the pipe at the same time"?

如果是这样,打开管道以同时读取&写作?没有互斥?

If so, what would happen when the pipe is open for both reading & writing? No mutual exclusion?

推荐答案

无论何时子进程想要从管道中读取,它都必须首先关闭管道的一侧以防止写入.

Whenever the child process wants to read from the pipe, it must first close the pipe's side from writing.

如果进程(父进程或子进程)将不使用管道的写端,则应关闭该文件描述符.对于管道的读取端类似.系统将假定在任何进程打开写端的情况下都可能发生写操作,即使唯一的此类进程是当前正在尝试从管道读取的进程,因此系统也不会报告EOF.此外,如果您过度填充管道,但仍有一个进程的读取端处于打开状态(即使该进程是尝试写入的进程),则写入将挂起,等待读取器腾出空间以完成写入操作.

If the process — parent or child — is not going to use the write end of a pipe, it should close that file descriptor. Similarly for the read end of a pipe. The system will assume that a write could occur while any process has the write end open, even if the only such process is the one that is currently trying to read from the pipe, and the system will not report EOF, therefore. Further, if you overfill a pipe and there is still a process with the read end open (even if that process is the one trying to write), then the write will hang, waiting for the reader to make space for the write to complete.

当我删除该行时close(pipefd [1]);从孩子的进程IF中,我基本上是说:好吧,孩子可以从管道中读取内容,但是我允许父母同时向管道中写入内容?"

When I remove that line close(pipefd[1]); from the child's process IF, I'm basically saying that "okay, the child can read from the pipe, but I'm allowing the parent to write to the pipe at the same time"?

否;您是说孩子和父母都可以写入到管道.具有管道写入文件描述符的任何进程都可以写入管道.

No; you're saying that the child can write to the pipe as well as the parent. Any process with the write file descriptor for the pipe can write to the pipe.

如果是这样,打开管道进行读写时会发生什么情况-不会互斥?

If so, what would happen when the pipe is open for both reading and writing — no mutual exclusion?

从来没有任何互斥.任何打开了管道写描述符的进程都可以随时向管道中写入数据.内核确保实际上并发了两个并发的写操作.任何打开了管道读取描述符的进程都可以随时从管道中读取数据.内核确保两个并发读取操作获得不同的数据字节.

There isn't any mutual exclusion ever. Any process with the pipe write descriptor open can write to the pipe at any time; the kernel ensures that two concurrent write operations are in fact serialized. Any process with the pipe read descriptor open can read from the pipe at any time; the kernel ensures that two concurrent read operations get different data bytes.

通过确保仅打开一个进程以进行写入而仅打开一个进程以进行读取来确保单向使用管道.但是,这是一个编程决定.您可能有N个进程的写端处于打开状态,而M个进程的读端处于打开状态(并且灭了这种想法,在N个和M个进程的集合之间可能存在相同的进程),并且它们都能够出奇地疯狂地工作.但是您无法轻易预测写入数据包后将在哪里读取数据.

You make sure a pipe is used unidirectionally by ensuring that only one process has it open for writing and only one process has it open for reading. However, that is a programming decision. You could have N processes with the write end open and M processes with the read end open (and, perish the thought, there could be processes in common between the set of N and set of M processes), and they'd all be able to work surprisingly sanely. But you'd not readily be able to predict where a packet of data would be read after it was written.

这篇关于如果子进程在阅读时无法关闭写入管道,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆