Advanced Programming in the UNIX® Environment, Second Edition


About the Book

Source Code

The Lost Chapter

Errata

Additional Acknowledgements

FAQs

Contact the Author

Rich Stevens home page

Buy the book from Addison-Wesley Professional

Contents

Foreword xix
Preface xxi
Preface to the First Edition xxv

Chapter 1.   UNIX System Overview

1
1.1  Introduction        1  
1.2  UNIX Architecture        1  
1.3  Logging In        2  
1.4  Files and Directories        4  
1.5  Input and Output        8  
1.6  Programs and Processes        10  
1.7  Error Handling        14  
1.8  User Identification        16  
1.9  Signals        18  
1.10  Time Values        20  
1.11  System Calls and Library Functions        21  
1.12  Summary        23  
Chapter 2.  UNIX Standardization and Implementations 25
2.1  Introduction        25  
2.2  UNIX Standardization        25  
2.2.1  ISO C        25  
2.2.2  IEEE POSIX        26  
2.2.3  The Single UNIX Specification        29  
2.2.4  FIPS        33  
2.3  UNIX System Implementations        33  
2.3.1  UNIX System V Release 4        33  
2.3.2  4.4BSD        34  
2.3.3  FreeBSD        35  
2.3.4  Linux        35  
2.3.5  Mac OS X        35  
2.3.6  Solaris        35  
2.3.7  Other UNIX Systems        36  
2.4  Relationship of Standards and Implementations        36  
2.5  Limits        36  
2.5.1  ISO C Limits        38  
2.5.2  POSIX Limits        38  
2.5.3  XSI Limits        40  
2.5.4  sysconf, pathconf, and fpathconf Functions        41  
2.5.5  Indeterminate Runtime Limits        48  
2.6  Options        52  
2.7  Feature Test Macros        55  
2.8  Primitive System Data Types        56  
2.9  Conflicts Between Standards        56  
2.10  Summary        58  
Chapter 3.  File I/O 59
3.1  Introduction        59  
3.2  File Descriptors        59  
3.3  open Function        60  
3.4  creat Function        62  
3.5  close Function        63  
3.6  lseek Function        63  
3.7  read Function        67  
3.8  write Function        68  
3.9  I/O Efficiency        68  
3.10  File Sharing        70  
3.11  Atomic Operations        74  
3.12  dup and dup2 Functions        76  
3.13  sync, fsync, and fdatasync Functions        77  
3.14  fcntl Function        78  
3.15  ioctl Function        83  
3.16  /dev/fd        84  
3.17  Summary        85  
Chapter 4.  Files and Directories 87
4.1  Introduction        87  
4.2  stat, fstat, and lstat Functions        87  
4.3  File Types        88  
4.4  Set-User-ID and Set-Group-ID        91  
4.5  File Access Permissions        92  
4.6  Ownership of New Files and Directories        95  
4.7  access Function        95  
4.8  umask Function        97  
4.9  chmod and fchmod Functions        99  
4.10  Sticky Bit        101  
4.11  chown, fchown, and lchown Functions        102  
4.12  File Size        103  
4.13  File Truncation        105  
4.14  File Systems        105  
4.15  link, unlink, remove, and rename Functions        108  
4.16  Symbolic Links        112  
4.17  symlink and readlink Functions        115  
4.18  File Times        115  
4.19  utime Function        116  
4.20  mkdir and rmdir Functions        119  
4.21  Reading Directories        120  
4.22  chdir, fchdir, and getcwd Functions        125  
4.23  Device Special Files        127  
4.24  Summary of File Access Permission Bits        130  
4.25  Summary        130  
Chapter 5.  Standard I/O Library 133
5.1  Introduction        133  
5.2  Streams and FILE Objects        133  
5.3  Standard Input, Standard Output, and Standard Error        135  
5.4  Buffering        135  
5.5  Opening a Stream        138  
5.6  Reading and Writing a Stream        140  
5.7  Line-at-a-Time I/O        142  
5.8  Standard I/O Efficiency        143  
5.9  Binary I/O        145  
5.10  Positioning a Stream        147  
5.11  Formatted I/O        149  
5.12  Implementation Details        153  
5.13  Temporary Files        155  
5.14  Alternatives to Standard I/O        159  
5.15  Summary        159  
Chapter 6.  System Data Files and Information 161
6.1  Introduction        161
 
6.2  Password File        161  
6.3  Shadow Passwords        165  
6.4  Group File        166  
6.5  Supplementary Group IDs        167
 
6.6  Implementation Differences        169  
6.7  Other Data Files        169  
6.8  Login Accounting        170  
6.9  System Identification        171
 
6.10  Time and Date Routines        173  
6.11  Summary        177  
Chapter 7.  Process Environment 179
7.1  Introduction        179  
7.2  main Function        179  
7.3  Process Termination        180
 
7.4  Command-Line Arguments        185
 
7.5  Environment List        185
 
7.6  Memory Layout of a C Program        186  
7.7  Shared Libraries        188
 
7.8  Memory Allocation        189
 
7.9  Environment Variables        192  
7.10  setjmp and longjmp Functions        195  
7.11  getrlimit and setrlimit Functions        202  
7.12  Summary        206  
Chapter 8.  Process Control 209
8.1  Introduction        209
 
8.2  Process Identifiers        209  
8.3  fork Function        211  
8.4  vfork Function        216  
8.5  exit Functions        218
 
8.6  wait and waitpid Functions        220  
8.7  waitid Function        226  
8.8  wait3 and wait4 Functions        227  
8.9  Race Conditions        227  
8.10  exec Functions        231  
8.11  Changing User IDs and Group IDs        237  
8.12  Interpreter Files        242  
8.13  system Function        246  
8.14  Process Accounting        250  
8.15  User Identification        256  
8.16  Process Times        257  
8.17  Summary        259  
Chapter 9.  Process Relationships 261
9.1  Introduction        261  
9.2  Terminal Logins        261  
9.3  Network Logins        266  
9.4  Process Groups        269  
9.5  Sessions        270  
9.6  Controlling Terminal        272  
9.7  tcgetpgrp, tcsetpgrp, and tcgetsid Functions        273
 
9.8  Job Control        274
 
9.9  Shell Execution of Programs        278
 
9.10  Orphaned Process Groups        282
 
9.11  FreeBSD Implementation        285
 
9.12  Summary        287
 
Chapter 10.  Signals 289
10.1  Introduction        289  
10.2  Signal Concepts        289  
10.3  signal Function        298  
10.4  Unreliable Signals        301  
10.5  Interrupted System Calls        303  
10.6  Reentrant Functions        305
 
10.7  SIGCLD Semantics        308
 
10.8  Reliable-Signal Terminology and Semantics        310
 
10.9  kill and raise Functions        311
 
10.10  alarm and pause Functions        313
 
10.11  Signal Sets        318
 
10.12  sigprocmask Function        320  
10.13  sigpending Function        322  
10.14  sigaction Function        324  
10.15  sigsetjmp and siglongjmp Functions        329  
10.16  sigsuspend Function        333  
10.17  abort Function        340
 
10.18  system Function        342  
10.19  sleep Function        347  
10.20  Job-Control Signals        349  
10.21  Additional Features        352  
10.22  Summary        353  
Chapter 11.  Threads 355
11.1  Introduction        355  
11.2  Thread Concepts        355
 
11.3  Thread Identification        356
 
11.4  Thread Creation        357  
11.5  Thread Termination        360  
11.6  Thread Synchronization        368  
11.7  Summary        385
 
Chapter 12.  Thread Control 387
12.1  Introduction        387  
12.2  Thread Limits        387  
12.3  Thread Attributes        388  
12.4  Synchronization Attributes        393
 
12.5  Reentrancy        401
 
12.6  Thread-Specific Data        406  
12.7  Cancel Options        410  
12.8  Threads and Signals        413  
12.9  Threads and fork        416  
12.10  Threads and I/O        420  
12.11  Summary        420  
Chapter 13.  Daemon Processes 423
13.1  Introduction        423
 
13.2  Daemon Characteristics        423
 
13.3  Coding Rules        425  
13.4  Error Logging        428  
13.5  Single-Instance Daemons        432  
13.6  Daemon Conventions        434  
13.7  Client-Server Model        439  
13.8  Summary        439  
Chapter 14.  Advanced I/O 441
14.1  Introduction        441  
14.2  Nonblocking I/O        441
 
14.3  Record Locking        444
 
14.4  STREAMS        460
 
14.5  I/O Multiplexing        472
 
14.5.1  select and pselect Functions        474  
14.5.2  poll Function        479  
14.6  Asynchronous I/O        481  
14.6.1  System V Asynchronous I/O        481
 
14.6.2  BSD Asynchronous I/O        482  
14.7  readv and writev Functions        483
 
14.8  readn and writen Functions        485  
14.9  Memory-Mapped I/O        487
 
14.10  Summary        492  
Chapter 15.  Interprocess Communication 495
15.1  Introduction        495
 
15.2  Pipes        496  
15.3  popen and pclose Functions        503
 
15.4  Coprocesses        510
 
15.5  FIFOs        514  
15.6  XSI IPC        518  
15.6.1  Identifiers and Keys        518  
15.6.2  Permission Structure        520  
15.6.3  Configuration Limits        521  
15.6.4  Advantages and Disadvantages        521  
15.7  Message Queues        522  
15.8  Semaphores        527
 
15.9  Shared Memory        533  
15.10  Client-Server Properties        541  
15.11  Summary        543
 
Chapter 16.  Network IPC: Sockets 545
16.1  Introduction        545
 
16.2  Socket Descriptors        546  
16.3  Addressing        549
 
16.3.1  Byte Ordering        549  
16.3.2  Address Formats        551
 
16.3.3  Address Lookup        553  
16.3.4  Associating Addresses with Sockets        560  
16.4  Connection Establishment        561  
16.5  Data Transfer        565
 
16.6  Socket Options        579  
16.7  Out-of-Band Data        581
 
16.8  Nonblocking and Asynchronous I/O        582
 
16.9  Summary        583
 
Chapter 17.  Advanced IPC 585
17.1  Introduction        585  
17.2  STREAMS-Based Pipes        585  
17.2.1  Naming STREAMS Pipes        589  
17.2.2  Unique Connections        590  
17.3  UNIX Domain Sockets        594  
17.3.1  Naming UNIX Domain Sockets        595  
17.3.2  Unique Connections        597
 
17.4  Passing File Descriptors        601  
17.4.1  Passing File Descriptors over STREAMS-Based Pipes        604
 
17.4.2  Passing File Descriptors over UNIX Domain Sockets        606  
17.5  An Open Server, Version 1        615  
17.6  An Open Server, Version 2        620  
17.7  Summary        629  
Chapter 18.  Terminal I/O 631
18.1  Introduction        631
 
18.2  Overview        631
 
18.3  Special Input Characters        638  
18.4  Getting and Setting Terminal Attributes        643  
18.5  Terminal Option Flags        643  
18.6  stty Command        651  
18.7  Baud Rate Functions        652  
18.8  Line Control Functions        653  
18.9  Terminal Identification        654  
18.10  Canonical Mode        660  
18.11  Noncanonical Mode        663  
18.12  Terminal Window Size        670
 
18.13  termcap, terminfo, and curses        672
 
18.14  Summary        673  
Chapter 19.  Pseudo Terminals 675
19.1  Introduction        675  
19.2  Overview        675  
19.3  Opening Pseudo-Terminal Devices        681  
19.3.1  STREAMS-Based Pseudo Terminals        683  
19.3.2  BSD-Based Pseudo Terminals        686
 
19.3.3  Linux-Based Pseudo Terminals        689
 
19.4  pty_fork Function        691  
19.5  pty Program        694  
19.6  Using the pty Program        698  
19.7  Advanced Features        705  
19.8  Summary        706
 
Chapter 20.  A Database Library 709
20.1  Introduction        709  
20.2  History        709  
20.3  The Library        710  
20.4  Implementation Overview        712
 
20.5  Centralized or Decentralized?        716
 
20.6  Concurrency        718  
20.7  Building the Library        719
 
20.8  Source Code        719  
20.9  Performance        747
 
20.10  Summary        752
 
Chapter 21.  Communicating with a Network Printer 753
21.1  Introduction        753  
21.2  The Internet Printing Protocol        753  
21.3  The Hypertext Transfer Protocol        756  
21.4  Printer Spooling        757  
21.5  Source Code        758  
21.6  Summary        805  
Appendix A.  Function Prototypes 807
Appendix B.  Miscellaneous Source Code 843
B.1  Our Header File        843  
B.2  Standard Error Routines        846  
Appendix C.  Solutions to Selected Exercises 853
Bibliography 885
Index 891