如果一个子进程将不会从写封闭管道,而读,会发生什么? [英] What happens if a son process won't close the pipe from writing , while reading?

查看:147
本文介绍了如果一个子进程将不会从写封闭管道,而读,会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于以下code:

  INT主(INT ARGC,CHAR *的argv [])
{
    INT pipefd [2];
    将为pid_t CPID;
    焦炭BUF;    如果(的argc!= 2){
        fprintf中(标准错误,用法:%S \\ N的argv [0]);
        出口(EXIT_FAILURE);
    }    如果(管(pipefd)== -1){
        PERROR(管子);
        出口(EXIT_FAILURE);
    }    CPID = fork()的;
    如果(CPID == -1){
        PERROR(叉);
        出口(EXIT_FAILURE);
    }    如果(CPID == 0){/ *儿童从管道中读取* /
        关闭(pipefd [1]); / *关闭未使用的写入结束* /        而(读(pipefd [0],&放大器; BUF,1)大于0)
            写(STDOUT_FILENO,&安培; BUF,1);        写(STDOUT_FILENO,\\ n,1);
        关闭(pipefd [0]);
        _exit(EXIT_SUCCESS);    }其他{/ *父写的argv [1]管道* /
        关闭(pipefd [0]); / *关闭未使用的读端* /
        写(pipefd [1],的argv [1],strlen的(的argv [1]));
        关闭(pipefd [1]); / *读者会看到EOF * /
        等待(NULL); / *等待孩子* /
        出口(EXIT_SUCCESS);
    }
返回0;}

每当子进程需要从管道中读取,他必须首先从写作关闭该管道的一面。当我删除了这一行关闭(pipefd [1]); 从孩子的过程中如果
我基本上说,还好,儿子可以从管道读取,但我让父亲在同一时间写管道?

如果是这样,当管道被打开供读取和放大器会发生什么;写作 ?没有互斥?

感谢


解决方案

  

每当子进程需要从管道中读取,他必须首先从写作关闭该管道的身边。


如果这个过程 - 父母或孩子 - 不打算使用管道的写端,它应该关闭文件描述符。同样,对于一个管道的读结束。该系统将假设而任何方法具有开放的写入结束,即使只有这样的处理是目前正在试图从管道来读取所述一个可能发生的写入,并且该系统不会报告EOF,因此。此外,如果您溢出管道,仍然有一个过程与读端开放(即使这个过程是一个尝试写),那么写操作将挂起,等待为读者做出写入完成空间。


  

当我删除该行关闭(pipefd [1]);从孩子的过程中,如果,我基本上说,还好,儿子可以从管道读取,但我让父亲写信给管道在同一时间?


没有;你说孩子能的应用于管道以及家长。与管道写文件描述符任何进程都可以写入到管道。


  

如果是这样,当管道是开放的读,写,会发生什么? - 没有互斥


有没有任何互斥永远。与管道写描述符开可以写在任何时候任何管道过程;内核保证了两个并发写入操作实际上是序列化。与管道读描述符打开可以随时从管道读取的任何处理;内核保证了两个并行的读操作获得不同的数据字节。

您确保管道是通过确保只有一个进程打开它进行写入,只有一个进程已经打开阅读单向使用。然而,这是一个编程的决定。你可以有写端打开N个流程和M过程与读端打开(在常见的,灭亡的思想,有可能是过程中的组N之间,并设置M个处理),并且他们会所有能令人惊讶的以工作三立。但是你不能够很容易地predict它被写入后会在哪里读取数据包。

Given the following code :

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;

}

Whenever the son process wants to read from the pipe , he must first close the pipe's side from writing . When I remove that line close(pipefd[1]); from the child's process IF , I'm basically saying that , okay , the son can read from the pipe , but I'm allowing the father 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 ?

Thanks

解决方案

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

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.

When I remove that line close(pipefd[1]); from the child's process IF, I'm basically saying that, okay, the son can read from the pipe, but I'm allowing the father 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.

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天全站免登陆