From owner-freebsd-hackers Wed Apr 7 15:55:42 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from colnta.acns.ab.ca (clgr000532.hs.telusplanet.net [161.184.82.24]) by hub.freebsd.org (Postfix) with ESMTP id 0F4F114FE7 for ; Wed, 7 Apr 1999 15:55:39 -0700 (PDT) (envelope-from davidc@colnta.acns.ab.ca) Received: from localhost (davidc@localhost) by colnta.acns.ab.ca (8.9.2/8.9.1) with ESMTP id QAA15452 for ; Wed, 7 Apr 1999 16:51:30 -0600 (MDT) (envelope-from davidc@colnta.acns.ab.ca) Date: Wed, 7 Apr 1999 16:51:30 -0600 (MDT) From: Chad David To: freebsd-hackers@freebsd.org Subject: Closing file descriptors with cc -pthread Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG I have been porting a threaded server from Solaris to FreeBSD, and I noticed that on FreeBSD the server uses 100% of the processor even when it is "idle". top shows it climb to the top of the list and eventually take up 99.8% on a fairly idle system. truss shows that the process is calling select() and gettimeofday() over and over... I tracked the problem to an init_daemon() call (much like daemon(), only a little more complete), and within that I found it was a loop that closed all open descriptors and dup2()'d 0,1,2 -> /dev/null that was causing the problem. In a seperate test I was able to duplicate the problem, but I am not sure what I am seeing or why. Is this a bug, or am I missing something? Also, why are descriptors 3 and 4 open when the program starts? --CODE-- /* testfd.c */ #include #include #include extern int errno; int main() { int fd; int nfile=256; int i; for (i = nfile; i >= 0; i--) { printf("[%d]",i); if (i == 3 || i == 4) { printf(" ??\n"); close(i); continue; } if (i == STDOUT_FILENO) { printf(" stdout\n"); continue; } if (close(i) != 0) printf(" error [%d]\n",errno); else printf(" ok\n"); } sleep(100); } If this code is compiled with cc -o testfd testfd.c it will run as expected. If it is compiled with cc -pthread -o testfd testfd.c then it seems to loop out of control. If I do not close 3 and 4 the code will run normally both with -pthread and without. Again, what are they for? Not closing all open descriptors, and dup2()'ing 0,1, and 2 like daemon() does solves my problem, but I do not understand why. Any information would be appreciated. Thanks. Chad David ACNS Inc. To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message