Killing Child Process - Unix

This is a discussion on Killing Child Process - Unix ; Hi, My query is... I have a parent process which forks a child process to perform certain task. I wish to terminate the child process after certain amount of time (say 60 secs). Since I dont wish to leave any ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Killing Child Process

  1. Killing Child Process

    Hi,
    My query is...
    I have a parent process which forks a child process to perform
    certain
    task.
    I wish to terminate the child process after certain amount of time
    (say 60 secs).
    Since I dont wish to leave any zombie process, I need to notify the
    Child that it has to terminate now.
    The parent can terminate only after all the child processes have
    terminated.
    How can I achieve this task.
    I heard something about SIGUSR1 signal but not sure how to use it and
    how it can be useful...
    Can anyone guide me please?
    Thanks.

  2. Re: Killing Child Process

    A**** Vora wrote:
    > My query is...
    > I have a parent process which forks a child process to perform
    > certain
    > task.
    > I wish to terminate the child process after certain amount of time
    > (say 60 secs).
    > Since I dont wish to leave any zombie process, I need to notify the
    > Child that it has to terminate now.
    > The parent can terminate only after all the child processes have
    > terminated.
    > How can I achieve this task.
    > I heard something about SIGUSR1 signal but not sure how to use it and
    > how it can be useful...


    In the simplest case you just store the PID of the child
    process (that's the return value of fork(2) in the parent
    process), wait for 60 seconds (e.g. using sleep(3)) and then
    send a SIGKILL signal to the child, using kill(2) with
    the child's PID as the first and SIGKILL as the second
    argument. That will make sure the child gets killed since
    it can't catch the signal.

    Things get slightly more complicated if you don't want to
    brutally kill the child but just ask it nicely to quit
    and then wait for it to exit. In that case you wouldn't
    use SIGKILL but some other, non-deadly signal with SIGUSR1
    being a potentioal candidate. Now you have to install a
    signal handler in the child (using sigaction(2)) which
    reacts to that signal and makes the child process exit on
    receipt of that signal (perhaps after doing a few clean-up
    tasks). Since the child now may not quit immediately you
    would wait for the child to exit in the parent by using
    waitpid(2) after sending the signal (you also can call
    waitpid(2) in case you send a SIGKILL signal to make 100%
    sure the child process is dead before you exit in the parent).

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ jt@toerring.de
    \__________________________ http://toerring.de

  3. Re: Killing Child Process

    On Sep 9, 3:34*am, j...@toerring.de (Jens Thoms Toerring) wrote:
    > A**** Vora wrote:
    > > My query is...
    > > I have a parent process which forks a child process to perform
    > > certain
    > > task.
    > > I wish to terminate the child process after certain amount of time
    > > (say 60 secs).
    > > Since I dont wish to leave any zombie process, I need to notify the
    > > Child that it has to terminate now.
    > > The parent can terminate only after all the child processes have
    > > terminated.
    > > How can I achieve this task.
    > > I heard something about SIGUSR1 signal but not sure how to use it and
    > > how it can be useful...

    >
    > In the simplest case you just store the PID of the child
    > process (that's the return value of fork(2) in the parent
    > process), wait for 60 seconds (e.g. using sleep(3)) and then
    > send a SIGKILL signal to the child, using kill(2) with
    > the child's PID as the first and SIGKILL as the second
    > argument. That will make sure the child gets killed since
    > it can't catch the signal.
    >
    > Things get slightly more complicated if you don't want to
    > brutally kill the child but just ask it nicely to quit
    > and then wait for it to exit. In that case you wouldn't
    > use SIGKILL but some other, non-deadly signal with SIGUSR1
    > being a potentioal candidate. Now you have to install a
    > signal handler in the child (using sigaction(2)) which
    > reacts to that signal and makes the child process exit on
    > receipt of that signal (perhaps after doing a few clean-up
    > tasks). Since the child now may not quit immediately you
    > would wait for the child to exit in the parent by using
    > waitpid(2) after sending the signal (you also can call
    > waitpid(2) in case you send a SIGKILL signal to make 100%
    > sure the child process is dead before you exit in the parent).
    >
    > * * * * * * * * * * * * *Regards, Jens
    > --
    > * \ * Jens Thoms Toerring *___ * * *j...@toerring.de
    > * *\__________________________ * * *http://toerring.de


    Thanks,
    I understood the idea...
    do u have any code snippet of this example?
    Thanks alot

+ Reply to Thread