Linux System Programming
Jon Mitchell writes "As a Perl programmer recently thrown in to the world of C development on
Linux, I have been looking for something that would take my K&R level of
experience and bring it up to date with modern methods, hopefully
letting me write more efficient and reliable programs.
Linux System Programming is a volume that targets this need. Robert Love, former "Chief Architect, Linux Desktop" at Novell, kernel
hacker of many years, and Gnome developer of well known features such as Beagle
and NetworkManager, attempts in this book to document the Linux system call
and C API to common systems programming tasks. Given that he developed
the pre-emptive kernel and inotify he has the knowledge." Read below for the rest of Jon's review.
Linux System Programming
author
Robert Love
pages
388
publisher
O'Reilly Media
rating
8/10
reviewer
Jon Mitchell
ISBN
9780596009588
summary
The Linux system call and C API explored in depth.
Getting this book out of the box, I had wrongly been expecting a cookbook
style that I would get instant gratification from. Although structured
around common programming tasks, it doesn't lend itself to just dipping in.
The section on time lists a handful of ways that "time" is available to the
programmer; jump into the middle of the section and you might miss
the most suitable one for the job in hand. The book rewards reading it
in larger chunks.
This doesn't mean it is necessary to read it from cover to cover. Logically organized into chapters around "things you want to do", such as file access, memory management and process management it will lead you in with a survey of techniques you might be familiar with, before drilling down with advanced methods.
Knowing advanced methods for performance is great, but not at all costs. One of the most useful and practical lessons this book gives is to encourage you to think about error conditions that may occur during a system call. Early on, in the section on reading files, a detailed example is given on reading from a file. Every possible case of return code from the read call is described together with what it means and how you should handle it — it can be surprising that 7 possible outcomes are listed, with good descriptions of what to do with each of them.
This good practice by example continues throughout the book. Every system call described also lists the errors that may occur. This does show up a slight weakness: many system calls share a common set of errors which are repeated many times in the text. If you are not paying attention it may feel like you are just flipping through man pages. However you are soon halted by the easy introduction of an advanced concept to get your teeth into.
These are done in a nicely graded level for each topic. In "file access" to give an example, you are lead from simple read/write calls, through to what the C library can provide in buffering, to improved performance using mmap. The techniques continue with descriptions of I/O schedulers and how the kernel will order hardware disk access, scatter/gather, and ends up with how it is possible to order block reads/writes yourself bypassing any scheduler.
You are hardly aware of the progression, as the pacing is very well done. New concepts clearly fit into what you have seen so far — current sections signpost the practical use of what is being explained and at what cost, allowing clear consideration of the use of advanced features against any consequences.
For process management discussion starts with fork and exec, before moving onto user ids and groups, covers daemonification and goes onto process scheduling, including real time scheduling. Throughout the book each new call is illustrated with a short code snippet showing the call being used in a practical situation.
Not everything is present and correct. The author immediately states that networking is not covered at all. This is a shame as this subject would benefit from the depth of coverage given to the topics in this book — although no doubt would increase the number of pages considerably. Perhaps scope for a second volume. The length of some sections seems odd — Asynchronous file I/O is whizzed through in a page with no code example, whereas I/O schedulers gets a luxurious 12.
On the other hand there are some unexpected and useful extras, such as a discussion in the appendix of gcc C language extensions and how they might be used to fine tune your code.
The books stated target is for modern Linux development, a 2.6.22 kernel, gcc 4.2 and glibc 2.5. Many calls have been standardized by POSIX, and where this is so it are noted in the text, so a large portion of the content is useful on other systems. There is even the occasional mention of non-Linux system calls, the use of which is not encouraged, but shown so you know how they function if you come across them in older code.
I recommend this book to anyone who has a need to developing Linux applications. The book is not a primer in C on Unix, so you are expected to be familiar at least to the level of K&R. From this level though the journey into getting the best from the kernel and C library into your programs is easy going and enjoyable.
You can purchase Linux System Programming from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
This doesn't mean it is necessary to read it from cover to cover. Logically organized into chapters around "things you want to do", such as file access, memory management and process management it will lead you in with a survey of techniques you might be familiar with, before drilling down with advanced methods.
Knowing advanced methods for performance is great, but not at all costs. One of the most useful and practical lessons this book gives is to encourage you to think about error conditions that may occur during a system call. Early on, in the section on reading files, a detailed example is given on reading from a file. Every possible case of return code from the read call is described together with what it means and how you should handle it — it can be surprising that 7 possible outcomes are listed, with good descriptions of what to do with each of them.
This good practice by example continues throughout the book. Every system call described also lists the errors that may occur. This does show up a slight weakness: many system calls share a common set of errors which are repeated many times in the text. If you are not paying attention it may feel like you are just flipping through man pages. However you are soon halted by the easy introduction of an advanced concept to get your teeth into.
These are done in a nicely graded level for each topic. In "file access" to give an example, you are lead from simple read/write calls, through to what the C library can provide in buffering, to improved performance using mmap. The techniques continue with descriptions of I/O schedulers and how the kernel will order hardware disk access, scatter/gather, and ends up with how it is possible to order block reads/writes yourself bypassing any scheduler.
You are hardly aware of the progression, as the pacing is very well done. New concepts clearly fit into what you have seen so far — current sections signpost the practical use of what is being explained and at what cost, allowing clear consideration of the use of advanced features against any consequences.
For process management discussion starts with fork and exec, before moving onto user ids and groups, covers daemonification and goes onto process scheduling, including real time scheduling. Throughout the book each new call is illustrated with a short code snippet showing the call being used in a practical situation.
Not everything is present and correct. The author immediately states that networking is not covered at all. This is a shame as this subject would benefit from the depth of coverage given to the topics in this book — although no doubt would increase the number of pages considerably. Perhaps scope for a second volume. The length of some sections seems odd — Asynchronous file I/O is whizzed through in a page with no code example, whereas I/O schedulers gets a luxurious 12.
On the other hand there are some unexpected and useful extras, such as a discussion in the appendix of gcc C language extensions and how they might be used to fine tune your code.
The books stated target is for modern Linux development, a 2.6.22 kernel, gcc 4.2 and glibc 2.5. Many calls have been standardized by POSIX, and where this is so it are noted in the text, so a large portion of the content is useful on other systems. There is even the occasional mention of non-Linux system calls, the use of which is not encouraged, but shown so you know how they function if you come across them in older code.
I recommend this book to anyone who has a need to developing Linux applications. The book is not a primer in C on Unix, so you are expected to be familiar at least to the level of K&R. From this level though the journey into getting the best from the kernel and C library into your programs is easy going and enjoyable.
You can purchase Linux System Programming from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
I to..err... know this poor bastard who took all his compsci courses in C++. How hard would it be for a C++ coder to dig into this book?
I only look human.
My mother is a halfling and my dad is an ogre, so that makes me an Ogreling
I like UNIX systems programming when it's complete; even when that surprises me. Recently, for example, I had to find a way to know how many processes had open file descriptors to a certain file. You know, the old shared database thing; so that I can make sure that I'm the only one in at a certain point (inside a file lock), to do some checks an'all. To no avail; UNIX basically said: 'if you can't do it with file locks, don't bother'. Then I discovered the good old sys/ipc.h and the associated sys/sem.h and sys/shm.h. Turns out that my issue *has* been thought about, and in a good way too. Sure, the APIs aren't all 'modern' feeling; lots of things are done with extremely short function-names, ellipsis and setting bits inside special structs, but it works. And it's fast too.
Now if they only had a good standard API to a versioned, networked filesystem. Then I would be in heaven. But a guy can dream...
Religion is what happens when nature strikes and groupthink goes wrong.
On most UNIX systems, the POSIX API is fully available to Perl scripts. One of the great things about Perl is that you get all sorts of high level features that aren't available in C, but then you also get all of the low level features that you often need when writing hardcore UNIX software. Best of all, Perl is damn fast, usually on par with C for most tasks. And it's often a lot faster when doing regular expressions work, for instance.
Too bad inotify doesn't monitor recursively. If you think it does, go look.
You can probably tell I can't program, but what is "K&R level of experience" ?
CDE open sourced! https://sourceforge.net/projects/cdesktopenv/
"...and where this is so it are noted in the text..."
Well, I hope it aren't noted using grammar like that.
how does it compare with Stevens (RIP)?
...if the amazon reviews are accurate.
O'Reilly is great, but I do think you gotta be careful; a lot of their books can, at times, seem to be mostly printouts of man pages (and other freely available documentation), as this reviewer notes:
If you expect the quality of the author's other books from this book, you'll be disappointed. It just lists system calls and their descriptions that you can find from man pages without any serious examples. It doesn't provide any insight or thorough coverage you can find from other books such as Steven's book.
Richard Stevens was definitely "the man" when it came to writing books like this; I'd recommend them to anyone. Anyone who attempts to cover the same ground (even years later) has a tough act to follow.
I've bought a lot of computer books over the years, and for my money, none have been as well-written and valuable as Stevens'.
RIP, Richard.
"Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth
Robert has done plenty of useful work, but it was George AnzigerAnzinger who developed the Linux preemption patch. Robert picked it up, maintained it and got it merged. The credits to George seemed to have gotten lost somewhere in that process.
Credit where credit is due please.
Have you got your LWN subscription yet?
Leave Britney alone!
Build your code with -Wall -Werror (or your compiler's equivalent). Once you clean up all the crud, that pops up, crank it up with -W -Wno-unused-parameter -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith. Once there — add -Wreturn-type -Wcast-qual -Wswitch -Wshadow -Wcast-align and tighten up by removing the no in -Wno-unused-parameter. The -Wwrite-strings is essential, if you wish your code to be compiled with a C++ compiler some day (hint: the correct type for static strings is " const char *").
For truly clean code, add -Wchar-subscripts -Winline -Wnested-externs -Wredundant-decls.
The people, who wrote and maintain the compiler, are, most likely, several levels above you in understanding programming in general and C-programming in particular. Ignoring the advice their code generates is foolish on your part...
As a minimum, solved warnings will make your code more readable by reducing/eliminating the "Why is he doing this?" questions. More often than not, they point out bugs you would otherwise spend hours chasing with a debugger later.
And they make your code more portable. But if you don't understand, why a warning is generated — ask around. Don't just "shut it up". For example, initializing a variable at declaration is usually a no-no. If the compiler thinks, the variable may be used before being initialized, scrutinize your program's flow. If you can't figure out, it may some times be better to disable this one warning temporarily with -Wno-uninitialized to move on, instead of shutting it up for ever by a bogus "= 0" or some such...
The book may well say something about respecting warnings, but the review does not, which is a shame.
In Soviet Washington the swamp drains you.
Their about page calls it the "perl interpreter" multiple times. How is it not an interpreted language?
Poor Anziger. You even had to copy+paste his name in ;)
-- Am I the only person who thinks that nature is unfair, and thus inherently immoral; moreover, that no higher authority has the right to define fairness, thus existence itself is immoral? Any human's continued existence is a contribution toward nature, thus the only moral path for every human is to commit suicide ASAP. Yes. At least the only one still alive. Why aren't you following through on your convictions?
Dave, this is fascinating, but rather unrelated to my post. I don't know, why you chose to post a follow-up, rather than start a thread of your own.
Interestingly, 42 is not listed in /usr/include/sysexits.h on neither Solaris, nor FreeBSD, nor Linux...
In Soviet Washington the swamp drains you.
Wow, not only from someone knowing gnome, which is famous for breaking all of Unix philisophy wherever it meets it (I personaly especially hate glib for ignoring my setting of $HOME), but even from NetworkManager, the tool you use when you want a non-working network configurable by graphic means between not-working, currently non-working and permanently non-working.
As soon as I heard that Robert Love had written this book about userspace programming, I rushed to buy it.
I had really enjoyed both "Linux Kernel Development" (Developer's Library, 2003) and "Linux Kernel Development 2nd ed." (Novell, 2005). I like how clearly and brightly the author describes linux internals, from the major architectural components to the key code chunks.
This book was a great surprise. It's the best you may desire when you have to quickly design and develop complex solutions with glibc&gcc, and you want to take full advantage of your powerful linux kernel. The most complete guide through system and library calls, with elegant code examples.
It is going to stay on my office desk for a quite while.
Is a great kernel developer/programmer (He also does columns for Linux Journal). He is not a general purpose Linux programming author.
Getting this book out of the box, I had wrongly been expecting a cookbook style that I would get instant gratification from. Although structured around common programming tasks, it doesn't lend itself to just dipping in.
For getting your feet wet with Linux programming I recommend GNU/LINUX Application Programming by M. Tim Jones or Linux Application Development by Michael K. Johnson and Erik W. Troan.
The Linux Unleashed series is also good (1000+ pages with hundreds dedicated to perl, python, and Gtk programming).
Enjoy,
It's just the normal noises in here.
If this is anything like Linux Kernel Development by the same author, it is not aimed at those new to C. I suggest getting at very least a C pocket book, and reading up (thoroughly) on pointers before diving into this.
Ride the Tour de France like the pro's. This book encourage you to think about the possible technical and physical hurdles you may encounter when riding the longest, toughest bicycle race known to men. The book covers some basic subjects like wheel tunning, seat height and handlebars using real life examples. Other chapters cover more advance subjects such as sprinting, time trials and team-riding. Mountain stages are not included in this book. I recommend this book to anyone who has a need to ride bicycles either professionally or in amateur circuits.
- these are not the droids you are looking for -