Bidirectional Pipes
Hi guys, I'm trying to make a GUI for a text based program and I need to connect the stdout of one to the stdin of the other and vice-versa.
I am trying use two pipes as in this link http://unixwiz.net/techtips/remap-pipe-fds.html but it is not working. I think I am missing something about how pipes work. For instance why does this program block in the fscanf call? If I fork() and do one call in the parent and the other in the child it works fine. Code:
#include <stdio.h> |
If you are using the basic approach you showed, you need to add fflush(out); after the fprintf line.
There is internal buffering within the process, so what you wrote with fprintf just sits in a buffer until some later event. Meanwhile, you hang in fscanf. Quote:
But the fact that the fprintf in one was not followed by any stall prior to reaching some point (such as program exit) that forced a flush, is a significant difference. In your full design, I assume the each process writing to the pipe will then do other things that might stall. So at any place where a process might stall after writing to the pipe, if you need what was written to be available to the other process during that stall, you need to fflush it before the stall. |
Perfect!!
Thank You. |
Quote:
Typically, in your situation you are writing a GUI for an existing text based program and you aren't changing that text based program. In the text based program outputting '\n' to a terminal probably implies an fflush even if there is no explicit fflush there in the code. But if the output is redirected to something that isn't a terminal, the same code might not cause an fflush. I'm not sure what you need to do to make an interactive console program remain interactive when run through pipes rather than through a terminal. If you can change both programs, then I understand why you didn't need to ask this follow up question. |
Quote:
|
All times are GMT -5. The time now is 10:06 AM. |