Where is OPEN_MAX defined for Linux systems?

OPEN_MAX is a constant that determines the maximum number of open files allowed for one program.

According to the start of Linux 4 th Edition programming, Page 101:

The limit, usually defined by the constant OPEN_MAX within .h, varies from system to system, ...

On my system, the limits.h file in the /usr/lib/gcc/x86_64-linux-gnu/4.6/include-fixed directory does not have this constant. Am I looking at the wrong limits.h or has the location OPEN_MAX since 2008?

+4
source share
3 answers

For what it's worth, in 2007 the 4th edition of Linux Programming Startup was published; parts of it may be a bit dated. (This is not criticism of a book that I have not read.)

OPEN_MAX seems to be deprecated, at least on Linux systems. The reason is that the maximum number of files that can be opened at the same time is not fixed, so a macro that expands to an integer literal is not a good way to get this information.

There will be another macro FOPEN_MAX , which should be similar; I cannot think of why OPEN_MAX and FOPEN_MAX , if both are defined, should have different meanings. But FOPEN_MAX defined by the C language standard, so the system does not have the ability not to define it. The C standard says FOPEN_MAX

expands to an integer constant expression, which is the minimum number of files that implementation guarantees can be opened at the same time

(If the word "minimum" is confusing, this ensures that the program can open at least several files at once.)

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

I searched for OPEN_MAX (word matching, excluding FOPEN_MAX ) on my Ubuntu system, and found the following (these are obviously just short 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. */ 
+4
source

In addition to the link given by cste, I would like to indicate that there is an entry /proc/sys/fs/file-max , which provides the number of files that the SYSTEM can open at any given time.

Here are some docs: https://access.redhat.com/knowledge/docs/en-US/Red_Hat_Directory_Server/8.2/html/Performance_Tuning_Guide/system-tuning.html

Please note that this does not mean that there is a WARRANTY that you can open, that many files - if a resource runs out in the system (for example, "there is no more memory"), then this can fail greatly.

FOPEN_MAX indicates that the C library allows you to open this many files (at least as discussed), but there are other limitations that may occur in the first place. For example, the SYSTEM limit is 4000 files, and in some already running applications 3990 files are open. Then you cannot open more than 7 files [since stdin, stdout and stderr also occupy three slots]. And if the rlimit parameter rlimit set to 5, you can open only 2 own files.

In my opinion, the best way to find out if you can open a file is to open it. If this fails, you must do something else. If you have some kind of process that should open MANY files [for example. multi-threaded search / comparison on a machine with 256 cores and 8 threads per core, and each thread uses three files (files "A", "B" and "diff")], then you may need to make sure that your FOPEN_MAX allows you to use 3 * 8 * 256 files open before starting to create streams, since a stream that cannot open its files will be meaningless. But for most ordinary applications, just try to open the file, if it doesn’t work, tell the user (log or something else) and / or try again ...

+3
source

I suggest using grep magic to find this constant on /usr/include :

 grep -rn --col OPEN_MAX /usr/include ... ... /usr/include/stdio.h:159: FOPEN_MAX Minimum number of files that can be open at once. ... ... 

Hope this helps you

+2
source

All Articles