artaxerxe
artaxerxe

Reputation: 6411

FOPEN_MAX and _SC_OPEN_MAX

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!

Upvotes: 9

Views: 8035

Answers (2)

Some programmer dude
Some programmer dude

Reputation: 409256

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.

Upvotes: 6

Nicholas Wilson
Nicholas Wilson

Reputation: 9685

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.

Upvotes: 10

Related Questions