LinuxQuestions.org
Help answer threads with 0 replies.
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 01-25-2013, 06:50 AM   #1
hervek
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Rep: Reputation: Disabled
How to mix orphan an non orphan child ?


scenario : a remanent daemon spawns different child process depending of incomming tcp requests. Some will be orphan : I do not care of their life or their status. For some others non orphan : I want to get an end status or to know if they crash.

What means an orphan for me in C

struct sigaction sa;
sa.sa_handler = SIG_IGN;
sa.sa_flags = SA_NOCLDWAIT;
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, NULL);

pid = fork

--> no zombie created when child end

For non orphan,

struct sigaction sa;
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGCHLD, &sa, NULL);

pid = fork
if (pid == 0) {
child
}
else {

--> zombie created when child end until I get the status

waitpid(pid, &status, WNOHANG);

// pid could be -1 for many

}


My problem :

After many non orphan child, when I create my first orphan child, I can't get anymore status of child non orphan. I just get ECHILD error from waitpid when last child end. If I recreate non orphan child, orphan child creates zombie.
It seems impossible to get orphan and non orphan cohabitation.

Did someone find a solution to fix it ? or could point the weakness of my algorithm ?
 
Old 01-26-2013, 04:45 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,770

Rep: Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210Reputation: 2210
The usual way to create a disowned child process is to fork twice:
Code:
if((pid = fork()) == 0) {
    if(fork() != 0) exit(0)  /* Intermediate process exits immediately */
    ...          /* Child continues, adopted by process 1 (init) */
}
/* Parent continues */
waitpid(pid, ...)  /* Reap the intermediate process */

Last edited by rknichols; 01-26-2013 at 04:51 PM. Reason: Accidentally hit "send" too early
 
Old 01-28-2013, 04:37 AM   #3
hervek
LQ Newbie
 
Registered: Jan 2013
Posts: 2

Original Poster
Rep: Reputation: Disabled
thanks

it sounds good
It was in my option list but with the full fork/exec code
I didn't think to this elegant solution with 2 fork piped
 
  


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
[SOLVED] About Arch orphan packages Mr. Alex Arch 5 04-13-2012 01:33 PM
[SOLVED] What are the ZombieZombie and Orphan processes anandg111 Linux - Newbie 1 01-05-2012 05:46 AM
ORPHAN (no passwd entry) markus1982 Linux - General 6 08-19-2010 08:57 AM
orphan child process happy78 Programming 3 08-30-2005 01:40 AM
Orphan Processes LiquidPenguin Linux - Newbie 0 07-15-2003 10:54 AM

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

All times are GMT -5. The time now is 08:59 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