Onway

我是一只菜菜菜菜鸟...
posts - 61, comments - 56, trackbacks - 0, articles - 34

APUE第二章 UNIX标准化及实现

Posted on 2015-12-08 22:35 Onway 阅读(410) 评论(0)  编辑 收藏 引用 所属分类: 使用说明
1, 三个标准
1.1, ISO C标准由ISO/IEC维护开发
最新版本是C11,共有29个标准头文件。

1.2, POSIX是一系列由IEEE制定的标准
POSIX包括ISO C标准库函数。
POSIX标准的1988版本是IEEE 1003.1-1988,经过修改后作为IEEE Std.1003.1-1990提交ISO,成为国际标准ISO/IEC 9945-1:1990,该标准通常称为POSIX.1。
当前最新版本是POSIX.1-2008,由IEEE和Open Group共同开发。

1.3, SUS是POSIX的超集,其系统接口全集称为XSI
The core specifications of the SUS are developed and maintained by the Austin Group, which is a joint working group of IEEE, ISO JTC 1 SC22 and The Open Group.
只有遵循XSI的实现才能称为UNIX系统。
当前的最新版本是SUSv4。

1.4, 找到一些网址
C11
http://www.iso.org/iso/home/store/catalogue_tc/catalogue_detail.htm?csnumber=57853

POSIX.1-2008
http://pubs.opengroup.org/onlinepubs/9699919799/
https://standards.ieee.org/findstds/standard/1003.1-2008.html

SUSv4
https://www2.opengroup.org/ogsys/jsp/publications/PublicationDetails.jsp?publicationid=12310
https://en.wikipedia.org/wiki/Single_UNIX_Specification#cite_note-11

2, 限制
2.1 两种限制
编译时限制和运行时限制。
编译时限制通过头文件获取;
不与文件或目录相关的运行时限制通过sysconf函数获取;
与文件或目录相关的运行时限制通过pathconf和fpathconf函数获取。

2.2 ISO C限制
都是编译时限制,主要定义在<limits.h>里面。
http://en.cppreference.com/w/c/types/limits

2.3 POSIX限制和XSI限制
书中列出的都是实现中必须支持的各种最小值,特定系统实际支持的限制值需要通过头文件或者三个函数函数获取。
三个函数的name参数是限制名前面加_SC_或者_PC_前缀得到。
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/limits.h.html

2.4 书中代码
/*
 * If  name  is  invalid, -1 is returned, and errno is set to EINVAL.
 * Otherwise, the value returned is the value of the system resource and errno is not changed.
 * In the case of options, a positive value is returned if a queried option is available, and -1 if it is not.
 * In the case of limits, -1 means that there is no definite limit.
*/

#include 
"apue.h"
#include 
<errno.h>
#include 
<limits.h>

#ifdef OPEN_MAX
static long openmax = OPEN_MAX;
#else
static long openmax = 0;
#endif

/*
 * If OPEN_MAX is indeterminate, we're not
 * guaranteed that this is adequate
 
*/
#define OPEN_MAX_GUESS 256

long
open_max(
void)
{
    
if (openmax == 0) { /* first time through */
        errno 
= 0;
        
if ((openmax = sysconf(_SC_OPEN_MAX)) < 0) {
            
if (errno == 0)
                openmax 
= OPEN_MAX_GUESS; /* it's indeterminate */
            
else
                err_sys(
"sysconf error for _SC_OPEN_MAX";)
        }
    }

    
return(openmax);
}

3, 选项
3.1, 选项确定方式
编译时选项定义在<unistd.h>中;
与文件或目录无关的选项用sysconf确定;
与文件或目录有关的选项用pathconf或者fpathconf确定;
http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html

3.2, 选项确定流程
如果符号常量未定义,
对_POSIX前缀的选项,将_POSIX前缀替换为_SC或_PC前缀,
对_XOPEN前缀的选项,在_XOPEN前面加上_SC或_PC前缀,
然后调用sysconf, pathconf或fpathconf函数。
如果符号常量已经定义,则有三种可能:
值为-1,不支持相应的选项;
值大于0,支持相应的选项;
值为0,需调用函数确定选项是否支持。
注:某些系统可能出现定义了符号常量,但没有定义值的情况。

3.4, 代码示例
先占坑。

4, 功能测试宏
Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled.
NOTE:  In  order  to be effective, a feature test macro must be defined before including any header files.  This can be done either in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any headers.
see man page feature_test_macros(7).

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理