public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Davy Durham <pubaddr2@davyandbeth.com>
To: Steven Rostedt <rostedt@goodmis.org>
Cc: linux-kernel@vger.kernel.org
Subject: Re: disowning a process
Date: Fri, 27 May 2005 14:05:06 -0500	[thread overview]
Message-ID: <42976F62.6000701@davyandbeth.com> (raw)
In-Reply-To: <42976D3A.5020200@davyandbeth.com>

Davy Durham wrote:

> Cool.. I looked at the daemon function and I might be able to use it..
>
> However, I compiled your code... seems to work.. but where is the 
> wait() done on the middle parrent so that it isn't left defunct?
>
I added "waitpid(pid,NULL,0);" after the outer-most if.. which is where 
the grand parent cleans up the pid of the intermediate parent. 

However, now trying the daemon() function..  which seems to work the 
same way.. it definately leaves a pid around.. so I guess you really 
need to do a wait() in the parent after forking.. however you don't know 
exactly which pid to wait for so you might be reaping some other child 
you've previously spawned.. 

So, for now I'm going to stick with the explicit double fork code.

Thanks!

> Steven Rostedt wrote:
>
>> Try man daemon.
>>
>> The way I use to do it was simply do a double fork. That is
>> (simplified)...
>>
>> if ((pid = fork()) < 0) {
>>     perror("fork");
>> } else if (!pid) {
>>     /* child */
>>     if ((pid = fork()) < 0) {
>>         perror("child fork");
>>         exit(-1);
>>     } if (pid) {
>>         /* child parent */
>>         /* Here we detach from the child */
>>         exit(0);
>>     }
>>     /* Now this code is a child running almost as a daemon
>>         with init as the parent. */
>>     setsid();
>>     /* Now the child is completely detached from the original
>>        parent */
>>     /* ... daemon code here ... */
>>     exit(0);
>> }
>>
>> /* parent code here */
>>
>> -- Steve
>>
>>  
>>
>
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe 
> linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/




  reply	other threads:[~2005-05-27 19:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-27 17:30 disowning a process Davy Durham
2005-05-27 18:04 ` Steven Rostedt
2005-05-27 18:55   ` Davy Durham
2005-05-27 19:05     ` Davy Durham [this message]
2005-05-27 19:27       ` Steven Rostedt
2005-05-27 19:07     ` Steven Rostedt
2005-05-27 20:57     ` Alan Cox
2005-05-27 23:34       ` Davy Durham
2005-05-28  1:38         ` Steven Rostedt
2005-05-28  1:48           ` Steven Rostedt
2005-05-28 23:18             ` Davy Durham
2005-05-27 18:54 ` J. Scott Kasten
2005-05-27 19:38   ` Steven Rostedt
     [not found] <OFA0F07206.30BD7843-ON8525700E.00611011@teal.com>
2005-05-27 18:15 ` Davy Durham

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42976F62.6000701@davyandbeth.com \
    --to=pubaddr2@davyandbeth.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rostedt@goodmis.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox