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

perlfunc:fork

by gods
on Aug 24, 1999 at 22:43 UTC ( #356=perlfunc: print w/ replies, xml ) Need Help??

fork

See the current Perl documentation for fork.

Here is our local, out-dated (pre-5.6) version:


fork - create a new process just like this one



fork



Does a fork(2) system call. Returns the child pid to the parent process, 0 to the child process, or undef if the fork is unsuccessful.

Note: unflushed buffers remain unflushed in both processes, which means you may need to set $| ($AUTOFLUSH in English) or call the autoflush() method of IO::Handle to avoid duplicate output.

If you fork() without ever waiting on your children, you will accumulate zombies:

    $SIG{CHLD} = sub { wait };

There's also the double-fork trick (error checking on fork() returns omitted);

    unless ($pid = fork) {
        unless (fork) {
            exec "what you really wanna do";
            die "no exec";
            # ... or ...
            ## (some_perl_code_here)
            exit 0;
        }
        exit 0;
    }
    waitpid($pid,0);

See also the perlipc manpage for more examples of forking and reaping moribund children.

Note that if your forked child inherits system file descriptors like STDIN and STDOUT that are actually connected by a pipe or socket, even if you exit, then the remote server (such as, say, httpd or rsh) won't think you're done. You should reopen those to /dev/null if it's any issue.


Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-09-30 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (363 votes), past polls