Find the answer to your Linux question:
Results 1 to 4 of 4
hi all i have a doubt..... wait system call will allow parent to wait till it's child finishes executing... how should we make a parent process which has many child's ...
  1. #1
    Just Joined!
    Join Date
    Feb 2007
    Location
    pilani,india
    Posts
    4

    wait for all the child processes to finish

    hi all
    i have a doubt..... wait system call will allow parent to wait till it's child finishes executing... how should we make a parent process which has many child's to wait till all of them finish their tasks??? plz explain me with a small example...
    thanks in advance

  2. #2
    Just Joined!
    Join Date
    Sep 2007
    Posts
    11
    one solution is when ever a fork() is executed, the return value from fork() to the parent i.e. PID of child is saved by the parent. Then call waitpid() specifying the child PID to block until child terminates. This way the parent knows which children have terminated and also whether all its children have terminated since it saved PIDs of all of its children. But I think this solution is laborious and time consuming.

  3. #3
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    Every time a child dies a SIGCHLD is delivered to the parent. Just keep a count on how many children you crated and decrement it in the signal handler. Should be working
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Surely you just wait in a loop until ECHILD is returned, meaning there are no more child processes to wait for.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...