Where is OPEN_MAX defined for Linux systems?

前端 未结 3 744
孤城傲影
孤城傲影 2021-02-09 08:33

OPEN_MAX is the constant that defines the maximum number of open files allowed for a single program.

According to Beginning Linux Programming 4th

3条回答
  •  爱一瞬间的悲伤
    2021-02-09 09:08

    For what it's worth, the 4th edition of Beginning Linux Programming was published in 2007; parts of it may be a bit out of date. (That's not a criticism of the book, which I haven't read.)

    It appears that OPEN_MAX is deprecated, at least on Linux systems. The reason appears to be that the maximum number of file that can be opened simultaneously is not fixed, so a macro that expands to an integer literal is not a good way to get that information.

    There's another macro FOPEN_MAX that should be similar; I can't think of a reason why OPEN_MAX and FOPEN_MAX, if they're both defined, should have different values. But FOPEN_MAX is mandated by the C language standard, so system's don't have the option of not defining it. The C standard says that FOPEN_MAX

    expands to an integer constant expression that is the minimum number of files that the implementation guarantees can be open simultaneously

    (If the word "minimum" is confusing, it's a guarantee that a program can open at least that many files at once.)

    If you want the current maximum number of files that can be opened, take a look at the sysconf() function; on my system, sysconf(_SC_OPEN_MAX) returns 1024. (The sysconf() man page refers to a symbol OPEN_MAX. This is not a count, but a value recognized by sysconf(). And it's not defined on my system.)

    I've searched for OPEN_MAX (word match, so excluding FOPEN_MAX) on my Ubuntu system, and found the following (these are obviously just brief excerpts):

    /usr/include/X11/Xos.h:

    # ifdef __GNU__
    #  define PATH_MAX 4096
    #  define MAXPATHLEN 4096
    #  define OPEN_MAX 256 /* We define a reasonable limit.  */
    # endif
    

    /usr/include/i386-linux-gnu/bits/local_lim.h:

    /* The kernel header pollutes the namespace with the NR_OPEN symbol
       and defines LINK_MAX although filesystems have different maxima.  A
       similar thing is true for OPEN_MAX: the limit can be changed at
       runtime and therefore the macro must not be defined.  Remove this
       after including the header if necessary.  */  
    #ifndef NR_OPEN
    # define __undef_NR_OPEN
    #endif
    #ifndef LINK_MAX
    # define __undef_LINK_MAX
    #endif
    #ifndef OPEN_MAX
    # define __undef_OPEN_MAX
    #endif
    #ifndef ARG_MAX
    # define __undef_ARG_MAX
    #endif
    

    /usr/include/i386-linux-gnu/bits/xopen_lim.h:

    /* We do not provide fixed values for 
    
       ARG_MAX      Maximum length of argument to the `exec' function
                    including environment data.
    
       ATEXIT_MAX   Maximum number of functions that may be registered
                    with `atexit'.
    
       CHILD_MAX    Maximum number of simultaneous processes per real
                    user ID. 
    
       OPEN_MAX     Maximum number of files that one process can have open
                    at anyone time.
    
       PAGESIZE
       PAGE_SIZE    Size of bytes of a page.
    
       PASS_MAX     Maximum number of significant bytes in a password.
    
       We only provide a fixed limit for
    
       IOV_MAX      Maximum number of `iovec' structures that one process has
                    available for use with `readv' or writev'.
    
       if this is indeed fixed by the underlying system.
    */
    

提交回复
热议问题