0. 思維導(dǎo)圖
1. Unix標(biāo)準(zhǔn)化
1.1 ISO C
目的
提供C程序的可移植性,使其能適合于不同的操作系統(tǒng)
工作組
ISO/IEC JTC1/SC22/WG14
版本
定義內(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)
版本
2. 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