From owner-freebsd-net@FreeBSD.ORG Tue Jul 15 23:37:01 2008 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 3F8EA1065676; Tue, 15 Jul 2008 23:37:01 +0000 (UTC) (envelope-from Jinmei_Tatuya@isc.org) Received: from mon.jinmei.org (mon.jinmei.org [IPv6:2001:4f8:3:36::162]) by mx1.freebsd.org (Postfix) with ESMTP id 21AF98FC15; Tue, 15 Jul 2008 23:37:00 +0000 (UTC) (envelope-from Jinmei_Tatuya@isc.org) Received: from jmb.jinmei.org (unknown [IPv6:2001:4f8:3:bb:217:f2ff:fee0:a91f]) by mon.jinmei.org (Postfix) with ESMTP id 44AAF33C2E; Tue, 15 Jul 2008 16:37:00 -0700 (PDT) Date: Tue, 15 Jul 2008 16:37:00 -0700 Message-ID: From: JINMEI Tatuya / =?ISO-2022-JP?B?GyRCP0BMQEMjOkgbKEI=?= To: Bakul Shah In-Reply-To: <20080715230917.DAC3B5B46@mail.bitblocks.com> References: <20080715230917.DAC3B5B46@mail.bitblocks.com> User-Agent: Wanderlust/2.14.0 (Africa) Emacs/22.1 Mule/5.0 (SAKAKI) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Cc: freebsd-net@freebsd.org, Kris Kennaway , Thomas Vogt Subject: Re: too many open file descriptors messages since bind 9.4.2-P1 (port dns94) X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 15 Jul 2008 23:37:01 -0000 At Tue, 15 Jul 2008 16:09:17 -0700, Bakul Shah wrote: > IIRC, when poll() returns n, you only look at the first n > values in the pollfd array so it is a win when you expect a > very small number of fds to be ready. In the select case you > have to test the bit array until you see the last ready fd. % uname -a FreeBSD opt1.jinmei.org 7.0-RC1 FreeBSD 7.0-RC1 #0: Fri Jan 25 15:17:04 PST 2008 root@opt1.jinmei.org:/usr/src/sys/amd64/compile/GENERIC_NOSMP amd64 (please ignore "RC1":-) % cat polltest.c (omitted here, see below) % cc -o polltest polltest.c % ./polltest poll returned: 1 999th socket is ready (fd=1002) Perhaps You're probably confused poll(2) with /dev/poll. The latter behaves as you described (but is not portable as poll(2)). --- JINMEI, Tatuya Internet Systems Consortium, Inc. out put of polltest.c #include #include #include #include #include #include #include main() { int i, n; struct pollfd pfds[1000]; struct sockaddr_in sin; socklen_t sin_len; char buf[16]; memset(pfds, 0, sizeof(pfds)); memset(&sin, 0, sizeof(sin)); sin.sin_family = AF_INET; sin.sin_len = sizeof(sin); inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr); for (i = 0; i < 1000; i ++) { if ((pfds[i].fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { perror("socket"); exit(1); } if (bind(pfds[i].fd, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("bind"); exit(1); } pfds[i].events = POLLIN; } sin_len = sizeof(sin); if (getsockname(pfds[999].fd, (struct sockaddr *)&sin, &sin_len) < 0) { perror("getsockname"); exit(1); } if (sendto(pfds[999].fd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) { perror("sendto"); exit(1); } n = poll(pfds, 1000, -1); printf("poll returned: %d\n", n); for (i = 0; i < 1000; i++) { if ((pfds[i].revents & POLLIN) != 0) { printf("%dth socket is ready (fd=%d)\n", i, pfds[i].fd); } } exit(0); }