I'm trying to write a client/server program with threads. I close the socket once the connexion is finished. The servers gets plenty of new connexions, and the socket 开发者_开发百科number (file descriptor) increases very quickly: after 5 minutes running I was already at around file descriptor number 800!
Is this a normal thing?
Do threads share file descriptors?
When I do close(sockfd);
is the number released immediatly or after a some time?
PS: I used to do with fork(), and I didn't have this issue. Thanks
From pthreads(7)
:
POSIX.1 also requires that threads share a range of other attributes (i.e., these attributes are process-wide rather than per-thread):
- open file descriptors
file descriptors are shared among all the threads, so closing it in one thread closes it for all the other threads. close() releases the fd when the call returns (unless an error occurs)
Note that close can return an error though:
Not checking the return value of close is a common but nevertheless serious programming error. It is quite possible that errors on a previous write(2) operation are first reported at the final close. Not checking the return value when closing the file may lead to silent loss of data. This can especially be observed with NFS and disk quotas.
Check for other file descriptor uses than your sockets, maybe you're leaking fds elsewhere - e.g. if you're opening normal files
精彩评论