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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |