下面介绍sysconf函数的使用,它允许应用程序在运行时获得系统的限制。
函数的原型为:
#includelong sysconf(int name); Returns value of limit specified by name, or –1 if limit is indeterminate or an error occurred
name参数是在unistd.h头文件中定义的一些_SC_*常量。如果传入的name参数是不合法的,sysconf()函数将返回-1。
示例代码如下:
[root@lavenliu syslim]# cat my_sysconf.c #include#include #include #include static void pr_sysconf(const char *msg, int name){ long lim; errno = 0; lim = sysconf(name); if (lim != -1) { printf("%s %ld\n", msg, lim); } else { if (errno == 0) { printf("%s (indeterminate)\n", msg); } else { perror("sysconf error"); exit(1); } }}int main(int argc, char *argv[]){ pr_sysconf("_SC_ARG_MAX: ", _SC_ARG_MAX); pr_sysconf("_SC_LOGIN_NAME_MAX: ", _SC_LOGIN_NAME_MAX); pr_sysconf("_SC_OPEN_MAX: ", _SC_OPEN_MAX); pr_sysconf("_SC_NGROUPS_MAX: ", _SC_NGROUPS_MAX); pr_sysconf("_SC_PAGESIZE: ", _SC_PAGESIZE); pr_sysconf("_SC_RTSIG_MAX: ", _SC_RTSIG_MAX); return 0;}
编译运行,
gcc -o my_sysconf my_sysconf.c./my_sysconf_SC_ARG_MAX: 2621440_SC_LOGIN_NAME_MAX: 256_SC_OPEN_MAX: 1024_SC_NGROUPS_MAX: 65536_SC_PAGESIZE: 4096_SC_RTSIG_MAX: 32
从命令行获取这些限制:
getconf variable-name [ pathname ]getconf ARG_MAX2621440getconf NAME_MAX /boot255