| 图书目录
Chapter 1. Introduction 1.1 Introduction 1.2 Logging In 1.3 Files and Directories 1.4 Input and Output 1.5 Programs and Processes 1.6 ANSI C Features 1.7 Error Handling 1.8 User Identification 1.9 Signals 1.10 Unix Time Values 1.11 System Calls and Library Functions 1.12 Summary Chapter 2. Unix Standardization and Implementations 2.1 Introduction 2.2 Unix Standardization 2.3 Unix Implementations 2.4 Relationship of Standards and Implementations 2.5 Limits 2.6 Feature Test Macros 2.7 Primitive System Data Types 2.8 Conflicts Between Standards 2.9 Summary Chapter 3. FiIe I/O 3.1 Introduction 3.2 File Descriptors 3.3 open Function 3.4 creat Function 3.5 close Function 3.6 lseek Function 3.7 read Function 3.8 write Function 3.9 I/O Efficiency 3.10 File Sharing 3.11 Atomic Operations 3.12 dup and dup2 Functions 3.13 fcntl Function 3.14 ioctl Function 3.15 /dev/fd 3.16 Summary Chapter 4. Files and Directories 4.1 Introduction 4.2 stat, fstat, and lstat Functions 4.3 File Types 4.4 Set-User-ID and Set-Group-lD 4.5 File Access Permissions 4.6 Ownership of New Files and Directories 4.7 access Function 4.8 umask Function 4.9 chmod and fchmod Functions 4.10 Sticky Bit 88 4.11 chown, fchown, and lchown Functions 4.12 File Size 4.13 File Truncation 4.14 Fijesystems 4.15 link, unlink, remove, and rename Functions 4.16 Symbolic Links 4.17 symlink and readlink Functions 4.18 File Times 4.19 utime Function 4.20 mkdir and rmdir Functions 4.21 Reading Directories 4.22 chdir, fchdir, and getcwd Functions 4.23 Special Device Files 4.24 sync and fsync Functions 4.25 Summary of File Access Permission Bits 4.26 Summary Chapter 5. Standard I/O Library 5.1 Introduction 5.2 Streams and FILE Objects 5.3 Standard Input, Standard Output, and Standard Error 5.4 Buffering 5.5 Opening a Stream 5.6 Reading and Writing a Stream 5.7 Line-at-a-Time I/O 5.8 Standard I/O Efficiency 5.9 Binary I/O 5.10 Posltioning a Stream 5.11 Formatted I/O 5.12 Implementation Details 5.13 Temporary Files 5.14 Alternatives to Standard I/O 5.15 Summary Chapter 6. System Data Files and Information 6.1 Introduction 6.2 Password File 6.3 Shadow Passwords 6.4 Group File 6.5 Supplementary Group IDs 6.6 Other Data Files 6.7 Login Accounting 6.8 System Identification 6.9 Time and Date Routines 6.10 Summary Chapter 7. The Environment of a Unix Process 7.1 Introduction 7.2 main Function 7.3 Process Termination 7.4 Command-Line Arguments 7.5 Environment List 7.6 Memory Layout of a C Program 7.7 Shared Libraries 7.8 Memory Allocation 7.9 Environment Variables 7.10 set jmp and ionqjmp Functions 7.11 qetrlimit and setrlimit Functions 7.12 Summary Chapter 8. Process Control 8.1 Introduction 8.2 Process Identifiers 8.3 fork Function 8.4 vfork Function 8.5 exit Functions 8.6 wait and waitpid Functions 8.7 wait3 and wait4 Functions 8.8 Race Conditions 8.9 exec Functions 8.10 Changing User IDs and Group IDs 8.11 Interpreter Files 8.12 system Function 8.13 Process Accounting 8.14 User Identification 8.15 Process Times 8.16 Summary Chapter 9. Process Relationships 9.1 Introduction 9.2 Terminal Logins 9.3 Network Logins 9.4 Process Groups 9.5 Sessions 9.6 Controlling Terminal 9.7 tcgetpqrp and tcsetpgrp Functions 9.8 Job Control 9.9 Shell Execution of Programs 9.10 Orphaned Process Groups 9.11 4.3+BSD Implementation 9.12 Summary Chapter 10. Signals 10.1 Introduction 10.2 Signal Concepts 10.3 signal Function 10.4 Unreliable Signals 10.5 Interrupted System Calls 10.6 Reentrant Functions 10.7 SIGCLD Semantics 10.8 Reliable Signal Terminology and Semantics 10.9 kill and ra1se Functions 10.10 alarm and pause Functions 10.11 Signal Sets 10.12 siqprocmask Function 10.13 siqpending Function 10.14 sigaction Function 10.15 sigsetjmp and siglongjmp Functions 10.16 siqsuspend Function 10.17 abort Function 10.18 system Function 10.19 sleep Function 10.20 Job-Control Signals 10.21 Addltional Features 10.22 Summary Chapter 11. Terminal I/O 11.1 Introduction 11.2 Overview 11.3 Special Input Characters 11.4 Getting and Setting Terminal Attributes 11.5 Terminal Option Flags 11.6 stty Command 11.7 Baud Rate Functions 11.8 Line Control Functions 11.9 Terminal Identification 11.10 Canonical Mode 11.11 Noncanonical Mode 11.12 Terminal Window Size 11.13 termcap, terminfo. and curses 11.14 Summary Chapter 12. Advanced I/O 12.1 Introduction 12.2 Nonblocking I/O 12.3 Record Locking 12.4 Streams 12 5 I/O Multiplexing 12.6 Asynchronous I/O 12.7 readv and writev Functions 12.8 readn and writen Functions 12.9 Memory Mapped I/O 12.10 Summary Chapter 13. Daemon Processes 13.1 Introduction 13.2 Daemon Characteristics 13.3 Coding Rules 13.4 Error Loggjng 13.5 Client-Server Model 13.6 Summary Chapter 14. Interprocess Communication 14.1 Introduction 14.2 Pipes 14.3 popen and pciose Functions 14.4 Coprocesses 14.5 FIFOs 14.6 System V IPC 14.7 Message Queues 14.8 Semaphores 14.9 Shared Memory 14.10 Client-Server Properties 14.11 Summary Chapter 15. Advanced Interprocess Communication 15.1 Introduction 15.2 Stream Pipes 15.3 Passing File Descriptors 15.4 An Open Server, Version 1 15.5 Client--Server Connection Functions 15.6 An Open Server, Version 2 15.7 Summary Chapter 16. A Database Library 16.1 Introduction 16.2 History 16.3 The Library 16.4 Implementation Overview 16.5 Centralized or Decentralized? 16.6 Concurrency 16.7 Source Code 16.8 Performance 16.9 Summary Chapter 17. Communicating with a PostScript Printer 17.1 Introduction 17.2 PostScript Communication Dynamics 17.3 Printer Spooling 17.4 Source Code 17.5 Summary Chapter 18. A Modem Dialer 18.1 Introduction 18.2 History 18.3 Program Design 18.4 Data Files 18.5 Server Design 18.6 Server Source Code 18.7 Client Design 18.8 Client Source Code 18.9 Summary Chapter 19. Pseudo Terminals 19.1 Introduction 19.2 Overview 19.3 Opening Pseudo-Terminal Devices 19.4 pty_fork Function 19.5 pty Program 19.6 Using the pty Program 19.7 Advanced Features 19.8 Summary Appendix A. Function Prototypes Appendix B. Miscellaneous Source Code B.1 Our Header File B.2 Standard Error Routines Appendix C. Solutions to Selected Exercises Bibliography Index
|
| 前 言
Introduction 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 Pr0grammer'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 incIude 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 realworld 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: 1. 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). 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 datafiles (Chapter 6). 3. Processes--the environment of a Unix process (Chapter 7), process control(Chapter 8), the relationships between different processes (Chapter 9), and signals (Chapter 10). 4. More I/O--terminal I/O (Chapter 11), advanced I/O (Chapter 12), and daemon processes (Chapter 13). 5. IPC--Interprocess communication (Chapters 14 and 15). 6. Examples--a database library (Chapter 16), communicating with a PostScrirpt 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 implementa tion-dependent features. Almost every function and system caIl 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 (Chapters16, 17, 18, and 19). These larger examples demonstrate the programming techniques inlarger, 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. advproq. tar. Z.Obtaining the source code allows you to modify the programs frOm this text and experiment with them on your system. Systems Used to Test the Examples Unfortunately all operating svstems are moving targets. Unix is no exception. The following diagram shows the recent evolution of the various versions of Svstem 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 svstems. 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 IEEF 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+B5D 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.4HSD 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: 1. Unix System V/386 Re1ease 4.0 Version 2.0 ("vanilla SVR4") from U.H. Corp.(UHC), on an Intel 80386 processor. 2. 4.3+BSD at the Computer Systems Research Group, Computer Science Division,University of California at Berkeley on a Hewlett Packard workstation. 3. BSD/386 (a derivative of the BSD Net 2 release) from Berkeley Software Design,Inc., on an Intel 80386 processor This systenl is almost identical to what we call 4.3+BSD. 4. SunOS 4.1.1 and 4.1.2 (systems with a strong Bcrkelev 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. Acknowledgments Once again I am indebted to my famiIy for their love, support, and many lost weekends over the past year and a half. Writing a book is, in many ways, a family affair. Thank you Sally' Bill, Ellen, and David.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 Citlin, Peter Holleyman, 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 increa8ing. A special thanks to the National Optical Astronomy Observatories (NOAO), especially Sidney Wolff, Richard Wolff, and Steve Crandi, 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. Tucson, Arizona Avril 1992 W. Richard Stevens rstevens@kohala. com http://www. kohala. com/--rstevens
|