Skip site navigation (1) Skip section navigation (2)

Re: Proper use of select() parameter nfds?

From: Doug McNaught <doug(at)wireboard(dot)com>
To: Matthew Hagerty <mhagerty(at)voyager(dot)net>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Proper use of select() parameter nfds?
Date: 2001-07-06 13:44:04
Message-ID: m33d8a895n.fsf@belphigor.mcnaught.org (view raw or flat)
Thread:
Lists: pgsql-hackers
Matthew Hagerty <mhagerty(at)voyager(dot)net> writes:

>  From MAN:
> 
> The first nfds descriptors are checked in each set; i.e., the
> descriptors from 0 through nfds-1 in the descriptor sets are
> examined.
> 
> 
> 
> I take this to mean that each descriptor set contains n descriptors and I am
> interested in examining the first nfds descriptors referenced in my sets.  I
> also understood it to mean that nfds has absolutely nothing to do with the
> actual *value* of a descriptor, i.e. the value returned by fopen(), socket(),
> etc..  Is this correct thinking? 

No.  Unix always gives you the lowest available descriptor value
(unless you ask for a value explicitly with dup2(), which is rare).
Since by default stdin/out/err are 0,1,2, you will get new descriptors
starting at 3.  You keep track of the highest descriptor value that
you're interested in, and pass that value +1 to select().

The reason for this is that FD_SETSIZE is often large (1024 by defaukt
in glibc) and you save the system some work by telling select() how
much of each set it needs to scan.

> if (select(conn->sock + 1, &input_mask, &output_mask, &except_mask,
>      (struct timeval *) NULL) < 0)
> 
> 
> Is this improper use?  conn->sock is set like this:

As long as conn->sock is the highest descriptor value you have (last
descriptor opened) this looks right.

You might want to get hold of _Unix Network Programming, Vol 1_ by
Stevens if you're going to do a lot of this stuff.

-Doug
-- 
The rain man gave me two cures; he said jump right in,
The first was Texas medicine--the second was just railroad gin,
And like a fool I mixed them, and it strangled up my mind,
Now people just get uglier, and I got no sense of time...          --Dylan

In response to

pgsql-hackers by date

Next:From: Alex PilosovDate: 2001-07-06 13:45:22
Subject: Re: Proper use of select() parameter nfds?
Previous:From: Jan WieckDate: 2001-07-06 13:40:47
Subject: pgsql/src/backend/rewrite rewriteHandler.c

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group