LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-05-2010, 03:23 PM   #1
Felipe Reigosa
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Rep: Reputation: 0
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.
 
Old 04-05-2010, 04:29 PM   #2
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
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 View Post
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.

Last edited by johnsfine; 04-05-2010 at 04:37 PM.
 
1 members found this post helpful.
Old 04-05-2010, 04:40 PM   #3
Felipe Reigosa
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Perfect!!

Thank You.
 
Old 04-05-2010, 04:50 PM   #4
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by Felipe Reigosa View Post
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.

Last edited by johnsfine; 04-05-2010 at 04:52 PM.
 
Old 04-05-2010, 05:40 PM   #5
Felipe Reigosa
LQ Newbie
 
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
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.

Last edited by Felipe Reigosa; 04-05-2010 at 06:06 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bidirectional data transfer using socket pgmming? ssg14j Programming 1 10-24-2005 08:06 AM
Bidirectional data transfer ssg14j Linux - Networking 1 10-24-2005 07:59 AM
where do i get a bidirectional popen pipe? Thinking Programming 9 10-21-2005 07:42 AM
unsymetric bidirectional transfer Soulstealer Linux - General 3 10-21-2005 04:04 AM
bidirectional piping w/ gpg and php yocompia Programming 0 04-20-2004 08:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration