LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-19-2010, 01:51 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
pipe programming


Hi all,

We have seen and used a lot of piping, e.g. % A | B
in which the output of program A is fed to the input of program B.

If I am to write program B, do I simply read stdin? <I really doubt this>

Also, how does B know when the input is done and then it can finish up?

e.g. % ll | grep 'jerry'

grep does know when ll has finished. How?

Thanks.
 
Old 01-19-2010, 02:00 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,039

Rep: Reputation: 98
What happens, is that Program A runs (to completion) and writes it's output to stdout, the pipe passes that as input stdin to Program B.

The output is only passed to Program B once Program A has completed.
 
Old 01-19-2010, 02:08 AM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
> The output is only passed to Program B once Program A has completed.

Then if Prog A outputs a huge amount of data or if Prog A doesn't stop writing, the pipe will overflow. Or isn't it the case?

/* Simple Prog A */
#include <stdio.h>
#include <unistd.h>
int main()
{
int i = 0;
do {
printf( "i = %d\n", i );
i ++;
usleep(10000);
} while(1);
return 0;
}

At the command line

% progA | grep '0'

gives output from time to time though.

Last edited by Jerry Mcguire; 01-19-2010 at 02:16 AM.
 
Old 01-19-2010, 04:08 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Disillusionist View Post
What happens, is that Program A runs (to completion) and writes it's output to stdout, the pipe passes that as input stdin to Program B.

The output is only passed to Program B once Program A has completed.
No. That is not what happens. That is the difference between using pipes or writing to a temp file and later read that back with some other program.
 
Old 01-19-2010, 04:20 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by Jerry Mcguire View Post
We have seen and used a lot of piping, e.g. % A | B
in which the output of program A is fed to the input of program B.

If I am to write program B, do I simply read stdin? <I really doubt this>
Yes, just read stdin. Until the read-function you use indicates end-of-file (EOF).

Quote:
Originally Posted by Jerry Mcguire View Post
e.g. % ll | grep 'jerry'

grep does know when ll has finished. How?
ll closes his stdout, or if it doesn't close his stdout, the kernel will when ll exits. This makes the pipe send EOF to the reading end (grep), where to code should detect EOF and act accordingly.

Last edited by Hko; 01-19-2010 at 08:22 AM.
 
Old 01-19-2010, 10:04 PM   #6
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Thanks. It works well.
 
  


Reply



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
PING .... pipe 2 vs pipe 3 hanen03 Linux - Networking 1 04-10-2009 10:50 AM
pipe programming ? Ashok_mittal Linux - Newbie 3 12-12-2007 07:06 AM
pipe programming ? Ashok_mittal Linux - Kernel 1 12-12-2007 05:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:53 AM.

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