Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Perl select function mystery

by macli (Beadle)
on Mar 27, 2007 at 23:17 UTC ( [id://606882]=perlquestion: print w/replies, xml ) Need Help??

macli has asked for the wisdom of the Perl Monks concerning the following question:

After reading perldoc -f select, I still can't get my head around the select function. especially this idom:

($nfound,$timeleft) = select($rout=$rin, $wout=$win, $eout=$ein,$timeout);

What is $rout=$rin, $wout=$win, $eout=$ein? and the return $nfound
Are they refering to default input, output, error output?
How select are used to manage multiple listening sockets?
I often see vec and fileno involved with select.

Anyone can exlain it in plain english to me, or maybe my english is not good engough to understand the perldoc :(

Replies are listed 'Best First'.
Re: Perl select function mystery
by Joost (Canon) on Mar 27, 2007 at 23:33 UTC
    The $rout = $rin stuff makes a copy of the read bits in $rin and passes that to select so that $rout is modified but $rin remains the same.

    $rout then contains a bitmask of the available file handles (selected from the bitmask in $rout $rin). $nfound is the total number of "ready" filehandles. $wout will contain file handles ready for writing and $eout will contain file handles that have errors.

    All this is done via bit masks. The bit number corresponds with the fileno of the handle. I.e. bit 0 (value 1) is for fileno 0, bit 1 (value 2) is for fileno 1 etc.

    At least that how I assume it works. Personally, I find IO::Select much clearer and easier to use. I suggest you take a look at that.

    update: fixed mistake - thanks rodion :-)

Re: Perl select function mystery
by duff (Parson) on Mar 28, 2007 at 01:25 UTC
Re: Perl select function mystery
by Fletch (Bishop) on Mar 27, 2007 at 23:43 UTC

    You may also want to look for documentation on the C / *NIX select(2) system call (such as your system's manual page), or a book like Stevens Advanced Programming in the UNIX Environment. Seeing the how the underlying system call is used from C may help clear things up (since it's pretty much the same in Perl, you just have to go through an extra step to get the file descriptor from a filehandle).

    But seconding the IO::Select recommendation, or possibly consider something like POE that provides an event driven framework and hides some of this nasty lower level interaction from you.

Re: Perl select function mystery
by rodion (Chaplain) on Mar 28, 2007 at 01:09 UTC
    How select are used to manage multiple listening sockets?
    When you need to wait for data coming in on either of two sockets, with file numbers 5 and 8, for example, you set bits 5 and 8 in $rin, and call select($rout=$rin,undef,undef). The select() will wait for data and return when either file number 5 or file number 8 has data to read. You will then need to know which one, and this is why you need $rout which select() will set for you. If bit 5 in $rout is set, then the socket with file number 5 has something to read. If bit 8 is set, then socket number 8. If both are set, then both sockets have something to read.

    This allows you to only check a socket when you know it's got something you want, and select will wait without chewing up a lot of CPU cycles.

    If you are writing code instead of reading it, I also second Joost's suggestion of IO::Select.

Re: Perl select function mystery
by macli (Beadle) on Mar 28, 2007 at 18:59 UTC

    Thank everyone for the clarification, duff's tutorial is quite helpful.

    I am acutally reading the code of Mail::SpamAssassin::SpamdForkScaling which spamd daemon use for children scaling.
    There is a method main_server_poll which communicates parent with childrens, it involves vec,fileno, select, timeout, error handling in that method. it is fun and pain to read it :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://606882]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-23 21:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found