Thread threads => Grundwissen (50 answers)
Opened by RPerl at 2007-01-21 18:07

renee
 2007-01-23 09:14
#73451 #73451
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Hier mal etwas mit fork:
Code: (dl )
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};
}
}
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/

View full thread threads => Grundwissen