Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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...


In reply to Re: Understanding fork by RMGir
in thread Understanding fork by neilwatson

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (1)
As of 2024-04-25 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found