memory/file handle limitation?

This is a discussion on memory/file handle limitation? within the Linux Programming forums, part of the Platform Specific Boards category; Hello everyone, I am wondering how to get the memory/file handle limitation on Linux platform in a programming way. The ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    memory/file handle limitation?

    Hello everyone,


    I am wondering how to get the memory/file handle limitation on Linux platform in a programming way. The memory limitation I mean, what are the total number of memory on the system, and what are the total number of memory which is remaining (free). The file handle limitation I mean, what are the total number of file handles regulated by the system, and what are the total number of file handles remaining which I could use by my application.

    Any sample programs are appreciated.


    thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    getrlimit(2), sysctl(3), and ulimit(3) looks promising.

    P.S. this needs to be moved to the Linux board.
    Last edited by zx-1; 09-17-2006 at 04:29 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you zx-1,


    Quote Originally Posted by zx-1
    getrlimit(2), sysctl(3), and ulimit(3) looks promising.

    P.S. this needs to be moved to the Linux board.
    From the man page, I think I should use getrlimit. But I found that the parameters are too complex and I feel confused. Could you provide or help to find a simple sample, which could display the maximum number of used file handlers which current process can use and the number of free file handlers (I mean not the total number of file handlers in the system, but the number of file handlers which current process can use)?


    regards,
    George

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    rlimit lim = {0, 10}; // Limits the number of open files for this process to 10
    getrlimit(RLIMIT_NOFILE, &lim);

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    int main()
    {
            struct rlimit rlp;   
    
            getrlimit(RLIMIT_NOFILE, &rlp);        /* RLIMIT_NOFILE: Max. no. of open files */
    
            printf("This process may open a maximum of %u files.\n", rlp.rlim_max);
    
            return 0;
    }
    $ ./a.out
    This process may open a maximum of 5364 files.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you Desolation,


    Quote Originally Posted by Desolation
    Code:
    rlimit lim = {0, 10}; // Limits the number of open files for this process to 10
    getrlimit(RLIMIT_NOFILE, &lim);
    My purpose is not to set the limitation for the current process when dealing with file handlers, but to get the limitation information ...


    regards,
    George

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you zx-1,


    Quote Originally Posted by zx-1
    Code:
    #include <stdio.h>
    #include <sys/types.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    int main()
    {
            struct rlimit rlp;   
    
            getrlimit(RLIMIT_NOFILE, &rlp);        /* RLIMIT_NOFILE: Max. no. of open files */
    
            printf("This process may open a maximum of %u files.\n", rlp.rlim_max);
    
            return 0;
    }
    $ ./a.out
    This process may open a maximum of 5364 files.
    In your sample, you can only get the information of the limitation of the maximum number of file handlers the current process could utilize, but how to get the actual freed number of file handlers (means, total - used)?


    regards,
    George

  8. #8
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    There's got to be some syscall available, but I can't find any info on it anywhere.
    The closest thing I can think of would be system("fstat -p $$ | wc -l") and capturing its output somewhere to get the number of used file handles. But that's a very pessimal solution.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thank you zx-1,


    Quote Originally Posted by zx-1
    There's got to be some syscall available, but I can't find any info on it anywhere.
    The closest thing I can think of would be system("fstat -p $$ | wc -l") and capturing its output somewhere to get the number of used file handles. But that's a very pessimal solution.
    I am using Red Hat 9.0. There is no command called fstat on my bash. Is it a command that I need to install from 3rd party?


    regards,
    George

  10. #10
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Silly Linux userland. Make that system("cat /proc/sys/fs/file-nr | cut -f 1 -d' '") or system("lsof | wc -l").
    Last edited by zx-1; 09-21-2006 at 07:47 AM.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with setting text into LTEXT using Window Handle
    By jasperleeabc in forum Windows Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 05:23 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21