Thursday, April 21, 2011

Does STDIN/STDOUT flush data all at once or character by character?

I'm using C# to read another program's STDOUT. If I do this:

StreamReader reader = process.StandardOutput;
reader.ReadToEnd();

Is it guaranteed to get the last thing flushed out to the program's STDOUT in its entirety? Or is kind of like TCP where I'd have to have a message terminator or length header?

From stackoverflow
  • by default StandardOutput is buffered, which means that you would likely get whole messages from the other end (or multiple whole messages). But its not really guaranteed, especially because process you are reading from could have changed the buffering of StandardOutput.

    A message terminator would be the best way to figure it out. though usually with processes communicating over StandardOutput everything is line based so simply using newlines as message terminators is probably the simplest and most common place to start.

  • reader.ReadToEnd() doesn't return until the process terminates, so after that call you should see everything that it wrote to stdout. Buffering would only affect how quickly it gets from the other program into your reader's buffer, but your code can't tell the difference (at least on this thread) because it's still waiting for ReadToEnd() to return.

0 comments:

Post a Comment