1. 限制
Unix系统实现定义了很多幻数和常量,这些在不同程度上依从POSIX,也遵从POSIX.1标准。这就有助于软件的可移植性。
以下两种类型的限制是必须的:
1) 编译时限制(例如,短整型的最大值是什么?)
2) 运行时限制(例如,文件名可以有多少个字符?)
编译时限制可在头文件中定义,程序在编译时可以包含头文件。但是,运行时限制则要求进程调用一个函数以获取此种限制值。
为了解决此类问题:
1) 编译时限制(例如,短整型的最大值是什么?)
2) 不与文件或目录相关联的运行时限制(sysconf函数)
3) 与文件或目录相关联的运行时限制(pathconf和fpathconf函数)
2. 函数原型
#include
long sysconf(int name);
long pathconf(const char *pathname, int name);
long fpathname(int filedes, int name);
所有函数返回值:若成功则返回相应值;若出错则返回-1.
返回值:
(1)如果 name 不是一个合适的常量,则所有三个函数都会返回-1,并将 errno 设置为EINVAL。
(2)有些 name可以返回变量的值(返回值>=0),或者返回-1,这表示该值是不确定的,此时不改变 errno的值。
因此,程序要检查错误,应该在调用 sysconf() 之前将 errno 设置为 0 ,然后如果返回-1,则检验到错误。表1 sysconf的限制及name参数,用于标识系统限制。以__SC__开始的常量用作标识运行时限制的sysconf参数。
Name of limit | Description | name argument |
ARG_MAX | maximum length, in bytes, of arguments to the exec functions | _SC_ARG_MAX |
ATEXIT_MAX | maximum number of functions that can be registered with the atexit function | _SC_ATEXIT_MAX |
CHILD_MAX | maximum number of processes per real user ID | _SC_CHILD_MAX |
clock ticks/second | number of clock ticks per second | _SC_CLK_TCK |
COLL_WEIGHTS_MAX | maximum number of weights that can be assigned to an entry of the LC_COLLATE order keyword in the locale definition file | _SC_COLL_WEIGHTS_MAX |
HOST_NAME_MAX | maximum length of a host name as returned by gethostname | _SC_HOST_NAME_MAX |
IOV_MAX | maximum number of iovec structures that can be used with readv or writev | _SC_IOV_MAX |
LINE_MAX | maximum length of a utility's input line | _SC_LINE_MAX |
LOGIN_NAME_MAX | maximum length of a login name | _SC_LOGIN_NAME_MAX |
NGROUPS_MAX | maximum number of simultaneous supplementary process group IDs per process | _SC_NGROUPS_MAX |
OPEN_MAX | maximum number of open files per process | _SC_OPEN_MAX |
PAGESIZE | system memory page size, in bytes | _SC_PAGESIZE |
PAGE_SIZE | system memory page size, in bytes | _SC_PAGE_SIZE |
RE_DUP_MAX | number of repeated occurrences of a basic regular expression permitted by the regexec and regcomp functions when using the interval notation /{m,n/} | _SC_RE_DUP_MAX |
STREAM_MAX | maximum number of standard I/O streams per process at any given time; if defined, it must have the same value as FOPEN_MAX | _SC_STREAM_MAX |
SYMLOOP_MAX | number of symbolic links that can be traversed during pathname resolution | _SC_SYMLOOP_MAX |
TTY_NAME_MAX | length of a terminal device name, including the terminating null | _SC_TTY_NAME_MAX |
TZNAME_MAX | maximum number of bytes for the name of a time zone | _SC_TZNAME_MAX |
表2 sysconf的限制及name参数,用于标识系统限制。以__PC__开始的常量用作标识运行时限制的pathconf参数和fpathconf参数。
Name of limit | Description | name argument |
FILESIZEBITS | minimum number of bits needed to represent, as a signed integer value, the maximum size of a regular file allowed in the specified directory | _PC_FILESIZEBITS |
LINK_MAX | maximum value of a file's link count | _PC_LINK_MAX |
MAX_CANON | maximum number of bytes on a terminal's canonical input queue | _PC_MAX_CANON |
MAX_INPUT | number of bytes for which space is available on terminal's input queue | _PC_MAX_INPUT |
NAME_MAX | maximum number of bytes in a filename (does not include a null at end) | _PC_NAME_MAX |
PATH_MAX | maximum number of bytes in a relative pathname, including the terminating null | _PC_PATH_MAX |
PIPE_BUF | maximum number of bytes that can be written atomically to a pipe | _PC_PIPE_BUF |
SYMLINK_MAX | number of bytes in a symbolic link | _PC_SYMLINK_MAX |