LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bidirectional Pipes (https://www.linuxquestions.org/questions/linux-newbie-8/bidirectional-pipes-800188/)

Felipe Reigosa 04-05-2010 03:23 PM

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>
#include <unistd.h>

int main()
{
        int fd[2];
       
        pipe(fd);
       
        FILE *in = fdopen(fd[0], "r");
        FILE *out = fdopen(fd[1], "w");
       
        int i;
       
        fprintf(out, "21\n");       
        fscanf(in, "%d", &i);
               
        return 0;
}

Thanks in advance.

johnsfine 04-05-2010 04:29 PM

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:

Originally Posted by Felipe Reigosa (Post 3925258)
If I fork() and do one call in the parent and the other in the child it works fine.

The fact that one call was in the parent and the other in the child does not significantly change the situation.

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.

Felipe Reigosa 04-05-2010 04:40 PM

Perfect!!

Thank You.

johnsfine 04-05-2010 04:50 PM

Quote:

Originally Posted by Felipe Reigosa (Post 3925258)
I'm trying to make a GUI for a text based program

I was expecting the obvious followup question and unfortunately I don't recall the answer.

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.

Felipe Reigosa 04-05-2010 05:40 PM

Quote:

But if the output is redirected to something that isn't a terminal, the same code might not cause an fflush.
Hello johnsfine, you are quite right apparently. It doesn't flush and I don't know how solve it. However, in the meantime I just used the linux write sys_call and it seems to be working. I will try to find out on the web but if you do remember please let me know, and thanks again for all the help. And no, I cannot modify both programs, it just didn't occur to me this flush problem, I just started this project.


All times are GMT -5. The time now is 10:06 AM.