| Author |
How to kill child & parent process simulanteously
|
|
| Niraj123 2005-04-26, 2:49 am |
| I created a child process by using fork() system call.
Now i am interested to stop the child process when the parent dies. I
don't want the child process to be adopted by init process.
e.g when i use kill -9 pid
in that case i want to kill the child of the (pid) process.
TIA
Niraj Kumar
| |
| junky_fellow@yahoo.co.in 2005-04-26, 2:49 am |
|
Niraj123 wrote:
> I created a child process by using fork() system call.
> Now i am interested to stop the child process when the parent dies.
I
> don't want the child process to be adopted by init process.
>
> e.g when i use kill -9 pid
> in that case i want to kill the child of the (pid) process.
>
> TIA
> Niraj Kumar
The successful completion of the fork() returns a value of 0 to the
child process and the process ID of the child to the parent.
In the parent process you may store the PIDs of all the child processes
and before exiting kill all of them.
You may also store the pids of all the child processes in some file.
| |
| niraj.kumar.ait@gmail.com 2005-04-26, 2:49 am |
| I know that I can store PID but when I use kill -9 pid at that time I
cant catch those signal and so I can't perform any operation.
sigkill,sigstop,sigterm cannt be catched .
Is there any way to kill child process with parent process
| |
| Pascal Bourguignon 2005-04-26, 2:49 am |
| niraj.kumar.ait@gmail.com (Niraj123) writes:
> I created a child process by using fork() system call.
> Now i am interested to stop the child process when the parent dies. I
> don't want the child process to be adopted by init process.
>
> e.g when i use kill -9 pid
> in that case i want to kill the child of the (pid) process.
You should start a process group at the parent process so the child be
in the same process group (instead of being in that of the grand
parent).
man setpgid
I guess calling: setpgid(0,0); in the parent would do it.
Normally, don't use kill -9. Use the normal kill to leave time to the
process to clean up cleanly. Only if it doesn't terminate in a
reasonable time you may kill -9.
( kill -TERM $pid ; sleep 15 ; kill -KILL $pid )
Note that if the process is doing I/O (eg. is in the kernel, 'D' state
in ps(1)) then it won't disappear anyway until this I/O terminates
(which may take a long time if it involves NFS for example).
--
__Pascal Bourguignon__ http://www.informatimago.com/
In a World without Walls and Fences,
who needs Windows and Gates?
| |
| junky_fellow@yahoo.co.in 2005-04-26, 2:49 am |
|
niraj.kumar....@gmail.com wrote:
> I know that I can store PID but when I use kill -9 pid at that time I
> cant catch those signal and so I can't perform any operation.
> sigkill,sigstop,sigterm cannt be catched .
> Is there any way to kill child process with parent process
Why are you posting SIGKILL to terminate your process. You may
post some other signal that may be caught. Install a handler
for that signal and in the handling kill all the child processes
before exiting.
| |
| Barry Margolin 2005-04-26, 2:49 am |
| In article <1114495029.810465.181730@o13g2000cwo.googlegroups.com>,
junky_fellow@yahoo.co.in wrote:
> Niraj123 wrote:
> I
You could have the parent become a process group leader, and then kill
the process group instead of just the process.
[vbcol=seagreen]
>
> The successful completion of the fork() returns a value of 0 to the
> child process and the process ID of the child to the parent.
>
> In the parent process you may store the PIDs of all the child processes
> and before exiting kill all of them.
Except that you can't have a handler for kill -9, so how would you make
the parent kill its children in that event?
> You may also store the pids of all the child processes in some file.
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
| |
| Pascal Bourguignon 2005-04-26, 2:49 am |
| niraj.kumar.ait@gmail.com writes:
> I know that I can store PID but when I use kill -9 pid at that time I
> cant catch those signal and so I can't perform any operation.
> sigkill,sigstop,sigterm cannt be catched .
Yes SIGTERM can be catched. See signal(7).
> Is there any way to kill child process with parent process
See: setpgrp(2).
--
__Pascal Bourguignon__ http://www.informatimago.com/
There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
| |
| phil_gg04@treefic.com 2005-04-26, 5:59 pm |
| > I created a child process by using fork() system call.
> Now i am interested to stop the child process when the parent dies
In many common cases, if your parent and child are communicating via a
pipe, the child will naturally get SIGPIPE at some point after the
parent has died. This makes things simpler for you if it applies in
your case.
Otherwise, as others have said, trapping termination signals in the
parent and then passing a negative pid to kill() to kill all members of
the process group is a good solution. If you send a kill -9 you are
asking the process to die without tidying up, so you should not try to
kill the children in that case.
--Phil.
|
|
|
|