Date: Wed, 7 Apr 1999 16:51:30 -0600 (MDT) From: Chad David <davidc@colnta.acns.ab.ca> To: freebsd-hackers@freebsd.org Subject: Closing file descriptors with cc -pthread Message-ID: <Pine.BSF.4.05.9904071630230.15390-100000@colnta.acns.ab.ca>
next in thread | raw e-mail | index | archive | help
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 <unistd.h>
#include <stdio.h>
#include <errno.h>
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
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.05.9904071630230.15390-100000>
