Re: Strange C-code behavior with pipes

2005-07-12 Thread Nadav Har'El
On Tue, Jul 12, 2005, Alon Altman wrote about Strange C-code behavior with 
pipes:
 When I compile this into a file called prog and then run: ./prog|cat I
 see that Hello is not printed until I give it input. Where is the buffer
 here and is there any way to bypass it?

Most people know, or at least assume, that stdio makes stderr unbuffered,
and stdout line buffered, i.e., whenever you print a line to stdout it
gets shown. Well, this is almost true. It is true if the standard output
is a terminal (see isatty(3)). However, if the standard output is *not*
a terminal - i.e., it is redirected to a file or a pipe - stdout becomes
fully buffered like any other file.

This is why your a.out showed it's output immediately, while a.out | cat,
or a.out  file, buffers its output.

This is actually explained in the setbuf(3) manual page, and this function
(and its relatives) is the one you can use to force line-buffered behavior
on your stdout always, regardless of where the output is going. You can
also use fflush(stdout) explicitly after the write, to make sure it gets
flushed.

-- 
Nadav Har'El|  Tuesday, Jul 12 2005, 5 Tammuz 5765
[EMAIL PROTECTED] |-
Phone +972-523-790466, ICQ 13349191 |Microchips: what's left at the bottom of
http://nadav.harel.org.il   |the bag when it reaches you.

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Strange C-code behavior with pipes

2005-07-11 Thread Peter


On Tue, 12 Jul 2005, Alon Altman wrote:


Hi,
 I have some strange behavior of a C program.
The code is simple:

#include stdio.h

int main(void)
{
  char input[255];
  printf(Hello\n);
  gets(input);
  return 0;
}

When I compile this into a file called prog and then run: ./prog|cat I
see that Hello is not printed until I give it input. Where is the buffer
here and is there any way to bypass it?


add fflush(stdout); after printf. The buffer is related to the stream 
stdout. You can turn it of using setvbuf() and friends. Don't do it if 
you don't have to. The pipes are set up by the shell and may defeat what 
you are trying to do with flush and setvbuf(). You can also use a fifo 
(mkfifo) instead of a pipe.


Peter

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]



Re: Strange C-code behavior with pipes

2005-07-11 Thread Alon Altman

On Tue, 12 Jul 2005, Peter wrote:
add fflush(stdout); after printf. The buffer is related to the stream stdout. 
You can turn it of using setvbuf() and friends. Don't do it if you don't have 
to. The pipes are set up by the shell and may defeat what you are trying to 
do with flush and setvbuf(). You can also use a fifo (mkfifo) instead of a 
pipe.


Thanks, that solved the problem.

  Alon

--
This message was sent by Alon Altman ([EMAIL PROTECTED]) ICQ:1366540
GPG public key at http://8ln.org/pubkey.txt
Key fingerprint = A670 6C81 19D3 3773 3627  DE14 B44A 50A3 FE06 7F24
--
 -=[ Random Fortune ]=-
I'm a nuclear submarine under the polar ice cap and I need a Kleenex!

=
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word unsubscribe in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]