NAME

Proc::Fork::Control

DESCRIPTION

Proc::Fork::Control is a simple to use library which functions much the same way as Proc::Fork. That said, Proc::Fork is not used, as fork() is accessed directly.

Proc::Fork::Control allows you to manage forks, control number of children allowed, monitor children, control blocking and nonblocking states, etc.

SYNOPSIS

 #!/usr/bin/perl 
 use Proc::Fork::Control;
 use Fcntl ':flock';

 # Initialize the system allowing 25 forks per cfork() level
 cfork_init(25);

 for(my $i = 0; $i < 50; $i++){
        # Fork this if possible, if all avaliable fork slots are full
        # block until one becomes avaliable.
        cfork(sub {
                # Initialize for children
                cfork_init(2);

                for('A' .. 'Z'){
                        cfork(sub {
                                # Lock STDOUT for writing.
                                flock(STDOUT, &LOCK_EX);

                                # Print out a string.
                                print STDOUT "Fork: $i: $_\n";
        
                                # Unlock STDOUT.
                                flock(STDOUT, &LOCK_UN);

                                cfork_exit();
                        });
                }

                # Wait for sub children to exit
                cfork_wait()

        });
 }

 # Wait until all forks have finished.
 cfork_wait();

METHODS

Note - because of the nature of forking within perl. I've against objectifying this library. Rather it uses direct function calls which are exported to the global namespace Below is a list of these calls and how to access them.

cfork(code, code, code)

Provide managed forking functions.

Returns nothing on error and sets the cfork_errstr error handler.

if cfork() is called with in an cfork()ed process the calling cfork() process will block until all children with in it die off.

cfork_nonblocking(BOOL)

Set the cfork() behavior to nonblocking mode if <BOOL> is true, This will result in the fork returning right away rather than waiting for any possible children to die.

Also, cfork_nonblocking() should always be turned off after the bit of code you want to run, runs.

EXAMPLE
 cfork_nonblocking(0);

 cfork(sub {
        do some work;
 });

 cfork_nonblocking(1);

cfork_is_child()

Return true if called with in a forked enviroment, otherwise return false.

cfork_has_children()

Return true if children exist with in a forked enviroment.

cfork_errstr()

Return the last error message.

cfork_init(children)

Initialize the CHLD reaper with a maximum number of <children>

This should be called prior to any cfork() calls

cfork_exit(int)

Exit a process cleanly and set an exit code.

Normally this can be easily handled with $? however, in some cases $? is not reliably delivered.

Once called, drop to END {} block and terminate.

cfork_exit_code()

Returns the last known cfork_exit() code.

cfork_maxchildren(int)

Set/Reset the maximum number of children allowed.

cfork_wait()

Block until all cfork() children have died off unless cfork_nonblocking() is enabled.

cfork_wait_pid(PID, PID, PID, ..)

cfork_wait_pid() functions much like cfork_wait() with the exception that it expects a list of PID's and blocks until those PID's have died off. Like cfork_wait(), cfork_wait_pid() will NOT block if cfork_nonblocking() mode is enabled.

cfork_active_children()

Return the total number of active children.

cfork_daemonize(BOOL)

Daemonize the the calling script.

If <BOOL> is true write _ALL_ output to /dev/null.

If you have termination handling, i.e. %SIG and END {} block control, cfork_daemonize triggers exit signal 2. So... $? == 4

cfork_sleep(int)

Provides an alarm safe sleep() wrapper. Beacuse we sleep() with in this, ALRM will be issued with in the fork once the sleep cycle has completed. This function wraps sleep with in a while() block and tests to make sure that the seconds requested for the sleep were slept.

cfork_usleep(int)

Provides an alarm safe Time::HiRes usleep() wrapper. Beacuse we sleep() with in this, ALRM will be issued with in the fork once the sleep cycle has completed. This function wraps sleep with in a while() block and tests to make sure that the seconds requested for the sleep were slept.

This function is only avaliable if Time::HiRes is avaliable otherwise it will simply return nothing at all.

cfork_ssleep(int)

Preform an cfork_sleep() except rather than using standard sleep() (with interruption handling) use a select() call to sleep. This can be useful in environments where sleep() does not behave correctly, and a select() will block for the desired number of seconds properly.

cfork_kill_children(SIGNAL)

Send all children (if any) this <SIGNAL>.

If the <SIGNAL> argument is omitted kill TERM will be used.

cfork_list_children(BOOL)

Return a list of PID's currently running under this fork.

If BOOL is true a hash will be returned rather than a list.

cfork_child_dob(PID)

Return the EPOCH Date of Birth for this childs <PID>

Returns 0 if no child exists under that PID for this fork.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 214:

'=item' outside of any '=over'

Around line 230:

You forgot a '=back' before '=head2'