Huge difference between _POSIX_OPEN_MAX and sysconf(_SC_OPEN_MAX).

On my Linux system there seems to be a massive difference between the value of _POSIX_OPEN_MAX and what sysconf(_SC_OPEN_MAX) returns and also what I'd expect from the table of examples of configuration limits from Advanced Programming In The UNIX Environment, 2nd Ed.

_POSIX_OPEN_MAX: 16
sysconf(_SC_OPEN_MAX): 65535

Advanced Programming In The UNIX Environment, 2nd Ed, 2005
Figure 2.14. Examples of configuration limits

OPEN_MAX:
FreeBSD 5.2.1: 1735
Linux 2.4.22: 1024
Mac OS X 10.3: 256
Solaris 9: 256

Doesn't the value of 65,535 returned by sysconf(_SC_OPEN_MAX) seem way too high?

#ifdef _POSIX_OPEN_MAX
        printf("_POSIX_OPEN_MAX: %d\n", _POSIX_OPEN_MAX);
#endif
#ifdef OPEN_MAX
    printf("OPEN_MAX: %d\n", OPEN_MAX);
#endif
#ifdef FOPEN_MAX
    printf("FOPEN_MAX: %d\n", FOPEN_MAX);
#endif

errno = 0;

long om = sysconf(_SC_OPEN_MAX);
printf("errno: %d\n", errno);
printf("_SC_OPEN_MAX: %ld\n", om);

long pcnm = pathconf("/home/gencon/up.zip", _PC_NAME_MAX);
printf("errno: %d\n", errno);
printf("_PC_NAME_MAX: %ld\n", pcnm);

long pcpm = pathconf("/home/gencon/", _PC_PATH_MAX);
printf("errno: %d\n", errno);
printf("_PC_PATH_MAX: %ld\n", pcpm);

Outputs:
_POSIX_OPEN_MAX: 16
FOPEN_MAX: 16
errno: 0
_SC_OPEN_MAX: 65535
errno: 0
_PC_NAME_MAX: 255
errno: 0
_PC_PATH_MAX: 4096

No. It is controlled by the kernel and the values in limits.h provided by the people who built the kernel. On some systems it may be dynamic, others it is fixed.
You may also have _POSIX2_ values for things. See /usr/include/limits.h

FWIW -
I have an old version of solaris with _POSIX_OPEN_MAX = 16

PS: you don't have to write C code to get those values - try the getconf command.

Thanks Jim and nice hint to use getconf.

The first thing I tried was: getconf _POSIX_OPEN_MAX

and got: 65535

How come in C code when I print the value of _POSIX_OPEN_MAX I get 16 but from bash with getconf I get 65535?

Confused. :confused:

You have to set your #define statements to invoke what you want.
Possible examples:

#define _POSIX_C_SOURCE
#define _POSIX_SOURCE
#define _XOPEN_SOURCE 600
.........

You cannot invoke all these all at once. You really need to read up on your compiler, then REALLY read /usr/limits.h very carefully. Take notes.

On the forums, follow the standards link here, download the big ol' pdf.
UNIX IEEE Std 1003.1-2001 (POSIX.1) - The UNIX and Linux Forums

There are updates to this - I think they still show up in that link.

IEEE Std 1003.1-2001 is no longer current. It was replaced by IEEE Std 1003.1-2008. Here is a link to this standard.

If you want to access this standard via your Firefox searchbar you can download an addon from https://addons.mozilla.org/en-US/firefox/addon/75095

Yeah, this is indeed somewhat confusing.

The _POSIX_OPEN_MAX that you print from you program is the limits as defined by the POSIX standard (as of IEEE 1003.1-2008, it's 20). This limit states the minimal number of files that a process can have opened at any time. That is, on a POSIX platform, it is guarantee that any process can have at least up to _POSIX_OPEN_MAX files opened.

Typically, systems like Linux support much more opened file per process that _POSIX_OPEN_MAX.

Now, the value you get with getconf _POSIX_OPEN_MAX is in fact the value obtained with sysconf(_SC_OPEN_MAX). That is, the max. number of files a process can have for the user executing the program (also, in your case what ulimit -n would return).

HTH,
Lo�c.