You are previewing Advanced Programming in the UNIX® Environment, Third Edition.
O'Reilly logo
Advanced Programming in the UNIX® Environment, Third Edition

Book Description

For more than twenty years, serious C programmers have relied on one book for practical, in-depth knowledge of the programming interfaces that drive the UNIX and Linux kernels: W. Richard Stevens’ Advanced Programming in the UNIX® Environment. Now, once again, Rich’s colleague Steve Rago has thoroughly updated this classic work. The new third edition supports today’s leading platforms, reflects new technical advances and best practices, and aligns with Version 4 of the Single UNIX Specification.

Steve carefully retains the spirit and approach that have made this book so valuable. Building on Rich’s pioneering work, he begins with files, directories, and processes, carefully laying the groundwork for more advanced techniques, such as signal handling and terminal I/O. He also thoroughly covers threads and multithreaded programming, and socket-based IPC.

This edition covers more than seventy new interfaces, including POSIX asynchronous I/O, spin locks, barriers, and POSIX semaphores. Most obsolete interfaces have been removed, except for a few that are ubiquitous. Nearly all examples have been tested on four modern platforms: Solaris 10, Mac OS X version 10.6.8 (Darwin 10.8.0), FreeBSD 8.0, and Ubuntu version 12.04 (based on Linux 3.2).

As in previous editions, you’ll learn through examples, including more than ten thousand lines of downloadable, ISO C source code. More than four hundred system calls and functions are demonstrated with concise, complete programs that clearly illustrate their usage, arguments, and return values. To tie together what you’ve learned, the book presents several chapter-length case studies, each reflecting contemporary environments.

Advanced Programming in the UNIX® Environment has helped generations of programmers write code with exceptional power, performance, and reliability. Now updated for today’s systems, this third edition will be even more valuable.

Table of Contents

  1. About This eBook
  2. Title Page
  3. Copyright Page
  4. Praise for Advanced Programming in the UNIX® Environment, Second Edition
  5. Praise for the First Edition
  6. Dedication Page
  7. Contents
  8. Foreword to the Second Edition
  9. Preface
    1. Introduction
    2. Changes from the Second Edition
    3. Acknowledgments
  10. Preface to the Second Edition
    1. Introduction
    2. Changes from the First Edition
    3. Acknowledgments
  11. Preface to the First Edition
    1. Introduction
    2. Unix Standards
    3. Organization of the Book
    4. Examples in the Text
    5. Systems Used to Test the Examples
    6. Acknowledgments
  12. 1. UNIX System Overview
    1. 1.1. Introduction
    2. 1.2. UNIX Architecture
    3. 1.3. Logging In
    4. 1.4. Files and Directories
    5. 1.5. Input and Output
    6. 1.6. Programs and Processes
    7. 1.7. Error Handling
    8. 1.8. User Identification
    9. 1.9. Signals
    10. 1.10. Time Values
    11. 1.11. System Calls and Library Functions
    12. 1.12. Summary
    13. Exercises
  13. 2. UNIX Standardization and Implementations
    1. 2.1. Introduction
    2. 2.2. UNIX Standardization
    3. 2.3. UNIX System Implementations
    4. 2.4. Relationship of Standards and Implementations
    5. 2.5. Limits
    6. 2.6. Options
    7. 2.7. Feature Test Macros
    8. 2.8. Primitive System Data Types
    9. 2.9. Differences Between Standards
    10. 2.10. Summary
    11. Exercises
  14. 3. File I/O
    1. 3.1. Introduction
    2. 3.2. File Descriptors
    3. 3.3. open and openat Functions
    4. 3.4. creat Function
    5. 3.5. close Function
    6. 3.6. lseek Function
    7. 3.7. read Function
    8. 3.8. write Function
    9. 3.9. I/O Efficiency
    10. 3.10. File Sharing
    11. 3.11. Atomic Operations
    12. 3.12. dup and dup2 Functions
    13. 3.13. sync, fsync, and fdatasync Functions
    14. 3.14. fcntl Function
    15. 3.15. ioctl Function
    16. 3.16. /dev/fd
    17. 3.17. Summary
    18. Exercises
  15. 4. Files and Directories
    1. 4.1. Introduction
    2. 4.2. stat, fstat, fstatat, and lstat Functions
    3. 4.3. File Types
    4. 4.4. Set-User-ID and Set-Group-ID
    5. 4.5. File Access Permissions
    6. 4.6. Ownership of New Files and Directories
    7. 4.7. access and faccessat Functions
    8. 4.8. umask Function
    9. 4.9. chmod, fchmod, and fchmodat Functions
    10. 4.10. Sticky Bit
    11. 4.11. chown, fchown, fchownat, and lchown Functions
    12. 4.12. File Size
    13. 4.13. File Truncation
    14. 4.14. File Systems
    15. 4.15. link, linkat, unlink, unlinkat, and remove Functions
    16. 4.16. rename and renameat Functions
    17. 4.17. Symbolic Links
    18. 4.18. Creating and Reading Symbolic Links
    19. 4.19. File Times
    20. 4.20. futimens, utimensat, and utimes Functions
    21. 4.21. mkdir, mkdirat, and rmdir Functions
    22. 4.22. Reading Directories
    23. 4.23. chdir, fchdir, and getcwd Functions
    24. 4.24. Device Special Files
    25. 4.25. Summary of File Access Permission Bits
    26. 4.26. Summary
    27. Exercises
  16. 5. Standard I/O Library
    1. 5.1. Introduction
    2. 5.2. Streams and FILE Objects
    3. 5.3. Standard Input, Standard Output, and Standard Error
    4. 5.4. Buffering
    5. 5.5. Opening a Stream
    6. 5.6. Reading and Writing a Stream
    7. 5.7. Line-at-a-Time I/O
    8. 5.8. Standard I/O Efficiency
    9. 5.9. Binary I/O
    10. 5.10. Positioning a Stream
    11. 5.11. Formatted I/O
    12. 5.12. Implementation Details
    13. 5.13. Temporary Files
    14. 5.14. Memory Streams
    15. 5.15. Alternatives to Standard I/O
    16. 5.16. Summary
    17. Exercises
  17. 6. System Data Files and Information
    1. 6.1. Introduction
    2. 6.2. Password File
    3. 6.3. Shadow Passwords
    4. 6.4. Group File
    5. 6.5. Supplementary Group IDs
    6. 6.6. Implementation Differences
    7. 6.7. Other Data Files
    8. 6.8. Login Accounting
    9. 6.9. System Identification
    10. 6.10. Time and Date Routines
    11. 6.11. Summary
    12. Exercises
  18. 7. Process Environment
    1. 7.1. Introduction
    2. 7.2. main Function
    3. 7.3. Process Termination
    4. 7.4. Command-Line Arguments
    5. 7.5. Environment List
    6. 7.6. Memory Layout of a C Program
    7. 7.7. Shared Libraries
    8. 7.8. Memory Allocation
    9. 7.9. Environment Variables
    10. 7.10. setjmp and longjmp Functions
    11. 7.11. getrlimit and setrlimit Functions
    12. 7.12. Summary
    13. Exercises
  19. 8. Process Control
    1. 8.1. Introduction
    2. 8.2. Process Identifiers
    3. 8.3. fork Function
    4. 8.4. vfork Function
    5. 8.5. exit Functions
    6. 8.6. wait and waitpid Functions
    7. 8.7. waitid Function
    8. 8.8. wait3 and wait4 Functions
    9. 8.9. Race Conditions
    10. 8.10. exec Functions
    11. 8.11. Changing User IDs and Group IDs
    12. 8.12. Interpreter Files
    13. 8.13. system Function
    14. 8.14. Process Accounting
    15. 8.15. User Identification
    16. 8.16. Process Scheduling
    17. 8.17. Process Times
    18. 8.18. Summary
    19. Exercises
  20. 9. Process Relationships
    1. 9.1. Introduction
    2. 9.2. Terminal Logins
    3. 9.3. Network Logins
    4. 9.4. Process Groups
    5. 9.5. Sessions
    6. 9.6. Controlling Terminal
    7. 9.7. tcgetpgrp, tcsetpgrp, and tcgetsid Functions
    8. 9.8. Job Control
    9. 9.9. Shell Execution of Programs
    10. 9.10. Orphaned Process Groups
    11. 9.11. FreeBSD Implementation
    12. 9.12. Summary
    13. Exercises
  21. 10. Signals
    1. 10.1. Introduction
    2. 10.2. Signal Concepts
    3. 10.3. signal Function
    4. 10.4. Unreliable Signals
    5. 10.5. Interrupted System Calls
    6. 10.6. Reentrant Functions
    7. 10.7. SIGCLD Semantics
    8. 10.8. Reliable-Signal Terminology and Semantics
    9. 10.9. kill and raise Functions
    10. 10.10. alarm and pause Functions
    11. 10.11. Signal Sets
    12. 10.12. sigprocmask Function
    13. 10.13. sigpending Function
    14. 10.14. sigaction Function
    15. 10.15. sigsetjmp and siglongjmp Functions
    16. 10.16. sigsuspend Function
    17. 10.17. abort Function
    18. 10.18. system Function
    19. 10.19. sleep, nanosleep, and clock_nanosleep Functions
    20. 10.20. sigqueue Function
    21. 10.21. Job-Control Signals
    22. 10.22. Signal Names and Numbers
    23. 10.23. Summary
    24. Exercises
  22. 11. Threads
    1. 11.1. Introduction
    2. 11.2. Thread Concepts
    3. 11.3. Thread Identification
    4. 11.4. Thread Creation
    5. 11.5. Thread Termination
    6. 11.6. Thread Synchronization
    7. 11.7. Summary
    8. Exercises
  23. 12. Thread Control
    1. 12.1. Introduction
    2. 12.2. Thread Limits
    3. 12.3. Thread Attributes
    4. 12.4. Synchronization Attributes
    5. 12.5. Reentrancy
    6. 12.6. Thread-Specific Data
    7. 12.7. Cancel Options
    8. 12.8. Threads and Signals
    9. 12.9. Threads and fork
    10. 12.10. Threads and I/O
    11. 12.11. Summary
    12. Exercises
  24. 13. Daemon Processes
    1. 13.1. Introduction
    2. 13.2. Daemon Characteristics
    3. 13.3. Coding Rules
    4. 13.4. Error Logging
    5. 13.5. Single-Instance Daemons
    6. 13.6. Daemon Conventions
    7. 13.7. Client–Server Model
    8. 13.8. Summary
    9. Exercises
  25. 14. Advanced I/O
    1. 14.1. Introduction
    2. 14.2. Nonblocking I/O
    3. 14.3. Record Locking
    4. 14.4. I/O Multiplexing
    5. 14.5. Asynchronous I/O
    6. 14.6. readv and writev Functions
    7. 14.7. readn and writen Functions
    8. 14.8. Memory-Mapped I/O
    9. 14.9. Summary
    10. Exercises
  26. 15. Interprocess Communication
    1. 15.1. Introduction
    2. 15.2. Pipes
    3. 15.3. popen and pclose Functions
    4. 15.4. Coprocesses
    5. 15.5. FIFOs
    6. 15.6. XSI IPC
    7. 15.7. Message Queues
    8. 15.8. Semaphores
    9. 15.9. Shared Memory
    10. 15.10. POSIX Semaphores
    11. 15.11. Client–Server Properties
    12. 15.12. Summary
    13. Exercises
  27. 16. Network IPC: Sockets
    1. 16.1. Introduction
    2. 16.2. Socket Descriptors
    3. 16.3. Addressing
    4. 16.4. Connection Establishment
    5. 16.5. Data Transfer
    6. 16.6. Socket Options
    7. 16.7. Out-of-Band Data
    8. 16.8. Nonblocking and Asynchronous I/O
    9. 16.9. Summary
    10. Exercises
  28. 17. Advanced IPC
    1. 17.1. Introduction
    2. 17.2. UNIX Domain Sockets
    3. 17.3. Unique Connections
    4. 17.4. Passing File Descriptors
    5. 17.5. An Open Server, Version 1
    6. 17.6. An Open Server, Version 2
    7. 17.7. Summary
    8. Exercises
  29. 18. Terminal I/O
    1. 18.1. Introduction
    2. 18.2. Overview
    3. 18.3. Special Input Characters
    4. 18.4. Getting and Setting Terminal Attributes
    5. 18.5. Terminal Option Flags
    6. 18.6. stty Command
    7. 18.7. Baud Rate Functions
    8. 18.8. Line Control Functions
    9. 18.9. Terminal Identification
    10. 18.10. Canonical Mode
    11. 18.11. Noncanonical Mode
    12. 18.12. Terminal Window Size
    13. 18.13. termcap, terminfo, and curses
    14. 18.14. Summary
    15. Exercises
  30. 19. Pseudo Terminals
    1. 19.1. Introduction
    2. 19.2. Overview
    3. 19.3. Opening Pseudo-Terminal Devices
    4. 19.4. pty_fork Function
    5. 19.5. pty Program
    6. 19.6. Using the pty Program
    7. 19.7. Advanced Features
    8. 19.8. Summary
    9. Exercises
  31. 20. A Database Library
    1. 20.1. Introduction
    2. 20.2. History
    3. 20.3. The Library
    4. 20.4. Implementation Overview
    5. 20.5. Centralized or Decentralized?
    6. 20.6. Concurrency
    7. 20.7. Building the Library
    8. 20.8. Source Code
    9. 20.9. Performance
    10. 20.10. Summary
    11. Exercises
  32. 21. Communicating with a Network Printer
    1. 21.1. Introduction
    2. 21.2. The Internet Printing Protocol
    3. 21.3. The Hypertext Transfer Protocol
    4. 21.4. Printer Spooling
    5. 21.5. Source Code
    6. 21.6. Summary
    7. Exercises
  33. Appendix A. Function Prototypes
  34. Appendix B. Miscellaneous Source Code
    1. B.1. Our Header File
    2. B.2. Standard Error Routines
  35. Appendix C. Solutions to Selected Exercises
    1. Chapter 1
    2. Chapter 2
    3. Chapter 3
    4. Chapter 4
    5. Chapter 5
    6. Chapter 6
    7. Chapter 7
    8. Chapter 8
    9. Chapter 9
    10. Chapter 10
    11. Chapter 11
    12. Chapter 12
    13. Chapter 13
    14. Chapter 14
    15. Chapter 15
    16. Chapter 16
    17. Chapter 17
    18. Chapter 18
    19. Chapter 19
    20. Chapter 20
    21. Chapter 21
  36. Bibliography
  37. Index