_POSIX_* (limits.h) 与 _SC_* (sysconf)

_POSIX_* (limits.h) vs _SC_* (sysconf)

我刚刚注意到 limits.h 中名为“_POSIX_*”的宏(名称上)类似于 的参数系统配置 功能。例如,有一个名为“_POSIX_ARG_MAX”的宏,我也可以用“_SC_ARG_MAX”的参数调用sysconf。当我们完全可以自由使用 limits.h 中的宏时,为什么我们首先需要 sysconf

_POSIX_* 值是符合 POSIX 的最低要求。它们在所有平台上都具有相同的价值。实现支持的特定值可能更高。

来自 man sysconf:

For variables or limits, typically, there is a constant _FOO, maybe defined in <limits.h>, or _POSIX_FOO, maybe defined in <unistd.h>. The constant will not be defined if the limit is unspecified. If the constant is defined, it gives a guaranteed value, and a greater value might actually be supported. If an application wants to take advantage of values which may change between systems, a call to sysconf() can be made. The sysconf() argument will be _SC_FOO.

例如_POSIX_ARG_MAX是4096。但是sysconf(_SC_ARG_MAX)可能return更大的数字,如果系统支持的话。