关于系统限制C语言标准和SUSv3提供了两种方法:
1.在编译程序时能够获得一些限制和选项。
2.一些限制在程序运行时可能会发生变化。对此SUSv3定义了3个函数sysconf() pathconf()和fpathconf(),供应用程序调用以检查系统实现的限制和选项。
SUSv3将其规定的限制归为三类:
运行时恒定值 路径名变量值 运行时可增加值
在shell中,可以使用getconf命令获取特定UNIX系统中已然实现的限制和选项。例:
getconf ARG_MAX
getconf NAME_MAX /boot
在运行时获取系统限制
#include <unistd.h>
long sysconf(int name);
sysconf函数允许应用程序在运行时获得系统限制值。
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2015. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 11-1 */
/* t_sysconf.c
Demonstrate the use of sysconf() to retrieve system limits.
*/
#include "tlpi_hdr.h"
static void /* Print 'msg' plus sysconf() value for 'name' */
sysconfPrint(const char *msg, int name)
{
long lim;
errno = 0;
lim = sysconf(name);
if (lim != -1) { /* Call succeeded, limit determinate */
printf("%s %ld\n", msg, lim);
} else {
if (errno == 0) /* Call succeeded, limit indeterminate */
printf("%s (indeterminate)\n", msg);
else /* Call failed */
errExit("sysconf %s", msg);
}
}
int
main(int argc, char *argv[])
{
sysconfPrint("_SC_ARG_MAX: ", _SC_ARG_MAX);
sysconfPrint("_SC_LOGIN_NAME_MAX: ", _SC_LOGIN_NAME_MAX);
sysconfPrint("_SC_OPEN_MAX: ", _SC_OPEN_MAX);
sysconfPrint("_SC_NGROUPS_MAX: ", _SC_NGROUPS_MAX);
sysconfPrint("_SC_PAGESIZE: ", _SC_PAGESIZE);
sysconfPrint("_SC_RTSIG_MAX: ", _SC_RTSIG_MAX);
exit(EXIT_SUCCESS);
}
运行时获取与文件相关的限制
pathconf()函数fpathconf()函数,之间的唯一区别在于对文件或目录的指定方式。pathconf()采用路径名方式来指定,而fpathconf()则使用已经打开的文件描述符。
/*************************************************************************\
* Copyright (C) Michael Kerrisk, 2015. *
* *
* This program is free software. You may use, modify, and redistribute it *
* under the terms of the GNU General Public License as published by the *
* Free Software Foundation, either version 3 or (at your option) any *
* later version. This program is distributed without any warranty. See *
* the file COPYING.gpl-v3 for details. *
\*************************************************************************/
/* Listing 11-2 */
/* t_fpathconf.c
Demonstrate the use of fpathconf() to retrieve the values of
pathname-related limits.
*/
#include "tlpi_hdr.h"
static void /* Print 'msg' plus value of fpathconf(fd, name) */
fpathconfPrint(const char *msg, int fd, int name)
{
long lim;
errno = 0;
lim = fpathconf(fd, name);
if (lim != -1) { /* Call succeeded, limit determinate */
printf("%s %ld\n", msg, lim);
} else {
if (errno == 0) /* Call succeeded, limit indeterminate */
printf("%s (indeterminate)\n", msg);
else /* Call failed */
errExit("fpathconf %s", msg);
}
}
int
main(int argc, char *argv[])
{
fpathconfPrint("_PC_NAME_MAX: ", STDIN_FILENO, _PC_NAME_MAX);
fpathconfPrint("_PC_PATH_MAX: ", STDIN_FILENO, _PC_PATH_MAX);
fpathconfPrint("_PC_PIPE_BUF: ", STDIN_FILENO, _PC_PIPE_BUF);
exit(EXIT_SUCCESS);
}
总结:
对于系统实现必须支持的限制和可能支持的系统选项,SUSv3都做了规范。
通常不建议将对系统限制和选项的假设值硬性写入应用程序代码,因为这些值既可能随系统的不同而变化,也可能在同一个系统实现中因不同的运行期间或文件系统而不同。
因此SUSv3规定了一干方法,借助于此,系统实现可发布其所支持的限制和选项。对于大多数限制,SUSv3规定了所有实现所必须支持的最小值。此外,每个实现还能再编译时或运行时发布其特有的限制和选项。
编译时通过定义于