Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Understanding fork

by RMGir (Prior)
on Nov 08, 2002 at 16:44 UTC ( #211465=note: print w/replies, xml ) Need Help??


in reply to Understanding fork

Seems fine to me.

Here's what's happening. When fork is called, it either fails or succeeds. If it fails, it returns undef, so you log an error and DieNice.

If it succeeds, it returns _twice_. Well, in reality, it spawns a separate process with its own copy of all your variables, file descriptors, etc, and both process run in parallel.

In the parent process, fork() returns the process ID of the new child. So in that process, you redirect the user, and exit. (At least, I THINK you should exit, but you're falling out of that else clause in your sample, and doing the while twice...).

The child process gets 0 back from fork. That process closes STDIN, STDOUT, and STDERR to detach itself from the parent process, and continues on to do the normal processing. Closing those 3 filehandles is not enough to completely detach in all situations. However, since you're being run by the web server, you don't have to worry about things like terminals. If you have any other files open before the fork, you should probably close them, as well.

You should consider doing an open of STDERR to a log file after the close(STDERR), in case your script dies/warns...
--
Mike

Edit: Was missing an important NOT in the "enough to completely detach" sentence...

Replies are listed 'Best First'.
Re: Re: Understanding fork
by neilwatson (Priest) on Nov 08, 2002 at 17:38 UTC
    In the parent process, fork() returns the process ID of the new child. So in that process, you redirect the user, and exit. (At least, I THINK you should exit, but you're falling out of that else clause in your sample, and doing the while twice...).

    How do you prevent that?

    Neil Watson
    watson-wilson.ca

      Where you have the comment about redirecting the user, add
      $SIG{CHLD}='IGNORE'; exit;
      That way, the parent process will exit immediately, hopefully without waiting for the child process.
      --
      Mike

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://211465]
help
Chatterbox?
and John Coltrane plays...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2018-07-16 09:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (333 votes). Check out past polls.

    Notices?