C叉管关闭结束

时间:2013-03-19 23:00:48

标签: process fork pipe

我正在构建一个需要与几个子进程进行双向通信的应用程序。我的父母就像一个查询引擎,不断从stdin读取单词并将其传递给每个子进程。子进程执行其处理并在其专用管道上写回父进程。

理论上这应该是如何工作的,但是我坚持实施细节。第一个问题是我在分娩之前创建了2个管道吗?当我分叉时,我知道孩子将继承父的文件描述符集,这是否意味着将创建4个管道或简单的2个管道只是重复。如果他们在孩子中被复制,那么这是否意味着如果我要关闭孩子中的文件描述符,它还会关闭父母的?

我的理论如下,我只需要澄清并走上正轨。这是未经测试的代码,我只是写它来让你知道我在想什么。谢谢,任何帮助表示赞赏。

int main(void){
    int fd[2][2]; //2 pipes for in/out

    //make the pipes
    pipe(fd[0]); //WRITING pipe
    pipe(fd[1]); //READING pipe

    if(fork() == 0){
        //child
        //close some ends
        close(fd[0][1]); //close the WRITING pipe write end
        close(fd[1][0]); //close the READING pipe read end

        //start the worker which will read from the WRITING pipe
        //and write back to the READING pipe
        start_worker(fd[0][0], fd[1][1]);
    }else{
        //parent
        //close the reading end of the WRITING pipe
        close(fd[0][0]);
        //close the writing end of the READING pipe
        close(fd[1][1]);
        //write data to the child down the WRITING pipe
        write(fd[0][1], "hello\n", 6);

        //read from the READING pipe 
        int nbytes;
        char word[MAX];
        while ((nbytes = read(fd[1][0], word, MAXWORD)) > 0){
            printf("Data from child is: %s\n", word);
        }
    }

}

1 个答案:

答案 0 :(得分:1)

管道本身在fork上重复。

单个管道是单向的,有2个描述符 - 一个用于读取,一个用于写入。所以在流程A中你关闭了写入描述符,在进程B中关闭读取描述符 - >你有一个从B到A的管道。

在一个进程中关闭描述符不会影响另一个进程中的描述符。分叉后,每个进程都有自己的描述符空间,这是父进程描述符的拷贝。以下是fork()手册页的摘录:

  

子进程应拥有自己父文件的副本   描述。每个孩子的文件描述符都应该引用   相同的打开文件描述与相应的文件描述符   父母。