Using daemon from /etc/init.d/functions
Michael Torrie
torriem at chem.byu.edu
Fri Mar 30 17:54:31 MDT 2007
On Fri, 2007-03-30 at 17:22 -0600, Steve wrote:
> It's really simple you just fork 2 times before entering the main
> application loop, then let the parent forks die.
>
> Look at it though, I think this may be a mistake. Looks like it's
> leaving one extra parent just hanging there, because it's not actually
> for daemonizing, it's for multi process spawning.
> I initially picked up this trick as a quick and dirty way of having a
> single server process running that could launch more copies of itself
> to handle load outside its realm. For instance if I have a server
> responsible for a certain set area in a 3D world say -1024,1024 and
> player wants to leave that area, I needed a quick way to spawn a new
> server to handle the area the player was walking into. However I
> didn't want umpteen million processes running empty zones, so doing a
> double fork allowed me to detach the child servers from the main root
> server, and when those zones emptied out the child process would die a
> natural death.
> Anyways for some reason I got that mixed up in my mind with Daemonizing.
No, indeed the recommended procedure for portably deamonizing a server
is to use the double fork. I'm not sure of all the reasons, but it is
in the examples I've seen and I've used that method myself.
See:
http://www.developerweb.net/forum/archive/index.php/t-3025.html
According to the page:
"The double fork() thing is meant to make the process an orphan, in fact
making it a child of the init process (usually PID 1).
The setsid() is used to create a new session, making the process a group
leader, dettaching it from the controlling tty (a Ctrl-C won't help ; )
and making it a session leader..."
Michael
More information about the PLUG
mailing list