如果父级不调用wait(),则同一父级的两个子级无法通过管道进行通信 [英] Two children of same parent are not communicating using pipe if parent do not call wait()

查看:100
本文介绍了如果父级不调用wait(),则同一父级的两个子级无法通过管道进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见下面的代码:

#include<stdio.h>

main(){
        int pid, fds[2], pid1;
        char buf[200];
        pipe(fds);
        pid = fork();

        if(pid==0)
        {
                close(fds[0]);
                scanf("%s", &buf);
                write(fds[1], buf, sizeof(buf)+1);
        }
        else
        {
                pid1 = fork();

                if(pid1==0)
                {
                        close(fds[1]);
                        read(fds[0], buf, sizeof(buf)+1);
                        printf("%s\n", buf);
                }
                else
                {
       Line1:              wait();
                }
        }
}

如果我不注释Line1,则工作正常.请参见以下内容:

If I do not comment out Line1, it is working fine. Please see below:

hduser@pc4:~/codes/c/os$ ./a.out
hello //*Entry from keyboard*
hello //Output
hduser@pc4:~/codes/c/os$ 

但是,如果我注释掉Line1,则两个子进程无法通信:

But if I comment out Line1, two child processes are not communicating:

hduser@pc4:~/codes/c/os$ ./a.out
hduser@pc4:~/codes/c/os$ 
hi //*Entry from keyboard*
hi: command not found
hduser@pc4:~/codes/c/os$

我在这里无法理解wait()的意义.

I cannot understand significance of wait() here.

推荐答案

此处发生的是父进程在子进程完成之前完成了执行.导致孩子无法使用终端机.

What's happening here is that the parent process completes execution before the child processes finish. Causing the children to lose access to the terminal.

让我们仔细看看这一切.

Let us have a closer look at all this.

wait()是做什么的?

wait()系统调用将暂停执行调用过程,直到 它的一个孩子终止.

The wait() system call suspends execution of the calling process until one of its children terminates.

您的程序就是这样

您的main Process派生2个子进程.第一个写入管道,而另一个从管道读取.所有这些都是在main process继续执行的过程中发生的.

Your main Process forks 2 child processes. The first one writes to a pipe while the other one reads from a pipe. All this happens while the main process continues to execute.

当主进程执行了代码后会发生什么?它终止.当终止时,它将放弃对终端的控制.这会导致孩子失去对终端的访问权限.

What happens when the main process has executed it's code ? It terminates. When it terminates, it gives up its control on the terminal. Which causes the children to lose access to the terminal.

这说明了为什么得到command not found的原因-输入的内容不在程序的stdin上,而是在shell提示符本身上.

This explains why you get command not found -- what you have typed is not on the stdin of your program but on the shell prompt itself.

您的代码还有其他一些问题,

There were a couple of other issues with your code too,

1)在代码的这一部分,

1) In this part of your code,

            scanf("%s", &buf);

这是错误的.您很不幸,也没有遇到细分错误.由于buf已经是地址,因此该应该是

This is wrong. You were unlucky and didn't get a segmentation fault. Since buf is already an address, this should have been

            scanf("%s", buf);

2)注意这一点,

            read(fds[0], buf, sizeof(buf)+1);

这是未定义的行为,如注释部分所指出.您正在尝试读取更多数据并将其存储在较小的内存空间中.这 应该是

This is undefined behavior as was pointed out in the comments section. You are trying to read more data and store it in a lesser memory space. This should have been,

            read(fds[0], buf, sizeof(buf));

3)呼叫wait().您已经创建了两个子进程,应该等待它们都完成,因此您应该调用wait() 两次.

3) Calling wait(). You have created two child processes, you should wait for both of them to finish, so you should call wait() twice.

这篇关于如果父级不调用wait(),则同一父级的两个子级无法通过管道进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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