[Q]when parent process keeps forking child but doesnot wait... - Unix

This is a discussion on [Q]when parent process keeps forking child but doesnot wait... - Unix ; Hi Unix guru, In my case, parent receives a request and forks itself and lets the child perform a task. But parent doesnot wait for child to complete and exit. This kind of behavior is normal ,right? But, when I ...

+ Reply to Thread
Results 1 to 5 of 5

Thread: [Q]when parent process keeps forking child but doesnot wait...

  1. [Q]when parent process keeps forking child but doesnot wait...

    Hi Unix guru,

    In my case, parent receives a request and forks itself and lets the
    child perform a task.
    But parent doesnot wait for child to complete and exit.
    This kind of behavior is normal ,right?

    But, when I ps , looks like it displays all those completed and
    exited child processes-->which is bothering me. Do I need to worry
    about it?


  2. Re: when parent process keeps forking child but doesnot wait...

    On 24 Apr., 06:16, djh...@gmail.com wrote:
    > In my case, parent receives a request and forks itself and lets the
    > child perform a task.
    > But parent doesnot wait for child to complete and exit.
    > This kind of behavior is normal ,right?
    >
    > But, when I ps , looks like it displays all those completed and
    > exited child processes-->which is bothering me. Do I need to worry
    > about it?


    Yes. The Zombie Processes (state Z) occupy space in the process table.
    The easiest way to solve this is to use double forking:
    pid1=fork();
    if (pid1==0) { /* son */
    pid2=fork();
    if (pid2==0) /* son of son */
    { do_work();
    _exit(0);
    }
    /* son */
    _exit(0);
    }
    /* parent, error checking pid<0 */
    wait(&status);

    Here, parent can wait since its son exists immediately after creating
    his own son. Init process takes care of sons son after son has exited.

    Hubble.



  3. Re: when parent process keeps forking child but doesnot wait...

    On Apr 24, 2:51 pm, Hubble wrote:
    > On 24 Apr., 06:16, djh...@gmail.com wrote:
    >
    > > In my case, parent receives a request and forks itself and lets the
    > > child perform a task.
    > > But parent doesnot wait for child to complete and exit.
    > > This kind of behavior is normal ,right?

    >
    > > But, when I ps , looks like it displays all those completed and
    > > exited child processes-->which is bothering me. Do I need to worry
    > > about it?

    >
    > Yes. The Zombie Processes (state Z) occupy space in the process table.
    > The easiest way to solve this is to use double forking:
    > pid1=fork();
    > if (pid1==0) { /* son */
    > pid2=fork();
    > if (pid2==0) /* son of son */
    > { do_work();
    > _exit(0);
    > }
    > /* son */
    > _exit(0);
    > }
    > /* parent, error checking pid<0 */
    > wait(&status);
    >
    > Here, parent can wait since its son exists immediately after creating
    > his own son. Init process takes care of sons son after son has exited.
    >
    > Hubble.



    or create a signal handler to catch SIG_CHLD and call wait within the
    handler to prevent childrens to become zombies -- zombies can cause ur
    system to run out of prossesses,

    sig_chld(int signo)
    {pid_t pid;
    int stat;
    pid = wait(&stat);
    }

    cheers,

    zaher el siddik
    http://elsiddik.blogspot.com/


  4. Re: when parent process keeps forking child but doesnot wait...

    On 4月24日, 下午12时16分, djh...@gmail.com wrote:
    > Hi Unix guru,
    >
    > In my case, parent receives a request and forks itself and lets the
    > child perform a task.
    > But parent doesnot wait for child to complete and exit.
    > This kind of behavior is normal ,right?
    >
    > But, when I ps , looks like it displays all those completed and
    > exited child processes-->which is bothering me. Do I need to worry
    > about it?


    Yes, zombie does not consume any resource except some bookkeeping info.


  5. Re: when parent process keeps forking child but doesnot wait...

    Bin Chen wrote:
    > On 4?24?, ??12?16?, djh...@gmail.com wrote:
    > > In my case, parent receives a request and forks itself and lets the
    > > child perform a task.
    > > But parent doesnot wait for child to complete and exit.
    > > This kind of behavior is normal ,right?
    > >
    > > But, when I ps , looks like it displays all those completed and
    > > exited child processes-->which is bothering me. Do I need to worry
    > > about it?


    > Yes, zombie does not consume any resource except some bookkeeping info.


    They consume at least one slot in the process table and since
    normally there's only a limited number of proceses you can have
    at any time (alive or zombies) it's prudent to have them go away,
    espcially if the parent is a long-lived program that fork()s a
    lot. And it's easy with waitpid() if you call from time to time

    while ( waitpid( -1, NULL, NOHANG ) > 0 )
    /* empty */ ;

    this removes all zombies that exist at that moment and should
    consume much time.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

+ Reply to Thread