LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   perl - reading from a pipe (https://www.linuxquestions.org/questions/programming-9/perl-reading-from-a-pipe-914481/)

kdelover 11-20-2011 02:11 AM

perl - reading from a pipe
 
Hi Folks, I seem to be having a problem doing a simple IPC between a child and a parent process. The child process runs a command and writes the output into a pipe which the parent reads. My program seems to be getting into some sort of a blocking state when i do
Code:

@from_pipe = <READX>
,but works for
Code:

$from_pipe = <READX>
. I have this code as an example.Is my program suffering from buffering ?? Thanks !!
Code:

#!/usr/bin/perl
use strict;
use warnings;
use POSIX;
use IO::Handle;

#
pipe (READ,WRITE);
#
sub _Forked {
    my $ip = shift;
    die "Couldn't fork" unless defined (my $pid = fork());
    if ($pid == 0) {
        # CHILD
        my @ping = `ping $ip -w 2 -q | sed -n '\$p'`;
        close (READ);
        select WRITE;
        print "@ping\n";
        close (WRITE);
        #
        exit (0);
    }
    #
    return ($pid);
}
#
sub main {
    my $cpid = _Forked('4.2.2.2');

    my $dpid;
    do {
        $dpid  = POSIX::waitpid(-1,WUNTRACED);
        my @val = <READ>;
        print "@val\n";
        #
        my $cpid = _Forked('4.2.2.2');
   
    } until ($dpid < 0)
}
main();



All times are GMT -5. The time now is 06:00 PM.