From owner-freebsd-hackers Fri Jul 6 7:39:45 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from ringworld.nanolink.com (ringworld.nanolink.com [195.24.48.13]) by hub.freebsd.org (Postfix) with SMTP id 9B3AD37B401 for ; Fri, 6 Jul 2001 07:39:38 -0700 (PDT) (envelope-from roam@orbitel.bg) Received: (qmail 947 invoked by uid 1000); 6 Jul 2001 14:44:03 -0000 Date: Fri, 6 Jul 2001 17:44:03 +0300 From: Peter Pentchev To: Matthew Hagerty Cc: freebsd-hackers@FreeBSD.ORG Subject: Re: Proper use of select() parameter nfds? Message-ID: <20010706174403.C700@ringworld.oblivion.bg> Mail-Followup-To: Matthew Hagerty , freebsd-hackers@FreeBSD.ORG References: <5.0.2.1.2.20010706095736.0233de30@pop.voyager.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <5.0.2.1.2.20010706095736.0233de30@pop.voyager.net>; from mhagerty@voyager.net on Fri, Jul 06, 2001 at 09:58:00AM -0400 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Fri, Jul 06, 2001 at 09:58:00AM -0400, Matthew Hagerty wrote: > Greetings, > > I am going over the use of select() for a server I'm writing and I > *thought* I understood the man page's description for the use of the first > parameter, nfds. > > 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? What got me > second-guessing myself was a use of select() that seems to indicate that > you have to make sure nfds is larger than the value of the largest > descriptor you want checked. Here is the select() from the questionable > code (I can provide the whole function if necessary, it's not very big): > > if (select(conn->sock + 1, &input_mask, &output_mask, &except_mask, > (struct timeval *) NULL) < 0) Actually, this is the correct use. nfds should be larger than the value of the largest fd in the set, no matter how many fd's there are. > Is this improper use? conn->sock is set like this: > > /* Open a socket */ > if ((conn->sock = socket(family, SOCK_STREAM, 0)) < 0) > > > Any clarification on how nfds should be set would be greatly appreciated. Maybe it would help if you thought of an fd set as of a bit array, with a '1' bit for each fd "in" the set, and a '0' for each fd not "in" the set. The nfds argument tells select(2) how far into the bit array to check for set bits. If you want to check fd's 0, 1 and 5, then nfds should be at least 6. For more information, I would suggest reading W. Richard Stevens' book "Unix Network Programming", vol. 1. G'luck, Peter -- You have, of course, just begun reading the sentence that you have just finished reading. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message