From owner-freebsd-hackers Sat Nov 14 09:54:19 1998 Return-Path: Received: (from majordom@localhost) by hub.freebsd.org (8.8.8/8.8.8) id JAA02133 for freebsd-hackers-outgoing; Sat, 14 Nov 1998 09:54:19 -0800 (PST) (envelope-from owner-freebsd-hackers@FreeBSD.ORG) Received: from torrentnet.com (bacardi.torrentnet.com [198.78.51.104]) by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id JAA02126 for ; Sat, 14 Nov 1998 09:54:11 -0800 (PST) (envelope-from bakul@torrentnet.com) Received: from chai.torrentnet.com (chai.torrentnet.com [198.78.51.73]) by torrentnet.com (8.8.5/8.8.5) with ESMTP id MAA25261; Sat, 14 Nov 1998 12:53:44 -0500 (EST) Received: from chai.torrentnet.com (localhost [127.0.0.1]) by chai.torrentnet.com (8.8.8/8.8.5) with ESMTP id MAA25077; Sat, 14 Nov 1998 12:53:43 -0500 (EST) Message-Id: <199811141753.MAA25077@chai.torrentnet.com> To: Nate Williams Cc: hackers@FreeBSD.ORG Subject: Re: Supporting more than FD_SETSIZE fd's In-reply-to: Your message of "Wed, 11 Nov 1998 12:03:08 MST." <199811111903.MAA18067@mt.sri.com> Date: Sat, 14 Nov 1998 12:53:43 -0500 From: Bakul Shah Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > > The code fragment I showed is approx. right as I've used > > something similar a couple of times before but I will try to > > turn it into a working example over the weekend. > Thanks. If it included the MACROS so much the better. :) :) :) Check out http://www.bitblocks.com/src/select-test.c It even includes code to create and manipulate an FD set object! Note that when used on a kernel that has fixed FD_SETSIZE select() implementation, you must allocate exactly FD_SETSIZE elements (and certainly no less) in an FD set. BTW, instead of my FD_xxx functions you can use bitstring macros from /usr/include/bitstring.h. Sample use: In window 1: 1307% cc select-test.c 1308% ./a.out 1234 accepting on 1234, fd_setsize = 1024 3: accept from 127.0.0.1:28681 4: open 3: accept from 127.0.0.1:28937 5: open 4: Sat Nov 14 12:14:45 EST 1998 4: close 5: Sat Nov 14 12:14:46 EST 1998 5: close ^C 1309% In Window 2: 852% (sleep 3 ; date)| telnet 1234& 853% (sleep 3 ; date)| telnet 1234& 854% This is a sample program and hence it is missing a few obvious optimizations, error checking, as well as code to deal with timeouts, write and exception FD_sets etc. In a program for real use, you would likely put all fds in nonblocking mode; you would have multiple accept fds (one per interface); in do_accept() you would accept all outstanding connections; in do_read() you would read everything the other side sends (or not, depending on any fairness criterion); you would likely keep an array of functions, indexed by a fd, for a quick dispatch; and so on. For any moderately complex processing you may be better off using pthreads than a select loop (unless you want to squeeze out the very last drop of performance without descending to assembly language code). -- bakul To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message