Re: Clogged pipe?

From: Matthias Apitz <guru_at_unixarea.de>
Date: Tue, 04 Apr 2023 05:36:14 UTC
El día lunes, abril 03, 2023 a las 08:38:15 -0700, Pete escribió:

> 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.

I fired this up on Linux and used strace to look what the processes are
doing:

tail -f testfile | cat -n  | sed -u 's/^/X/'
X     1	11111111111
X     2	22222222222
X     3	33333333333
(hangs)

ps ax | egrep ' tail| cat| sed'
25014 pts/2    S+     0:00 tail -f testfile
25015 pts/2    S+     0:00 cat -n
25016 pts/2    S+     0:00 sed -u s/^/X/

srap53dxr1:/home/sisis/install # strace -p 25016
strace: Process 25016 attached
read(0,
^C
strace: Process 25016 detached <detached ...>

srap53dxr1:/home/sisis/install # strace -p 25015
strace: Process 25015 attached
read(0, 
^C
strace: Process 25015 detached <detached ...>

srap53dxr1:/home/sisis/install # strace -p 25014
strace: Process 25014 attached
select(5, [1 4], NULL, NULL, NULL

i.e. the sed(1) and the cat(1) are just reading STDIN waiting for
more input to appear. The problem is with the "tail -f"
The -f flag let the tail(1) after it has read all the lines of
"testfile" just waiting for the file cwto grow which
could happen if other processes would write to the file.

Nothing magic, wrong usage of -f here.

	matthias

-- 
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub

No euro for the war! Keinen Euro für den Krieg! ¡No un Euro por la guerra!