Register your product to gain access to bonus material or receive a coupon.
If you are an experienced C programmer with a working knowledge of UNIX, you cannot afford to be without this up-to-date tutorial on the system call interface and the most important functions found in the ANSI C library. Rich Stevens describes more than 200 system calls and functions; since he believes the best way to learn code is to read code, a brief example accompanies each description.
Building upon information presented in the first 15 chapters, the author offers chapter-long examples teaching you how to create a database library, a PostScript printer driver, a modem dialer, and a program that runs other programs under a pseudo terminal. To make your analysis and understanding of this code even easier, and to allow you to modify it, all of the code in the book is available via UUNET.
A 20-page appendix provides detailed function prototypes for all the UNIX, POSIX, and ANSI C functions that are described in the book, and lists the page on which each prototype function is described in detail. Additional tables throughout the text and a thorough index make Advanced Programming in the UNIX Environment an invaluable reference tool that all UNIX programmers - beginners to experts - will want on their bookshelves.
Advanced Programming in the UNIX Environment is applicable to all major UNIX releases, especially System V Release 4 and the latest release of 4.3BSD, including 386BSD. These real-world implementations allow you to more clearly understand the status of the current and future standards, including IEEE POSIX and XPG3.
0201563177B04062001
Advanced Programming in the UNIX® Environment: UNIX File I/O
(Each chapter begins with an Introduction and concludes with a Summary.)
1. Introduction.
Logging In.
Files and Directories.
Input and Output.
Programs and Processes.
ANSI C Features.
Error Handling.
User Identification.
Signals.
Unix Time Values.
System Calls and Library Functions.
Unix Standardization.
ANSI C.
IEEE POSIX.
X/Open XPG3.
FIPS.
Unix Implementations.
System V Release 4.
4.3+BSD.
Relationship of Standards and Implementations.
Limits.
ANSI C Limits.
POSIX Limits.
XPG3 Limits.
sysconf, pathconf, and fpathconf Functions.
FIPS 151-1 Requirements.
Summary of Limits.
Indeterminate Run-Time Limits.
Feature Test Macros.
Primitive System Data Types.
Conflicts Between Standards.
File Descriptors.
open Function.
creat Function.
close Function.
lseek Function.
read Function.
write Function.
I/O Efficiency.
File Sharing.
Atomic Operations.
dup and dup2 Functions.
fcntl Function.
ioctl Function.
/dev/fd.
stat, fstat, and lstat Functions.
File Types.
Set-User-ID and Set-Group-ID.
File Access Permissions.
Ownership of New Files and Directories.
access Function.
umask Function.
chmod and fchmod Functions.
Sticky Bit.
chown, fchown, and lchown Functions.
File Size.
File Truncation.
Filesystems.
link, unlink, remove, and rename Functions.
Symbolic Links.
symlink and readlink Functions.
File Times.
utime Function.
mkdir and rmdir Functions.
Reading Directories.
chdir, fchdir, and getcwd Functions.
Special Device Files.
sync and fsync Functions.
Summary of File Access Permission Bits.
Streams and FILE Objects.
Standard Input, Standard Output, and Standard Error.
Buffering.
Opening a Stream.
Reading and Writing a Stream.
Line-at-a-Time I/O.
Standard I/O Efficiency.
Binary I/O.
Positioning a Stream.
Formatted I/O.
Implementation Details.
Temporary Files.
Alternatives to Standard I/O.
Password File.
Shadow Passwords.
Group File.
Supplementary Group IDs.
Other Data Files.
Login Accounting.
System Identification.
Time and Date Routines.
main Function.
Process Termination.
Command-Line Arguments.
Environment List.
Memory Layout of a C Program.
Shared Libraries.
Memory Allocation.
Environment Variables.
setjmp and longjmp Functions.
getrlimit and setrlimit Functions.
Process Identifiers.
fork Function.
vfork Function.
exit Functions.
wait and waitpid Functions.
wait3 and wait4 Functions.
Race Conditions.
exec Functions.
Changing User IDs and Group IDs.
Interpreter Files.
system Function.
Process Accounting.
User Identification.
Process Times.
Terminal Logins.
Network Logins.
Process Groups.
Sessions.
Controlling Terminal.
tcgetpgrp and tcsetpgrp Functions.
Job Control.
Shell Execution of Programs.
Orphaned Process Groups.
4.3+BSD Implementation.
Signal Concepts.
signal Function.
Unreliable Signals.
Interrupted System Calls.
Reentrant Functions.
SIGCLD Semantics.
Reliable Signal Terminology and Semantics.
kill and raise Functions.
alarm and pause Functions.
Signal Sets.
sigprocmask Function.
sigpending Function.
sigaction Function.
sigsetjmp and siglongjmp Functions.
sigsuspend Function.
abort Function.
system Function.
sleep Function.
Job-Control Signals.
Additional Features.
Overview.
Special Input Characters.
Getting and Setting Terminal Attributes.
Terminal Option Flags.
stty Command.
Baud Rate Functions.
Line Control Functions.
Terminal Identification.
Canonical Mode.
Noncanonical Mode.
Terminal Window Size.
termcap, terminfo, and curses.
Nonblocking I/O.
Record Locking.
Streams.
I/O Multiplexing.
select Function.
poll Function.
Asynchronous I/O.
System V Release 4.
4.3+BSD.
readv and writev Functions.
readn and writen Functions.
Memory Mapped I/O.
Daemon Characteristics.
Coding Rules.
Error Logging.
SVR4 Streams log Driver.
4.3+BSD syslog Facility.
Client-Server Model.
Pipes.
popen and pclose Functions.
Coprocesses.
FIFOs.
System V IPC.
Identifiers and Keys.
Permission Structure.
Configuration Limits.
Advantages and Disadvantages.
Message Queues.
Semaphores.
Shared Memory.
Client-Server Properties.
Stream Pipes.
Passing File Descriptors.
System V Release 4.
4.3BSD.
4.3+BSD.
An Open Server, Version 1.
Client-Server Connection Functions.
System V Release 4.
4.3+BSD.
An Open Server, Version 2.
History.
The Library.
Implementation Overview.
Centralized or Decentralized?
Concurrency.
Source Code.
Performance.
PostScript Communication Dynamics.
Printer Spooling.
Source.
Code.
History.
Program Design.
Data Files.
Server Design.
Server Source Code.
Client Design.
Client Source Code.
Overview.
Opening Pseudo-Terminal Devices.
System V Release 4.
4.3+BSD.
pty_fork Function.
pty Program.
Using the pty Program.
Advanced Features.
Our Header File.
Standard Error Routines.
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.
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.
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.
All the examples have been included in the text directly from their source files. A machine-readable copy of all the examples is available via anonymous FTP from the Internet host ftp.uu.net in the file published/books/stevens.advprog.tar.Z. Obtaining the source code allows you to modify the programs from this text and experiment with them on your system.
4.3+BSD 4.3BSD 4.3BSD Tahoe 4.3BSD Reno 4.4BSD? | | BSD Net 1 | BSD Net 2 | | | | | | | | | | | | | | | | | +-+----+-------+---+--+---+---+----+-+---+---+-+--+-+ 1986 1987 1988 + 1989 + 1990 + 1991 1992 | | | + + | + SVR3.0 SVR3.1 SVR3.2+ + SVR4 + XPG3 ANSI C POSIX.1
In this text we use the term 4.3+BSD to 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:
I am especially grateful to Brian Kernighan for his help in the book. His numerous thorough reviews of the entire manuscript and his gentle prodding for better prose hopefully show in the final result. Steve Rago was also a great resource, both in reviewing the entire manuscript and answering many questions about the details and history of System V. My thanks to the other technical reviewers used by Addison-Wesley, who provided valuable comments on various portions of the manuscript: Maury Bach, Mark Ellis, Jeff Gitlin, Peter Honeyman, John Linderman, Doug McIlroy, Evi Nemeth, Craig Partridge, Dave Presotto, Gary Wilson, and Gary Wright.
Keith Bostic and Kirk McKusick at the U.C. Berkeley CSRG provided an account that was used to test the examples on the latest BSD system. (Many thanks to Peter Salus too.) Sam Nataros and Joachim Sacksen at UHC provided the copy of SVR4 used to test the examples. Trent Hein helped obtain the alpha and beta copies of BSD/386.
Other friends have helped in many small, but significant ways over the past few years: Paul Lucchina, Joe Godsil, Jim Hogue, Ed Tankus, and Gary Wright. My editor at Addison-Wesley, John Wait, has been a great friend through it all. He never complained when the due date slipped and the page count kept increasing. A special thanks to the National Optical Astronomy Observatories (NOAO), especially Sidney Wolff, Richard Wolff, and Steve Grandi, for providing computer time.
Real Unix books are written using troff and this book follows that time-honored tradition. Camera-ready copy of the book was produced by the author using the groff package written by James Clark. Many thanks to James Clark for providing this excellent system and for his rapid response to bug fixes. Perhaps someday I will really understand troff footer traps.
I welcome electronic mail from any readers with comments, suggestions, or bug fixes: rstevens@kohala.com.
W. Richard Stevens
Tucson, Arizona
April 1992
http://www.kohala.com/~rstevens
0201563177P04062001