notifying child process to terminate - Unix

This is a discussion on notifying child process to terminate - Unix ; Hi, I have a small query. I have a parent process which forks many child processes (and keeps forking it until it terminates). Now after some time interval (say 1 min) I wanna make graceful shutdown of parent process. Hence ...

+ Reply to Thread
Results 1 to 6 of 6

Thread: notifying child process to terminate

  1. notifying child process to terminate

    Hi,
    I have a small query.
    I have a parent process which forks many child processes (and keeps
    forking it until it terminates).
    Now after some time interval (say 1 min) I wanna make graceful
    shutdown of parent process.
    Hence I wanna inform all the child processes to terminate [I think, I
    should use KILL API for this.]
    and once all the child processes have terminated, I want parent to
    terminate.


    I guess, I need to use KILL(child's process ID, some signal)
    and WAIT() functions...
    But I couldnt figure out how to use it.


    Can anyone please me with this issue...

    Thanks

  2. Re: notifying child process to terminate

    Hi

    On Wed, 10 Sep 2008 23:27:01 -0700, A**** Vora wrote:
    > Hence I wanna inform all the child processes to terminate [I think, I
    > should use KILL API for this.]
    > and once all the child processes have terminated, I want parent to
    > terminate.


    The kill() [Note lower-case] function sends a "signal" to another process.

    Different signals have different meanings. For this purpose you could
    send:

    SIGKILL
    SIGTERM
    SIGQUIT
    SIGINT
    SIGUSR1
    SIGHUP

    SIGTERM usually means "please stop" whereas SIGKILL means "stop now".

    For any signal other than SIGKILL the child process can install a "signal
    hander" function using sigaction(). The handler function is called
    asynchronously when the signal is received and can take some action to
    terminate the program, or may do nothing.

    When SIGKILL is received the kernel terminates the process whether it is
    ready or not.

    Assuming you want to install a handler and terminate gracefully then you
    can use any of the other signals, but your choice might be limited by
    when other signals are sent other than by your parent process.

    Once the signal is sent you can use wait() or waitpid() to suspend the
    parent thread for the children to finish.

    For details of signals see `man 7 signal`.
    For details of how to install a handler see `man sigaction`.
    For details of waiting, see `man 2 wait` and `man waitpid`.

    HTH
    viza

  3. Re: notifying child process to terminate

    A**** Vora writes:
    > I have a small query.
    > I have a parent process which forks many child processes (and keeps
    > forking it until it terminates).
    > Now after some time interval (say 1 min) I wanna make graceful
    > shutdown of parent process.
    > Hence I wanna inform all the child processes to terminate [I think, I
    > should use KILL API for this.]
    > and once all the child processes have terminated, I want parent to
    > terminate.
    >
    > I guess, I need to use KILL(child's process ID, some signal)
    > and WAIT() functions...
    > But I couldnt figure out how to use it.


    An easy way to do so would be to put all children into the same
    process group and send a SIGTERM to the corresponding pgid (basically,
    kill(-pgid, SIGTERM). Afterwards, you can just call wait in a loop
    until it returns -1, errno == ECHILD.

  4. Re: notifying child process to terminate

    On 11 Sep, 09:38, viza
    wrote:

    > For any signal other than SIGKILL the child process can install a "signal
    > hander" function using sigaction().


    SIGSTOP also cannot be caught.

  5. Re: notifying child process to terminate

    Hi

    On Thu, 11 Sep 2008 19:45:50 -0700, William Pursell wrote:
    > On 11 Sep, 09:38, viza wrote:
    >
    >> For any signal other than SIGKILL the child process can install a
    >> "signal hander" function using sigaction().

    >
    > SIGSTOP also cannot be caught.


    Yes, but it wasn't in my list of "signals that you might want to use to
    notify a child process to exit".

  6. Re: notifying child process to terminate

    On Sep 11, 2:11 am, Rainer Weikusat wrote:
    > A**** Vora writes:
    > > I have a small query.
    > > I have a parent process which forks many child processes (and keeps
    > > forking it until it terminates).
    > > Now after some time interval (say 1 min) I wanna make graceful
    > > shutdown of parent process.
    > > Hence I wanna inform all the child processes to terminate [I think, I
    > > should use KILL API for this.]
    > > and once all the child processes have terminated, I want parent to
    > > terminate.

    >
    > > I guess, I need to use KILL(child's process ID, some signal)
    > > and WAIT() functions...
    > > But I couldnt figure out how to use it.

    >
    > An easy way to do so would be to put all children into the same
    > process group and send a SIGTERM to the corresponding pgid (basically,
    > kill(-pgid, SIGTERM). Afterwards, you can just call wait in a loop
    > until it returns -1, errno == ECHILD.


    Couldn't he just trap SIGCHILD and then call wait() in the signal
    handler function?

+ Reply to Thread