File: Fork.pm

package info (click to toggle)
libproc-fork-perl 0.807-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 136 kB
  • sloc: perl: 165; makefile: 2
file content (280 lines) | stat: -rw-r--r-- 7,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use 5.006; use strict; use warnings;

package Proc::Fork;
our $VERSION = '0.807';

use Exporter::Tidy (
	default => [ ':all' ],
	wrapper => [ 'run_fork' ],
	blocks  => [ qw( parent child error retry ) ],
);

sub _croak { require Carp; goto &Carp::croak }

my $do_clear = 1;
my ( $parent, $child, $error, $retry );

sub run_fork(&) {
	my $setup = shift;

	my @r = $setup->();
	_croak "Garbage in Proc::Fork setup (semicolon after last block clause?)" if @r;
	$do_clear = 1;

	my $pid;
	my $i;

	{
		$pid = fork;
		last if defined $pid;
		redo if $retry and $retry->( ++$i );
		die "Cannot fork: $!\n" if not $error;
		$error->();
		return;
	}

	$_->( $pid || () ) for ( $pid ? $parent : $child ) || ();

	return;
}

for my $block ( qw( parent child error retry ) ) {
	my $code = q{sub _BLOCK_ (&;@) {
		$parent = $child = $error = $retry = $do_clear = undef if $do_clear;
		_croak "Duplicate _BLOCK_ clause in Proc::Fork setup" if $_BLOCK_;
		$_BLOCK_ = shift if 'CODE' eq ref $_[0];
		_croak "Garbage in Proc::Fork setup (after _BLOCK_ clause)" if @_;
		run_fork {} if not defined wantarray; # backcompat
		();
	}};
	$code =~ s/_BLOCK_/$block/g;
	eval $code;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Proc::Fork - simple, intuitive interface to the fork() system call

=head1 SYNOPSIS

 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)
     }
 };

=head1 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 "fork block". The clauses may appear in any order, but they must be consecutive (without any other statements in between).

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:

 # spawn off a child process to do some simple processing
 run_fork { child {
     exec '/bin/ls', '-l';
     die "Couldn't exec ls: $!\n";
 } };
 # Parent will continue execution from here
 # ...

If the code in any of the clauses does not die or exit, it will continue execution after the fork block.

=head1 INTERFACE

All of the following functions are exported by default:

=head2 run_fork

 run_fork { ... }

Performs the fork operation configured in its block.

=head2 child

 child { ... }

Declares the block that should run in the child process.

=head2 parent

 parent { ... }

Declares the block that should run in the parent process. The child's PID is passed as an argument to the block.

=head2 retry

 retry { ... }

Declares the block that should run in case of an error, ie. if C<fork> returned C<undef>. If the code returns true, another C<fork> is attempted. The number of fork attempts so far is passed as an argument to the block.

This can be used to implement a wait-and-retry logic that may be essential for some applications like daemons.

If a C<retry> clause is not used, no retries will be attempted and a fork failure will immediately lead to the C<error> clause being called.

=head2 error

 error { ... }

Declares the block that should run if there was an error, ie when C<fork> returns C<undef> and the C<retry> clause returns false. The number of forks attempted is passed as an argument to the block.

If an C<error> clause is not used, errors will raise an exception using C<die>.

=head1 EXAMPLES

The distribution includes the following examples as separate files in the F<eg/> directory:

=head2 Simple example with IPC via pipe

 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\n";
         print $p "Line 2\n";
         exit;
     }
     retry {
         if( $_[0] < 5 ) {
             sleep 1;
             return 1;
         }
         return 0;
     }
     error {
         die "That's all folks\n";
     }
 };

=head2 Multi-child example

 use strict;
 use Proc::Fork;
 use IO::Pipe;

 my $num_children = 5;    # How many children we'll create
 my @children;            # Store connections to them
 $SIG{CHLD} = 'IGNORE';   # Don't 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]\n";
         }
         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.\n";
 }

=head2 Daemon example

 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} = \&some_signal_handler;
 POSIX::setsid() == -1 and die "Cannot start a new session: $!\n";
 close $_ for *STDIN, *STDOUT, *STDERR;

 # rest of daemon program follows

=head2 Forking socket-based network server example

 use strict;
 use IO::Socket::INET;
 use Proc::Fork;

 $SIG{CHLD} = 'IGNORE';

 my $server = IO::Socket::INET->new(
     LocalPort => 7111,
     Type      => SOCK_STREAM,
     Reuse     => 1,
     Listen    => 10,
 ) or die "Couln't start server: $!\n";

 my $client;
 while ($client = $server->accept) {
     run_fork { child {
         # Service the socket
         sleep(10);
         print $client "Ooga! ", time % 1000, "\n";
         exit; # child exits. Parent loops to accept another connection.
     } }
 }

=head1 AUTHOR

Aristotle Pagaltzis <pagaltzis@gmx.de>

Documentation by Eric J. Roode.

=head1 COPYRIGHT AND LICENSE

This documentation is copyright (c) 2002 by Eric J. Roode.

This software is copyright (c) 2018 by Aristotle Pagaltzis.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut