美文网首页Linux开发UNIX环境高级编程
从零开始UNIX环境高级编程(2):Unix标准及实现

从零开始UNIX环境高级编程(2):Unix标准及实现

作者: 伤口不该结疤 | 来源:发表于2017-01-22 22:03 被阅读84次

    0. 思维导图

    Unix标准及实现

    1. Unix标准化

    1.1 ISO C

    目的

    提供C程序的可移植性,使其能适合于不同的操作系统

    工作组

    ISO/IEC JTC1/SC22/WG14

    版本

    ISO/C 版本

    定义内容

    It specifies
    — the representation of C programs;
    — the syntax and constraints of the C language;
    — the semantic rules for interpreting C programs;
    — the representation of input data to be processed by C programs;
    — the representation of output data produced by C programs;
    — the restrictions and limits imposed by a conforming implementation of C.

    1.2 IEEE POSIX

    目的

    提升应用程序在各种Unix系统环境之间的可移植性

    版本

    • Parts before 1997
      POSIX.1: Core Services
      POSIX.1b: Real-time extensions
      POSIX.1c: Threads extensions
      POSIX.2: Shell and Utilities
    • Versions after 1997
      POSIX.1-2001
      POSIX.1-2004 (with two TCs)
      POSIX.1-2008 (with one TC)

    工作组

    IEEE 1003.1

    定义内容

    • General terms, concepts, and interfaces common to all volumes of this standard, including utility conventions and C-language header definitions, are included in the Base Definitions volume.
    • Definitions for system service functions and subroutines, language-specific system services for the C programming language, function issues, including portability, error handling, and error recovery, are included in the System Interfaces volume.
    • Definitions for a standard source code-level interface to command interpretation services (a “shell”) and common utility programs for application programs are included in the Shell and Utilities volume.
    • Extended rationale that did not fit well into the rest of the document structure, which contains historical information concerning the contents of POSIX.1-2008 and why features were included or discarded by the standard developers, is included in the Rationale (Informative) volume.

    1.3 Single Unix Specification(SUS)

    目的

    改善应用的可移植性

    X/Open系统接口(XSI)

    X/Open系统接口

    版本

    Single Unix Specification(SUS)

    2. Unix系统实现

    Unix系统实现

    3. 限制

    3.1 sysconf

    • 实现代码
    #include "apue.h"
    
    int main(int argc, char const *argv[])
    {
        // Maximum length of a login name
        printf("LOGIN_NAME_MAX = %ld \n", sysconf(_SC_LOGIN_NAME_MAX));
        return 0;
    }
    
    • 运行结果
    ckt@ubuntu:~/work/unix/code/Chapter2$ cc sysconf_test.c -o sysconf_test
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./sysconf_test
    LOGIN_NAME_MAX = 256 
    

    3.2 pathconf

    • 实现代码
    #include "apue.h"
    
    int main(int argc, char const *argv[])
    {
        printf("_PC_PATH_MAX = %ld \n", pathconf("/home/ckt/work/unix/
              code/Chapter2",_PC_PATH_MAX));
        return 0;
    }
    
    • 运行结果
    ckt@ubuntu:~/work/unix/code/Chapter2$ cc pathconf_test.c -o pathconf_test
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./pathconf_test
    _PC_PATH_MAX = 4096 
    

    3.3 fpathconf

    • 实现代码
    #include "apue.h"
    #include <fcntl.h>
    
    int main(int argc, char const *argv[])
    { 
        int file_descr = -1;
        file_descr = open("/home/ckt/work/unix/code/
                             Chapter2/fpathconf_test.c", O_RDONLY);
        printf("file descriptor = %d\n", file_descr);
        printf("fpathconf = %ld \n",fpathconf(file_descr,_PC_NAME_MAX));
        return 0;
    }
    
    • 运行结果
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./fpathconf_test
    file descriptor = 3
    fpathconf = 255 
    

    4. 功能测试宏


    4.1 作用

    4.2 示例

    定义功能测试宏_TEST1和_TEST2

    • 实现代码
    #include "apue.h"
    #include <fcntl.h>
    
    int main(int argc, char const *argv[])
    {
    #if defined(_TEST1)
        printf("_TEST1\n");
    #elif defined(_TEST2)
        printf("_TEST2\n");
    #else
        printf("not defined\n");
    #endif
        return 0;
    }
    
    • 运行结果
    ckt@ubuntu:~/work/unix/code/Chapter2$ cc -D_TEST1 macros_test.c -o macros_test1
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./macros_test1
    _TEST1
    
    ckt@ubuntu:~/work/unix/code/Chapter2$ cc -D_TEST2 macros_test.c -o macros_test2
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./macros_test2
    _TEST2
    
    ckt@ubuntu:~/work/unix/code/Chapter2$ cc macros_test.c -o macros_test
    ckt@ubuntu:~/work/unix/code/Chapter2$ ./macros_test
    not defined
    

    5. 参考

    相关文章

      网友评论

        本文标题:从零开始UNIX环境高级编程(2):Unix标准及实现

        本文链接:https://www.haomeiwen.com/subject/xrymbttx.html