.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Proc::Fork 3pm" .TH Proc::Fork 3pm "2021-01-06" "perl v5.32.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Proc::Fork \- simple, intuitive interface to the fork() system call .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Proc::Fork; \& \& run_fork { \& child { \& # child code goes here. \& } \& parent { \& my $child_pid = shift; \& # parent code goes here. \& waitpid $child_pid, 0; \& } \& retry { \& my $attempts = shift; \& # what to do if fork() fails: \& # return true to try again, false to abort \& return if $attempts > 5; \& sleep 1, return 1; \& } \& error { \& # Error\-handling code goes here \& # (fork() failed and the retry block returned false) \& } \& }; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" This module provides an intuitive, Perl-ish way to write forking programs by letting you use blocks to illustrate which code section executes in which fork. The code for the parent, child, retry handler and error handler are grouped together in a \*(L"fork block\*(R". The clauses may appear in any order, but they must be consecutive (without any other statements in between). .PP All four clauses need not be specified. If the retry clause is omitted, only one fork will be attempted. If the error clause is omitted the program will die with a simple message if it can't retry. If the parent or child clause is omitted, the respective (parent or child) process will start execution after the final clause. So if one or the other only has to do some simple action, you need only specify that one. For example: .PP .Vb 7 \& # spawn off a child process to do some simple processing \& run_fork { child { \& exec \*(Aq/bin/ls\*(Aq, \*(Aq\-l\*(Aq; \& die "Couldn\*(Aqt exec ls: $!\en"; \& } }; \& # Parent will continue execution from here \& # ... .Ve .PP If the code in any of the clauses does not die or exit, it will continue execution after the fork block. .SH "INTERFACE" .IX Header "INTERFACE" All of the following functions are exported by default: .SS "run_fork" .IX Subsection "run_fork" .Vb 1 \& run_fork { ... } .Ve .PP Performs the fork operation configured in its block. .SS "child" .IX Subsection "child" .Vb 1 \& child { ... } .Ve .PP Declares the block that should run in the child process. .SS "parent" .IX Subsection "parent" .Vb 1 \& parent { ... } .Ve .PP Declares the block that should run in the parent process. The child's \s-1PID\s0 is passed as an argument to the block. .SS "retry" .IX Subsection "retry" .Vb 1 \& retry { ... } .Ve .PP Declares the block that should run in case of an error, ie. if \f(CW\*(C`fork\*(C'\fR returned \f(CW\*(C`undef\*(C'\fR. If the code returns true, another \f(CW\*(C`fork\*(C'\fR is attempted. The number of fork attempts so far is passed as an argument to the block. .PP This can be used to implement a wait-and-retry logic that may be essential for some applications like daemons. .PP If a \f(CW\*(C`retry\*(C'\fR clause is not used, no retries will be attempted and a fork failure will immediately lead to the \f(CW\*(C`error\*(C'\fR clause being called. .SS "error" .IX Subsection "error" .Vb 1 \& error { ... } .Ve .PP Declares the block that should run if there was an error, ie when \f(CW\*(C`fork\*(C'\fR returns \f(CW\*(C`undef\*(C'\fR and the \f(CW\*(C`retry\*(C'\fR clause returns false. The number of forks attempted is passed as an argument to the block. .PP If an \f(CW\*(C`error\*(C'\fR clause is not used, errors will raise an exception using \f(CW\*(C`die\*(C'\fR. .SH "EXAMPLES" .IX Header "EXAMPLES" .SS "Simple example with \s-1IPC\s0 via pipe" .IX Subsection "Simple example with IPC via pipe" .Vb 2 \& use strict; \& use Proc::Fork; \& \& use IO::Pipe; \& my $p = IO::Pipe\->new; \& \& run_fork { \& parent { \& my $child = shift; \& $p\->reader; \& print while <$p>; \& waitpid $child,0; \& } \& child { \& $p\->writer; \& print $p "Line 1\en"; \& print $p "Line 2\en"; \& exit; \& } \& retry { \& if( $_[0] < 5 ) { \& sleep 1; \& return 1; \& } \& return 0; \& } \& error { \& die "That\*(Aqs all folks\en"; \& } \& }; .Ve .SS "Multi-child example" .IX Subsection "Multi-child example" .Vb 3 \& use strict; \& use Proc::Fork; \& use IO::Pipe; \& \& my $num_children = 5; # How many children we\*(Aqll create \& my @children; # Store connections to them \& $SIG{CHLD} = \*(AqIGNORE\*(Aq; # Don\*(Aqt worry about reaping zombies \& \& # Spawn off some children \& for my $num ( 1 .. $num_children ) { \& # Create a pipe for parent\-child communication \& my $pipe = IO::Pipe\->new; \& \& # Child simply echoes data it receives, until EOF \& run_fork { child { \& $pipe\->reader; \& my $data; \& while ( $data = <$pipe> ) { \& chomp $data; \& print STDERR "child $num: [$data]\en"; \& } \& exit; \& } }; \& \& # Parent here \& $pipe\->writer; \& push @children, $pipe; \& } \& \& # Send some data to the kids \& for ( 1 .. 20 ) { \& # pick a child at random \& my $num = int rand $num_children; \& my $child = $children[$num]; \& print $child "Hey there.\en"; \& } .Ve .SS "Daemon example" .IX Subsection "Daemon example" .Vb 3 \& use strict; \& use Proc::Fork; \& use POSIX; \& \& # One\-stop shopping: fork, die on error, parent process exits. \& run_fork { parent { exit } }; \& \& # Other daemon initialization activities. \& $SIG{INT} = $SIG{TERM} = $SIG{HUP} = $SIG{PIPE} = \e&some_signal_handler; \& POSIX::setsid() == \-1 and die "Cannot start a new session: $!\en"; \& close $_ for *STDIN, *STDOUT, *STDERR; \& \& # rest of daemon program follows .Ve .SS "Forking socket-based network server example" .IX Subsection "Forking socket-based network server example" .Vb 3 \& use strict; \& use IO::Socket::INET; \& use Proc::Fork; \& \& $SIG{CHLD} = \*(AqIGNORE\*(Aq; \& \& my $server = IO::Socket::INET\->new( \& LocalPort => 7111, \& Type => SOCK_STREAM, \& Reuse => 1, \& Listen => 10, \& ) or die "Couln\*(Aqt start server: $!\en"; \& \& my $client; \& while ($client = $server\->accept) { \& run_fork { child { \& # Service the socket \& sleep(10); \& print $client "Ooga! ", time % 1000, "\en"; \& exit; # child exits. Parent loops to accept another connection. \& } } \& } .Ve .SH "AUTHOR" .IX Header "AUTHOR" Aristotle Pagaltzis .PP Documentation by Eric J. Roode. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This documentation is copyright (c) 2002 by Eric J. Roode. .PP This software is copyright (c) 2018 by Aristotle Pagaltzis. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.