Re: Perl Quiz of the Week #23

Xavier Noria <[email protected]>
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
On Sep 3, 2004, at 15:43, Mark Jason Dominus wrote:

>> I knew head(1) just showed the first 10 lines by default, but I 
>> assumed
>> the rest were ignored but printed no matter what by the script, since
>> there's nothing about the pipeline in the code.
>
> What's happening here is that 'head' exits after printing the first
> ten lines, and this closes the pipe.  balanced_gen continues running,
> writing data into the pipe, until the pipe becomes full.  When it
> tries to write to a full pipe, Unix sends it a PIPE signal, which
> kills the balanced_gen process.
>
> So it probably writes about ten lines plus 8192 bytes, more or less,
> and then gets prematurely terminated.

I see.

> Some Unix shells will display the message "Broken pipe" when a process
> is killed by a PIPE signal, analogous to the "Segmentation fault" or
> "Killed" messages that are displayed when processes are killed by SEGV
> or KILL signals, but PIPE happens so often and it's such a normal
> occurrence that many shells suppress the message.

This is zsh, I remember that "Broken pipe" message from other shells.

> You can see the
> abnormal termination with
>
>         (perl balanced_gen 12; echo $? >/dev/tty) | head
>
> which should print '141'.  This is 128 (indicating that the process
> was killed via a signal) plus 13 (the signal number arbitrarily
> assigned to PIPE).  The 13 may vary on your system, but whatever
> number you get should match the output of 'kill -l'.

Exactly:

% (perl balanced_gen.pl 12; echo $? >/dev/tty) | head
(((((((((((())))))))))))
((((((((((()()))))))))))
((((((((((())())))))))))
((((((((((()))()))))))))
((((((((((())))())))))))
((((((((((()))))()))))))
((((((((((())))))())))))
((((((((((()))))))()))))
((((((((((())))))))())))
((((((((((()))))))))()))
141

Now everything makes sense, thank you very much Mark!

-- fxn
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.