icon Top 9 categories map      RocketAware > Perl >

fork

Tips: Browse or Search all pages for efficient awareness of Perl functions, operators, and FAQs.



Home

Search Perl pages


Subjects

By activity
Professions, Sciences, Humanities, Business, ...

User Interface
Text-based, GUI, Audio, Video, Keyboards, Mouse, Images,...

Text Strings
Conversions, tests, processing, manipulation,...

Math
Integer, Floating point, Matrix, Statistics, Boolean, ...

Processing
Algorithms, Memory, Process control, Debugging, ...

Stored Data
Data storage, Integrity, Encryption, Compression, ...

Communications
Networks, protocols, Interprocess, Remote, Client Server, ...

Hard World
Timing, Calendar and Clock, Audio, Video, Printer, Controls...

File System
Management, Filtering, File & Directory access, Viewers, ...

    
fork
Does a fork(2) system call. Returns the child pid to the parent process and 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, 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.

Source: Perl builtin functions
Copyright: Larry Wall, et al.
Next: format

Previous: flock FILEHANDLE,OPERATION



(Corrections, notes, and links courtesy of RocketAware.com)


[Overview Topics]

Up to: Process Creation and Control




Rapid-Links: Search | About | Comments | Submit Path: RocketAware > Perl > perlfunc/fork.htm
RocketAware.com is a service of Mib Software
Copyright 2000, Forrest J. Cavalier III. All Rights Reserved.
We welcome submissions and comments