Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

How to kill child & parent process simulanteously
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

Report this thread to moderator Post Follow-up to this message
Old Post
Niraj123
04-26-05 09:01 AM


Re: How to kill child & parent process simulanteously
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.


Report this thread to moderator Post Follow-up to this message
Old Post
junky_fellow@yahoo.co.in
04-26-05 09:01 AM


Re: How to kill child & parent process simulanteously
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


Report this thread to moderator Post Follow-up to this message
Old Post
niraj.kumar.ait@gmail.com
04-26-05 09:01 AM


Re: How to kill child & parent process simulanteously
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?

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
04-26-05 01:59 PM


Re: How to kill child & parent process simulanteously
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.


Report this thread to moderator Post Follow-up to this message
Old Post
junky_fellow@yahoo.co.in
04-26-05 01:59 PM


Re: How to kill child & parent process simulanteously
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.
 
>
> 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 ***

Report this thread to moderator Post Follow-up to this message
Old Post
Barry Margolin
04-26-05 01:59 PM


Re: How to kill child & parent process simulanteously
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

Report this thread to moderator Post Follow-up to this message
Old Post
Pascal Bourguignon
04-26-05 01:59 PM


Re: How to kill child & parent process simulanteously
> 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.


Report this thread to moderator Post Follow-up to this message
Old Post
phil_gg04@treefic.com
04-27-05 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:32 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.