從零開始UNIX環(huán)境高級(jí)編程(2):Unix標(biāo)準(zhǔn)及實(shí)現(xiàn)

0. 思維導(dǎo)圖

Unix標(biāo)準(zhǔn)及實(shí)現(xiàn)

1. Unix標(biāo)準(zhǔn)化

1.1 ISO C

目的

提供C程序的可移植性,使其能適合于不同的操作系統(tǒng)

工作組

ISO/IEC JTC1/SC22/WG14

版本

ISO/C 版本

定義內(nèi)容

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

目的

提升應(yīng)用程序在各種Unix系統(tǒng)環(huán)境之間的可移植性

版本

  • 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

定義內(nèi)容

  • 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)

目的

改善應(yīng)用的可移植性

X/Open系統(tǒng)接口(XSI)

X/Open系統(tǒng)接口

版本

Single Unix Specification(SUS)

2. Unix系統(tǒng)實(shí)現(xiàn)

Unix系統(tǒng)實(shí)現(xiàn)

3. 限制

3.1 sysconf

  • 實(shí)現(xiàn)代碼
#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;
}
  • 運(yùn)行結(jié)果
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

  • 實(shí)現(xiàn)代碼
#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;
}
  • 運(yùn)行結(jié)果
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

  • 實(shí)現(xiàn)代碼
#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;
}
  • 運(yùn)行結(jié)果
ckt@ubuntu:~/work/unix/code/Chapter2$ ./fpathconf_test
file descriptor = 3
fpathconf = 255 

4. 功能測(cè)試宏


4.1 作用

4.2 示例

定義功能測(cè)試宏_TEST1和_TEST2

  • 實(shí)現(xiàn)代碼
#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;
}
  • 運(yùn)行結(jié)果
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. 參考

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容