Hi,
I am trying to write a de-multiplixer and need some help.
I need to read from N file descriptors in a non-blocking mode. My
main loop utilizes select() to register a bit map of descriptor
that I anticipate to read from and write the data out to a common
descriptor. Hence a de-mux.
Here is my code
#!/bin/perl
$|=1;
# Lets assume I have a hash of descriptor => filenames
# in fact I have a function that assembles such a hash by visiting
# a directory where files are. The hash would look like this
%Files = ( FD0 => /tmp/w1.out, FD1 => /tmp/w2.out );
# Now I assemble the ReadBitVector
$rin = $win = $ein = '';
while ( ($fd, $file) = each (%Files) )
{
open($fd, $file) || die $!;
vec($rin, fileno($fd), 1) = 1;
}
# In real life, this loop would be different, this is just for testing
while($Cnt++ < 5) {
if ( $found = select($rin, undef, undef, undef) )
{
while (($fd, $file) = each (%Files) )
{
if (vec($rin, fileno($fd), 1))
{
sysread($fd, $buf, 100);
syswrite(STDOUT, $buf);
}
}
}
}
I then write a couple of writers to randomly (with delay of 1-5 secs)
write something to those files. These writers are emulating my
producers. My program, the consumer should then read from these writers.
Sort of like named pipes.
I was under the impression that select() would block, until something
is ready. My experiment is showing that the outer loop is executing
as the select is not waiting at all. And I don't have anything in my
buffers where I read from the producers.
By the way, this would be a useful program. For sites that haven't
heard of syslogd(1). Which is where I am in. So it solves a after
the fact problem. And potentially other applications.
-------------------------------------------------------------------------
Medi Montaseri [EMAIL PROTECTED]
Unix Distributed Systems Engineer HTTP://www.CyberShell.com
CyberShell Engineering
-------------------------------------------------------------------------