Thanks Roland for the hint. I did further test and found that the sleep place at the end of slave process does not matter, it has to be placed precisely before master talks each time in terms of time. It appears as if the master and slave cannot talk at the same time, or some kind of chaos will happen. This is demonstrated with the code below, and commented therein. In this simple example, we know when the master talks and we can place a sleep there. But in real situation, we do not know when the master talks, and how long it will talk, we have to have a general mechanism of control. I thought the control is provided by IO::Pty, or otherwise why we should have a pair of master and slave terminals. If we have to provide the control, then we can just use master or slave, no? Thanks a lot.
#!/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 or $line == 7 ) { my $j = $line++; $master->print("$j\n"); } } wait(); } else { $master->close(); $master->make_slave_controlling_terminal(); $slave->set_raw(); $slave->print("1\n"); $slave->print("3\n"); sleep 1; $slave->print("5\n"); $slave->print("7\n"); sleep 2; $slave->print("9\n"); $slave->print("11\n"); # sleep 3; } __END__ (1) with $master->print(), and without sleeps: $master->print("$j\n"); # sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:27:53 2014 OUTPUT: 1 TIME: Wed Jul 9 11:27:53 2014 OUTPUT: 3 (2) Without $master->print(), and without sleeps: # $master->print("$j\n"); # sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 1 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 3 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 5 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 7 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 9 TIME: Wed Jul 9 11:31:42 2014 OUTPUT: 11 (3) "sleep 3" at the end makes no difference in terms of output. (4) with $master->print(), and with "sleep 1": $master->print("$j\n"); sleep 1; # sleep 2; # sleep 3; We have the output: TIME: Wed Jul 9 11:41:05 2014 OUTPUT: 1 TIME: Wed Jul 9 11:41:05 2014 OUTPUT: 3 TIME: Wed Jul 9 11:41:06 2014 OUTPUT: 5 TIME: Wed Jul 9 11:41:06 2014 OUTPUT: 7 (5) with $master->print(), and with "sleep 2": $master->print("$j\n"); # sleep 1; sleep 2; # sleep 3; TIME: Wed Jul 9 11:42:52 2014 OUTPUT: 1 TIME: Wed Jul 9 11:42:52 2014 OUTPUT: 3 TIME: Wed Jul 9 11:42:54 2014 OUTPUT: 9 TIME: Wed Jul 9 11:42:54 2014 OUTPUT: 11 (6) with $master->print(), and with both "sleep 1" and "sleep 2": $master->print("$j\n"); sleep 1; sleep 2; # sleep 3; TIME: Wed Jul 9 11:43:31 2014 OUTPUT: 1 TIME: Wed Jul 9 11:43:31 2014 OUTPUT: 3 TIME: Wed Jul 9 11:43:32 2014 OUTPUT: 5 TIME: Wed Jul 9 11:43:32 2014 OUTPUT: 7 TIME: Wed Jul 9 11:43:34 2014 OUTPUT: 9 TIME: Wed Jul 9 11:43:34 2014 OUTPUT: 11 Conclusion: When master talks, the slave has to wait, otherwise what is written to slave is lost.

In reply to Re^2: [IO::Pty] How to read from and write to slave? by puravida
in thread [IO::Pty] How to read from and write to slave? by puravida

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.