Hi, I have written a perl script in which the parent spawns a child process using perl Pipes

$pid = open(PARENT_FILE_HANDLE,"-|");

Child does some stuff like ssh to a remote machine and gets some info which it then prints to STDOUT. The parent then reads the information from the PARENT_FILE_HANDLE.

The problem is that when the child tries to print a lot of data to STDOUT, the child just hangs after printing a few lines.

To simulate this problem in a simpler way , I have written an equivalent script below in which the child reads data from a file "/tmp/testfile" which contains around 200 lines and then writes the lines to STDOUT which the parent process then tries to read from PARENT_FILE_HANDLE. But the child process just hangs after printing a few lines to STDOUT and the output from the code below shows that the parent process waits for 30 seconds and then kills the hung child process.

The script works fine only if /tmp/testfile has fewer than 50 lines.

Please let me know why this could be happening and how to counter this problem


#!/usr/bin/perl
#fork a process
$pid=open(PARENT_READ_HANDLE, "-|");
if ($pid==0) # child process
{
open (CHILDREADHANDLE, "</tmp/testfile");
while (<CHILDREADHANDLE>)
{
($sec,$min,$hour) = (localtime) 0,1,2;
print "$hour:$min:$sec - $_ \n";
}
close(CHILDREADHANDLE);
exit(0);
}
else #parent process
{
# thIs section checks and makes sure child process if Terminated
# if its hung for more than 30 seconds.
$count =0;
$childprocess = qx (ps -ef|grep -v defunct|grep -v grep |grep $pid);
while (($childprocess ne "") && ($count < 6))
{
$count+=1;
printf ("Found child process still running count=$count\n");
sleep (5);
$childprocess = qx (ps -ef|grep -v defunct|grep -v grep |grep $pid);
}
if ($childprocess ne "")
{
qx (kill -9 $pid);
printf (STDERR "Child ssh process hung. So forcibly killed it\n");
}
waitpid($pid,0);
# read the data printed by child
while (<PARENT_READ_HANDLE>)
{
printf ("Parent received from the child : $_\n");
}
}
exit(0);



-waavman

In reply to Issue with communication of large data between Parent and child using Perl pipes by waavman

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.