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获取限制值
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)举例
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)习题
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)习题
相关文章推荐
- The Linux Programming Interface 12 System And Process Information 系统和进程信息
- The Linux Programming Interface 03 System Programming Concepts 系统编程观念
- The Linux Programming Interface - A Linux and UNIX System Programming Handbook
- Linux System Programming: Talking Directly to the Kernel and C Library
- Linux System Programming: Talking Directly to the Kernel and, C Library [ILLUSTRATED] (Paperback) Sep.2007.eBook-BBL
- The Linux Programming Interface 01 History and Standards UNIX/Linux历史和标准
- The Linux Programming Interface Appendix B Parsing Command-Line options 解析命令行中的选项
- The Linux Programming Interface 08 Users And Groups 用户和组
- Linux System Programming: Talking Directly to the Kernel and C Library [ILLUSTRATED]
- The Linux Programming Interface 14 File Systems 文件系统
- Linux System Programming note 11 ——Time
- Linux系统下出现"The ext4 file system is not supported by you system""
- Linux系统:an error occurred during the file system check 错误解决方法
- linux系统中:an error occurred during the file system check 错误解决方法
- 分享一个图:Linux kernel System Call Interface and glibc
- 用VMware安装虚拟系统时出现Invalid system disk,Replace the disk and then press any key
- Linux System Programming note 8 ——File and Directory Management
- Cracking the Coding Interview-ch11 | System Design and Memory Limits
- Analysing the ASM code of a simple sample of C programming language at Linux Platform by GCC and GDB