Re: Perl Quiz of the Week #23
Mark Jason Dominus <[email protected]>
| Newsgroups | gmane.comp.lang.perl.qotw.discuss |
|---|---|
| Message-ID | <[email protected]> |
Xavier Noria <[email protected]>: > On Sep 3, 2004, at 14:24, colin.rafferty-/PgpppG8B+R7qynMiXIxWgC/[email protected] wrote: > > >> % time perl balanced_gen.pl 12 > >> perl balanced_gen.pl 12 4.38s user 0.03s system 83% cpu 5.255 total > > > > You are looking at the wrong number. In the one you're showing, I'm > > doing `head', so it's cut off after 10 results. You really should be > > looking at the results of the full run to see that we're in the same > > ballpark. > > 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. 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. 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'.