Note that there are some explanatory texts on larger screens.

plurals
  1. POforking multiple processes and making the parent wait for all of them (in C)
    primarykey
    data
    text
    <p>I'm creating various processes (3 to be precise) and making them do different things. So far so good. I'm trying to wait in the parent until all children are completed. I've played around with many options (such as the one listed below) but either the parent waits but I have to press enter to return to the shell (meaning that some child completes after the parent?) or the parent never returns to the shell. Any ideas? pointers to where to look for more help? Thanks</p> <pre><code> #include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;errno.h&gt; #define READ_END 0 #define WRITE_END 1 int main (int argc, char **argv) { pid_t pid; int fd[2]; int fd2[2]; pipe(fd); pipe(fd2); for (int i=0; i&lt;3; i++) { pid=fork(); if (pid==0 &amp;&amp; i==0) { //never uses fd2, so close both descriptors close(fd2[READ_END]); close(fd2[WRITE_END]); printf("i'm the child used for ls \n"); close(fd[READ_END]); /*close read end since I don't need it */ dup2(fd[WRITE_END], STDOUT_FILENO); close(fd[WRITE_END]); execlp("ls", "ls", "-hal", NULL); break; /*exit for loop to end child's code */ } else if (pid==0 &amp;&amp; i==1) { printf("i'm in the second child, which will be used to run grep\n"); close(fd[WRITE_END]); dup2(fd[READ_END], STDIN_FILENO); close(fd[READ_END]); close(fd2[READ_END]); dup2(fd2[WRITE_END], STDOUT_FILENO); close(fd2[WRITE_END]); execlp("grep", "grep","p",NULL); break; } else if (pid==0 &amp;&amp; i==2) { //never uses fd so close both descriptors close(fd[READ_END]); close(fd[WRITE_END]); printf("i'm in the original process which will be replaced with wc\n"); close(fd2[WRITE_END]); dup2(fd2[READ_END], STDIN_FILENO); close(fd2[READ_END]); printf("going to exec wc\n"); execlp("wc","wc","-w",NULL); break; } else { //do parenty things } } wait(NULL); while (1){ wait(NULL); if(errno== ECHILD) { printf("all children ended\n"); break; } } close(fd[READ_END]); close(fd[WRITE_END]); close(fd2[READ_END]); close(fd2[WRITE_END]); return 0; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload