C父级和子级之间的双向管道

C父级和子级之间的双向管道,c,unix,fork,pipe,C,Unix,Fork,Pipe,我有一个结构,其中包括两组文件描述符 int (*pfdsParent)[2], (*pfdsChild)[2] 当我malloc结构中的其他数组时,它们被分配内存 s -> pfdsParent = malloc(3 * x * sizeof(int)) s -> pfdsChild = malloc(3 * x * sizeof(int)) 结构包含文件中每一行的信息,该信息用于为文件中的每一行创建一个进程。子进程执行一个程序,管道需要是双向的,或者更确切地说,每个管道需要

我有一个结构,其中包括两组文件描述符

int (*pfdsParent)[2], (*pfdsChild)[2]
当我malloc结构中的其他数组时,它们被分配内存

s -> pfdsParent = malloc(3 * x * sizeof(int))
s -> pfdsChild = malloc(3 * x * sizeof(int)) 
结构包含文件中每一行的信息,该信息用于为文件中的每一行创建一个进程。子进程执行一个程序,管道需要是双向的,或者更确切地说,每个管道需要有两个管道。我的第一个问题是关于“分配”这些文件描述符的值。在我看到的例子中,你只是做了这样的事情

 int pfds[2]; 
 pipe(pfds); 
所以我认为您不需要初始化这些描述符。但是如果我在一个循环中试图访问

(s -> pfdsChild[i][0]) 
它知道我在谈论我正在创建的第I个进程的子进程的stdin吗?我在这些行上没有收到任何错误/警告,但如果我从未对其编制索引,它怎么会这样工作呢。无论如何,第二个问题是关于父母和孩子之间的沟通。我对管道的有限理解让我想到了这个功能

int pipeFork(struct *s, int* Length){

    int i, status;

    for(i = 0; i < *Length; i++){

        char *args[] = {(s-> Name[i]), (s-> Args[i]), NULL};

        pipe(s-> pfdsParent[i]);
        pipe(s-> pfdsChild[i]);

        if(!fork()){ // child 
            // parent stdout goes to child stdin (read from child) 
            dup2((s-> pfdsChild[i][0]), (s-> pfdsParent[i][1])); 
            close((s-> pfdsParent[i][0])); 
            close((s-> pfdsChild[i][1])); 

            // child stdout goes to parents stdin (read from parent)
            dup2((s-> pfdsParent[i][0]), (s-> pfdsChild[i][1])); 
            close((s-> pfdsParent[i][1]));
            close((s-> pfdsChild[i][0])); 

            execvp((s-> Name[i]), args);

         } else{ // parent 

            dup2((s-> pfdsChild[i][0]), (s-> pfdsParent[i][1])); 
            close((s-> pfdsParent[i][0])); 
            close((s-> pfdsChild[i][1]));

            dup2((s-> pfdsParent[i][0]), (s-> pfdsChild[i][1])); 
            close((s-> pfdsParent[i][1]));
            close((s-> pfdsChild[i][0])); 


            write(s-> pfdsParent[i][1], "test", sizeof("test")); 
            wait(&status); \\ see if child gets it 
    }


return 0; 

}

但它不会在标准输入时从父级接收字符串。我所做的有什么严重的错误吗?我的最终目标是能够从孩子身上打印出来,让家长阅读,并从家长身上打印东西给孩子。感谢您的帮助

将父级和子级声明为指向具有int*pfdsParent[2]的2 int数组的指针。如果您希望它是一个多维数组指针,那么可能意味着int**pfdsparents和所有这些文件描述符很容易丢失。一个完整的可运行程序就好了。有一件事肯定是遗漏了,那就是尝试将任何管道连接到子进程的实际stdin和stdout。当你执行一个新的进程时,它总是把fd0看作stdin,把fd1看作stdout,所以你应该把一些东西复制到fd的0和1上。@brainseel,我认为我声明它们的方式是正确的。指向3整数数组的指针。我已经将数组中的字符串声明为长度为100的*stringname[100]I字符串,并且它的工作方式也与我预期的一样,除非它对数组的行为不同?@WumpusQ.Wumbley,所以仅查看子进程,我使用的两个dup2调用实际上并没有将管道连接到stdin和stdout?前两次dup2调用上面的评论描述了我对它的理解,它们是否不正确?或者你是说我需要为父母和孩子分别打4个dup2电话?我不明白你怎么认为你的dup2没有提到fd 0会对孩子的stdin产生任何影响。您移动了一堆管道FD,但从未将它们中的任何一个放置在stdin 0或stdout 1上。我不明白为什么malloc是3的倍数,而管道fd是成对的,而不是三元组。。。您能否尝试一个只有一个子进程的版本,并首先使其工作?
read(stdin, buffer, sizeof(buffer)); 
printf("I have %s\n", buffer); 
printf("Child done!\n);