2

I wonder what happens if stdout buffer will be overflowed?

For example: app 1 launches the app 2, writes N lines to its stdin and THEN reads its stdout. Second app is just rewrites all it's got from stdin to stdout. At the moment app 1 finishes writing of N lines to app 2 and switches to reading its stdout the app 2 has already finished printing out lines to stdout. And this data is in its stdout's buffer. With increase of N we can overflow buffer.

What happens then? Will app 2 crash or its process blocks? If it's a crash what will be error code (linux)?

EDIT: Some code http://pastebin.com/msMRdxGR
I'm getting SIGPIPE error. (and used wrong labels - app1 is app2 and vice versa).

Sorry for not asking from beginning, but is there a way to avoid this error? If app2 uses 2 threads - one for reading and other for writing. And internal dynamically allocated buffer in heap to exchange data between them. Then I could just suspend writing thread from reading thread if noone reads my stdout. But how can I detect that stdout buf will be soon overflowed?

truf
  • 2,843
  • 26
  • 39
  • What, write to `stdin` and read from `stdout`? How would that work? – Some programmer dude Dec 11 '15 at 10:07
  • 1
    What buffer are you talking about exactly? Do you mean the C++ stdio/stream buffer? Or do you mean the actual OS device buffer? – David Schwartz Dec 11 '15 at 10:08
  • Assuming you are talking about using a bidirectional pipe (or pair of pipes working in each direction) to transfer data to and from the same application, you would have to ensure that you don't deadlock - the app will block in the OS when it can't write more, if the "other end of the pipe" isn't reading, so it is possible to come up with a scenario where both apps are waiting, if you are not careful. – Mats Petersson Dec 11 '15 at 10:12
  • @JoachimPileborg I mean writing to 2nd app's stdin and reading 2nd app stdout. I'm using Qt and QProcess for that. – truf Dec 11 '15 at 10:17
  • @DavidSchwartz OS device buffer. – truf Dec 11 '15 at 10:18
  • @truf Show us your code if you have any, because depending on how you create your `QProcess` and make them communicate, the answer won't be the same. – Holt Dec 11 '15 at 10:27
  • @Holt I have one big app that uses signals and slots. And in described case app2 crashes and app1's slot just never called. In more simple example: http://pastebin.com/msMRdxGR I'm getting SIGPIPE – truf Dec 11 '15 at 10:33
  • 1
    @truf You should add this to your question. See [this question](http://stackoverflow.com/questions/8369506/why-does-sigpipe-exist) which has information about SIGPIPE. Basically, depending on how the PIPE between the two processes is created, writing on a full buffer may fail instead of simply block (`O_NON_BLOCK`). – Holt Dec 11 '15 at 10:40
  • @Holt Good reference. I've find out that I can ignore this segfault with this piece of code http://pastebin.com/nHJ1CjiD or install my own handler to suspend writing data and wait when reader unblocks. Or do whatever else. Could you post this as answer? – truf Dec 11 '15 at 11:07

1 Answers1

4

The process will block if the OS device buffer is full.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278