Re: Clogged pipe?

From: Pete <freebsd-questions-3_at_voidcaptain.com>
Date: Tue, 04 Apr 2023 03:38:15 UTC
Christian Weisgerber wrote on 4/3/23 16:01:
> Pete:
> 
>> On FreeBSD 13.1-RELEASE-p7 I have a small test file, named testfile,
>> which contains three short lines of text, "one," "two," and "three"
>> without the quotes.
>>
>> This command waits indefinitely without producing any output:
>>
>>     tail -f testfile | cat -n | sed -u 's/^/X/'
> 
> Stdio buffering.  See setbuf(3).
> 
> By default, output using stdio functions is buffered.  If it goes
> to a tty, it is line-buffered, i.e., output is saved up until a
> whole line terminated by '\n' is complete, then that line is written.
> Output to files or pipes is block-buffered.  Output is saved up
> until 8 kB or 16 kB or such, and only then is it written.  The
> buffer is also flushed on program exit.


Hi Christian,

So, I guess the short answer is that cat -n does block buffering to the 
pipe, but cat with no options does not.

   tail -f testfile | cat     | sed -u 's/^/X/'
   tail -f testfile | cat -u  | sed -u 's/^/X/'
   tail -f testfile | cat -nu | sed -u 's/^/X/'

all provide immediate output, but

   tail -f testfile | cat -n  | sed -u 's/^/X/'

does not and waits.

Seems slightly unintuitive that cat -n operates differently in this 
regard than cat with no parameters, but the behavior makes sense once 
that is understood.

Thank for a clear and complete explanation!

Pete