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
Views: Raw Message | Whole Thread | Download mbox | Resend email
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

Browse pgsql-hackers by date

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