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.
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.


eBook
Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
Related collections and offers
Overview
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.
Product Details
ISBN-13: | 9780321638007 |
---|---|
Publisher: | Pearson Education |
Publication date: | 06/10/2013 |
Series: | Addison-Wesley Professional Computing Series |
Sold by: | Barnes & Noble |
Format: | eBook |
Pages: | 1032 |
File size: | 84 MB |
Note: | This product may take a few minutes to download. |
Age Range: | 18 Years |
About the Author
The 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.
Read an Excerpt
Introduction
Rich Stevens and I first met through an e-mail exchange when I reported a typographical error in his first book, UNIX Network Programming. He used to kid me about being the person to send him his first errata notice for the book. Until his death in 1999, we exchanged e-mail irregularly, usually when one of us had a question we thought the other might be able to answer. We met for dinner at USENIX conferences and when Rich was teaching in the area.
Rich Stevens was a friend who always conducted himself as a gentleman. When I wrote UNIX System V Network Programming in 1993, I intended it to be a System V version of Rich's UNIX Network Programming. As was his nature, Rich gladly reviewed chapters for me, and treated me not as a competitor, but as a colleague. We often talked about collaborating on a STREAMS version of his TCP/IP Illustrated book. Had events been different, we might have actually done it, but since Rich is no longer with us, revising Advanced Programming in the UNIX Environment is the closest I'll ever get to writing a book with him.When the editors at Addison-Wesley told me that they wanted to update Rich's book, I thought that there wouldn't be too much to change. Even after 13 years, Rich's work still holds up well. But the UNIX industry is vastly different today from what it was when the book was first published.
- The System V variants are slowly being replaced by Linux. The major system vendors that ship their hardware with their own versions of the UNIX System have either made Linux ports available or announced support for Linux. Solaris is perhaps the last descendant of UNIX System V Release 4 with any appreciablemarket share.
- After 4.4BSD was released, the Computing Science Research Group (CSRG) from the University of California at Berkeley decided to put an end to its development of the UNIX operating system, but several different groups of volunteers still maintain publicly available versions.
- The introduction of Linux, supported by thousands of volunteers, has made it possible for anyone with a computer to run an operating system similar to the UNIX System, with freely available source code for the newest hardware devices. The success of Linux is something of a curiosity, given that several free BSD alternatives are readily available.
- Continuing its trend as an innovative company, Apple Computer abandoned its old Mac operating system and replaced it with one based on Mach and FreeBSD.
Thus, I've tried to update the information presented in this book to reflect these four platforms.
After Rich wrote Advanced Programming in the UNIX Environment in 1992, I got rid of most of my UNIX programmer 's manuals. To this day, the two books I keep closest to my desk are a dictionary and a copy of Advanced Programming in the UNIX Environment. I hope you find this revision equally useful.Changes from the First Edition
Rich's work holds up well. I've tried not to change his original vision for this book, but a lot has happened in 13 years. This is especially true with the standards that affect the UNIX programming interface.
Throughout the book, I've updated interfaces that have changed from the ongoing efforts in standards organizations. This is most noticeable in Chapter 2, since its primary topic is standards. The 2001 version of the POSIX.1 standard, which we use in this revision, is much more comprehensive than the 1990 version on which the first edition of this book was based. The 1990 ISO C standard was updated in 1999, and some changes affect the interfaces in the POSIX.1 standard.A lot more interfaces are now covered by the POSIX.1 specification. The base specifications of the Single UNIX Specification (published by The Open Group, formerly X/Open) have been merged with POSIX.1. POSIX.1 now includes several 1003.1 standards and draft standards that were formerly published separately. Accordingly, I've added chapters to cover some new topics. Threads and multithreaded programming are important concepts because they present a cleaner way for programmers to deal with concurrency and asynchrony.
The socket interface is now part of POSIX.1. It provides a single interface to interprocess communication (IPC), regardless of the location of the process, and is a natural extension of the IPC chapters.
I've omitted most of the real-time interfaces that appear in POSIX.1. These are best treated in a text devoted to real-time programming. One such book appears in the bibliography.
I've updated the case studies in the last chapters to cover more relevant real-world examples. For example, few systems these days are connected to a PostScript printer via a serial or parallel port. Most PostScript printers today are accessed via a network, so I've changed the case study that deals with PostScript printer communication to take this into account.
The chapter on modem communication is less relevant these days. So that the original material is not lost, however, it is available on the book's Web site in two formats: PostScript (http://www.apuebook.com/lostchapter/modem.ps) and PDF (http://www.apuebook.com/lostchapter/modem.pdf).
The source code for the examples shown in this book is also available at www.apuebook.com. Most of the examples have been run on four platforms:
- FreeBSD 5.2.1, a derivative of the 4.4BSD release from the Computer Systems Research Group at the University of California at Berkeley, running on an Intel Pentium processor
- Linux 2.4.22 (the Mandrake 9.2 distribution), a free UNIX-like operating system, running on Intel Pentium processors
- Solaris 9, a derivative of System V Release 4 from Sun Microsystems, running on a64-bit UltraSPARC IIi processor
- Darwin 7.4.0, an operating environment based on FreeBSD and Mach, supported by Apple Mac OS X, version 10.3, on a PowerPC processor
Stephen A. Rago
Warren, New Jersey
April 2005
sar@apuebook.comPreface to the First EditionIntroduction
This book describes the programming interface to the Unix system—the system call interface and many of the functions provided in the standard C library. It is intended for anyone writing programs that run under Unix.
Like most operating systems, Unix provides numerous services to the programs that are running—open a file, read a file, start a new program, allocate a region of memory, get the current time-of-day, and so on. This has been termed the system call interface. Additionally, the standard C library provides numerous functions that are used by almost every C program (format a variable's value for output, compare two strings, etc.).
The system call interface and the library routines have traditionally been described in Sections 2 and 3 of the Unix Programmer 's Manual. This book is not a duplication of these sections. Examples and rationale are missing from the Unix Programmer 's Manual, and that's what this book provides.Unix Standards
The proliferation of different versions of Unix during the 1980s has been tempered by the various international standards that were started during the late 1980s. These include the ANSI standard for the C programming language, the IEEE POSIX family (still being developed), and the X/Open portability guide. This book also describes these standards. But instead of just describing the standards by themselves, we describe them in relation to popular implementations of the standards—System V Release 4 and the forthcoming 4.4BSD. This provides a real-world description, which is often lacking from the standard itself and from books that describe only the standard.Organization of the Book
This book is divided into six parts:
An overview and introduction to basic Unix programming concepts and terminology (Chapter 1), with a discussion of the various Unix standardization efforts and different Unix implementations (Chapter 2).
- I/O—unbuffered I/O (Chapter 3), properties of files and directories (Chapter 4), the standard I/O library (Chapter 5), and the standard system data files (Chapter 6).
- Processes—the environment of a Unix process (Chapter 7), process control (Chapter 8), the relationships between different processes (Chapter 9), and signals (Chapter 10).
- More I/O—terminal I/O (Chapter 11), advanced I/O (Chapter 12), and daemon processes (Chapter 13).
- IPC—Interprocess communication (Chapters 14 and 15).
- Examples—a database library (Chapter 16), communicating with a PostScript printer (Chapter 17), a modem dialing program (Chapter 18), and using pseudo terminals (Chapter 19).
A reading familiarity with C would be beneficial as would some experience using Unix. No prior programming experience with Unix is assumed. This text is intended for programmers familiar with Unix and programmers familiar with some other operating system who wish to learn the details of the services provided by most Unix systems.Examples in the Text
This book contains many examples—approximately 10,000 lines of source code. All the examples are in the C programming language. Furthermore, these examples are in ANSI C. You should have a copy of the Unix Programmer's Manual for your system handy while reading this book, since reference is made to it for some of the more esoteric and implementation-dependent features.
Almost every function and system call is demonstrated with a small, complete program. This lets us see the arguments and return values and is often easier to comprehend than the use of the function in a much larger program. But since some of the small programs are contrived examples, a few bigger examples are also included (Chapters 16, 17, 18, and 19). These larger examples demonstrate the programming techniques in larger, real-world examples.Systems Used to Test the Examples
Unfortunately all operating systems are moving targets. Unix is no exception. The following diagram shows the recent evolution of the various versions of System V and 4.xBSD.
4.xBSD are the various systems from the Computer Systems Research Group at the University of California at Berkeley. This group also distributes the BSD Net 1 and BSD Net 2 releases—publicly available source code from the 4.xBSD systems. SVRx refers to System V Release x from AT&T. XPG3 is the X/Open Portability Guide, Issue 3, and ANSI C is the ANSI standard for the C programming language. POSIX.1 is the IEEE and ISO standard for the interface to a Unix-like system. We'll have more to say about these different standards and the various versions of Unix in Sections 2.2 and 2.3.
In this text we use the term 4.3+BSDto refer to the Unix system from Berkeley that is somewhere between the BSD Net 2 release and 4.4BSD. At the time of this writing, 4.4BSD was not released, so the system could not be called 4.4BSD. Nevertheless a simple name was needed to refer to this system and 4.3+BSD is used throughout the text.
Most of the examples in this text have been run on four different versions of Unix:
- Unix System V/386 Release 4.0 Version 2.0 (''vanilla SVR4'') from U.H. Corp. (UHC), on an Intel 80386 processor.
- 4.3+BSD at the Computer Systems Research Group, Computer Science Division, University of California at Berkeley, on a Hewlett Packard workstation.
- BSD/386 (a derivative of the BSD Net 2 release) from Berkeley Software Design, Inc., on an Intel 80386 processor. This system is almost identical to what we call 4.3+BSD.
- SunOS 4.1.1 and 4.1.2 (systems with a strong Berkeley heritage but many System V features) from Sun Microsystems, on a SPARCstation SLC.
Numerous timing tests are provided in the text and the systems used for the test are identified.
W. Richard Stevens
Tucson, Arizona
April 1992
Table of Contents
Foreword to the Second Edition xix
Preface xxi
Preface to the Second Edition xxv
Preface to the First Edition xxix
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 Librar y Functions 21
1.12 Summary 23
Chapter 2: UNIX Standardization and Implementations 25
2.1 Introduction 25
2.2 UNIX Standardization 25
2.3 UNIX System Implementations 33
2.4 Relationship of Standards and Implementations 36
2.5 Limits 36
2.6 Options 53
2.7 Feature Test Macros 57
2.8 Primitive System Data Types 58
2.9 Differences Between Standards 58
2.10 Summary 60
Chapter 3: File I/O 61
3.1 Introduction 61
3.2 File Descr iptors 61
3.3 open and openat Functions 62
3.4 creat Function 66
3.5 close Function 66
3.6 lseek Function 66
3.7 read Function 71
3.8 write Function 72
3.9 I/O Efficiency 72
3.10 File Shar ing 74
3.11 Atomic Operations 77
3.12 dup and dup2 Functions 79
3.13 sync, fsync, and fdatasync Functions 81
3.14 fcntl Function 82
3.15 ioctl Function 87
3.16 /dev/fd 88
3.17 Summary 90
Chapter 4: Files and Directories 93
4.1 Introduction 93
4.2 stat, fstat, fstatat, and lstat Functions 93
4.3 File Types 95
4.4 Set-User-ID and Set-Group-ID 98
4.5 File Access Per missions 99
4.6 Ownership of New Files and Directories 101
4.7 access and faccessat Functions 102
4.8 umask Function 104
4.9 chmod, fchmod, and fchmodat Functions 106
4.10 Sticky Bit 108
4.11 chown, fchown, fchownat, and lchown Functions 109
4.12 File Size 111
4.13 File Tr uncation 112
4.14 File Systems 113
4.15 link, linkat, unlink, unlinkat, and remove Functions 116
4.16 rename and renameat Functions 119
4.17 Symbolic Links 120
4.18 Creating and Reading Symbolic Links 123
4.19 File Times 124
4.20 futimens, utimensat, and utimes Functions 126
4.21 mkdir, mkdirat, and rmdir Functions 129
4.22 Reading Director ies 130
4.23 chdir, fchdir, and getcwd Functions 135
4.24 Device Special Files 137
4.25 Summary of File Access Per mission Bits 140
4.26 Summary 140
Chapter 5: Standard I/O Library 143
5.1 Introduction 143
5.2 Streams and FILE Objects 143
5.3 Standard Input, Standard Output, and Standard Error 145
5.4 Buffer ing 145
5.5 Opening a Stream 148
5.6 Reading and Writing a Stream 150
5.7 Line-at-a-Time I/O 152
5.8 Standard I/O Efficiency 153
5.9 Binary I/O 156
5.10 Positioning a Stream 157
5.11 For matted I/O 159
5.12 Implementation Details 164
5.13 Temporar y Files 167
5.14 Memory Streams 171
5.15 Alternatives to Standard I/O 174
5.16 Summary 175
Chapter 6: System Data Files and Information 177
6.1 Introduction 177
6.2 Password File 177
6.3 Shadow Passwords 181
6.4 Group File 182
6.5 Supplementary Group IDs 183
6.6 Implementation Differences 184
6.7 Other Data Files 185
6.8 Login Accounting 186
6.9 System Identification 187
6.10 Time and Date Routines 189
6.11 Summary 196
Chapter 7: Process Environment 197
7.1 Introduction 197
7.2 main Function 197
7.3 Process Termination 198
7.4 Command-Line Arguments 203
7.5 Environment List 203
7.6 Memory Lay out of a C Program 204
7.7 Shared Librar ies 206
7.8 Memory Allocation 207
7.9 Environment Var iables 210
7.10 setjmp and longjmp Functions 213
7.11 getrlimit and setrlimit Functions 220
7.12 Summary 225
Chapter 8: Process Control 227
8.1 Introduction 227
8.2 Process Identifiers 227
8.3 fork Function 229
8.4 vfork Function 234
8.5 exit Functions 236
8.6 wait and waitpid Functions 238
8.7 waitid Function 244
8.8 wait3 and wait4 Functions 245
8.9 Race Conditions 245
8.10 exec Functions 249
8.11 Changing User IDs and Group IDs 255
8.12 Interpreter Files 260
8.13 system Function 264
8.14 Process Accounting 269
8.15 User Identification 275
8.16 Process Scheduling 276
8.17 Process Times 280
8.18 Summary 282
Chapter 9: Process Relationships 285
9.1 Introduction 285
9.2 Ter minal Logins 285
9.3 Networ k Logins 290
9.4 Process Groups 293
9.5 Sessions 295
9.6 Controlling Terminal 296
9.7 tcgetpgrp, tcsetpgrp, and tcgetsid Functions 298
9.8 Job Control 299
9.9 Shell Execution of Programs 303
9.10 Orphaned Process Groups 307
9.11 FreeBSD Implementation 310
9.12 Summary 312
Chapter 10: Signals 313
10.1 Introduction 313
10.2 Signal Concepts 313
10.3 signal Function 323
10.4 Unreliable Signals 326
10.5 Interrupted System Calls 327
10.6 Reentrant Functions 330
10.7 SIGCLD Semantics 332
10.8 Reliable-Signal Ter minology and Semantics 335
10.9 kill and raise Functions 336
10.10 alarm and pause Functions 338
10.11 Signal Sets 344
10.12 sigprocmask Function 346
10.13 sigpending Function 347
10.14 sigaction Function 349
10.15 sigsetjmp and siglongjmp Functions 355
10.16 sigsuspend Function 359
10.17 abort Function 365
10.18 system Function 367
10.19 sleep, nanosleep, and clock_nanosleep Functions 373
10.20 sigqueue Function 376
10.21 Job-Control Signals 377
10.22 Signal Names and Numbers 379
10.23 Summary 381
Chapter 11: Threads 383
11.1 Introduction 383
11.2 Thread Concepts 383
11.3 Thread Identification 384
11.4 Thread Creation 385
11.5 Thread Termination 388
11.6 Thread Synchronization 397
11.7 Summary 422
Chapter 12: Thread Control 425
12.1 Introduction 425
12.2 Thread Limits 425
12.3 Thread Attr ibutes 426
12.4 Synchronization Attr ibutes 430
12.5 Reentrancy 442
12.6 Thread-Specific Data 446
12.7 Cancel Options 451
12.8 Threads and Signals 453
12.9 Threads and fork 457
12.10 Threads and I/O 461
12.11 Summary 462
Chapter 13: Daemon Processes 463
13.1 Introduction 463
13.2 Daemon Character istics 463
13.3 Coding Rules 466
13.4 Error Logging 469
13.5 Single-Instance Daemons 473
13.6 Daemon Conventions 474
13.7 Client–Server Model 479
13.8 Summary 480
Chapter 14: Advanced I/O 481
14.1 Introduction 481
14.2 Nonblocking I/O 481
14.3 Record Locking 485
14.4 I/O Multiplexing 500
14.5 Asynchronous I/O 509
14.6 readv and writev Functions 521
14.7 readn and writen Functions 523
14.8 Memory-Mapped I/O 525
14.9 Summary 531
Chapter 15: Interprocess Communication 533
15.1 Introduction 533
15.2 Pipes 534
15.3 popen and pclose Functions 541
15.4 Coprocesses 548
15.5 FIFOs 552
15.6 XSI IPC 556
15.7 Message Queues 561
15.8 Semaphores 565
15.9 Shared Memor y 571
15.10 POSIX Semaphores 579
15.11 Client–Server Proper ties 585
15.12 Summary 587
Chapter 16: Network IPC: Sockets 589
16.1 Introduction 589
16.2 Socket Descr iptors 590
16.3 Addressing 593
16.4 Connection Establishment 605
16.5 Data Tr ansfer 610
16.6 Socket Options 623
16.7 Out-of-Band Data 626
16.8 Nonblocking and Asynchronous I/O 627
16.9 Summary 628
Chapter 17: Advanced IPC 629
17.1 Introduction 629
17.2 UNIX Domain Sockets 629
17.3 Unique Connections 635
17.4 Passing File Descriptors 642
17.5 An Open Server, Version 1 653
17.6 An Open Server, Version 2 659
17.7 Summary 669
Chapter 18: Terminal I/O 671
18.1 Introduction 671
18.2 Over view 671
18.3 Special Input Characters 678
18.4 Getting and Setting Ter minal Attr ibutes 683
18.5 Ter minal Option Flags 683
18.6 stty Command 691
18.7 Baud Rate Functions 692
18.8 Line Control Functions 693
18.9 Ter minal Identification 694
18.10 Canonical Mode 700
18.11 Noncanonical Mode 703
18.12 Ter minal Window Size 710
18.13 termcap, terminfo, and curses 712
18.14 Summary 713
Chapter 19: Pseudo Terminals 715
19.1 Introduction 715
19.2 Over view 715
19.3 Opening Pseudo-Ter minal Devices 722
19.4 pty_fork Function 726
19.5 pty Program 729
19.6 Using the pty Program 733
19.7 Advanced Features 740
19.8 Summary 741
Chapter 20: A Database Library 743
20.1 Introduction 743
20.2 History 743
20.3 The Librar y 744
20.4 Implementation Over view 746
20.5 Centralized or Decentralized? 750
20.6 Concurrency 752
20.7 Building the Librar y 753
20.8 Source Code 753
20.9 Perfor mance 781
20.10 Summary 786
Chapter 21: Communicating with a Network Printer 789
21.1 Introduction 789
21.2 The Inter net Pr inting Protocol 789
21.3 The Hyper text Transfer Protocol 792
21.4 Printer Spooling 793
21.5 Source Code 795
21.6 Summary 843
Appendix A: Function Prototypes 845
Appendix B: Miscellaneous Source Code 895
B.1 Our Header File 895
B.2 Standard Error Routines 898
Appendix C: Solutions to Selected Exercises 905
Bibliography 947
Index 955
Preface
Rich Stevens and I first met through an e-mail exchange when I reported a typographical error in his first book, UNIX Network Programming. He used to kid me about being the person to send him his first errata notice for the book. Until his death in 1999, we exchanged e-mail irregularly, usually when one of us had a question we thought the other might be able to answer. We met for dinner at USENIX conferences and when Rich was teaching in the area.
Rich Stevens was a friend who always conducted himself as a gentleman. When I wrote UNIX System V Network Programming in 1993, I intended it to be a System V version of Rich's UNIX Network Programming. As was his nature, Rich gladly reviewed chapters for me, and treated me not as a competitor, but as a colleague. We often talked about collaborating on a STREAMS version of his TCP/IP Illustrated book. Had events been different, we might have actually done it, but since Rich is no longer with us, revising Advanced Programming in the UNIX Environment is the closest I'll ever get to writing a book with him.When the editors at Addison-Wesley told me that they wanted to update Rich's book, I thought that there wouldn't be too much to change. Even after 13 years, Rich's work still holds up well. But the UNIX industry is vastly different today from what it was when the book was first published.
- The System V variants are slowly being replaced by Linux. The major system vendors that ship their hardware with their own versions of the UNIX System have either made Linux ports available or announced support for Linux. Solaris is perhaps the last descendant of UNIX System V Release 4 with any appreciablemarket share.
- After 4.4BSD was released, the Computing Science Research Group (CSRG) from the University of California at Berkeley decided to put an end to its development of the UNIX operating system, but several different groups of volunteers still maintain publicly available versions.
- The introduction of Linux, supported by thousands of volunteers, has made it possible for anyone with a computer to run an operating system similar to the UNIX System, with freely available source code for the newest hardware devices. The success of Linux is something of a curiosity, given that several free BSD alternatives are readily available.
- Continuing its trend as an innovative company, Apple Computer abandoned its old Mac operating system and replaced it with one based on Mach and FreeBSD.
Thus, I've tried to update the information presented in this book to reflect these four platforms.
After Rich wrote Advanced Programming in the UNIX Environment in 1992, I got rid of most of my UNIX programmer 's manuals. To this day, the two books I keep closest to my desk are a dictionary and a copy of Advanced Programming in the UNIX Environment. I hope you find this revision equally useful.
Changes from the First EditionRich's work holds up well. I've tried not to change his original vision for this book, but a lot has happened in 13 years. This is especially true with the standards that affect the UNIX programming interface.
Throughout the book, I've updated interfaces that have changed from the ongoing efforts in standards organizations. This is most noticeable in Chapter 2, since its primary topic is standards. The 2001 version of the POSIX.1 standard, which we use in this revision, is much more comprehensive than the 1990 version on which the first edition of this book was based. The 1990 ISO C standard was updated in 1999, and some changes affect the interfaces in the POSIX.1 standard.A lot more interfaces are now covered by the POSIX.1 specification. The base specifications of the Single UNIX Specification (published by The Open Group, formerly X/Open) have been merged with POSIX.1. POSIX.1 now includes several 1003.1 standards and draft standards that were formerly published separately. Accordingly, I've added chapters to cover some new topics. Threads and multithreaded programming are important concepts because they present a cleaner way for programmers to deal with concurrency and asynchrony.
The socket interface is now part of POSIX.1. It provides a single interface to interprocess communication (IPC), regardless of the location of the process, and is a natural extension of the IPC chapters.
I've omitted most of the real-time interfaces that appear in POSIX.1. These are best treated in a text devoted to real-time programming. One such book appears in the bibliography.
I've updated the case studies in the last chapters to cover more relevant real-world examples. For example, few systems these days are connected to a PostScript printer via a serial or parallel port. Most PostScript printers today are accessed via a network, so I've changed the case study that deals with PostScript printer communication to take this into account.
The chapter on modem communication is less relevant these days. So that the original material is not lost, however, it is available on the book's Web site in two formats: PostScript (
http://www.apuebook.com/lostchapter/modem.ps) and PDF (
http://www.apuebook.com/lostchapter/modem.pdf).
The source code for the examples shown in this book is also available at
www.apuebook.com. Most of the examples have been run on four platforms:
- FreeBSD 5.2.1, a derivative of the 4.4BSD release from the Computer Systems Research Group at the University of California at Berkeley, running on an Intel Pentium processor
- Linux 2.4.22 (the Mandrake 9.2 distribution), a free UNIX-like operating system, running on Intel Pentium processors
- Solaris 9, a derivative of System V Release 4 from Sun Microsystems, running on a64-bit UltraSPARC IIi processor
- Darwin 7.4.0, an operating environment based on FreeBSD and Mach, supported by Apple Mac OS X, version 10.3, on a PowerPC processor
Stephen A. Rago
Warren, New Jersey
April 2005
sar@apuebook.com
Preface to the First EditionIntroductionThis book describes the programming interface to the Unix system—the system call interface and many of the functions provided in the standard C library. It is intended for anyone writing programs that run under Unix.
Like most operating systems, Unix provides numerous services to the programs that are running—open a file, read a file, start a new program, allocate a region of memory, get the current time-of-day, and so on. This has been termed the system call interface. Additionally, the standard C library provides numerous functions that are used by almost every C program (format a variable's value for output, compare two strings, etc.).
The system call interface and the library routines have traditionally been described in Sections 2 and 3 of the Unix Programmer 's Manual. This book is not a duplication of these sections. Examples and rationale are missing from the Unix Programmer 's Manual, and that's what this book provides.
Unix StandardsThe proliferation of different versions of Unix during the 1980s has been tempered by the various international standards that were started during the late 1980s. These include the ANSI standard for the C programming language, the IEEE POSIX family (still being developed), and the X/Open portability guide. This book also describes these standards. But instead of just describing the standards by themselves, we describe them in relation to popular implementations of the standards—System V Release 4 and the forthcoming 4.4BSD. This provides a real-world description, which is often lacking from the standard itself and from books that describe only the standard.
Organization of the BookThis book is divided into six parts:
An overview and introduction to basic Unix programming concepts and terminology (Chapter 1), with a discussion of the various Unix standardization efforts and different Unix implementations (Chapter 2).
- I/O—unbuffered I/O (Chapter 3), properties of files and directories (Chapter 4), the standard I/O library (Chapter 5), and the standard system data files (Chapter 6).
- Processes—the environment of a Unix process (Chapter 7), process control (Chapter 8), the relationships between different processes (Chapter 9), and signals (Chapter 10).
- More I/O—terminal I/O (Chapter 11), advanced I/O (Chapter 12), and daemon processes (Chapter 13).
- IPC—Interprocess communication (Chapters 14 and 15).
- Examples—a database library (Chapter 16), communicating with a PostScript printer (Chapter 17), a modem dialing program (Chapter 18), and using pseudo terminals (Chapter 19).
A reading familiarity with C would be beneficial as would some experience using Unix. No prior programming experience with Unix is assumed. This text is intended for programmers familiar with Unix and programmers familiar with some other operating system who wish to learn the details of the services provided by most Unix systems.
Examples in the TextThis book contains many examples—approximately 10,000 lines of source code. All the examples are in the C programming language. Furthermore, these examples are in ANSI C. You should have a copy of the Unix Programmer's Manual for your system handy while reading this book, since reference is made to it for some of the more esoteric and implementation-dependent features.
Almost every function and system call is demonstrated with a small, complete program. This lets us see the arguments and return values and is often easier to comprehend than the use of the function in a much larger program. But since some of the small programs are contrived examples, a few bigger examples are also included (Chapters 16, 17, 18, and 19). These larger examples demonstrate the programming techniques in larger, real-world examples.
Systems Used to Test the ExamplesUnfortunately all operating systems are moving targets. Unix is no exception. The following diagram shows the recent evolution of the various versions of System V and 4.xBSD.
4.xBSD are the various systems from the Computer Systems Research Group at the University of California at Berkeley. This group also distributes the BSD Net 1 and BSD Net 2 releases—publicly available source code from the 4.xBSD systems. SVRx refers to System V Release x from AT&T. XPG3 is the X/Open Portability Guide, Issue 3, and ANSI C is the ANSI standard for the C programming language. POSIX.1 is the IEEE and ISO standard for the interface to a Unix-like system. We'll have more to say about these different standards and the various versions of Unix in Sections 2.2 and 2.3.
In this text we use the term 4.3+BSDto refer to the Unix system from Berkeley that is somewhere between the BSD Net 2 release and 4.4BSD. At the time of this writing, 4.4BSD was not released, so the system could not be called 4.4BSD. Nevertheless a simple name was needed to refer to this system and 4.3+BSD is used throughout the text.
Most of the examples in this text have been run on four different versions of Unix:
- Unix System V/386 Release 4.0 Version 2.0 (''vanilla SVR4'') from U.H. Corp. (UHC), on an Intel 80386 processor.
- 4.3+BSD at the Computer Systems Research Group, Computer Science Division, University of California at Berkeley, on a Hewlett Packard workstation.
- BSD/386 (a derivative of the BSD Net 2 release) from Berkeley Software Design, Inc., on an Intel 80386 processor. This system is almost identical to what we call 4.3+BSD.
- SunOS 4.1.1 and 4.1.2 (systems with a strong Berkeley heritage but many System V features) from Sun Microsystems, on a SPARCstation SLC.
Numerous timing tests are provided in the text and the systems used for the test are identified.
W. Richard Stevens
Tucson, Arizona
April 1992