Sergeyk has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl $file_name='config'; if ( ! open CISCOFILE, $file_name ) { die "Couldnt open router config file! ($!)"; } @cisco_list=<CISCOFILE>; use POSIX qw(:signal_h :errno_h :sys_wait_h); $SIG{CHLD} = \&REAPER; sub REAPER { my $pid; $pid = waitpid(-1, &WNOHANG); if ($pid == -1) { # no child waiting. Ignore it. } elsif (WIFEXITED($?)) { $exit_value = $? >> 8; $signal_num = $? & 127; $dumped_core = $? & 128; # print "$pid dead. exit_value=$exit_value, signal_num=$signal_n +um, dumped_core=$dumped_core\n"; $kids{"$pid"}="$pid dead. exit_value=$exit_value, signal_num=$ +signal_num, dumped_core=$dumped_core\n"; } else { print "false warn $pid.\n"; } $SIG{CHLD} = \&REAPER; } use IO::Handle; my ($reader, $writer); pipe $reader, $writer; $writer->autoflush(1); %kids=(); $SIG{INT} = sub { die "$$ dying\n" }; for (1 .. 10) { unless ($child = fork) { die "cannot fork: $!" unless defined $child; squabble( ); exit; } $kids{"$child"}="$child start \n"; } @key_arr=keys(%kids); foreach $string(@key_arr) { print $kids{"$string"}; } close $reader; foreach $string(@cisco_list) { print $writer "$string"; } close $writer; #-----Waiting for child processes---- $flag=0; while($flag==0){ print "\n--------------------\n"; sleep 5; $flag=1; @key_arr=keys(%kids); foreach $string(@key_arr) { if($kids{"$string"}=~/start/){$flag=0;} print $kids{"$string"}; } } #------Child process function------- sub squabble { close $writer; open(SUBINTFILE, ">","child $$.txt") or die "Can't open file f +or writing $!"; select SUBINTFILE; while ($line = <$reader>) { chomp($line); print "$line\n"; sleep 1; } close $reader; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Feeding processes through one pipe
by kennethk (Abbot) on May 07, 2012 at 15:14 UTC | |
|
Re: Feeding processes through one pipe
by locked_user sundialsvc4 (Abbot) on May 07, 2012 at 20:20 UTC | |
|
Re: Feeding processes through one pipe
by Anonymous Monk on May 07, 2012 at 16:09 UTC | |
|
Re: Feeding processes through one pipe
by seefurst (Initiate) on May 07, 2012 at 21:25 UTC | |
|
Re: Feeding processes through one pipe
by Sergeyk (Novice) on May 08, 2012 at 04:35 UTC |