Re: Proper use of select() parameter nfds?

Поиск
Список
Период
Сортировка
От Doug McNaught
Тема Re: Proper use of select() parameter nfds?
Дата
Msg-id m33d8a895n.fsf@belphigor.mcnaught.org
обсуждение исходный текст
Ответ на Proper use of select() parameter nfds?  (Matthew Hagerty <mhagerty@voyager.net>)
Список pgsql-hackers
Matthew Hagerty <mhagerty@voyager.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


В списке pgsql-hackers по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Proper use of select() parameter nfds?
Следующее
От: Zeugswetter Andreas SB
Дата:
Сообщение: AW: AW: Re: Backup and Recovery