From nobody Tue Apr 04 03:38:15 2023 X-Original-To: questions@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PrD4X383mz43nXJ for ; Tue, 4 Apr 2023 03:38:20 +0000 (UTC) (envelope-from freebsd-questions-3@voidcaptain.com) Received: from mx3.mx00.net (mx3.mx00.net [IPv6:2600:3c01::f03c:91ff:fe89:a3f5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature ECDSA (P-256) client-digest SHA256) (Client CN "mx3.mx00.net", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PrD4X0vYyz49ml for ; Tue, 4 Apr 2023 03:38:20 +0000 (UTC) (envelope-from freebsd-questions-3@voidcaptain.com) Authentication-Results: mx1.freebsd.org; none Received: from razz.mx00.net [2600:3c01::f03c:91ff:fed5:a231] by mx3.mx00.net with ESMTP id 20230329-1pjXUv-0002VB-1j; Tue, 04 Apr 2023 03:38:17 +0000 Message-ID: Date: Mon, 3 Apr 2023 20:38:15 -0700 List-Id: User questions List-Archive: https://lists.freebsd.org/archives/freebsd-questions List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org MIME-Version: 1.0 Subject: Re: Clogged pipe? Content-Language: en-US To: Christian Weisgerber Cc: questions@freebsd.org References: From: Pete In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 4PrD4X0vYyz49ml X-Spamd-Bar: ---- X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:63949, ipnet:2600:3c01::/32, country:SG] X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-ThisMailContainsUnwantedMimeParts: N 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