Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOPEN_MAX and _SC_OPEN_MAX

Tags:

c

On my system (Ubunut 13.10), value for FOPEN_MAX is 16, value for _POSIX_OPEN_MAX is 20, and value for _SC_OPEN_MAX (I got it through sysconf()) is 4096. I know that _POSIX_OPEN_MAX is the minimum value for OPEN_MAX defined by POSIX.1 standard. So here, the real value is _SC_OPEN_MAX. Also, value for FOPEN_MAX is defined by ISO C. Both of them call themselves to designate the same thing: maximum open files supported by a process.

Question: But why of the discrepancy between FOPEN_MAX and _SC_OPEN_MAX, and what should be the one that I rely on when I'm writing my C application?

Thanks in advance!

like image 846
artaxerxe Avatar asked Nov 15 '13 11:11

artaxerxe


2 Answers

The discrepancy is because one is dynamic, one is static. If you write software for Ubuntu systems, the numbers are telling you'll always be able to rely on 20 open files (POSIX here is guaranteeing more than plain C's 16 limit). So you could theoretically compile some stuff conditionally to do crazy stuff to work around a low limit, to make sure the software will work on all systems with the same headers.

The runtime limit, _SC_OPEN_MAX, is the actual fd limit. It might be lower on some systems, but not below 20 (for any POSIX OS).

Finally, OPEN_MAX is the OS-specific lower limit for _SC_OPEN_MAX (that is, _POSIX_OPEN_MAX is telling you that on any POSIX system, OPEN_MAX has to be at least 20). Linux will define OPEN_MAX to be something higher, so you can rely on having more fds available, and will prevent _SC_OPEN_MAX going below the actual OPEN_MAX.

like image 105
Nicholas Wilson Avatar answered Oct 28 '22 15:10

Nicholas Wilson


You got to remember that on POSIX systems a file descriptor doesn't actual have to be a file. It may also be a socket, a pipe or something else that can be used by e.g. read or write. So _SC_OPEN_MAX will return the max number of "open" descriptors you can have, not the max number of files.

Also, FOPEN_MAX is, as you note, specific to the C language and the C library, and is related to the fopen library call and not the lower open system call.

like image 41
Some programmer dude Avatar answered Oct 28 '22 15:10

Some programmer dude