Re: open file descriptors
From: James Antill (james-netnews_at_and.org)
Date: 10/29/03
- Previous message: Michael Fuhr: "Re: Q: SIGALRM deferred during read() ?"
- In reply to: santoshjoseph73: "open file descriptors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 28 Oct 2003 20:45:06 -0500
On Mon, 27 Oct 2003 14:35:17 -0500, santoshjoseph73 wrote:
> I think this question has been posted before, but I don't think the
> answers were clear.
>
> The problem I have is I'm running a server on unix(solairs 2.7) and i'm
> running out of sockets. I use the setulimit function like this
>
> new_res.rlim_cur = res.rlim_max;
> new_res.rlim_max = res.rlim_max;
>
> if ( -1 == setrlimit(RLIMIT_NOFILE, &new_res) )
> return 0;
>
> and this sets the open file descriptor limit to 1024. The question what
Well it actually sets the upper limit on the number a file descriptor can
be, this can be different if you are using Eg. dup2().
> should I do when I get a socket error on a accept and the error is
> EMFILE (Too many open files).
>
> Should I sleep for a bit (1 second) and retry the accept? But what
> happened the connection that was being tried did it fail (so the client
> would get an error?)
Sleeping won't help. Assuming you have many connections and so need more
than 1024 sockets open at once ... then you can either change the max
limit as root or change a config file like /etc/security/limits.conf to
get a higher limit.
> And how can I avoid this type of situation? Can I find out how many
> open file descriptors are currently being used (in code) so I avoid
> accepting new connections?
Portably find out how many are used is not possible AFAIK, unportably you
can do things like look in the directory /proc/self/fd on Linux. You can
also look at all the places you open a file descriptor
socket/accept/open/dup/etc. and do some accounting, or even further you
can use some malloc() accounting type code to track where the file
descriptors came from.
> How do Unix web servers solve this problem?
They change the limit, or have multiple processes each using less than
the max.
-- James Antill -- james@and.org Need an efficient and powerful string library for C? http://www.and.org/vstr/
- Previous message: Michael Fuhr: "Re: Q: SIGALRM deferred during read() ?"
- In reply to: santoshjoseph73: "open file descriptors"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|