LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 10-29-2009, 01:32 AM   #1
minimol
LQ Newbie
 
Registered: Aug 2006
Distribution: Fedora
Posts: 10

Rep: Reputation: 0
Unhappy terminate child process when parent is killed


A parent process creates 3 children using fork/exec. When parent crashes the children should terminate.

Can I do this with some other IPC mechanism like shared memory or mutexes. All I need is

1)child should wait on some object ( like an event or named semaphore)
(Parent can be the owner of this object )

2) when parent crashes ( or if some one sends kill -9 ) children should be notified..


I tried using a named semaphore and made the children wait on the semaphore. But the children doesn't get any information when parent is crashed.

I can use getppid in children and get status periodically from /proc/$PID. But I don't want to use a polling mechanism from the child process.
 
Old 10-29-2009, 04:11 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
  1. Parent creates the three children, making note of their pids.
  2. Parent creates a pipe, and forks a fourth child having access to that pipe. The pipe is written to by the parent and read by the fourth child. So the parent should close the reading side of the pipe, and the child should close the writing side of the pipe.
  3. Over that pipe, the parent passes the pids of the first three children.
  4. The fourth child makes note of the three pids and waits for additional input on the pipe.
  5. The parent never makes additional output to that pipe.
  6. The parent eventually exits or dies. Example: someone sends it signal -9.
  7. The fourth child, waiting on input, sees end of file on the pipe.
  8. The fourth child then sends a signal to each of the first three children. USR1 is a good choice for the signal, or maybe HUP if you wish.
  9. Each of the first three children, upon receiving that signal, knows to clean up and exit.
Hope this helps.
 
1 members found this post helpful.
  


Reply



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
return value from child to parent process prabhat ranjan Programming 3 02-12-2008 06:40 PM
child process usses same amount of ram as parent process socialjazz Programming 7 10-19-2006 05:48 PM
When I kill parent process, child isn't killed BengeBoy Linux - Software 0 08-19-2004 04:38 PM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM
about parent and child process winwar Solaris / OpenSolaris 3 07-23-2003 06:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:10 PM.

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
Open Source Consulting | Domain Registration