The following example can help you.
The main process forks a number of children with logical
IDs between 0 and $np. The processes concurrently gain exclusive access to a file using a lock.
lhp@nereida:~/Lperl/src/perl_networking/ch2$ cat -n flock.pl
1 #!/usr/bin/perl -w
2 use strict;
3 use Fcntl qw(:DEFAULT :flock);
4 use POSIX qw(WNOHANG);
5 $| = 1;
6
7 local $SIG{CHLD} = sub {
8 while (my $kid = waitpid(-1, WNOHANG) > 0) {}
9 };
10
11 sub create_child {
12 my ($id, $task) = splice @_, 0, 2;
13 my $pid;
14
15 return $pid if $pid = fork();
16 die "Cannot fork $!" unless defined $pid;
17 $task->($id, @_); # do something
18 exit;
19 }
20
21 sub parfor {
22 my $LAST = shift;
23 my $task = shift;
24 my @pid;
25
26 $pid[0] = $$;
27 $pid[$_] = create_child($_, $task, @_) for 1..$LAST;
28 return @pid;
29 }
30
31 sub task {
32 my $id = shift;
33 my $fn = shift;
34
35 sleep(int(rand(2)));
36 open my $f, "+<$fn" or die "Can't open $fn. $!\n";
37 flock($f, 2);
38 seek $f, 0, 0;
39 my $num = <$f>;
40 warn "Process $id reads $num.\n";
41 seek $f, 0, 0;
42 my $s = $num+$id;
43 warn "Process $id writing $s.\n";
44 print $f $s;
45 close($f);
46 exit;
47 }
48
49 #main
50 my $np = shift || 3;
51 my $fn = shift || "sync.txt";
52 open my $f, "> $fn" or die "Can't open file $fn. $!\n";
53 print $f 0;
54 close($f);
55 &parfor($np, \&task, $fn);
56 do {} while wait > 0;
The answer to your question is in line 56
Hope it helps
Casiano |