how to kill a child process that runs other children processes

how to kill a child process that runs other children processes

Post by Burc Arpa » Tue, 31 Dec 2002 11:18:17



here's my problem:  i am spawning a child process using spawnv in c,
launching an executable and passing some arguments to it.  then, in another
thread i wait for a certain amount of time and if the first thread reaches a
certain threshold, i kill the spawned child process.  this works total okay
but because the executable that i run in the spawned process launches some
other programs on its own, they continue to run regardless of the fact that
i killed their parent process.  to make things clear:  i spawn process A.
process A is an executable and i don't have its source.  process A launches
a bunch of other processes B, C and D ( they are also separate executables
w/o source code ).  when i kill process A, it dies but processes B, C and D
continue to run.

so, the question is:  is there a way to kill the children of a child process
( i.e. grandchildren ) without actually knowing their pids explicitly, i.e.
can i say to a process "die and kill all your children, too" *without*
modifying the code for that child process?

thanx in advance...

 
 
 

how to kill a child process that runs other children processes

Post by Burc Arpa » Tue, 31 Dec 2002 11:31:58


i almost forgot: FAQ 1.16 does not work because the children processes
somehow end up not being affected by the process group, i.e. when i put the
spawned process in a separate group, killing that group kills the child
process but not its children ( i.e. my grandchildren )

 
 
 

how to kill a child process that runs other children processes

Post by Andy Isaacs » Tue, 31 Dec 2002 15:23:24




>here's my problem:  i am spawning a child process using spawnv in c,

Uh, what's "spawnv"?

Assuming you're actually talking about fork/exec, you should look into
process groups.  Have your parent process create a new process group,
and all the children will automatically be a part of it; then you can
signal the group as a whole.  The children can escape if they try,
though, so this isn't a way to keep malicious programs under control.

Read setpgid(2) and check Stevens' "Advanced Programming in the UNIX
Environment".

-andy

 
 
 

how to kill a child process that runs other children processes

Post by those who know me have no need of my nam » Tue, 31 Dec 2002 18:44:25


in comp.unix.programmer i read:

Quote:>i almost forgot: FAQ 1.16 does not work because the children processes
>somehow end up not being affected by the process group, i.e. when i put the
>spawned process in a separate group, killing that group kills the child
>process but not its children ( i.e. my grandchildren )

you need to re-read what you wrote.  how can signalling a group to which a
process no longer belongs have any effect on that process?  if you need to
signal processes that might leave your process group you must not rely on
the group concept, i.e., record all the pids and signal each directly,
which implies that you must track their status.

--
bringing you boring signatures for 17 years

 
 
 

how to kill a child process that runs other children processes

Post by j.. » Wed, 01 Jan 2003 00:23:18



> i almost forgot: FAQ 1.16 does not work because the children
> processes somehow end up not being affected by the process group,
> i.e. when i put the spawned process in a separate group, killing
> that group kills the child process but not its children ( i.e. my
> grandchildren )

You said you were using spawnv() to launch the child process. How are
you setting up that child process in its own process group? The only
spawnv() I'm aware of doesn't give you that option.
 
 
 

how to kill a child process that runs other children processes

Post by Chuck Dillo » Wed, 01 Jan 2003 01:17:14



> here's my problem:  i am spawning a child process using spawnv in c,
> launching an executable and passing some arguments to it.  then, in another
> thread i wait for a certain amount of time and if the first thread reaches a
> certain threshold, i kill the spawned child process.  this works total okay
> but because the executable that i run in the spawned process launches some
> other programs on its own, they continue to run regardless of the fact that
> i killed their parent process.  to make things clear:  i spawn process A.
> process A is an executable and i don't have its source.  process A launches
> a bunch of other processes B, C and D ( they are also separate executables
> w/o source code ).  when i kill process A, it dies but processes B, C and D
> continue to run.

> so, the question is:  is there a way to kill the children of a child process
> ( i.e. grandchildren ) without actually knowing their pids explicitly, i.e.
> can i say to a process "die and kill all your children, too" *without*
> modifying the code for that child process?

No.  In general, job control requires cooperation.  Given that your
follow-up indicates you can't use a process group you have to get
cooperation from the programs that spawn the grandchildren or rely on
heuristics.  If they 'escape' your process group it implies they want
to run as daemons and not to fit into your little job control scheme.

It's very difficult to build a shell that can manage more than one
generation of offspring transparently when you have no control over the
implementation of the apps your are trying to control.

Do the noncooperative grandchildren redirect there stdio?  If not,
perhaps you could try closing the stdio streams to get them to terminate.

Failing that you'll probably have to search the process table to try
and figure out which to terminate.

-- ced

--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

 
 
 

how to kill a child process that runs other children processes

Post by Burc Arpa » Wed, 01 Jan 2003 11:34:20


Chuck Dillon said:

Quote:> "Failing that you'll probably have to search the process table to try and

figure out which to terminate."

sigh.  that's also what our unix guy said...  i was hoping there might
another solution but obviously not.  anyway, thanx a lot for all the
answers...

 
 
 

1. How can kill all child processes without killing parent process ?

: Hi all UNIX expert,

: I am now writing a shell script to kill all child processes of a
: particular process.  I have written the following
: script:

: ps -ef | grep o\\ra  | awk '{print $2}' | xargs kill

: where ora is the name of the parent process

: However, this shell script only kills out the parent process,
: Does anyone know how to kill all child process without
: killing that parent process ? Please advise !!! Many thanks !!!
: Have a nice UNIX day !!!

you will have to read all matching processes and only kill the last in the
chain by using PID and PPID; unfortunately we can not assume that the
child is the process with the highest PPID:
assume $2=PID , $3=PPID ( coming from   ps -ef | grep o\\ra  | grep -v grep )

{
        PID[ $2 ]=""
        PPID[ $3 ]=""

END{
# process ids which are also parents should be deleted
        for( pid in PPID ) if ( pid in PID ) delete PID[ pid ]
        for( pid in PID ) { do_whatever_you_like_on_your_own_risk( pid ) }
        }

function do_whatever_you_like_on_your_own_risk( param ){
        code...
        }

2. bootp / in.arpd

3. smail failed to reap child process: No child processes

4. Windows 2000 NTFS5 partitions and RedHat 6.1

5. kill child and its all grad child processes?

6. this time I need a reall help

7. Getting a child and child's child process-id

8. K-Menu - get rid of Personal, Panel, etc.?

9. Parent process also terminating after killing child process in SIGINT

10. Child process terminates when parent process killed

11. killing a shell script is not killing child processes