gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
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]); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Communicating with unflushed child process
by tilly (Archbishop) on Nov 12, 2004 at 19:20 UTC | |
|
Re: Communicating with unflushed child process
by PreferredUserName (Pilgrim) on Nov 12, 2004 at 19:42 UTC | |
|
Re: Communicating with unflushed child process
by insaniac (Friar) on Nov 12, 2004 at 20:03 UTC | |
|
Re: Communicating with unflushed child process
by zentara (Cardinal) on Nov 13, 2004 at 13:04 UTC |