免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1947 | 回复: 1
打印 上一主题 下一主题

Advanced Programming in the UNIX Environment(2) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-01 10:42 |只看该作者 |倒序浏览
    Much has happened over the past two decades with the standardization of the UNIX programming environment. This chapter described the dominant standards ISO C, POSIX, and the Single UNIX Specification and their effect on the four implementations that we'll examine in this text: FreeBSD, Linux, Mac OS X, and Solaris.

    本章我重点学习两个问题:c语言标准及库函数;unix标准及系统调用。
    从执行者的角度来看,系统调用和库函数之间有重大区别,但从用户角度来看,其区别并不非常重要。在本书中系统调用和库函数都以C语言函数的形式出现,两者都对应用程序提供服务,但是,我们应当理解,如果希望的话,我们可以替换库函数但是通常却不能替换系统调用。
    本书除了必须要区分两者时,都将使用术语函数(function)来指代系统调用和库函数两者。

一、c语言标准及库函数
1、c语言标准演变
    20世纪70年代初,Dennis Ritchie在贝尔实验室设计了C语言。
    从70年代初期的早期C语言到后来的K&R C(K&R指两个人:Brian Kernighan和Dennis Ritchie),ANSI C(C89),C95,在20年中C语言多次发展演化,一直到1999年C语言又重新定案,成为新的C语言标准C99。这是标准C的当前版本,它具有所有最新的特性。
2、c语言库函数
    C99有24个标准库:
在C89中有15个:
、  、、 、  、、  、、
、 、 、 、、
在C95中增加3个:
、 、
在C99中又增加了6个:
、 、 、、  、 。
    (1)assert.h:验证程序断言
assert NDEBUG
    (2)complex.h:支持复数算术运算
cabs cabsf cabsl cacos cacosf cacoshf cacosl carg cargf cargl casin casinf
casih casinhf casinhl casinl catan catanf catanh catanhf catanhl catanl ccos ccosf
ccosh ccoshf ccoshl ccosl cexpf cexpf cexpl cimag cimagf cimagl clog clogf
clogl complex _Complex_I conj conjf conjl cpow cpowf cpowl cproj cprojf
cprojl creal crealf creall csin csinf csinh csinf csinhf csinhl csinl csqrt
csqrtf csqrtl ctan ctanf ctanh ctanhf ctanhl ctanl CX_LIMITED_RANGE I
imaginary _Imaginary_I
    (3)ctype.h:字符类型
isalnum isalphs isblank iscntrl isdigit isgraph islower isprint ispunct isspace isupper  
isxdigit tolower toupper
    (4)errno.h:出错码
EDOM ERANGE EILSEQ errno
    (5)fenv.h:浮点环境
FE_ALL_EXCEPT FE_DFL_ENV FE_DIVBYZERO FE_DOWNWARD FE_INEXACT FE_INVALID
FE_OVERFLOW FE_TONEAREST FE_TOWATDZERO FE_UNDERFLOW FE_UPWARD feclearexcept
fegetenv fegetexceptflag fegetround feholdexcept FENV_ACCESS fenv_t  
feraiseexcept fesetenv fesetexceptflag fesetround fetestexcept feupdateenv
fexcept_t
    (6)float.h:浮点常量
DBL_DIG DBL-EPSILON DBL_MANT_DIG DBL_MAX DBL_MAX_10_EXP DBL_MAX_EXP DBL_MIN  
DBL_MIN_10_EXP DBL_MIN_EXP DECIMAL_DIG FLT_DIG FLT_EPSILON FLT_EVAL_METHOD  
FLT_MANT_DIG FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP FLT_MIN FLT_MIN_10_EXP FLT_MIN_EXP
FLT_RADIX FLT_ROUNDS LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG LDBL_MAX
LDBL_MAX_10_EXP LDBL_MAX_EXP LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP
    (7)inttypes.h:整型格式转换
CNiLEASTN imaxabs imaxdiv imaxdiv_t PRIdFASTN PRIdLEASTN PRIdMAX  
PRIdN PRIdPTR PRIiFASTN PRIiLEASTN PRIiMAX PRIiN PRIiPTR PRIoFASTN  
PRIoLEASTN PRIoMAX PRIoN PRIoPTR PRIuFASTN PRIuLEASTN PRInMAX PRIuN  
PRIuPTR PRIxFASTN PRIXFASTN PRIxLEASTN PRIXLEASTN PRIxMAX PRIXMAX  
PRIxN PRIXN PRIxPTR PRIXPTR SCNdFASTN SCNdLEASTN SCNdMAX SCNdN SCNdPTR  
SCNiRASTN SCNiMAX SCNiN SCNiPTR SCNoFASTN SCNoLEASTN SCNoMAX SCNoN  
SCNoPTR SCNuFASTN SCNuLEASTN SCNuMAX SCNuN SCNuPTR SCNxFASTN SCNxLEASTN
SCNxMAX SCNxn SCNxPTR strtoimax strtoumax wcstoimax wcstoumax
    (8)iso64.h:替代关系操作符宏
and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq
    (9)limits.h:实现常量
CHAR_BIT CHAR_MAX CHAR_MIN INT_MAX INT_MIN LLONG_MAX LLONG_MIN
LONG_MAX LONG_MIN MB_LEN_MAX SCHAR_MAX SCHAR_MIN SHRT_MAX
SHRT_MIN UCHAR_MAX UINT_MAX ULLONG_MAX ULONG_MAX USHRT_MAX
    (10)locale.h:局部类别
LC_ALL LC_CLLLATE LC_CTYPE LC_MONETARY LC_NUMERIC LC_TIME lconv  
localeconv NULL setlocale
    (11)math.h:数学函数
acos acosf acosh acoshg acosl asin asinf asinh asinhf asinl atan atan2
atan2f atan2l atanf atanh atanhf atanhl atanl cbrt cbrtf cbrtl ceil ceilf
ceil copysign copysignf copysignl cos cosf cosh coshf coshl
cosl double_t erf erfc erfcf erfcl erff erfl exp exp2 exp2f
exp2l expf expl expml expmlf expmll fabs fabsf fabsl fdim fdimf fdiml
float_t floor floorf floorl fma fmaf fmal fmax fmaxf fmaxl fmin fminf
fminl fmod fmodf fmodl FP_CONTRACT FP_FAST_FMA FP_FAST_FMAF FP_FAST_FMAL
FP_ILOGB0 FP_ILOGBNAN FP_INFINITE FP_NAN FP_NORMAL FP_SUBNORMAL  
FP_ZERO  fpclassify frexp frexpf frexpl HUGE_VAL HUGE_VALF  
HUGE_VALL hypot hypotf hpotl ilogb ilogbf ilogbl INGINITY isfinite
isgreater isgreaterequal ising isless islessequal islessgreater isnan  
isnormal isunorderedldex p ldexpf ldexpl lgamma lgamaf lgamal llrint  
llrintf llrintl llround llroundf llroundllog log10 log10f log10l log1p log1pf
log1pl log2 log2f log2l logb logbf logbl logf logl lrint lrintf lrintl
lround lroundf lroundl MATH_ERREXCEPT math_errhandling MATH_ERRNO modff mldfl
NAN nan nanf nanl nearbyint nearbyintf nearbyintl nextafter
nextafterf nextafterl nexttoward nexttowardf nexttowardl pow powf
powl remainder remainderf remainderl remquo remquof remquol rint rintf
rintl round roundf roundl scalbln scalblnf scalblnl scalbn scalbnf  
scalbnl signbit sin sinf sinh sinhf sinhl sinl sqrt sqrtf sqrtl tan
tanf tanh tanhf tanhl tanl tgamma tgamaf tgamal trunc truncf truncl
    (12)setjmp.h:非局部goto
jmp_buf longjmp setjmp
    (13)signal.h:信号
raise sig_atomic_t SIG_DFL SIG_ERR SIG_IGN SIGABRT SIGFPE SIGILL SIGINT signal  
SIGSEGV SIGTERM
    (14)stdarg.h:可变参数表
va_arg va_copy va_end va_list va_start
    (15)stdbool.h:布尔类型和值
bool _bool_true_false_are_defined false true
    (16)stddef.h:标准定义
NULL offsetof prtdiff_t size_t wchar_t
    (17)stdint.h:整型
INT_FASTN_MAX INT_FASTN_MIN int_fastN_t INT_LEASTN_MAX INT_LEASTN_MIN int_leastN_t
INTMAX_C INTMAX_MAX INTMAX_MIN intmax_t INTN_C INTN_MAX  
INTN_MIN intN_t INTPTR_MAX INTPTR_MIN intptr_t PTRDIFF_MAX  
PTRDIFF_MIN SIG_ATOMIC_MAX SIG_ATOMIC_MIN SIZE_MAX UINT_FASTN_MAX uint_festN_t
UINT_LEASTN_MAX uint_leastN_t UINTMAX_C UINTMAX_MAX uintmax_t UINTN_C  
UINTN_MAX uintN_t UINTPTR_MAX uintptr_t WCHAR_MAX WCHAR_MIN  
WINT_MAX WINT_MIN
    (18)stdio.h:标准I/O库
BUFSIZ clearerr FOF fclose feof ferror fflush fgetc fgetpos fgets FILE
FILENAME_MAX fopen FOPEN_MAX fpos_t fprintf fputc fputs fread freopen fscanf
fseek fsetpos ftell fwrite getc getchar gets _IOFBF _IOLBF _IONBF NULL perror
printf putc putchar puts remove rename rewind scanf SEEK_CUR SEEK_END
SEEK_SET setbuf setvbuf size_t snprintf sprintf sscanf stderr sedin stdout
TMP_MAX tmpfile tmpnam ungetc vfprintf vfscanf vpintf vscanf vsnprintf  
vsprintf vsscanf
    (19)stdlib.h:实用程序库函数
abort abs atexit atof atoi atol atoll bsearch calloc div div_t exit
_Exit EXIT_FAILURE EXIT_SUCCESS free getenv labs ldiv ldiv_t llabs lldiv
lldiv_t malloc MB_CUR_MAX mblen mbstowcs mbtowc NULL qsort rand  
RAND_MAX realloc size_t srand strtod strtof strtol strtold strtoll strtoul  
strtoull system wchar_t wcstombs wctomb
    (20)string.h:字符串操作
memchr memcmp memcpy memmove memset NULL size_t strcat strchar strcmp strcoll strcpy
strcspn strerror strlen strncat strncmp strncpy strpbrk strrchr strspn strstr strtok
strxfrm
    (21)tgmath.h:通用类型数学宏
acos acosh asin asinh atan atan2 atanh carg cbrt ceil cimag conj
copysign cos cosh cproj creal erf erfc exp exp2 expml fabs
fdim floor fma fmax fmin fmod frexp hypot ilogb ldexp lgamma llrint
llround log log10 loglp log1p log2 logb lrint lround nearbying  
nextafter nexttoward pow remainder remquo rint round scalbln scalbn
sin sinh sqrt tan tanh tgamma trunc
    (22)time.h:日期和时间
asctime clock clock_t CLOCK_PER_SEC ctime difftime gmtime localtime mktime
NULL size_t strftime struct tm time time_t
    (23)wchar.h:扩展的多字节和宽字符支持
btowc fgetwc fgetws fputwc fputws fwide fwprintf fwscanf getwc getwchar
mbrlen mbsinit mbsrtowcs mbstate_t NULL putwc putwchar size_t  
swprintf swscanf tm ungetwc vfwprintf vfwscanf vswprintf  
vswscanf vwprintf vwscanf WCHAR_MAX WCHAR_MIN wchar_t wcrtomb wcscat
wcschr wcscmp wcscoll wcscoll wcscpy wcscspn wcsftime wcslen wcsncat wcsncmp  
wcsncpy wcspbrk wcsrchr wcsrtombs wcsspn wcsstr wcstod wcstof wcstok wcstol  
wcstold wcstoll wcstoul wcstoull wcsxfrm wctob WEOF wint_t wmemchr wmetcmp  
wmetcpy wmemmove wmemset wprintf wscanf
    (24)wctype.h:宽字符分类和映射支持
iswalnum iswalpha iswblank iswcntrl iswctype iswdigit
iswgraph iswlower iswprint iswpunct iswspace iswupper
iswxdigit towctrans towlower towupper wctrans  wctrans_t
wctype  wctype_t WEOF  wint_t
    24个标准库,函数如此之多以至于不能在本文中做详细学习,故仅列出函数名,待以后遇到某一方面问题时再做探讨。
   
二、UNIX标准及系统调用
    POSIX is a family of standards developed by the IEEE (Institute of Electrical and Electronics Engineers). POSIX stands for Portable Operating System Interface.
    POSIX是一个由IEEE(电气和电子工程师学会)制订的标准族。POSIX的意思是计算机环境的可移植操作系统界面(Portable Operating System Interface for Computer Environment)。

    POSIX是基于UNIX的,这一标准意在期望获得源代码级的软件可移植性。换句话说,为一个POSIX兼容的操作系统编写的程序,应该可以在任何其它的POSIX操作系统(即使是来自另一个厂商)上编译执行。POSIX标准定义了操作系统应该为应用程序提供的接口:系统调用集。POSIX是由IEEE(Institute of Electrical and Electronic Engineering)开发的,并由ANSI(American National Standards Institute)和ISO(International Standards Organisation)标准化。大多数的操作系统(包括Windows NT)都倾向于开发它们的变体版本与POSIX兼容。
    Figure 2.2, Figure 2.3, and Figure 2.4 summarize the required and optional headers as specified by POSIX.1. Because POSIX.1 includes the ISO C standard library functions, it also requires the headers of ISO C.

    Figure 2.2 Required headers defined by the POSIX standard
    POSIX标准定义的必须的头文件(26项):
    (1) , directory entries,目录项;
    (2) , file control, 文件控制;
    (3) , filename-matching types,文件名匹配类型;
    (4) , pathname pattern-matching types,路径名模式匹配类型;
    (5) , group file,组文件;
    (6) , network database operations,网络数据库操作;
    (7) , password file,口令文件;
    (8) , regular expressions,正则表达式;
    (9) , tar archive values,tar归档值;
    (10) , terminal I/O,终端I/O;
    (11) , symbolic constants,符号常量;
    (12) , file times,文件时间;
    (13) , word-expansion types,字扩展类型;
    (14) , Internet definitions,Internet定义;
    (15) , socket local interfaces,套接字本地接口;
    (16) , Internet address family,Internet地址族;
    (17) , Transmission Control Protocol definitions,传输控制协议定义;
    (18) , memory management declarations,内存管理声明;
    (19) , select function,select函数;
    (20) , sockets interface,套接字接口;
    (21) , file status,文件状态;
    (22) , process times,进程时间;
    (23) , primitive system data types,基本系统数据类型;
    (24) , UNIX domain socket definitions,UNIX域套接字定义;
    (25) , system name,系统名;
    (26) , process control,进程控制;

    Figure 2.3 XSI extension headers defined by the POSIX standard
    POSIX标准定义的XSI扩展头文件(26项):
    (1) , cpio archive values,cpio归档值;
    (2) , dynamic linking,动态链接;
    (3) , message display structures,消息显示结构;
    (4) , file tree walking,文件树漫游;
    (5) , codeset conversion utility,代码集转换实用程序;
    (6) , language information constants,语言信息常量;
    (7) , definitions for pattern-matching function,模式匹配函数定义;
    (8) , monetary types,货币类型;
    (9) , database operations,数据库操作;
    (10) , message catalogs,消息类别;
    (11) , poll function,轮询函数;
    (12) , search tables,搜索表;
    (13) , string operations,字符串操作;
    (14) , system error logging,系统出错日志记录;
    (15) , user context,用户上下文;
    (16) , user limits,用户限制;
    (17) , user accounting database,用户帐户数据库;
    (18) , IPC,IPC;
    (19) , message queues,消息队列;
    (20) , resource operations,资源操作;
    (21) , semaphores,信号量;
    (22) , shared memory,信号量;
    (23) , file system information,;文件系统信息
    (24) , time types。时间类型;
    (25) , additional date and time definitions,附加的日期和时间定义;
    (26) , vector I/O operations,矢量I/O操作;
   
    Figures 2.4 Optional headers defined by the POSIX standard
    POSIX标准定义的可选头文件(8项):
    (1) , asynchronous I/O,异步I/O;
    (2) , message queues,消息队列;
    (3) , threads,执行调度;
    (4) , execution scheduling,执行调度;
    (5) , semaphores,信号量;
    (6) , real-time spawn interface,实时spawn接口;
    (7) , XSI STREAMS inteface,XSI STREAMS 接口;
    (8) , event tracing,时间跟踪;

    Because the 1003.1 standard specifies an interface and not an implementation, no distinction is made between system calls and library functions. All the routines in the standard are called functions.
    由于1003.1标准说明了一个界面(interfaces)而不是一种实现(implementation) ,所以并不区分系统调用和库函数。所有在标准中的例程都被称为函数。
    以上这些文件中包括的函数正是本书后面章节要详细介绍的内容。

    有关POSIX的详细信息请查看:
http://www.opengroup.org/austin/papers/posix_faq.html
它包括1000多个函数。
   


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/95093/showart_1982524.html

论坛徽章:
0
2 [报告]
发表于 2013-05-05 17:46 |只看该作者
请问到哪里可下载《Advanced Programming in the UNIX Environment,2nd Edition》(APUE)这本书呢?能给我一个链接么?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP