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
| #!/usr/bin/perl
use strict;
use warnings;
use IO::Select;
use IO::Handle;
use IPC::Run qw( run start );
$| = 1;
sub child {
$| = 1;
# $$ est le pid du processus courant
print( "$$ : debut\n" );
sleep int(rand(10));
print( "$$ : fin\n" );
close STDOUT;
sleep 2;
exit int(rand(10));
}
my $sel = IO::Select->new();
my %ipcrs;
foreach my $i ( 1 .. 8 ) {
my $h = IO::Handle->new();
# demarre un nouveau processus en arriere plan
$ipcrs{"$h"} = start( \&child, '>pipe', $h );
$sel->add( $h );
}
while( my @ready = $sel->can_read() ) {
foreach my $h ( @ready ) {
my $line = $h->getline();
if ( not defined $line ) {
my $kpid = $ipcrs{"$h"}->{KIDS}->[0]->{PID};
print( "fermeture du handle : $kpid\n" );
$sel->remove( $h );
$ipcrs{"$h"}->finish();
my $rt = $ipcrs{"$h"}->result;
print( "fin du processus : $kpid, retour : $rt\n" );
} else {
print $line;
}
}
} |
Partager