The classic guide to UNIX® programming-completely updated!
UNIX application programming requires a mastery of system-level services. Making sense of the many functions-more than 1,100 functions in the current UNIX specification-is a daunting task, so for years programmers have turned to Advanced UNIX Programming for its clear, expert advice on how to use the key functions reliably.
An enormous number of changes have taken place in the UNIX environment since the landmark first edition. In Advanced UNIX Programming, Second Edition, UNIX pioneer Marc J. Rochkind brings the book fully up to date, with all-new, comprehensive coverage including:
- Darwin, the Mac™ OS X kernel
- And more than 200 new system calls
Rochkind's fully updated classic explains all the UNIX system calls you're likely to need, all in a single volume!
- Interprocess communication, networking (sockets), pseudo terminals, asynchronous I/O, advanced signals, realtime, and threads
- Covers the system calls you'll actually use-no need to plow through hundreds of improperly implemented, obsolete, and otherwise unnecessary system calls!
- Thousands of lines of example code include a Web browser and server, a keystroke recorder/player, and a shell complete with pipelines, redirection, and background processes
- Emphasis on the practical-ensuring portability, avoiding pitfalls, and much more!
Since 1985, the one book to have for mastering UNIX application programming has been Rochkind's Advanced UNIX Programming. Now completely updated, the second edition remains the choice for up-to-the-minute, in-depth coverage of the essential system-level services of the UNIX family of operating systems.
About the Author
MARC J. ROCHKIND was fortunate enough to have worked at Bell Laboratories in the 1970s, when UNIX was still in its infancy. It was there that Rochkind made several key contributions to UNIX, notably the Source Code Control System. He wrote the first edition of Advanced UNIX Programming in 1984. This complete revision benefits from his years of post-Bell application systems development experience.
Table of Contents
1 Fundamental Concepts.
A Whirlwind Tour of UNIX and Linux. Versions of UNIX. Using System Calls. Error Handling. UNIX Standards. Common Header File. Dates and Times. About the Example Code. Essential Resources.
2. Basic File I/O.
Introduction to File I/O. File Descriptors and Open File Descriptions. Symbols for File Permission Bits. open and creat System Calls. umask System Call. unlink System Call. Creating Temporary Files. File Offsets and O_APPEND. write System Call. read System Call. close System Call. User Buffered I/O. lseek System Call. pread and pwrite System Calls. readv and writev System Calls. Synchronized I/O. truncate and ftruncate System Calls.
3. Advanced File I/O.
Introduction. Disk Special Files and File Systems. Hard and Symbolic Links. Pathnames. Accessing and Displaying File Metadata. Directories. Changing an I-Node. More File-Manipulation Calls. Asynchronous I/O.
4. Terminal I/O.
Introduction. Reading from a Terminal. Sessions and Process Groups (Jobs). ioctl System Call. Setting Terminal Attributes. Additional Terminal-Control System Calls. Terminal-Identification System Calls. Full-Screen Applications. STREAMS I/O. Pseudo Terminals.
5. Processes and Threads.
Introduction. Environment. exec System Calls. Implementing a Shell (Version 1). fork System Call. Implementing a Shell (Version 2). exit System Calls and Process Termination. wait, waitpid, and waitid System Calls. Signals, Termination, and Waiting. Implementing a Shell (Version 3). Getting User and Group Ids. Setting User and Group Ids. Getting Process Ids. chroot System Call. Getting and Setting the Priority. Process Limits. Introduction to Threads. The Blocking Problem.
6. Basic Interprocess Communication.
Introduction. Pipes. dup and dup2 System Calls. A Real Shell. Two-Way Communication with Unidirectional Pipes. Two-Way Communication with Bidirectional Pipes.
7. Advanced Interprocess Communication.
Introduction. FIFOs, or Named Pipes. An Abstract Simple Messaging Interface (SMI). System V IPC (Interprocess Communication). System V Message Queues. POSIX IPC. POSIX Message Queues. About Semaphores. System V Semaphores. POSIX Semaphores. File Locking. About Shared Memory. System V Shared Memory. POSIX Shared Memory. Performance Comparisons.
8. Networking and Sockets.
Socket Basics. Socket Addresses. Socket Options. Simple Socket Interface (SSI). Socket Implementation of SMI. Connectionless Sockets. Out-of-Band Data. Network Database Functions. Miscellaneous System Calls. High-Performance Considerations.
9. Signals and Timers.
Signal Basics. Waiting for a Signal. Miscellaneous Signal System Calls. Deprecated Signal System Calls. Realtime Signals Extension (RTS). Global Jumps. Clocks and Timers.
Appendix A. Process Attributes.
Appendix B. Ux: A C++ Wrapper for Standard UNIX Functions.
Appendix C. Jtux: A Java/Jython Interface to Standard UNIX Functions.
Appendix D. Alphabetical and Categorical Function Lists.
This book updates the 1985 edition of Advanced UNIX Programming to cover a few changes that have occurred in the last eighteen years. Well, maybe "few" isn't the right word! And "updates" isn't right either. Indeed, aside from a sentence here and there, this book is all new. The first edition included about 70 system calls; this one includes about 300. And none of the UNIX standards and imple-mentations discussed in this book--POSIX, Solaris, Linux, FreeBSD, and Darwin (Mac OS X)--were even around in 1985. A few sentences from the 1985 Preface, however, are among those that I can leave almost unchanged:The subject of this book is UNIX system calls--the interface between the UNIX kernel and the user programs that run on top of it. Those who interact only with commands, like the shell, text editors, and other application programs, may have little need to know much about system calls, but a thorough knowledge of them is essential for UNIX programmers. System calls are the only way to access kernel facilities such as the file system, the multitasking mechanisms, and the interprocess communication primitives.
System calls define what UNIX is. Everything else--subroutines and commands--is built on this foundation. While the novelty of many of these higher-level programs has been responsi-ble for much of UNIX's renown, they could as well have been programmed on any modern operating system. When one describes UNIX as elegant, simple, efficient, reliable, and porta-ble, one is referring not to the commands (some of which are none of these things), but to the kernel.That's all still true, except that, regrettably, the programming interface to the ker-nel is no longer elegant or simple. In fact, because UNIX development has splintered into many directions over the last couple of decades, and because the principal standards organization, The Open Group, sweeps up almost everything that's out there (1108 functions altogether), the interface is clumsy, inconsistent, redundant, error-prone, and confusing. But it's still efficient, reliably imple-mented, and portable, and that's why UNIX and UNIX-like systems are so successful. Indeed, the UNIX system-call interface is the only widely imple-mented portable one we have and are likely to have in our lifetime.
To sort things out, it's not enough to have complete documentation, just as the Yellow Pages isn't enough to find a good restaurant or hotel. You need a guide that tells you what's good and bad, not just what exists. That's the purpose of this book, and why it's different from most other UNIX programming books. I tell you not only how to use the system calls, but also which ones to stay away from because they're unnecessary, obsolete, improperly implemented, or just plain poorly designed.
Here's how I decided what to include in this book: I started with the 1108 func-tions defined in Version 3 of the Single UNIX Specification and eliminated about 590 Standard C and other library functions that aren't at the kernel-interface level, about 90 POSIX Threads functions (keeping a dozen of the most important), about 25 accounting and logging functions, about 50 tracing functions, about 15 obscure and obsolete functions, and about 40 functions for scheduling and other things that didn't seem to be generally useful. That left exactly 307 for this book. (See Appendix D for a list.) Not that the 307 are all good--some of them are use-less, or even dangerous. But those 307 are the ones you need to know.
This book doesn't cover kernel implementation (other than some basics), writing device drivers, C programming (except indirectly), UNIX commands (shell, vi, emacs, etc.), or system administration.
There are nine chapters: Fundamental Concepts, Basic File I/O, Advanced File I/O, Terminal I/O, Processes and Threads, Basic Interprocess Communication, Advanced Interprocess Communication, Networking and Sockets, and Signals and Timers. Read all of Chapter 1, but then feel free to skip around. There are lots of cross-references to keep you from getting lost.
Like the first edition, this new book includes thousands of lines of example code, most of which are from realistic, if simplified, applications such as a shell, a full- screen menu system, a Web server, and a real-time output recorder. The examples are all in C, but I've provided interfaces in Appendices B and C so you can pro-gram in C++, Java, or Jython (a variant of Python) if you like.
The text and example code are just resources; you really learn UNIX program-ming by doing it. To give you something to do, I've included exercises at the end of each chapter. They range in difficulty from questions that can be answered in a few sentences to simple programming problems to semester-long projects.I used four UNIX systems for nuts-and-bolts research and to test the examples: Solaris 8, SuSE Linux 8 (2.4 kernel), FreeBSD 4.6, and Darwin (the Mac OS X kernel) 6.8. I kept the source on the FreeBSD system, mounted on the others with NFS or Samba.
I edited the code with TextPad on a Windows system and accessed the four test systems with Telnet or SSH (PuTTY) or with the X Window System (XFree86 and Cygwin). Having the text editor and the four Telnet/SSH/Xterm windows open on the same screen turned out to be incredibly convenient, because it takes only a few minutes to write some code and check it out on the four systems. In*addition, I usually had one browser window open to the Single UNIX Specifi-cation and one to Google, and another window running Microsoft Word for editing the book. With the exception of Word, which is terrible for big documents like books (crashes, mixed-up styles, weak cross-referencing, flakey document- assembly), all of these tools worked great. I used Perl and Python for various things like extracting code samples and maintaining the database of system calls.
Most Helpful Customer Reviews
(reviewed for the Calgary Linux Users Group Guild) There is a saying which goes, 'UNIX is user-friendly; it's just picky about who its friends are.' Rochkind's book presents enough material to make the reader an acquaintance, leaving the building of a friendship as an exercise for the highly committed. He makes a careful selection of just over 300 of the most important system calls and groups them into a handful of broad topics: I/O (file and terminal), processes and threads, inter-process communication (including sockets), signals, and timers. He takes great care to highlight what is available in Solaris (version 8), Linux (SuSE 8), BSD (FreeBSD 4.6), and Darwin (6.8; MacOS 10.2.8), and how to write something that has a hope of running on all of them. The system calls he describes are the ones anyone writing UNIX applications must know about. That is his target audience, and he meets that target squarely. If you are not already a C programmer and UNIX user, this book will not be useful for you. The exercises and code samples are reflective of the title, and demonstrates in a short space how to put the kernel to use. Rochkind also includes some example code that should work, but will not, just to illustrate that things are not as simple as they could be. He then walks the reader through the necessary corrections, bringing enlightenment as he goes. Each chapter contains this mix of tutorial material, useful to those who are starting to explore the UNIX kernel and how it can serve their applications, and reference material, useful to those looking for the bit of wisdom a man page cannot provide, but years of experience can. There are two technical things that I found missing from the book. The significant one is a discussion of how to deal with multiple processors, particularly around semaphores and other kinds of locks. This would be a valuable addition to the chapter on inter-process communications, especially now that these machines are becoming more common. The second, and by no means serious, omission is actually one that arises because of what he does include in an appendix: if he can describe a Java class library, why not make a reference to a set of Perl modules, too? There are two reasons why I give the book four stars and not five. One reason is a matter of taste. I am not a fan of C preprocessor macro functions, but Rochkind makes heavy use of them in his examples to simplify error trapping and reduce the volume of code that had to be printed. This forced me to read code in a different way than my colleagues and I write it. I found it awkward to pick out the particular system call being used when it was coded as a parameter to a macro. The second is that I found his treatment of signals to be confusing. Admittedly, signal handling is not simple (and the newer signal-handling calls do little to help), but the chapter on signals felt like it was rushed to completion to meet the publication deadline. I read that chapter twice, and am still scratching my head. Add a handful of typographical errors to these irritants, and I wound up with a book that was not a joy to read, but a chore. I will still use the book as an occasional reference, reflecting the comfortable professional relationship, but not intimate friendship, I have with UNIX, and maybe I will learn more on a subsequent reading.
In 1987, I encountered the first edition of Rochkind's book. Grew to depend on it as an authoritative discourse on serious unix interprocess programming. Now (finally!) he has issued an updated edition. (Has it really been 19 years?) What is different? Much. Indeed, most of the book. In the first chapter, he summarises myriad developments in the unix world and, of course, the rise of linux. He writes for all the current unix variations and linux. Since 1985, there has been a plethora of different interprocess communications methods. So the chapters on these form a prominent part of the book. Then, naturally, in the networking chapter, who can ignore the web? So Rochkind gives a concise detour into http, with example code for a simple browser and web server. One thing unchanged from the first edition is the well deserved use of 'Advanced' in the title. The treatment is sophisticated and, frankly, not an easy read for someone new to the subject. Rochkind writes well, but you do have to concentrate. He goes at a pace that at a very minimum assumes you are already facile in C and with using unix at the user level. The source code examples are simple, once you grasp the concepts. The problems he furnishes in each chapter can be extremely useful if you want to deepen your understanding. The questions are nontrivial and usually expand on salient points discussed in the chapters. If you are indeed new to all this, you should make a serious attempt at the problems.