Re: open file descriptors

From: James Antill (james-netnews_at_and.org)
Date: 10/29/03

  • Next message: James Antill: "Re: recv not completely overwriting last string"
    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/
    

  • Next message: James Antill: "Re: recv not completely overwriting last string"

    Relevant Pages

    • Re: open file descriptors
      ... >and this sets the open file descriptor limit to 1024. ... Are you sure you really need so many simultaneous connections? ... you're forgetting to close sockets when they're no longer needed. ... They either set the limit very high, or they use multiple processes. ...
      (comp.unix.programmer)
    • Re: DRb connection error with more than 250+ DRb services
      ... > for a flaw in UNIX design that introduces possible DoS by exhausting ... > kernel memory/structures. ... means is that the system resources consumed by the file descriptor (which is ... resources consumed by an actual open file or network connection, ...
      (comp.lang.ruby)
    • Re: open fd management
      ... >> number of sockets this process has open. ... >> have an file descriptor associated with it or does it only get the file ... > performance as the number of entries increases. ... Memory is more manageable - I can readily detect my app's own ...
      (comp.unix.programmer)
    • Re: open fd management
      ... descriptors are a per process resource. ... process normally has its own private file descriptor table. ... and in some UNIX implementations are ... > number of sockets this process has open. ...
      (comp.unix.programmer)
    • Re: Using select() with multiple threads?
      ... a second thread to process the established connections (call it ... So when the listening thread accepts a new ... it will add a new file descriptor to the processing threads ... 'select' also selects on the pipe and does a dummy read when it gets a hit ...
      (comp.os.linux.development.apps)