I am using Open2 to run a C program that outputs a line with a timestamp in reply to each input line. The problem is that the reply lines are buffered together.

Desired results (happens when program is run from terminal or if C program calls fflush(stdout) after each printf):

Input1 time(): Output1 END time(): exit

Observed with Open2 and no fflush in the C program:

Input1 END time(): Output1 time(): exit

I believe the problem is that C program's libc tries to detect if stdout is connected to a terminal. If it is connected to a terminal it uses line buffering, otherwise it uses full buffering. How can I make the stdout of a program started with Open2 appear to be connected to a terminal?

I do not want to change the source of the C program. I am using IO::Select to see if there is any output as the actual program I want to use will only sometimes produce output.

I tried looking at the glibc-2.3.2 source to find how it decides what buffering to use on stdout, but could not work out how the line "./sysdeps/standalone/open.c: setvbuf( stdout, NULL, _IOLBF, BUFSIZ );" is called.

Here is a strace dump of running the C program with Open2:

read(0, "Input1\n", 4096) = 7 time(NULL) = 1066404185 read(0, "END\n", 4096) = 4 time(NULL) = 1066404190 write(1, "1066404185: \"Input1\"\n1066404190:"..., 38) = 38

Here is a run with the same inputs from the shell:

read(0, "Input1\n", 1024) = 7 time(NULL) = 1066404693 write(1, "1066404693: \"Input1\"\n", 21) = 21 read(0, "END\n", 1024) = 4 time(NULL) = 1066404697 write(1, "1066404697: exit\n", 17) = 17

Thank you for any ideas you may have to help me,
Tom


In reply to How to change Open2 input buffering by thecap

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.