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

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: 4096
_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)习题


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值