NAME

Paraniod::Process - Process Management Functions

MODULE VERSION

$Id: Process.pm,v 0.6 2008/02/27 06:49:59 acorliss Exp $

SYNOPSIS

use Paranoid::Process;

$SIG{CHLD} = \&sigchld;
$count = childrenCount();
installChldHandler($sub);
$rv = pfork();

$uid = ptranslateUser("foo");
$gid = ptranslateGroup("foo");
$rv = switchUser($user, $group);

REQUIREMENTS

Paranoid Paranoid::Debug POSIX

DESCRIPTION

This module provides a few functions meant to make life easier when managing processes. The following export targets are provided:

all               All functions within this module
pfork             All child management functions

Only the function switchUser is currently exported by default.

VARIABLES

MAXCHILDREN

Setting this variable sets a limit to how many children will be forked at a time by pfork. The default is zero, which allows unlimited children. Once the limit is met pfork becomes a blocking call until a child exits so the new one can be spawned.

FUNCTIONS

childrenCount

$count = childrenCount();

This function returns the current number of children spawned by pfork.

installChldHandler

installChldHandler($sub);

This function takes a reference to a subroutine. If used the subroutine will be called every time a child exits. That subroutine will be called with the child's PID and exit value as arguments.

sigchld

$SIG{CHLD} = \&sigchld;

This function decrements the child counter necessary for pfork's operation, as well as calling the user's signal handler with each child's PID and exit value.

pfork

$rv = pfork();

This function should be used in lieu of Perl's fork if you want to take advantage of a blocking fork call that respects the MAXCHILDREN limit. Use of this function, however, also assumes the use of sigchld as the signal handler for SIGCHLD.

ptranslateUser

$uid = ptranslateUser("foo");

This function takes a username and returns the corresponding UID as returned by getpwent. If no match is found it returns undef.

ptranslateGroup

$gid = ptranslateGroup("foo");

This function takes a group name and returns the corresponding GID as returned by getgrent. If no match is found it returns undef.

ptranslateGroup

switchUser

$rv = switchUser($user, $group);

This function can be fed one or two arguments, both either named user or group, or UID or GID. The group argument is optional, but you can pass undef as the user to only switch the group.

EXAMPLES

pfork

This following example caps the number of children processes to three at a time:

$SIG{CHLD}  = \&sigchld;
MAXCHILDREN = 3;
for (1 .. 5) {

  # Only the children execute the following block
  unless ($pid = pfork()) {
    # ....
    exit 0;
  }
}

You can also install a child-exit routine to be called by sigchld. For instance, to track the children's history in the parent:

sub recordChild ($$) {
  my ($cpid, $cexit) = @_;

  push(@chistory, [$cpid, $cexit]);
}

installChldHandler(\&recordChild);
for (1 .. 5) {
  unless ($pid = pfork()) {
    # ....
    exit $rv;
  }
}

# Prints the child process history
foreach (@chistory) { print "PID: $$_[0] EXIT: $$_[1]\n" };

HISTORY

None as of yet.

AUTHOR/COPYRIGHT

(c) 2005 Arthur Corliss (corliss@digitalmages.com)