how to fork/exec and not wait for child - Unix

This is a discussion on how to fork/exec and not wait for child - Unix ; Hi, I am a newbie coder. I want to fork a child and then I dont want the parent to wait on the child to return but rather want the parent to continue, because the child in my case takes ...

+ Reply to Thread
Results 1 to 12 of 12

Thread: how to fork/exec and not wait for child

  1. how to fork/exec and not wait for child

    Hi,

    I am a newbie coder. I want to fork a child and then I dont want the
    parent to wait on the child to return but rather want the parent to
    continue, because the child in my case takes 10-15 seconds to return
    and thats a waste of time.

    I then want that when the child return, the parent waitpids on it and
    reaps it. Can you please let me know how to do these things.

    I am not sure when to register the SIGCLD even handler ?

    Any help will be greatly apprecaited.

    PS: this is not my homework...I am doing a project by myself to learn
    where a parent forks but goes on to do its own work.

    thanks,

  2. Re: how to fork/exec and not wait for child

    In comp.unix.programmer, DanielJohnson wrote:

    > Hi,
    >
    > I am a newbie coder. I want to fork a child and then I dont want the
    > parent to wait on the child to return but rather want the parent to
    > continue, because the child in my case takes 10-15 seconds to return
    > and thats a waste of time.
    > I then want that when the child return, the parent waitpids on it and
    > reaps it. Can you please let me know how to do these things.


    It seems like you already know how to handle it. At least, you've described
    it properly above.

    You want your parent to do something like....

    {
    pid_t child_pid;

    switch (child_pid = fork())
    {
    case 0: /* in child */
    do_child_logic();
    break;
    case -1: /* error in fork */
    do_parent_error_logic();
    break;
    default: /* in parent */
    do_parent_processing();
    waitpid(child_pid,NULL,0);
    }
    }

    > I am not sure when to register the SIGCLD even handler ?


    Do you want the parent process to receive a signal when the child process
    terminates? If so, register the event handler /before/ you fork off the
    child.

    > Any help will be greatly apprecaited.
    >
    > PS: this is not my homework...I am doing a project by myself to learn
    > where a parent forks but goes on to do its own work.
    >
    > thanks,


    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------



  3. Re: how to fork/exec and not wait for child

    >
    > It seems like you already know how to handle it. At least, you've described
    > it properly above.
    >


    I theoretically know it but my code is breaking the build.

    > You want your parent to do something like....
    >
    > {
    > pid_t child_pid;
    >
    > switch (child_pid = fork())
    > {
    > case 0: /* in child */
    > do_child_logic();
    > break;
    > case -1: /* error in fork */
    > do_parent_error_logic();
    > break;
    > default: /* in parent */
    > do_parent_processing();
    > waitpid(child_pid,NULL,0);
    > }
    > }
    >


    I did exactly the same events but my code breaks the build. In my case
    parent doesn't do anything just returns. Am I missing something ?


    > > I am not sure when to register the SIGCLD even handler ?

    >
    > Do you want the parent process to receive a signal when the child process
    > terminates? If so, register the event handler /before/ you fork off the
    > child.


    I register the event handler by CatchChild() which basically intimates
    when SIGCLD happens.


    Any more input as to what might be going wrong in my code.

    thanks

  4. Re: how to fork/exec and not wait for child

    >I am a newbie coder. I want to fork a child and then I dont want the
    >parent to wait on the child to return but rather want the parent to
    >continue, because the child in my case takes 10-15 seconds to return
    >and thats a waste of time.


    And what is the parent to be doing during that time?
    Make more children? Waste time elsewhere?

    >I then want that when the child return, the parent waitpids on it and
    >reaps it. Can you please let me know how to do these things.


    You need to make up your mind whether you want to wait or not wait
    on the child. waitpid() or wait*() with WNOHANG allows you to poll
    on child termination without blocking if the child has not exited yet.

    >I am not sure when to register the SIGCLD even handler ?


    You haven't indicated anything that requires a SIGCLD handler.


  5. Re: how to fork/exec and not wait for child

    On Jun 6, 2:30 pm, gordonb.cd...@burditt.org (Gordon Burditt) wrote:
    > >I am a newbie coder. I want to fork a child and then I dont want the
    > >parent to wait on the child to return but rather want the parent to
    > >continue, because the child in my case takes 10-15 seconds to return
    > >and thats a waste of time.

    >
    > And what is the parent to be doing during that time?
    > Make more children? Waste time elsewhere?


    I mean, parent is a daemon by itself which serves to other
    connections, so when its forks it should just go back and continue
    serving other connections.

    >
    > >I then want that when the child return, the parent waitpids on it and
    > >reaps it. Can you please let me know how to do these things.

    >
    > You need to make up your mind whether you want to wait or not wait
    > on the child. waitpid() or wait*() with WNOHANG allows you to poll
    > on child termination without blocking if the child has not exited yet.
    >


    So when the parent process is serving other connections, it should
    know that the child it had forked sometime back has terminated and it
    should reap it so that we don't get too many zombies. I guess thats
    where I would need to waitpid.

    > >I am not sure when to register the SIGCLD even handler ?

    >
    > You haven't indicated anything that requires a SIGCLD handler.


    Since the parent process wants to reap the child upon termination, I
    was guessing that we need to register a SIGCLD event handler.


    Correct me if I am wrong. I am newbie in unix programming and still
    learning the nuances of it.

    thanks

  6. Re: how to fork/exec and not wait for child

    DanielJohnson wrote:

    > So when the parent process is serving other connections, it should
    > know that the child it had forked sometime back has terminated and it
    > should reap it so that we don't get too many zombies. I guess thats
    > where I would need to waitpid.


    If all you're trying to accomplish is the prevention of zombie processes,
    then set the SIGCHLD handler to SIG_IGN. That's the proper way to do what
    you just described, which has special, well-defined semantics.

    If you want to do things the hard way--for pedagogic purposes--install your
    own handler for SIGCHLD before the fork, and from the handler call waitpid
    in a loop like so:

    do {
    retval = waitpid(-1, 0, WNOHANG);
    } while (retval != -1 || errno == EINTR);

    waitpid(2) is specified to be async-signal safe for just this sort of code.
    You must use the loop because--and correct me if I'm wrong--the kernel
    doesn't queue SIGCHLD signals like it might for real-time signals; the
    handler might execute once to clear multiple SIGCHLD signals. You must check
    for EINTR unless you've blocked all other signals when you installed the
    SIGCHLD handler with sigaction(2).

    Dealing with signals are tricky. You should read more about what it means to
    ignore or block a signal, and how signal masks operate--or don't
    operate--when inside a signal handler.



  7. Re: how to fork/exec and not wait for child

    On Jun 6, 4:52 pm, William Ahern
    wrote:
    > DanielJohnson wrote:
    >
    >
    >
    > > So when the parent process is serving other connections, it should
    > > know that the child it had forked sometime back has terminated and it
    > > should reap it so that we don't get too many zombies. I guess thats
    > > where I would need to waitpid.

    >
    > If all you're trying to accomplish is the prevention of zombie processes,
    > then set the SIGCHLD handler to SIG_IGN. That's the proper way to do what
    > you just described, which has special, well-defined semantics.
    >
    > If you want to do things the hard way--for pedagogic purposes--install your
    > own handler for SIGCHLD before the fork, and from the handler call waitpid
    > in a loop like so:
    >
    > do {
    > retval = waitpid(-1, 0, WNOHANG);
    > } while (retval != -1 || errno == EINTR);
    >
    > waitpid(2) is specified to be async-signal safe for just this sort of code.
    > You must use the loop because--and correct me if I'm wrong--the kernel
    > doesn't queue SIGCHLD signals like it might for real-time signals; the
    > handler might execute once to clear multiple SIGCHLD signals. You must check
    > for EINTR unless you've blocked all other signals when you installed the
    > SIGCHLD handler with sigaction(2).
    >
    > Dealing with signals are tricky. You should read more about what it means to
    > ignore or block a signal, and how signal masks operate--or don't
    > operate--when inside a signal handler.


    Thanks. I will read up more and if I get stuck I will post my
    question. I appreciate your time.

  8. Re: how to fork/exec and not wait for child

    DanielJohnson wrote:
    > On Jun 6, 4:52 pm, William Ahern
    > wrote:


    > >
    > > do {
    > > retval = waitpid(-1, 0, WNOHANG);
    > > } while (retval != -1 || errno == EINTR);
    > >


    > > Dealing with signals are tricky. You should read more about what it means to
    > > ignore or block a signal, and how signal masks operate--or don't
    > > operate--when inside a signal handler.


    > Thanks. I will read up more and if I get stuck I will post my
    > question. I appreciate your time.


    Come to think of it, the errno check is racy unless you either (a) block all
    other signals or (b) save/restore errno in all handlers that might cause it
    to be altered. Another signal could execute in the interval between
    waitpid() returning and the comparison, and make inopportune system or
    library calls.

    So, indeed, signals are tricky. In the pathological case, the loop would
    terminate early, and zombies might begin to pile up unless/until the handler
    can loop through them all.

    Conscientious programmers often spend more time trying to get signal
    processing code to execute synchronously than vetting asynchronous handlers.
    In threaded programs that often means having a dedicated signal-handling
    thread use sigwait(2), or various libraries like libevent or platform
    specific APIs--kqueue(2), signalfd(2)--provide the programmer a way to
    synchronously collect signals.

    In your case, none of this beats SIG_IGN, of course.

  9. Re: how to fork/exec and not wait for child

    >> >I am a newbie coder. I want to fork a child and then I dont want the
    >> >parent to wait on the child to return but rather want the parent to
    >> >continue, because the child in my case takes 10-15 seconds to return
    >> >and thats a waste of time.

    >>
    >> And what is the parent to be doing during that time?
    >> Make more children? Waste time elsewhere?

    >
    >I mean, parent is a daemon by itself which serves to other
    >connections, so when its forks it should just go back and continue
    >serving other connections.
    >
    >>
    >> >I then want that when the child return, the parent waitpids on it and
    >> >reaps it. Can you please let me know how to do these things.

    >>
    >> You need to make up your mind whether you want to wait or not wait
    >> on the child. waitpid() or wait*() with WNOHANG allows you to poll
    >> on child termination without blocking if the child has not exited yet.
    >>

    >
    >So when the parent process is serving other connections, it should
    >know that the child it had forked sometime back has terminated and it
    >should reap it so that we don't get too many zombies. I guess thats
    >where I would need to waitpid.
    >
    >> >I am not sure when to register the SIGCLD even handler ?

    >>
    >> You haven't indicated anything that requires a SIGCLD handler.

    >
    >Since the parent process wants to reap the child upon termination, I
    >was guessing that we need to register a SIGCLD event handler.
    >
    >
    >Correct me if I am wrong. I am newbie in unix programming and still
    >learning the nuances of it.


    It is possible to set up a loop this way:

    loop {
    accept a connection;
    fork() a child for this connection.
    In the child, process what it should do, then exit.
    In the parent, close the fd for the connection.

    Call waitpid() with WNOHANG until there are no more
    children to reap. Optionally you may look at the pid
    of the child that terminated and do something about it.
    }

    Note: no signal handlers.

    A disadvantage of this is that the child may be (probably will be) reaped
    after the *next* client connects.

  10. Re: how to fork/exec and not wait for child

    William Ahern writes:
    > DanielJohnson wrote:
    >> On Jun 6, 4:52 pm, William Ahern
    >> wrote:

    >
    >> >
    >> > do {
    >> > retval = waitpid(-1, 0, WNOHANG);
    >> > } while (retval != -1 || errno == EINTR);
    >> >

    >
    >> > Dealing with signals are tricky. You should read more about what it means to
    >> > ignore or block a signal, and how signal masks operate--or don't
    >> > operate--when inside a signal handler.

    >
    >> Thanks. I will read up more and if I get stuck I will post my
    >> question. I appreciate your time.

    >
    > Come to think of it, the errno check is racy unless you either (a) block all
    > other signals or (b) save/restore errno in all handlers that might cause it
    > to be altered.


    The latter should IMO be done in any case, because there is no way to
    (generally) tell what the interrupted thread expects to be stored in
    errno. And your loop is buggy: Assuming there are only live children
    by the time waitpid(..., WNOHANG) is called, it is supposed to return
    0.

  11. Re: how to fork/exec and not wait for child

    Rainer Weikusat wrote:

    > errno. And your loop is buggy: Assuming there are only live children
    > by the time waitpid(..., WNOHANG) is called, it is supposed to return
    > 0.


    That's what I get for writing code from memory....


  12. Re: how to fork/exec and not wait for child

    In comp.unix.programmer, K-mart Cashier wrote:

    > On Jun 5, 5:38 pm, Lew Pitcher wrote:
    >> In comp.unix.programmer, DanielJohnson wrote:

    [snip]
    >> > I am not sure when to register the SIGCLD even handler ?

    >>
    >> Do you want the parent process to receive a signal when the child process
    >> terminates? If so, register the event handler /before/ you fork off the
    >> child.
    >>

    >
    > When wouldn't a process want to receive a signal when the child
    > process terminates?


    When the parent process intends to terminate before the child process (as in
    processes that daemonize themselves), or when the parent process intends to
    perform an explicit wait() call for the child process termination.

    --
    Lew Pitcher

    Master Codewright & JOAT-in-training | Registered Linux User #112576
    http://pitcher.digitalfreehold.ca/ | GPG public key available by request
    ---------- Slackware - Because I know what I'm doing. ------



+ Reply to Thread