Fellow monks,

I have the following dilemma. I have a program that prints out a few lines of text and then prompts the user for some input. I was asked to automate execution of this program, so, I came up with this:

use strict; use Socket; use IO::Handle; my $childPid; socketpair(CHILD, PARENT, AF_UNIX, SOCK_STREAM, PF_UNSPEC); CHILD->autoflush(1); PARENT->autoflush(1); die unless defined($childPid=fork()); if ($childPid){ close PARENT; print "about toread\n"; while (<CHILD>){ print "got $_"; last if (/InputPrompt/); } print CHILD "MyInput\n"; waitpid($childPid) exit; } close CHILD; open(STDOUT, ">&PARENT") or die "Can't dup stdout: $!\n"; open(STDIN, "<&PARENT") or die "Can't dup stdin: $!\n"; exec "myProgram" or die "Can't start my program: $!\n";

This works beautifully when "myProgram" is something like 'ls', so I know the communication works. However, when I run my program, the parent never gets anything, and hangs! It's as though the output of "myProgram" is not flushed when it gets to the input part.

Is that what's actually going on? If it is, how can I force the parent to flush "myProgram"'s output?

Update: myProgram is actually something similar to the following C code.

#include <stdlib.h> #include <stdio.h> int main(int argc, char* argv[]) { char b[32]; printf("Hello there\n"); printf("InputPrompt:\n "); gets(b); printf("Thanks, %s (%s)\n", b, argv[0]); }

In reply to Communicating with unflushed child process by gri6507

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.