Let's say I have such a C program:
...
puts("What is your name?");
scanf("%s", name);
puts("Thank You!");
...
This program asks you to type your name, accepts input, displays "Thank You!" message and exits.
I want to write a program which automatically writes a name to my C program, receives output and prints it. Following program in perl works fine:
use strict;
use IPC::Open3;
my $bin = './myniceprogram';
my $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, "$bin")
or die "open3() failed $!";
my $r;
print CHLD_IN "A"x10 . "\n";
$r = <CHLD_OUT>;
print "$r";
$r = <CHLD_OUT>;
print "$r";
waitpid $pid, 0;
It produces following output:
What is your name?
Thank You!
However, I would want to read the first line from my C program ("What is your name?") BEFORE writing to it. But if I change the order of read/writes in my perl program, it just hangs:
use strict;
use IPC::Open3;
my $bin = './myniceprogram';
my $pid = open3(\*CHLD_IN, \*CHLD_OUT, \*CHLD_ERR, "$bin")
or die "open3() failed $!";
my $r;
$r = <CHLD_OUT>;
print "$r";
print CHLD_IN "A"x10 . "\n";
$r = <CHLD_OUT>;
print "$r";
waitpid $pid, 0;
With strace I can see that it's stuck on read call:
[myuser@myhost mydir]$ strace perl test2.pl
...
close(6) = 0
close(8) = 0
read(5,
But why?