导语
内容提要
20多年来,严谨的C程序员都是依靠一本书来深入了解驱动UNIX和Linux内核的编程接口的实用知识的,这本书就是W.Richard Stevens所著的《UNIX环境高级编程》。现在,Stevens的同事Stephen Rago彻底更新了这本经典著作。新的第3版支持当今领先的系统平台,反映了最新技术进展和最佳实践,并且符合最新的Single UNIXSpecification 第4版(SUSv4)。
Rago保留了使本书前版成为经典之作的精髓和方法。他在Stevens原著的基础上,从基础的文件、目录和进程讲起,并给诸如信号处理和终端I/O之类的先进技术保留较大的篇幅。他还深入讨论了线程和多线程编程、使用套接字接口驱动进程间通信(IPC)等方面的内容。
这一版涵盖了70多个新接口,包括POSIX 异步I/O、旋转锁、屏障(barrier)和POSIX信号量。此外,这一版删除了许多过时的接口,保留了一些广泛使用的接口。书中几乎所有实例都已经在主流的4个平台上测试过,包括Solaris10、Mac OS X 10.6.8(Darwin 10.8.0)、FreeBSD 8.0和Ubuntu 12.04(基于Linux 3.2)。
与前两版一样,读者仍可以通过实例学习,这些实例包括了1 万多行可下载的ISO C源代码,书中通过简明但完整的程序阐述了400多个系统调用和函数,清楚地说明它们的用法、参数和返回值。为了使读者能融会贯通,书中还提供了几个贯穿整章的案例,每个案例都根据现在的技术环境进行了全面更新。
本书帮助几代程序员写出了可靠、强大、高性能的代码。第3版根据当今主流系统进行更新,更具实用价值。
目录
Chapter 1. UNIX System Overview / UNIX基础知识
1.1 Introduction / 引言
1.2 UNIX Architecture / UNIX体系结构
1.3 Logging In / 登录
1.4 Files and Directories / 文件和目录
1.5 Input and Output / 输入和输出
1.6 Programs and Processes / 程序和进程
1.7 Error Handling / 出错处理
1.8 User Identification / 用户标识
1.9 Signals / 信号
1.10 Time Values / 时间值
1.11 System Calls and Library Functions / 系统调用和库函数
1.12 Summary / 小结
Exercises / 习题
Chapter 2. UNIX Standardization and Implementations / UNIX标准及实现
2.1 Introduction / 引言
2.2 UNIX Standardization / UNIX标准化
2.2.1 ISO C
2.2.2 IEEE POSIX
2.2.3 The Single UNIX Specification
2.2.4 FIPS
2.3 UNIX System Implementations / UNIX系统实现
2.3.1 UNIX System V Release 4 / SVR4
2.3.2 4.4 BSD
2.3.3 FreeBSD
2.3.4 Linux
2.3.5 Mac OS X
2.3.6 Solaris
2.3.7 Other UNIX Systems / 其他UNIX系统
2.4 Relationship of Standards and Implementations / 标准和实现的关系
2.5 Limits / 限制
2.5.1 ISO C Limits / ISO C限制
2.5.2 POSIX Limits / POSIX限制
2.5.3 XSI Limits / XSI限制
2.5.4 sysconf, pathconf, and fpathconf Functions / 函数sysconf、pathconf和fpathconf
2.5.5 Indeterminate Runtime Limits / 不确定的运行时限制
2.6 Options / 选项
2.7 Feature Test Macros / 功能测试宏
2.8 Primitive System Data Types / 基本系统数据类型
2.9 Differences Between Standards / 标准之间的冲突
2.10 Summary / 小结
Exercises / 习题
Chapter 3. File I/O / 文件I/O
3.1 Introduction / 引言
3.2 File Descriptors / 文件描述符
3.3 open and openat Functions / 函数open和openat
3.4 creat Function / 函数creat
3.5 close Function / 函数close
3.6 lseek Function / 函数lseek
3.7 read Function / 函数read
3.8 write Function / 函数write
3.9 I/O Efficiency / I/O的效率
3.10 File Sharing / 文件共享
3.11 Atomic Operations / 原子操作
3.12 dup and dup2 Functions / 函数dup和dup2
3.13 sync, fsync, and fdatasync Functions / 函数sync、fsync和fdatasync
3.14 fcntl Function / 函数fcntl
3.15 ioctl Function / 函数ioctl
3.16 /dev/fd
3.17 Summary / 小结
Exercises / 习题
Chapter 4. Files and Directories / 文件和目录
4.1 Introduction / 引言
4.2 stat, fstat, fstatat, and lstat Functions / 函数stat、fstat、fstatat和lstat
4.3 File Types / 文件类型
4.4 Set-User-ID and Set-Group-ID / 设置用户ID和设置组ID
4.5 File Access Permissions / 文件访问权限
4.6 Ownership of New Files and Directories / 新文件和目录的所有权
4.7 access and faccessat Functions / 函数access和faccessat
4.8 umask Function / 函数umask
4.9 chmod, fchmod, and fchmodat Functions / 函数chmod、fchmod和fchmodat
4.10 Sticky Bit / 粘着位
4.11 chown, fchown, fchownat, and lchown Functions / 函数chown、fchown、fchownat和lchown
4.12 File Size / 文件长度
4.13 File Truncation / 文件截断
4.14 File Systems / 文件系统
4.15 link, linkat, unlink, unlinkat, and remove Functions / 函数link、linkat、unlink、unlinkat和remove
4.16 rename and renameat Functions / 函数rename和renameat
4.17 Symbolic Links / 符号链接
4.18 Creating and Reading Symbolic Links / 创建和读取符号链接
4.19 File Times / 文件的时间
4.20 futimens, utimensat, and utimes Functions / 函数futimens、utimensat和utimes
4.21 mkdir, mkdirat, and rmdir Functions / 函数mkdir、mkdirat和rmdir
4.22 Reading Directories / 读目录
4.23 chdir, fchdir, and getcwd Functions / 函数chdir、fchdir和getcwd
4.24 Device Special Files / 设备特殊文件
4.25 Summary of File Access Permission Bits / 文件访问权限位小结
4.26 Summary / 小结
Exercises / 习题
Chapter 5. Standard I/O Library / 标准I/O库
5.1 Introduction / 引言
5.2 Streams and FILE Objects / 流和FILE对象
5.3 Standard Input, Standard Output, and Standard Error / 标准输入、标准输出和标准错误
5.4 Buffering / 缓冲
5.5 Opening a Stream / 打开流
5.6 Reading and Writing a Stream / 读和写流
5.7 Line-at-a-Time I/O / 每次一行I/O
5.8 Standard I/O Efficiency / 标准I/O的效率
5.9 Binary I/O / 二进制I/O
5.10 Positioning a Stream / 定位流
5.11 Formatted I/O / 格式化I/O
5.12 Implementation Details / 实现细节
5.13 Temporary Files / 临时文件
5.14 Memory Streams / 内存流
5.15 Alternatives to Standard I/O / 标准I/O的替代软件
5.16 Summary / 小结
Exercises / 习题
Chapter 6. System Data Files and Information / 系统数据文件和信息
6.1 Introduction / 引言
6.2 Password File / 口令文件
6.3 Shadow Passwords / 阴影口令
6.4 Group File / 组文件
6.5 Supplementary Group IDs / 附属组ID
6.6 Implementation Differences / 实现区别
6.7 Other Data Files / 其他数据文件
6.8 Login Accounting / 登录账户记录
6.9 System Identification / 系统标识
6.10 Time and Date Routines / 时间和日期例程
6.11 Summary / 小结
Exercises / 习题
Chapter 7. Process Environment / 进程环境
7.1 Introduction / 引言
7.2 main Function / main函数
7.3 Process Termination / 进程终止
7.4 Command-Line Arguments / 命令行参数
7.5 Environment List / 环境表
7.6 Memory Layout of a C Program / C程序的存储空间布局
7.7 Shared Libraries / 共享库
7.8 Memory Allocation / 存储空间分配
7.9 Environment Variables / 环境变量
7.10 setjmp and longjmp Functions / 函数setjmp和longjmp
7.11 getrlimit and setrlimit Functions / 函数getrlimit和setrlimit
7.12 Summary / 小结
Exercises / 习题
Chapter 8. Process Control / 进程控制
8.1 Introduction / 引言
8.2 Process Identifiers / 进程标识
8.3 fork Function / 函数fork
8.4 vfork Function / 函数vfork
8.5 exit Functions / 函数exit
8.6 wait and waitpid Functions / 函数wait和waitpid
8.7 waitid Function / 函数waitid
8.8 wait3 and wait4 Functions / 函数wait3和wait4
8.9 Race Conditions / 竞争条件
8.10 exec Functions / 函数exec
8.11 Changing User IDs and Group IDs / 更改用户ID和更改组ID
8.12 Interpreter Files / 解释器文件
8.13 system Function / 函数system
8.14 Process Accounting / 进程会计
8.15 User Identification / 用户标识
8.16 Process Scheduling / 进程调度
8.17 Process Times / 进程时间
8.18 Summary / 小结
Exercises / 习题
Chapter 9. Process Relationships / 进程关系
9.1 Introduction / 引言
9.2 Terminal Logins / 终端登录
9.3 Network Logins / 网络登录
9.4 Process Groups / 进程组
9.5 Sessions / 会话
9.6 Controlling Terminal / 控制终端
9.7 tcgetpgrp, tcsetpgrp, and tcgetsid Functions / 函数tcgetpgrp、tcsetpgrp和tcgetsid
9.8 Job Control / 作业控制
9.9 Shell Execution of Programs / Shell执行程序
9.10 Orphaned Process Groups / 孤儿进程组
9.11 FreeBSD Implementation / FreeBSD实现
9.12 Summary / 小结
Exercises / 习题
Chapter 10. Signals / 信号
10.1 Introduction / 引言
10.2 Signal Concepts / 信号概念
10.3 signal Function / 函数signal
10.4 Unreliable Signals / 不可靠的信号
10.5 Interrupted System Calls / 中断的系统调用
10.6 Reentrant Functions / 可重入函数
10.7 SIGCLD Semantics / SIGCLD语义
10.8 Reliable-Signal Terminology and Semantics / 可靠信号术语和语义
10.9 kill and raise Functions / 函数kill和raise
10.10 alarm and pause Functions / 函数alarm和pause
10.11 Signal Sets / 信号集
10.12 sigprocmask Function / 函数sigprocmask
10.13 sigpending Function / 函数sigpending
10.14 sigaction Function / 函数sigaction
10.15 sigsetjmp and siglongjmp Functions / 函数sigsetjmp和siglongjmp
10.16 sigsuspend Function / 函数sigsuspend
10.17 abort Function / 函数abort
10.18 system Function / 函数system
10.19 sleep, nanosleep, and clock_nanosleep Functions / 函数sleep、nanosleep和clock_nanosleep
10.20 sigqueue Function / 函数sigqueue
10.21 Job-Control Signals / 作业控制信号
10.22 Signal Names and Numbers / 信号名和编号
10.23 Summary / 小结
Exercises / 习题
Chapter 11. Threads / 线程
11.1 Introduction / 引言
11.2 Thread Concepts / 线程概念
11.3 Thread Identification / 线程标识
11.4 Thread Creation / 线程创建
11.5 Thread Termination / 线程终止
11.6 Thread Synchronization / 线程同步
11.6.1 Mutexes / 互斥量
11.6.2 Deadlock Avoidance / 避免死锁
11.6.3 pthread_mutex_timedlock Function / 函数pthread_mutex_timedlock
11.6.4 Reader-Writer Locks / 读写锁
11.6.5 Reader-Writer Locking with Timeouts / 带有超时的读写锁
11.6.6 Condition Variables / 条件变量
11.6.7 Spin Locks / 自旋锁
11.6.8 Barriers / 屏障
11.7 Summary / 小结
Exercises / 习题
Chapter 12. Thread Control / 线程控制
12.1 Introduction / 引言
12.2 Thread Limits / 线程限制
12.3 Thread Attributes / 线程属性
12.4 Synchronization Attributes / 同步属性
12.4.1 Mutex Attributes / 互斥量属性
12.4.2 Reader-Writer Lock Attributes / 读写锁属性
12.4.3 Condition Variable Attributes / 条件变量属性
12.4.4 Barrier Attributes / 屏障属性
12.5 Reentrancy / 重入
12.6 Thread-Specific Data / 线程特定数据
12.7 Cancel Options / 取消选项
12.8 Threads and Signals / 线程和信号
12.9 Threads and fork / 线程和fork
12.10 Threads and I/O / 线程和I/O
12.11 Summary / 小结
Exercises / 习题
Chapter 13. Daemon Processes / 守护进程
13.1 Introduction / 引言
13.2 Daemon Characteristics / 守护进程的特征
13.3 Coding Rules / 编程规则
13.4 Error Logging / 出错记录
13.5 Single-Instance Daemons / 单实例守护进程
13.6 Daemon Conventions / 守护进程的惯例
13.7 Client-Server Model / 客户进程-服务器进程模型
13.8 Summary / 小结
Exercises / 习题
Chapter 14. Advanced I/O / 高级I/O
14.1 Introduction / 引言
14.2 Nonblocking I/O / 非阻塞I/O
14.3 Record Locking / 记录锁
14.4 I/O Multiplexing / I/O多路转接
14.4.1 select and pselect Functions / 函数select和pselect
14.4.2 poll Function / 函数poll
14.5 Asynchronous I/O / 异步I/O
14.5.1 System V Asynchronous I/O / System V异步I/O
14.5.2 BSD Asynchronous I/O / BSD异步I/O
14.5.3 POSIX Asynchronous I/O / POSIX异步I/O
14.6 readv and writev Functions / 函数readv和writev
14.7 readn and writen Functions / 函数readn和writen
14.8 Memory-Mapped I/O / 存储映射I/O
14.9 Summary / 小结
Exercises / 习题
Chapter 15. Interprocess Communication / 进程间通信
15.1 Introduction / 引言
15.2 Pipes / 管道
15.3 popen and pclose Functions / 函数popen和pclose
15.4 Coprocesses / 协同进程
15.5 FIFOs
15.6 XSI IPC
15.6.1 Identifiers and Keys / 标识符和键
15.6.2 Permission Structure / 权限结构
15.6.3 Configuration Limits / 结构限制
15.6.4 Advantages and Disadvantages / 优点和缺点
15.7 Message Queues / 消息队列
15.8 Semaphores / 信号量
15.9 Shared Memory / 共享存储
15.10 POSIX Semaphores / POSIX信号量
15.11 Client-Server Properties / 客户进程-服务器进程属性
15.12 Summary / 小结
Exercises / 习题
Chapter 16. Network IPC: Sockets / 网络IPC:套接字
16.1 Introduction / 引言
16.2 Socket Descriptors / 套接字描述符
16.3 Addressing / 寻址
16.3.1 Byte Ordering / 字节序
16.3.2 Address Formats / 地址格式
16.3.3 Address Lookup / 地址查询
16.3.4 Associating Addresses with Sockets / 将套接字与地址关联
16.4 Connection Establishment / 建立连接
16.5 Data Transfer / 数据传输
16.6 Socket Options / 套接字选项
16.7 Out-of-Band Data / 带外数据
16.8 Nonblocking and Asynchronous I/O / 非阻塞和异步I/O
16.9 Summary / 小结
Exercises / 习题
Chapter 17. Advanced IPC / 高级进程间通信
17.1 Introduction / 引言
17.2 UNIX Domain Sockets / UNIX域套接字
17.3 Unique Connections / 唯一连接
17.4 Passing File Descriptors / 传送文件描述符
17.5 An Open Server, Version 1 / 打开服务器进程第 1版
17.6 An Open Server, Version 2 / 打开服务器进程第 2版
17.7 Summary / 小结
Exercises / 习题
Chapter 18. Terminal I/O / 终端I/O
18.1 Introduction / 引言
18.2 Overview / 概述
18.3 Special Input Characters / 特殊输入字符
18.4 Getting and Setting Terminal Attributes / 获得和设置终端属性
18.5 Terminal Option Flags / 终端选项标志
18.6 stty Command / stty命令
18.7 Baud Rate Functions / 波特率函数
18.8 Line Control Functions / 行控制函数
18.9 Terminal Identification / 终端标识
18.10 Canonical Mode / 规范模式
18.11 Noncanonical Mode / 非规范模式
18.12 Terminal Window Size / 终端窗口大小
18.13 termcap, terminfo, and curses / termcap、terminfo和curses
18.14 Summary / 小结
Exercises / 习题
Chapter 19. Pseudo Terminals / 伪终端
19.1 Introduction / 引言
19.2 Overview / 概述
19.3 Opening Pseudo-Terminal Devices / 打开伪终端设备
19.4 pty_fork Function / 函数pty_fork
19.5 pty Program / pty程序
19.6 Using the pty Program / 使用pty程序
19.7 Advanced Features / 高级特性
19.8 Summary / 小结
Exercises / 习题
Chapter 20. A Database Library / 数据库函数库
20.1 Introduction / 引言
20.2 History / 历史
20.3 The Library / 函数库
20.4 Implementation Overview / 实现概述
20.5 Centralized or Decentralized / 集中式还是非集中式
20.6 Concurrency / 并发
20.7 Building the Library / 构造函数库
20.8 Source Code / 源代码
20.9 Performance / 性能
20.10 Summary / 小结
Exercises / 习题
Chapter 21. Communicating with a Network Printer / 与网络打印机通信
21.1 Introduction / 引言
21.2 The Internet Printing Protocol / 网络打印协议
21.3 The Hypertext Transfer Protocol / 超文本传输协议HTTP
21.4 Printer Spooling / 打印假脱机技术
21.5 Source Code / 源代码
21.6 Summary / 小结
Exercises / 习题
Appendix A. Function Prototypes / 函数原型
Appendix B. Miscellaneous Source Code / 其他源代码
B.1 Our Header File / 本书使用的头文件
B.2 Standard Error Routines / 标准出错例程
Appendix C. Solutions to Selected Exercises / 部分习题答案
Bibliography / 参考书目