APUE Chapter 2 - UNIX Standardization and Implementations

2.1. Introduction

2.2. UNIX Standardization

2.2.1. ISO C

2.2.2. IEEE POSIX

  POSIX is a family of standards developed by the IEEE (Institute of Electrical and Electronics Engineers).
  POSIX stands for Portable Operating System Interface.

2.3. UNIX System Implementations

2.4. Relationship of Standards and Implementations

2.5. Limits

Two types of limits are needed:
1. Compile-time limits (e.g., what's the largest value of a short integer?)
2. Runtime limits (e.g., how many characters in a filename?)

To solve these problems, three types of limits are provided:
1. Compile-time limits (headers)
2. Runtime limits that are not associated with a file or directory (the sysconf function)
3. Runtime limits that are associated with a file or a directory (the pathconf and fpathconf functions)

1. 限制(Limits)
Unix系统实现定义了很多幻数(magic numbers)和常量(constants),这些在不同程度上依从POSIX,也遵从POSIX.1标准。这就有助于软件的可移植性。

以下两种类型的限制是必须的:
1) 编译时限制(例如,短整型的最大值是什么?)
2) 运行时限制(例如,文件名可以有多少个字符?)
编译时限制可在头文件中定义,程序在编译时可以包含头文件。但是,运行时限制则要求进程调用一个函数以获取此种限制值。

为了解决此类问题:
1) 编译时限制(例如,短整型的最大值是什么?)
2) 不与文件或目录相关联的运行时限制(sysconf函数)
3) 与文件或目录相关联的运行时限制(pathconf和fpathconf函数)

2. 函数原型
#include <unistd.h>
long sysconf(int name);
long pathconf(const char *pathname, int name);
long fpathconf(int filedes, int name);
pathconf和fpathconf函数之间的区别是:前者用路径名作为参数,后者取文件描述符作为参数。

返回值:
(1)如果 name 不是一个合适的常量,则所有三个函数都会返回-1,并将 errno 设置为 EINVAL。
(2)有些 name 可以返回变量的值(返回值>=0),或者返回-1,这表示该值是不确定的,此时不改变 errno 的值。
因此,程序要检查错误,应该在调用 sysconf() 之前将 errno 设置为 0 ,然后如果返回 -1,则检验到错误。

1的补码?

2的补码?

posted on 2011-11-29 08:38  s7vens  阅读(162)  评论(0编辑  收藏  举报