Re: disowning a process
From: Davy Durham
Date: Fri May 27 2005 - 14:14:49 EST
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@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/