LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-11-2004, 09:22 PM   #1
jpan
Member
 
Registered: Aug 2004
Distribution: Debian Libranet
Posts: 69

Rep: Reputation: 15
how do I make the children gone with the parents


how to make the child process "die with its parent" when the user
terminate the parent process?
 
Old 08-12-2004, 01:31 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
I think that there can be many senarios, First is that a signal will be generated and all child's will have a signal handler that will kill the childs, If you are talking about threads, then you can send the termenate signal to the child threads, all you have to do is to get their ThreadID, Make it global or just return them to parent thread.
 
Old 08-12-2004, 07:23 AM   #3
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 50
i dont know a specific way to do it(there probably is one though) but some thoughts that come to mind are:

a) in the child process get the process id of parent, if it changes, then parent has been killed, this will only work though if parent is alive when getppid() is called.

b) use pipe() and make each child regularly communicate with the parent, and handle SIGPIPE - should work every case but will quickly open lots and lots of file descriptors

c) method b implemented with any other type of ipc you care to come up with.

d) a combination of method a and b, where you just create the pipe until you know the value returned by getppid() is correct then close it.
 
Old 08-12-2004, 02:42 PM   #4
bruce ford
Member
 
Registered: Jul 2004
Location: Munich, Germany
Distribution: Sun Solaris 8, SuSE 9.0
Posts: 43

Rep: Reputation: 15
hi,

it may sound heretic but if it is possible for your application to change the roles of parent and child process the handling would be straight forward (SIGCHILD).

Don't know if that helps but sometimes one needs only a kick to think the other way round...

So long...
bruce
 
Old 08-12-2004, 03:25 PM   #5
jpan
Member
 
Registered: Aug 2004
Distribution: Debian Libranet
Posts: 69

Original Poster
Rep: Reputation: 15
also, if i do

// in the child process
close(STDIN_FILENO);
execl(".....ssh","ssh",.....);


in my code, then after running the program, a message says that
"Pseudo terminal will not be allocated because stdin is not a terminal."

so using close(STDIN_FILENO) before execl() seems not working for my case.
 
Old 09-18-2004, 09:30 PM   #6
dougpotter
LQ Newbie
 
Registered: Sep 2004
Posts: 2

Rep: Reputation: 0
You can use prctl() to send the child a SIGTERM when the parent dies.

Code:
int pid;

pid = fork();
if ( pid == 0 ) {
    // This will cause a SIGTERM to be sent to the child if
    // the parent dies for some reason.
    prctl( PR_SET_PDEATHSIG, SIGTERM, 0, 0, 0 );

    execl( ... ); // Run your program here

    perror("FAILED");
    exit(EXIT_FAILURE);
}
 
Old 09-20-2004, 12:40 AM   #7
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
Quote:
Originally posted by bruce ford
hi,

it may sound heretic but if it is possible for your application to change the roles of parent and child process the handling would be straight forward (SIGCHILD).

Don't know if that helps but sometimes one needs only a kick to think the other way round...

So long...
bruce
this is exactly how to clean up children when they finish so they don't lie around as zombies
while parent is still running
but the original post was

[quote]
"die with its parent" when the user
terminate the parent process
[quote]

when a program exits it's children are inherited automatically by init and init always cleans them up
no need to do nothing (automatic filicide)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parents mikedeatworld Linux - Software 5 08-01-2005 03:05 PM
Getting parents used to Linux? wondershowzen General 13 06-20-2005 02:40 PM
Remote admin of parents linux w/ krfb pH* Linux - General 1 01-02-2005 09:05 AM
Frustrated with Parents MvD General 8 10-17-2003 12:02 PM
Stupid no knowing parents Leseta Linux - Newbie 15 08-15-2003 01:00 PM


All times are GMT -5. The time now is 02:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration