您的位置:首页 > 运维架构 > Linux

The Linux Programming Interface 11 System Limits And Options 系统限制和选择

2017-03-09 19:55 459 查看
The Linux Programming Interface

System Limit And Options

(01) 概念

Each UNIX implementation set limits on various system features and resources, and provides - or chooses not to provide - options defined in various standards.

(02)limit.h

In most cases, this minimum value is defined as a constant in <limits.h> with a name prefixed by the string _POSIX.

(03)使用sysconf获取限制值

1 #include "tlpi_hdr.h"
2
3 /* print 'msg' plus sysconf() value for 'name' */
4 static void sysconfPrint(const char *msg, int name)
5 {
6     long lim;
7     errno = 0;
8     lim = sysconf(name);
9     if(lim != -1) { /* call succeeded, limit determinate */
10         printf("%s %ld\n", msg, lim);
11     } else {
12         if (errno == 0) /* call succeeded, limit indeterminate */
13             printf("%s (indeterminate)\n", msg);
14         else            /* call failed */
15             errExit("sysconf %s", msg);
16     }
17 }
18
19 int main(int argc, char *argv[]) {
20     sysconfPrint("_SC_ARG_MAX:      ", _SC_ARG_MAX);
21     sysconfPrint("_SC_LOGIN_NAME_MAX:   ", _SC_LOGIN_NAME_MAX);
22     sysconfPrint("_SC_OPEN_MAX:     ", _SC_OPEN_MAX);
23     sysconfPrint("_SC_NGROUPS_MAX:  ", _SC_NGROUPS_MAX);
24     sysconfPrint("_SC_PAGESIZE:     ", _SC_PAGESIZE);
25     sysconfPrint("_SC_RTSIG_MAX:    ", _SC_RTSIG_MAX);
26     exit(EXIT_SUCCESS);
27 }
输出:

wang@wang:~/test/tlpi-dist/lib$ ./t_sysconf

_SC_ARG_MAX:         2097152

_SC_LOGIN_NAME_MAX:     256

_SC_OPEN_MAX:         1024

_SC_NGROUPS_MAX:      65536

_SC_PAGESIZE:         4096

_SC_RTSIG_MAX:     32

(04)pathconf 和fpathconf()函数

The pathconf() and fpathconf() functions allow an application to obtain the values of file-related limits at run time.

#include <unistd.h>

long pathconf(const char *pathname, int name);

long fpathconf(int fd, int name);

name可以使用以下参数

_PC_NAME_MAX

_PC_PATH_MAX

_PC_PIPE_BUF

(05)举例

1 #include "tlpi_hdr.h"
2
3 /* Print 'msg' plus value of fpathconf(fd, name) */
4
5 fpathconfPrint(const char *msg, int fd, int name) {
6     long lim;
7     errno = 0;
8     lim = fpathconf(fd, name);
9     if (lim != -1)
10         /* call succeed, limit determinate */
11         printf("%s %ld\n", msg, lim);
12     else {
13         if (errno == 0)
14             printf("%s (indeterminate)\n", msg);
15         else
16             errExit("fpathconf %s", msg);
17     }
18 }
19
20 int main(int argc, char *argv[]) {
21     fpathconfPrint("_PC_NAME_MAX:", STDIN_FILENO, _PC_NAME_MAX);
22     fpathconfPrint("_PC_PATH_MAX:", STDIN_FILENO, _PC_PATH_MAX);
23     fpathconfPrint("_PC_PIPE_BUF:", STDIN_FILENO, _PC_PIPE_BUF);
24     exit(EXIT_SUCCESS);
25 }
输出:

wang@wang:~/test/tlpi-dist/lib$ ./t_fpathconf < .

_PC_NAME_MAX: 255

_PC_PATH_MAX: 409
4000
6

_PC_PIPE_BUF: 4096

(06)间接限制 indeterminate limits

(07)总结

SUSv3 specifies limits that an implementation may enforce and system options that an implementation ma support.

    Often, it is desirable not to hard-code assumptions about system limits and options into a program, since ma vary across implementations and also on a single implementation, either at run time or across file systems. Therefore, SUSv3 specifies methods
by which an implementation can advertise the limits and options it supports. For most limits, SUSv3 specifies a minimum value that all implementations must support. Additionally, each implementation can advertise its implementation-specific limits and options
at compile time (via a constant definition in <limits.h> or <unistd.h> and/or run time (via a call to sysconf(), pathconf(), or fpathconf()). These techniques may similarly be used to find out which SUSv3 options an implementation supports. In some cases,
it may not be possible to determine a particular limit using either of these methods. For such indeterminate limits, we must resort to ad hoc techniques to determine the limit to which an application should adhere.

(08)习题
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐