puravida has asked for the wisdom of the Perl Monks concerning the following question:
Please look at the code below:
#!/usr/bin/env perl -w use strict; use IO::Pty; my $master = new IO::Pty; my $slave = $master->slave(); my $pid = fork(); die "Couldn't fork: $!" unless defined $pid; if ($pid) { $master->close_slave(); while ( !$master->eof ) { my $line = $master->getline; chomp $line; print "TIME: " . localtime() . " OUTPUT: " . $line . "\n"; if ( $line == 3 ) { $master->print("2\n"); $master->print("4\n"); $master->print("6\n"); } } wait(); } else { $master->close(); $master->make_slave_controlling_terminal(); $slave->print("1\n"); $slave->print("3\n"); $slave->print("5\n"); }
and the result is:
$ perl realtime5d.pl TIME: Thu Jul 3 21:05:10 2014 OUTPUT: 1 TIME: Thu Jul 3 21:05:10 2014 OUTPUT: 3 TIME: Thu Jul 3 21:05:10 2014 OUTPUT: 2 TIME: Thu Jul 3 21:05:10 2014 OUTPUT: 4 TIME: Thu Jul 3 21:05:10 2014 OUTPUT: 6
Question 1: I expect 1, 3, 2, 4, 6, 5, why 5 written to $slave is lost? If you look at the picture from here: http://en.wikipedia.org/wiki/Pseudo_terminal, the traffic is bidirectional from master <=> slave, no?
Question 2: How do I turn off the echo? i.e. I do not want to read what I wrote myself, 2, 4, 6 in this case.
Question 3: Above 5 line output was from Perl 5.16 on Mac OS X 10.9.3. I did not think it is OS dependent for this simple script, but when I run with Perl 5.8 on Linux kernel 2.6, I got the following:
$ perl realtime5d.pl TIME: Fri Jul 4 10:08:50 2014 OUTPUT: 1 TIME: Fri Jul 4 10:08:50 2014 OUTPUT: 3
It looks that the echo is turned off, but once I write to the master, the is no more reading from slave. Why?
Question 4: The other very similar script http://www.perlmonks.org/?node_id=1092227 produces the same behavior on Perl 5.16 on Mac OS X 10.9.3 and on Perl 5.8 on Linux kernel 2.6, what is the real difference that these two scripts that causes the different behavior?
Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: [IO::Pty] How to read from and write to slave?
by choroba (Cardinal) on Jul 04, 2014 at 06:40 UTC | |
|
Re: [IO::Pty] How to read from and write to slave?
by zentara (Cardinal) on Jul 04, 2014 at 11:14 UTC | |
|
Re: [IO::Pty] How to read from and write to slave?
by rgiersig (Initiate) on Jul 09, 2014 at 13:16 UTC | |
by puravida (Initiate) on Jul 09, 2014 at 16:02 UTC |