perlplexer has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone!

Most of you are probably familiar with perlipc and, specifically, the "Complete dissociation of child from parent" section of it.
I myself used the code from that page a few times thinking that it does indeed completely detach child from parent. Until today that is.
If my understanding of what setsid() is supposed to do is correct, as soon as it gets called and returns 'true', parent process should no longer see the process that called setsid() as child.
Consider the fragment of code below.
daemon(); sub daemon{ # Create a daemon which does nothing but "sleep 100" my $dpid = fork(); return 0 unless defined $dpid; unless ($dpid){ exit 255 unless open STDIN, '/dev/null'; exit 255 unless open STDOUT, '>/dev/null'; exit 255 unless open STDERR, '>/dev/null'; exit 255 unless setsid(); sleep 100; exit 0; } return 0 unless wait() == -1; return $dpid; }
On HPUX 11, Perl 5.6.1, this sub wait()s for the daemon to finish as if it was still a normal child process. And I know that setsid() succeeded since parent keeps wait()ing for 100 seconds.
So, what is this? A feature? Is this how setsid() is supposed to work? Or should I not work on Sundays and get more sleep?

--perlplexer

Replies are listed 'Best First'.
Re: setsid() and complete (?) dissociation of child from parent
by Abigail-II (Bishop) on Mar 03, 2003 at 01:07 UTC
    I don't know where you got your information about setsid, but you are wrong. While it will create a new session, it doesn't break the relation between parent and child.

    I checked this in "Advanced Programming in the UNIX Environment", by W. Richard Stevens.

    Abigail

      OK, good. This means I'm not crazy and the "complete dissociation" is not as complete as one may think. :)

      Thanks,
      --perlplexer
Re: setsid() and complete (?) dissociation of child from parent (more fork)
by tye (Sage) on Mar 03, 2003 at 19:57 UTC

    You have all of the code that is used to daemonize a process. But the parent of fork is supposed to immediately exit. So either do that or add another fork (where the parent immediately exits) so that the daemon is the grandchild of the process that calls wait.

                    - tye