dear lazy web ... (UNIX pipes)

Dirk Koopman djk at tobit.co.uk
Mon Apr 21 10:46:08 BST 2008


Mark Overmeer wrote:
> * Greg McCarroll (greg at mccarroll.org.uk) [080419 14:40]:
>> now today i wondered about,
>>
>>     process_that_generates_output | grep something | head
>>
>> wondering if when head had enough output to make it happy it would
>> terminate and that would propogate to grep and the first process.
>>
>> and so i experimented timing the first process with and without head
>> as the final pipe, and in a very unscientific test it came out with
>> the same result.
> 
> The pipe is buffered in 4kB blocks by default.  Your process will
> terminate if 'head' got its 4 kB from grep (or EOD).  Probably, grep
> doesn't pass enough data through, such that the initial process has to
> do all its work before head decides to quit and kill the pipe-line.

Another thing that should be said is that processes can get SIGPIPE when 
writing to non-pipes that could (at a stretch) be considered pipelike. 
Specifically: sockets. And the same buffering rules (as above) apply, 
although they may stretch to 32KB on some systems before a signal appears.

This really only manifests itself on TCP sessions where the other end 
has gone away and is not sending ICMP to that effect (or a firewall or 
router is in the way dropping your packets). In the past, this was more 
common with dialup and/or adsl links that shared a large address pool 
and had a forced "reconnnect every xxx minutes" policy.

Dirk


More information about the london.pm mailing list