DescriptionThe new third edition of Advanced Programming in the UNIX® Environment supports today’s leading platforms, reflects new technical advances and best practices, and aligns with Version 4 of the Single UNIX Specification. This valuable tool begins with files, directories, and processes, carefully laying the groundwork for more advanced techniques, such as signal handling and terminal I/O then 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. Students are given 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 they’ve learned, the book presents several chapter-length case studies, each reflecting contemporary environments.Key FeaturesW. Richard Stevens’ definitive, classic guide to UNIX® programming, now 100% updated for today’s leading systems and distributions!The #1 guide to UNIX/Linux programming since 1992: now updated to address newer versions of Red Hat, Solaris, FreeBSD, and Mac OS X, cover Ubuntu Linux, and reflect newer techniques and best practicesContains nearly 10,000 lines of code, all carefully tested on today’s most widely-used UNIX/Linux platformsNew to this EditionUpdates of systems and distributionsIncludes RedHat 2.6x, Solaris 10, MacOS 10.5, FreeBSD 7.0, and Ubuntu LinuxTable of ContentsForeword to the Second Edition xixPreface xxiPreface to the Second Edition xxvPreface to the First Edition xxixChapter 1: UNIX System Overview 1Introduction 1UNIX Architecture 1Logging In 2Files and Directories 4Input and Output 8Programs and Processes 10Error Handling 14User Identification 16Signals 18Time Values 20System Calls and Library Functions 21Summary 23Chapter 2: UNIX Standardization and Implementations 25Introduction 25UNIX Standardization 25UNIX System Implementations 33Relationship of Standards and Implementations 36Limits 36Options 53Feature Test Macros 57Primitive System Data Types 58Differences Between Standards 58Summary 60Chapter 3: File I/O 61Introduction 61File Descriptors 61open and openat Functions 62creat Function 66close Function 66lseek Function 66read Function 71write Function 72I/O Efficiency 72File Shar ing 74Atomic Operations 77dup and dup2 Functions 79sync, fsync, and fdatasync Functions 81fcntl Function 82ioctl Function 87/dev/fd 88Summary 90Chapter 4: Files and Directories 93Introduction 93stat, fstat, fstatat, and lstat Functions 93File Types 95Set-User-ID and Set-Group-ID 98File Access Per missions 99Ownership of New Files and Directories 101access and faccessat Functions 102umask Function 104chmod, fchmod, and fchmodat Functions 106Sticky Bit 108chown, fchown, fchownat, and lchown Functions 109File Size 111File Tr uncation 112File Systems 113link, linkat, unlink, unlinkat, and remove Functions 116rename and renameat Functions 119Symbolic Links 120Creating and Reading Symbolic Links 123File Times 124futimens, utimensat, and utimes Functions 126mkdir, mkdirat, and rmdir Functions 129Reading Director ies 130chdir, fchdir, and getcwd Functions 135Device Special Files 137Summary of File Access Per mission Bits 140Summary 140Chapter 5: Standard I/O Library 143Introduction 143Streams and FILE Objects 143Standard Input, Standard Output, and Standard Error 145Buffering 145Opening a Stream 148Reading and Writing a Stream 150Line-at-a-Time I/O 152Standard I/O Efficiency 153Binary I/O 156Positioning a Stream 157For matted I/O 159Implementation Details 164Temporary Files 167Memory Streams 171Alternatives to Standard I/O 174Summary 175Chapter 6: System Data Files and Information 177Introduction 177Password File 177Shadow Passwords 181Group File 182Supplementary Group IDs 183Implementation Differences 184Other Data Files 185Login Accounting 186System Identification 187Time and Date Routines 189Summary 196Chapter 7: Process Environment 197Introduction 197main Function 197Process Termination 198Command-Line Arguments 203Environment List 203Memory Lay out of a C Program 204Shared Libraries 206Memory Allocation 207Environment Var iables 210setjmp and longjmp Functions 213getrlimit and setrlimit Functions 220Summary 225Chapter 8: Process Control 227Introduction 227Process Identifiers 227fork Function 229vfork Function 234exit Functions 236wait and waitpid Functions 238waitid Function 244wait3 and wait4 Functions 245Race Conditions 245exec Functions 249Changing User IDs and Group IDs 255Interpreter Files 260system Function 264Process Accounting 269User Identification 275Process Scheduling 276Process Times 280Summary 282Chapter 9: Process Relationships 285Introduction 285Terminal Logins 285Network Logins 290Process Groups 293Sessions 295Controlling Terminal 296tcgetpgrp, tcsetpgrp, and tcgetsid Functions 298Job Control 299Shell Execution of Programs 303Orphaned Process Groups 307FreeBSD Implementation 310Summary 312Chapter 10: Signals 313Introduction 313Signal Concepts 313signal Function 323Unreliable Signals 326Interrupted System Calls 327Reentrant Functions 330SIGCLD Semantics 332Reliable-Signal Terminology and Semantics 335kill and raise Functions 336alarm and pause Functions 338Signal Sets 344sigprocmask Function 346sigpending Function 347sigaction Function 349sigsetjmp and siglongjmp Functions 355sigsuspend Function 359abort Function 365system Function 367sleep, nanosleep, and clock_nanosleep Functions 373sigqueue Function 376Job-Control Signals 377Signal Names and Numbers 379Summary 381Chapter 11: Threads 383Introduction 383Thread Concepts 383Thread Identification 384Thread Creation 385Thread Termination 388Thread Synchronization 397Summary 422Chapter 12: Thread Control 425Introduction 425Thread Limits 425Thread Attributes 426Synchronization Attributes 430Reentrancy 442Thread-Specific Data 446Cancel Options 451Threads and Signals 453Threads and fork 457Threads and I/O 461Summary 462Chapter 13: Daemon Processes 463Introduction 463Daemon Characteristics 463Coding Rules 466Error Logging 469Single-Instance Daemons 473Daemon Conventions 474Client–Server Model 479Summary 480Chapter 14: Advanced I/O 481Introduction 481Nonblocking I/O 481Record Locking 485I/O Multiplexing 500Asynchronous I/O 509readv and writev Functions 521readn and writen Functions 523Memory-Mapped I/O 525Summary 531Chapter 15: Interprocess Communication 533Introduction 533Pipes 534popen and pclose Functions 541Coprocesses 548FIFOs 552XSI IPC 556Message Queues 561Semaphores 565Shared Memory 571POSIX Semaphores 579Client–Server Proper ties 585Summary 587Chapter 16: Network IPC: Sockets 589Introduction 589Socket Descriptors 590Addressing 593Connection Establishment 605Data Transfer 610Socket Options 623Out-of-Band Data 626Nonblocking and Asynchronous I/O 627Summary 628Chapter 17: Advanced IPC 629Introduction 629UNIX Domain Sockets 629Unique Connections 635Passing File Descriptors 642An Open Server, Version 1 653An Open Server, Version 2 659Summary 669Chapter 18: Terminal I/O 671Introduction 671Over view 671Special Input Characters 678Getting and Setting Terminal Attributes 683Terminal Option Flags 683stty Command 691Baud Rate Functions 692Line Control Functions 693Terminal Identification 694Canonical Mode 700Noncanonical Mode 703Terminal Window Size 710termcap, terminfo, and curses 712Summary 713Chapter 19: Pseudo Terminals 715Introduction 715Over view 715Opening Pseudo-Terminal Devices 722pty_fork Function 726pty Program 729Using the pty Program 733Advanced Features 740Summary 741Chapter 20: A Database Library 743Introduction 743History 743The Library 744Implementation Over view 746Centralized or Decentralized? 750Concurrency 752Building the Library 753Source Code 753Performance 781Summary 786Chapter 21: Communicating with a Network Printer 789Introduction 789The Inter net Printing Protocol 789The Hyper text Transfer Protocol 792Printer Spooling 793Source Code 795Summary 843Appendix A: Function Prototypes 845Appendix B: Miscellaneous Source Code 895B.1 Our Header File 895B.2 Standard Error Routines 898Appendix C: Solutions to Selected Exercises 905Bibliography 947Index 955Authors BiographyThe late W. Richard Stevens was the acclaimed author of UNIX® Network Programming, Volumes 1 and 2, widely recognized as the classic texts in UNIX networking; TCP/IP Illustrated, Volumes 1-3; and the first edition of this book.Stephen A. Rago is the author of UNIX® System V Network Programming (Addison-Wesley, 1993). Rago was one of the Bell Laboratories developers who built UNIX System V Release 4. He served as a technical reviewer for the first edition of Advanced Programming in the UNIX® Environment. Rago currently works as a research staff member in the Storage Systems Group at NEC Laboratories America.