I'm trying to transfer data to child processes through single pipe. One parent, one pipe, multiple processes. Now this code has the following problems: 1) The parent immediately writes to the pipe and goes to the expectation of completing child processes. 2) Child processes immediately begin reading from this pipe, some of them immediately get EOF and die. therefore, processes get different load I decided that there is need to use semaphores Who can tell me how I can implement semaphores here or tell an alternative solution
#!/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; }

In reply to Feeding processes through one pipe by Sergeyk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.