Venus::Process

Process Class

Process Class for Perl 5

method: chdir method: check method: daemon method: disengage method: engage method: exit method: fork method: forks method: kill method: ping method: setsid method: stderr method: stdin method: stdout method: trap method: wait method: work method: untrap

package main;

use Venus::Process;

my $parent = Venus::Process->new;

my $process = $parent->fork;

if ($process) {
  # do something in child process ...
  $process->exit;
}
else {
  # do something in parent process ...
  $parent->wait(-1);
}

# $parent->exit;

This package provides methods for handling and forking processes.

Venus::Kind::Utility

Venus::Role::Accessible Venus::Role::Buildable Venus::Role::Explainable Venus::Role::Valuable

The chdir method changes the working directory the current process is operating within.

chdir(Str $path) (Process)

{ since => '0.06', }

=example-1 chdir

# given: synopsis;

$parent = $parent->chdir;

# bless({...}, 'Venus::Process')

The check method does a non-blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).

check(Int $pid) (Int, Int)

{ since => '0.06', }

=example-1 check

package main;

use Venus::Process;

my $parent = Venus::Process->new;

my ($process, $pid) = $parent->fork;

if ($process) {
  # in forked process ...
  $process->exit;
}

my $check = $parent->check($pid);

# 0

The daemon method detaches the process from controlling terminal and runs it in the background as system daemon. This method internally calls "disengage" and "setsid" and attempts to change the working directory to the root directory.

daemon() (Process)

{ since => '0.06', }

=example-1 daemon

# given: synopsis;

my $daemon = $parent->daemon; # exits parent immediately

# in forked process ...

# $daemon->exit;

The disengage method limits the interactivity of the process by changing the working directory to the root directory and redirecting its standard file descriptors from and to /dev/null, or the OS' equivalent. These state changes can be undone by calling the "engage" method.

disengage() (Process)

{ since => '0.06', }

=example-1 disengage

# given: synopsis;

$parent = $parent->disengage;

# bless({...}, 'Venus::Process')

The engage method ensures the interactivity of the process by changing the working directory to the directory used to launch the process, and by redirecting/returning its standard file descriptors from and to their defaults. This method effectively does the opposite of the "disengage" method.

engage() (Process)

{ since => '0.06', }

=example-1 engage

# given: synopsis;

$parent = $parent->engage;

# bless({...}, 'Venus::Process')

The exit method exits the program immediately.

exit(Int $status) (Int)

{ since => '0.06', }

=example-1 exit

# given: synopsis;

my $exit = $parent->exit;

# 0

The fork method calls the system "fork" in perlfunc function and creates a new process running the same program at the same point (or call site). This method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef to the parent (or originating) process. In list context, this method returns both the process and PID (or process ID) of the child process. If a callback or argument is provided it will be executed in the child process.

fork(Str | CodeRef $code, Any @args) (Process, Int)

{ since => '0.06', }

=example-1 fork

# given: synopsis;

$process = $parent->fork;

# if ($process) {
#   # in forked process ...
#   $process->exit;
# }
# else {
#   # in parent process ...
#   $parent->wait(-1);
# }

# in child process

# bless({...}, 'Venus::Process')

The forks method creates multiple forks by calling the "fork" method n times, based on the count specified. As with the "fork" method, this method returns a new Venus::Process object representing the child process (from within the execution of the child process (or fork)), and returns undef to the parent (or originating) process. In list context, this method returns both the process and an arrayref of PID values (or process IDs) for each of the child processes created. If a callback or argument is provided it will be executed in each child process.

forks(Str | CodeRef $code, Any @args) (Process, ArrayRef[Int])

{ since => '0.06', }

=example-1 forks

# given: synopsis;

$process = $parent->forks(5);

# if ($process) {
#   # do something in (each) forked process ...
#   $process->exit;
# }
# else {
#   # do something in parent process ...
#   $parent->wait(-1);
# }

# bless({...}, 'Venus::Process')

The kill method calls the system "kill" in perlfunc function which sends a signal to a list of processes and returns truthy or falsy. Note: A truthy result doesn't necessarily mean all processes were successfully signalled.

kill(Str $signal, Int @pids) (Int)

{ since => '0.06', }

=example-1 kill

# given: synopsis;

if ($process = $parent->fork) {
  # in forked process ...
  $process->exit;
}

my $kill = $parent->kill('term', int$process);

# 1

The ping method returns truthy if the process of the PID provided is active. If multiple PIDs are provided, this method will return the count of active PIDs.

ping(Int @pids) (Int)

{ since => '2.01', }

=example-1 ping

# given: synopsis;

if ($process = $parent->fork) {
  # in forked process ...
  $process->exit;
}

my $ping = $parent->ping(int$process);

# 1

The setsid method calls the "setsid" in POSIX function and sets the process group identifier of the current process.

setsid() (Int)

{ since => '0.06', }

=example-1 setsid

# given: synopsis;

my $setsid = $parent->setsid;

# 1

The stderr method redirects STDERR to the path provided, typically /dev/null or some equivalent. If called with no arguments STDERR will be restored to its default.

stderr(Str $path) (Process)

{ since => '0.06', }

=example-1 stderr

# given: synopsis;

$parent = $parent->stderr;

# bless({...}, 'Venus::Process')

The stdin method redirects STDIN to the path provided, typically /dev/null or some equivalent. If called with no arguments STDIN will be restored to its default.

stdin(Str $path) (Process)

{ since => '0.06', }

=example-1 stdin

# given: synopsis;

$parent = $parent->stdin;

# bless({...}, 'Venus::Process')

The stdout method redirects STDOUT to the path provided, typically /dev/null or some equivalent. If called with no arguments STDOUT will be restored to its default.

stdout(Str $path) (Process)

{ since => '0.06', }

=example-1 stdout

# given: synopsis;

$parent = $parent->stdout;

# bless({...}, 'Venus::Process')

The trap method registers a process signal trap (or callback) which will be invoked whenever the current process receives that matching signal. The signal traps are globally installed and will overwrite any preexisting behavior. Signal traps are inherited by child processes (or forks) but can be overwritten using this method, or reverted to the default behavior by using the "untrap" method.

trap(Str $name, Str | CodeRef $expr) (Process)

{ since => '0.06', }

=example-1 trap

# given: synopsis;

$parent = $parent->trap(term => sub{
  die 'Something failed!';
});

# bless({...}, 'Venus::Process')

The wait method does a blocking "waitpid" in perlfunc operation and returns the wait status. In list context, returns the specified process' exit code (if terminated).

wait(Int $pid) (Int, Int)

{ since => '0.06', }

=example-1 wait

package main;

use Venus::Process;

my $parent = Venus::Process->new;

my ($process, $pid) = $parent->fork;

if ($process) {
  # in forked process ...
  $process->exit;
}

my $wait = $parent->wait($pid);

# 0

The work method forks the current process, runs the callback provided in the child process, and immediately exits after. This method returns the PID of the child process. It is recommended to install an "alarm" in perlfunc in the child process (i.e. callback) to avoid creating zombie processes in situations where the parent process might exit before the child process is done working.

work(Str | CodeRef $code, Any @args) (Int)

{ since => '0.06', }

=example-1 work

# given: synopsis;

my $pid = $parent->work(sub{
  my ($process) = @_;
  # in forked process ...
  $process->exit;
});

# $pid

The untrap method restores the process signal trap specified to its default behavior. If called with no arguments, it restores all signal traps overwriting any user-defined signal traps in the current process.

untrap(Str $name) (Process)

{ since => '0.06', }

=example-1 untrap

# given: synopsis;

$parent->trap(chld => 'ignore')->trap(term => sub{
  die 'Something failed!';
});

$parent = $parent->untrap('term');

# bless({...}, 'Venus::Process')

This package overloads the "" operator.

This package overloads the ~~ operator.

t/Venus.t: pdml: authors t/Venus.t: pdml: license

84 POD Errors

The following errors were encountered while parsing the POD:

Around line 132:

Unknown directive: =name

Around line 140:

Unknown directive: =tagline

Around line 148:

Unknown directive: =abstract

Around line 156:

Unknown directive: =includes

Around line 181:

Unknown directive: =synopsis

Around line 212:

Unknown directive: =description

Around line 220:

Unknown directive: =inherits

Around line 228:

Unknown directive: =integrates

Around line 239:

Unknown directive: =method

Around line 244:

Unknown directive: =signature

Around line 248:

Unknown directive: =metadata

Around line 279:

=cut found outside a pod block. Skipping to next block.

Around line 296:

=cut found outside a pod block. Skipping to next block.

Around line 311:

Unknown directive: =method

Around line 317:

Unknown directive: =signature

Around line 321:

Unknown directive: =metadata

Around line 377:

=cut found outside a pod block. Skipping to next block.

Around line 408:

=cut found outside a pod block. Skipping to next block.

Around line 420:

Unknown directive: =method

Around line 426:

Unknown directive: =signature

Around line 430:

Unknown directive: =metadata

Around line 456:

Unknown directive: =method

Around line 463:

Unknown directive: =signature

Around line 467:

Unknown directive: =metadata

Around line 490:

Unknown directive: =method

Around line 497:

Unknown directive: =signature

Around line 501:

Unknown directive: =metadata

Around line 524:

Unknown directive: =method

Around line 528:

Unknown directive: =signature

Around line 532:

Unknown directive: =metadata

Around line 565:

=cut found outside a pod block. Skipping to next block.

Around line 576:

Unknown directive: =method

Around line 586:

Unknown directive: =signature

Around line 590:

Unknown directive: =metadata

Around line 647:

=cut found outside a pod block. Skipping to next block.

Around line 681:

=cut found outside a pod block. Skipping to next block.

Around line 705:

=cut found outside a pod block. Skipping to next block.

Around line 718:

Unknown directive: =method

Around line 729:

Unknown directive: =signature

Around line 733:

Unknown directive: =metadata

Around line 788:

=cut found outside a pod block. Skipping to next block.

Around line 826:

=cut found outside a pod block. Skipping to next block.

Around line 837:

Unknown directive: =method

Around line 843:

Unknown directive: =signature

Around line 847:

Unknown directive: =metadata

Around line 878:

Unknown directive: =method

Around line 883:

Unknown directive: =signature

Around line 887:

Unknown directive: =metadata

Around line 918:

Unknown directive: =method

Around line 923:

Unknown directive: =signature

Around line 927:

Unknown directive: =metadata

Around line 958:

=cut found outside a pod block. Skipping to next block.

Around line 970:

Unknown directive: =method

Around line 976:

Unknown directive: =signature

Around line 980:

Unknown directive: =metadata

Around line 1012:

=cut found outside a pod block. Skipping to next block.

Around line 1024:

Unknown directive: =method

Around line 1030:

Unknown directive: =signature

Around line 1034:

Unknown directive: =metadata

Around line 1066:

=cut found outside a pod block. Skipping to next block.

Around line 1078:

Unknown directive: =method

Around line 1084:

Unknown directive: =signature

Around line 1088:

Unknown directive: =metadata

Around line 1120:

=cut found outside a pod block. Skipping to next block.

Around line 1132:

Unknown directive: =method

Around line 1141:

Unknown directive: =signature

Around line 1145:

Unknown directive: =metadata

Around line 1172:

Unknown directive: =method

Around line 1178:

Unknown directive: =signature

Around line 1182:

Unknown directive: =metadata

Around line 1238:

=cut found outside a pod block. Skipping to next block.

Around line 1269:

=cut found outside a pod block. Skipping to next block.

Around line 1281:

Unknown directive: =method

Around line 1289:

Unknown directive: =signature

Around line 1293:

Unknown directive: =metadata

Around line 1322:

Unknown directive: =method

Around line 1328:

Unknown directive: =signature

Around line 1332:

Unknown directive: =metadata

Around line 1374:

=cut found outside a pod block. Skipping to next block.

Around line 1386:

Unknown directive: =operator

Around line 1402:

=cut found outside a pod block. Skipping to next block.

Around line 1411:

Unknown directive: =operator

Around line 1427:

=cut found outside a pod block. Skipping to next block.

Around line 1433:

Unknown directive: =partials