help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is there a way to make sure 141 exit status is resulted when pipelin


From: Greg Wooledge
Subject: Re: Is there a way to make sure 141 exit status is resulted when pipeline is terminated prematurelly?
Date: Wed, 7 Oct 2020 07:45:57 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Wed, Oct 07, 2020 at 02:11:34AM +0100, Marco Ippolito wrote:
> yOn 06/10/2020 23:20, Peng Yu wrote:
> > $ seq 10 | head -n 3; declare -p PIPESTATUS

> > $ seq 10000000 | head -n 3; declare -p PIPESTATUS

> Buffers.
> 
> In your first example, seq is not being terminated by SIGPIPE whereas in
> your second example it is, so the PIPESTATUS is correct.

To expand on this, in the first command, the total output of the seq
command is only 21 bytes, which is smaller than the size of the pipe
buffer on every operating system.  So, the buffered output (since stdout
is not a terminal) is written all at once when the seq command exits.
At that point, the pipe is still up and "running", the reader is still
blocked, and there is no reason to expect a SIGPIPE for the writer.

In the second command, the full output is very large, much larger than
the pipe buffer size on any operating system.  The seq command's output
is buffered (because output isn't going to a terminal), so seq writes
the first buffer whenever it has enough output -- typically a few
kilobytes.  The pipe receives this chunk of output, and passes it along
to the reader, head.  head reads the first three lines of this buffered
chunk, writes those lines to stdout, and then terminates.  This closes
the pipe.

Meanwhile, seq is still running, and eventually produces a second buffer's
worth of output, and tries to write the second buffered chunk to the
pipe.  But the pipe is closed at this point, so seq receives a SIGPIPE.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]