public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Davy Durham <pubaddr2@davyandbeth.com>
Cc: Alan Cox <alan@lxorguk.ukuu.org.uk>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Subject: Re: disowning a process
Date: Fri, 27 May 2005 21:38:36 -0400	[thread overview]
Message-ID: <1117244316.6477.22.camel@localhost.localdomain> (raw)
In-Reply-To: <4297AE6F.9040707@davyandbeth.com>

[-- Attachment #1: Type: text/plain, Size: 1032 bytes --]

On Fri, 2005-05-27 at 18:34 -0500, Davy Durham wrote:
> Well, when I tried using it in a program with some sleeps to test.. I 
> noticed that the intermediate process that daemon creates is not cleaned 
> up with a wait() call (so I see a defunct process in the ps listing).

You didn't use it right. See below.

> 
> If I manually do the double fork() then I can call waitpid() myself for 
> the pid that I know it spawned.   But if I just call wait() after 
> calling daemon, then I don't know if I just cleaned up the pid it 
> spawned (do I?), or some other previously spawned one (for perhaps 
> totally different reasons)..

You still need to fork the first child, and the daemon does the second
fork and exits the parent, and does all the necessary things for the
system to make a proper daemon.

> 
> For my specifics it may not be a problem, but I guess I'm just whining 
> about the fact that daemon() doesn't clean it up itself (or can it?)

Attached is a simple C program that uses daemon the way you want to.

-- Steve

[-- Attachment #2: daemon.c --]
[-- Type: text/x-csrc, Size: 482 bytes --]

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>


int main (int argc, char **argv)
{
	pid_t pid;

	if ((pid = fork()) < 0) {
		perror("fork");
	} else if (!pid) {
		if (daemon(1,1) < 0) {
			perror("daemon");
			exit(-1);
		}
		printf("daemon is %d\n",getpid());
		sleep(100);
		exit(0);
	}

	printf("middle child is %d (and died)\n",pid);
	waitpid(pid,NULL,0);

	printf("parent is %d\n",getpid());

	sleep(100);
	return 0;
}

  reply	other threads:[~2005-05-28  1:38 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
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 [this message]
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=1117244316.6477.22.camel@localhost.localdomain \
    --to=rostedt@goodmis.org \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pubaddr2@davyandbeth.com \
    /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