moderated >> "Dup signals" in perlipc?
by Martin Becker » Tue, 06 May 2008 15:28:14 GMT
lya Zakharevich < XXXX@XXXXX.COM > wrote:
Certainly not. In our scenario the reader would not have
closed the file, so there is no re-opening magic involved.
I do have to correct my statement, however, in that sleeping
will not cater for additional reading attempts after eof,
but rather help that eof is noticed at all. I'll elaborate
presently.
On system call level, end of file is notified through a read
operation returning a byte count of zero. Subsequent reads
may or may not get the same result, especially if the file is
actually a tty or a fifo with no physical end.
Closing the (last remaining) writing connection is a way of
telling "there is nothing more to read right now", which allows
an ongoing read operation to finish, even when no bytes have been
read so far, which is precisely what is needed to detect eof.
If, however, another writing connection is established and
firing away before the reading process gets a time slice from the
scheduler, the "nothing to read" condition will pass unnoticed.
The pipe will not be empty and disconnected anymore by the time
it is finally looked at.
A sleep, even for a short time, changes the process status to
"not ready" and thus greatly improves the chance of "ready"
processes, like the one with the pending read operation, to
be resumed first. Of course that is not guaranteed to work,
but seems good enough in practice.
Yes there was a stat call in the code part you quoted in
your original posting, in the guise of a "-p" file test:
Ilya> unless (-p $FIFO) {
Ilya> [...]
Ilya> }
Fishy indeed, as in "one man's shiny, slim and streamlined creature,
roaming the oceans with swift and elegant motions, is another man's
smelly thing the cat found in a dustbin".
The amount of two seconds might have been chosen because with
an accuracy of one second the actual sleeping time for sleep(1)
could be next to nothing in the "worst" case.
To sum it up, our short delay is supposed to keep the fifo long
enough in a "nothing to read" state for the current reader to
notice.
Admittedly, this is not a particularly nice example. As one
of the communicating parties is not supposed to be aware of
an active communication going on at all, there are not much
options for the protocol to deal with all sorts of special cases.
For a moment, I toyed with the idea of unlinking the fifo and
creating a new one each time through the loop, to deal with the
possibility of one reader getting more than one signature.
However, that would put the reader in danger of getting
blocked indefinitely (reading from a fifo no longer accessible
to anyone else), which would be far worse than the occasional
matter-of-seconds delay.
-Martin