Hier mal etwas mit fork:
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
#! /usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use POSIX ":sys_wait_h";
GetOptions('-i=s' => \my $infile);
my ($counter,$index,@lines) = (1,0);
open my $fh,"<",$infile or die "Can't open $infile: $!";
while(my $line = <$fh>){
push @{$lines[$index]},$line;
$index++ if $counter++ % 10 == 0;
}
fork_it(\@lines);
#------------------------------------------#
# Subroutines #
#------------------------------------------#
sub do_something_with_lines{
my ($lines,$nr) = @_;
print $nr,": ",$_ for @$lines;
}
sub fork_it{
my ($linesref) = @_;
my %pids;
for (1..scalar(@$linesref)){
my $pid=fork();
if($pid==-1){
warn($!);
last;
}
if($pid){
$pids{$pid}=1;
}
else{
do_something_with_lines($linesref->[$_-1],$_-1);
exit(0);
}
}
while(keys %pids){
my $pid=waitpid( -1, WNOHANG );
die "$!" if $pid == -1;
delete $pids{$pid};
}
}