Slashdot Mirror


Advanced Unix Programming, 2nd Ed.

prostoalex writes "Advanced Unix Programming by Marc Rochkind is published by Addison-Wesley this year in its second edition. A book that has been considered a timeless classic, a title that saw its first edition back in 1985 and its second edition almost two decades later, in 2004. Where do you even start to review?" Read on below to see read prostoalex's evaluation. Advanced Unix Programming, 2nd Ed. author Marc Rochkind pages 736 publisher Addison Wesley Professional rating 9/10 reviewer Alex Moskalyuk ISBN 0131411543 summary An introduction and guided course through the world of Linux I/O and interprocess communications, with C++ source code provided for your viewing pleasure. More than 1100 functions explained.

Advanced Unix Programming (AUP) has been updated to include information relevant to Solaris, Linux, FreeBSD, Darwin and Mac OS X. Rochkind has added more than 200 system calls, according to the preface. But who is the book for?

First off, if you look at the table of contents, you will find that AUP is largely a book on input-output in Unix operating systems. The input-output varies from Basic (Chapter 2) and Advanced (Chapter 3) File I/O to Interprocess Communications (Chapters 6, 7), Network I/O (Chapter 8) and Terminal I/O (Chapter 4). The rest of the book consists of purely informational chapters on fundamental concepts of Unix operating systems (Chapter 1), working with threads and processes (Chapter 5) and signals and timers (Chapter 9).

If you get the impression that this is an academic title, you're not mistaken - if your university has some kind of Advanced Unix/Linux or Unix Networking course, they probably use some AUP material. Note that the book is not a how-to or manual on setting up Apache, Samba, FTP, various filesystems or Jabber servers - it does have a chapter on networking but teaches Unix I/O concepts from developer's perspective only, meaning you have to know C and C++. If you prefer to look at the source code, it's on the author's Web site.

There are two types of readers for AUP: those who start off programming in Unix/Linux, and those who are quite good at it, have read the first edition and are now wondering whether the second one is worth it.

If you are just starting with programming in Unix/Linux environment, don't let the word "Advanced" scare you off. The first chapter is pretty good in getting the reader up to speed with the concepts discussed in the book. It talks about such common tasks as getting the system to tell you what it has in terms of POSIX, getting a Unix box to tell you the date and time inside a C++ application, and counting your app's execution time. In many aspects, the second half of each chapter falls under O'Reilly cookbook format, where you are given a certain task and then provided the source code and explanations of what needs to be done to accomplish the task.

The author also "falls" into the trap of using some quick solutions only to "discover" that they do not work on all the systems. For example, subchapter 3.6.1 Reading Directories first tries to access the contents of the directory via ec_neg (fd = open (".", O_RDONLY) and ec_neg (nread = read (fd, buffer, sizeof(buffer))) only to find out that under Linux the call retrieves unhelpful "*** EISDIR (21: "Is a directory") ***" message. After that we are introduced into proper, not quick and dirty ways, to access Unix directories via opendir(), closedir() and readdir().

From experience, it looks like most of the people I know who own a copy of the first edition of AUP bought it because of its section on Interprocess Communications. The author does indeed provide a great learning and reference resource when in Chapter 5 he takes the reader through Unix processes and threads, explains how fork() works. The simple pop quizzes are there as well. A way to win friends and amuse the opposite sex during watercooler talks is to offer the following example:

void forktest (void)
{
int pid;
printf ("Start of test.\n");
pid = fork();
printf ("Returned %d.\n", pid);
}

Run this example as forktest and you will get a message:

Start of test.
Returned 11111.
Returned 0.

Run this test as forktest > tmp and suddenly the message in tmp file changes:

Start of test.
Returned 22222.
Start of test.
Returned 0.

Why is "Start of test" printed twice in the second example? Warning: the book contains an early spoiler in 5.5 fork System Call

By this point, you probably wonder whether the code examples will work on your system. The author tested the code on Solaris 8, SuSE Linux 8, FreeBSD 4.6 and Darwin (Mac OS X kernel) 6.8. In the preface, he talks about using a Windows box with SSH client to upload the code to the destination systems and run them there.

The book is very convenient to read; the chapter numbering system always gives you a good feel of where you are at. As reading of the entire book is not required, and a lot of people use AUP as a reference, an index containing just functions and system calls is included in Appendix D. Don't know what tcgetpgrp() does? The index will point you to 4.3.4. All the code is printed in monospace font, so it's quite easy to differentiate from the regular text. All the function definitions are boxed with function name, description and signature provided. The signature itself contains comments on what the parameter represents. They also are not saving whitespace on function samples, using the style where each line of source code and each { gets a separate line in text. Overall, more than 1100 functions are covered.

The book is quite practical, too, so don't think of it as pure API rehash. For example, in 8.4.3 (the chapter 8 deals with Networking), you are given the source code for a text-based browser that's written in less than 50 lines of code (although it doesn't quite understand HTML and just dumps everything to standard output).

Overall, if any part of your job description or hobby list includes Unix/Linux development, especially if it's high on that list, this book is a must have. Moreover, looking at the job market defined by keyword "unix", it looks like half the positions include some kind of "Sr." or "Architect" or "Networking" attribute, for which the knowledge provided in AUP would be indispensable.

You can purchase Advanced Unix Programming, 2nd Ed. from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, carefully read the book review guidelines, then visit the submission page.

13 of 143 comments (clear)

  1. slashdotted already? by spangineer · · Score: 2, Informative

    Well, I'm having trouble getting to the link, so here's the amazon.com page (not a referral link):

    Advanced Unix Programming They have 27 used copies, and the book's gotten high reviews.

    1. Re:slashdotted already? by spangineer · · Score: 5, Informative

      Sorry, that's 27 copies of the 1st edition, the second ed. obviously doesn't because it just came out 6 days ago. The link for the second ed. is:

      Advanced Unix Programming.

  2. Does does it print twice? by kyz · · Score: 5, Informative

    Because the first printf was automatically flushed after the newline, because it's going to a terminal. Some stdio implementations are like that.

    It wasn't flushed in the second example, it would only write out data once there was a full buffer's worth (e.g. 32kb or such), or when the stream was closed. Because it wasn't flushed, both fork()ed copies had this unflushed data in their buffer and both printed it.

    I'm sure it scares a few newbies, but it's fairly obvious.

    --
    Does my bum look big in this?
    1. Re:Does does it print twice? by pclminion · · Score: 5, Informative
      If you want the line buffered behavior even when outputting to non-terminal devices, without having to explicitly call fflush() after every line, you can force the stdout stream into line buffered mode like this:

      setvbuf(stdout, NULL, _IOLBF, 0);

      You must do this before you use stdout in any way.

  3. Good UNIX Reference by mcx101 · · Score: 3, Informative

    I spotted the first edition of this book in my university library when I was doing some coding on FreeBSD. Whilst it didn't have anything specific to FreeBSD it was still a handy reference. I look forward to reading the additions in this version. Perhaps I'll get the university library to order it for me ;-)

    --
    My operat~1 system unders~1 long filena~1 , does yours?
  4. Re:*NIX by mcx101 · · Score: 5, Informative

    Don't forget though that UNIX is a registered trademark of The Open Group, so even though SCO has (claims to have?) the rights to the UNIX source code they don't own the name.

    --
    My operat~1 system unders~1 long filena~1 , does yours?
  5. Re:Terminal I/O? by NickDngr · · Score: 1, Informative

    Believe it or not, some of us still use terminals. Sometimes it is an efficient way to work with a system.

    --
    Yoda of Borg am I! Assimilated shall you be! Futile resistance is, hmm?
  6. Excellent Reference by muppetsrule · · Score: 4, Informative

    I have used this book for the past few years mostly as a reference for some of the really hairy stuff/problems that I have sometimes run into.

    It belongs on my bookshelf right along with my Unix Network Programming books (Richard Stevens auth).

  7. POSIX Reference by the+frizz · · Score: 5, Informative
    AUP really is a classic. I may buy it just for sentimental reasons, even though I don't need the tutorial introducton to Unix anymore.

    Nowdays though, my definitive reference for writing portable unix programs is the merged IEEE POSIX and Open groups's Single Unix Specification. Registration is free.

  8. Re:Marc vs. Stevens by hitchhacker · · Score: 4, Informative

    "Advanced Unix Programming"

    I believe you mean:
    "Advanced Programming in the UNIX Environment"

    His "TCP/IP Illustrated" volumes 1-3 are also great.
    I havn't read AUP, so I can't compare him to Stevens.

    -metric

  9. File handle passing by Nate+Eldredge · · Score: 2, Informative

    They leave out my favorite example of an advanced Unix programming technique, which is file handle passing. You can actually pass an open file handle from one unrelated process to another.

    Sure, it's easy to have two processes open the same file. If it's something like a pipe that exists anonymously, you can still give it to a child process by having it open when you fork. But to pass it to a process that isn't a child? Tougher, but not, surprisingly, impossible. (It involves Unix domain sockets, of all things.)

    I generally don't find too many people that know about this, but it can be very useful on occasion. I think it definitely qualifies as an important technique, and the fact that this book doesn't appear to mention it is a strike against it. (Stevens discusses the topic, of course.)

    1. Re:File handle passing by Nate+Eldredge · · Score: 2, Informative
      Okay, then, I'll put up. Please see this code. (I tried to post it here but the lameness filter prevented me.)

      Notice:

      • proc1 and proc2 are siblings, not parent and child
      • /tmp/foobar is never opened by proc2 or its parent
      • only proc2 writes the message "hello world"
      Yet somehow /tmp/foobar gets the message in it anyway.

      Credit Kragen Sitaker for the original code which I hacked to be a better demo. (I never claimed I could remember offhand how to do this, and I no longer have my copy of Stevens, but I do know it can be done.) It's at this url if you want to see the original.

      Tested on Linux and FreeBSD. On Solaris a couple of changes with respect to the CMSG_* macros are needed; I'm too lazy to figure this out.

  10. opendir() is a new feature by Albert+Cahalan · · Score: 2, Informative

    The old way was to call open() on the directory,
    then simply use read() to get an array of structs.
    Each struct had a 16-bit inode number and a
    14-character filename.

    Linux broke support for this, because 32-bit inode
    numbers and 255-character filenames would not fit.
    Linux would get stuck with DOS-style name mangling
    and some sort of inode remapping. Like this:

    Linux_i~1.html

    (but hey, 14 characters beats 8.3 style names)