0
votes

A parent process D creates 3 child processes , A/B/C , with fork.

The parent process shall communicate with child processes , until all child processes finish .

Is there any simple way to make parent work with children and make him terminate when children do , so ?

Parent Process                                  Child Process
--------------                                  --------------
int main(){                                     int main(){
  // create children ... 
  while( all children are alive ){               for(sometime){
      // ipc with children                         // ipc with parent
   }                                              }

   return 0;                                      return 0;
}                                               }
1
Yes, you'd use wait (and possibly a SIGCHLD handler). - melpomene
You can do it using wait and waitpid. - Ali Hussam

1 Answers

0
votes

Is there any simple way to make parent work with children and make him terminate when children do , so ? yes it's very simple, all you need to do is use fork() to create the child process, and each child need to send their status to parent by calling exit() and in parent use waitpid() or wait such that parent process should wait for all child i.e when there is no child exists waitpid() returns -1.

For example below is the sample child parent code.

int a[3]; //to store pid's of children
int main(void) {
        if( (a[0]=fork()) == 0) { /* 1st child */
                int randNo;
                srand(getpid());
                randNo=rand()%10+1;
                printf("1st_child sleep for %d sec\n",randNo);
                sleep(randNo);
                exit(1); /* sending the exit status to parent */
        }
        else {
                if( (a[1]=fork()) == 0) { /* 1nd child */
                        int randNo;
                        srand(getpid());
                        randNo=rand()%10+1;
                        printf("2nd_child sleep for %d sec\n",randNo);
                        sleep(randNo);
                        exit(2);
                }
                else {
                        if( (a[3]=fork()) == 0) { /*1rd child */
                                int randNo;
                                srand(getpid());
                                randNo=rand()%10+1;
                                printf("3rd_child sleep for %d sec\n",randNo);
                                sleep(randNo);
                                exit(3);
                        }
                        else { /* common parent for all 3 child */
                                int status; //exit status of child
                                //signal(SIGCHLD,my_isr);
                                while(wait(&status) != -1) { /* when there is no child, wait() returns -1 */
                                        if( status>>8 == 1 ) { /* if 1st child status received */
                                                a[0] = 0;
                                                printf("child_1 removed from zombie \n");
                                        }
                                        else if( status>>8 == 2) {
                                                a[1] = 0;
                                                printf("child_2 removed from zombie \n");
                                        }
                                        else if( status>>8 == 3) {
                                                a[2] = 0;
                                                printf("child_3 removed from zombie \n");
                                        }
                                }
                        }
                }
        }
        return 0;
}

Also you can set one signal handler as when child completes it sends SIGCHLD to parent, hence upon receiving this signal parent understood that child's completed & it needs to free resources occupied by parents.