Jeff Abrahamson on 31 Oct 2006 20:18:04 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Basic unix pipe question


On Tue, Oct 31, 2006 at 03:00:32PM -0500, Ben Dugan wrote:
>   [29 lines, 105 words, 866 characters]  Top characters: _noiserl
> 
> 
> I'm confused by what is probably normal behavior:
> 
> 
> [~]# program1 | program2
> 
> 
> I don't get any output (on stdout) from program2 until program1
> finishes. (I'm trying this out with simple C programs; program1 does
> a printf once every second, while program2 just watches stdin using
> fscanf and copies its stdin to stdout.)
> 
> I've been googling and looking at some reference books I have, but I
> can't really figure out why this is. I can't coerce program2's
> output to happen sooner using flush, either.

The pipe is buffered.  So writes to the pipe by program1 don't
actually get written until enough is in the buffer.

In your sample program, add "fflush(stdout);" after each write.  It
should do what you expect.

The other way to do this is to use ioctl (I think) to set the file
descriptor to be unbuffered.

-- 
 Jeff

 Jeff Abrahamson  <http://jeff.purple.com/>          +1 215/837-2287
 GPG fingerprint: 1A1A BA95 D082 A558 A276  63C6 16BF 8C4C 0D1D AE4B

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug