Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 3 Apr 2023 20:38:15 -0700
From:      Pete <freebsd-questions-3@voidcaptain.com>
To:        Christian Weisgerber <naddy@mips.inka.de>
Cc:        questions@freebsd.org
Subject:   Re: Clogged pipe?
Message-ID:  <d7565419-9dda-566c-5fc8-8815a43be3ca@slagle.net>
In-Reply-To: <ZCtavPuXQtYoV0TC@lorvorc.mips.inka.de>
References:  <f8a7a545-9731-7481-4fb2-bfc77b8ad6f0@slagle.net> <ZCtavPuXQtYoV0TC@lorvorc.mips.inka.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?d7565419-9dda-566c-5fc8-8815a43be3ca>