in reply to Malfunctioning select() call on a FIFO
One should not attempt to mix buffered I/O (like "read" or <FH>) with "select"
Most definitely. Data could be waiting in the buffer and select wouldn't know anything about it.
while (sysread(FIFO, $l, 255)) { $fifodata .= $l; }
Using while there defies the purpose of using select. If there's less than 256 bytes available, sysread will block the second time through the loop. You want:
my $rv = sysread(FIFO, $fifodata, 64*1024, length($fifodata));
(Big is good. It's not a problem if fewer bytes are available.)
The "ready for reading" half of your select loop should look something like:
my $rv = sysread(FIFO, $fifodata, 64*1024, length($fifodata)); if (!defined($rv)) { ... handle error ... } if (!$rv) { ... handle eof. There might be a partial message in $fifodata ... } while (...$fifodata contains a complete message...) { ...extract message from $fifodata... ...process message... }
For fixed length records, the bottom loop might look like
while (length($fifodata) > $MSG_SIZE) { my $msg = substr($fifodata, 0, $MSG_SIZE, ''); process_msg($msg); }
For variable length records, s/// is convenient
while ($fifodata =~ s/^([^\n]*\n)//) { my $msg = $1; process_msg($msg); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Malfunctioning select() call on a FIFO
by Llew_Llaw_Gyffes (Scribe) on Jan 06, 2010 at 18:55 UTC | |
by ikegami (Patriarch) on Jan 06, 2010 at 19:30 UTC | |
by Llew_Llaw_Gyffes (Scribe) on Jan 06, 2010 at 20:46 UTC | |
by ikegami (Patriarch) on Jan 06, 2010 at 21:46 UTC | |
by Llew_Llaw_Gyffes (Scribe) on Jan 06, 2010 at 21:52 UTC |