04-08-2012, 12:57 PM
Advanced Linux Programming
linux_book.pdf (Size: 3.69 MB / Downloads: 31)
Introduction
GNU/Linux has taken the world of computers by storm.At one time, personal computer
users were forced to choose among proprietary operating environments and
applications. Users had no way of fixing or improving these programs, could not look
“under the hood,” and were often forced to accept restrictive licenses. GNU/Linux
and other open source systems have changed that—now PC users, administrators, and
developers can choose a free operating environment complete with tools, applications,
and full source code.
A great deal of the success of GNU/Linux is owed to its open source nature.
Because the source code for programs is publicly available, everyone can take part in
development, whether by fixing a small bug or by developing and distributing a complete
major application.This opportunity has enticed thousands of capable developers
worldwide to contribute new components and improvements to GNU/Linux, to the
point that modern GNU/Linux systems rival the features of any proprietary system,
and distributions include thousands of programs and applications spanning many CDROMs
or DVDs.
The success of GNU/Linux has also validated much of the UNIX philosophy.
Many of the application programming interfaces (APIs) introduced in AT&T and BSD
UNIX variants survive in Linux and form the foundation on which programs are
built.The UNIX philosophy of many small command line-oriented programs working
together is the organizational principle that makes GNU/Linux so powerful. Even
when these programs are wrapped in easy-to-use graphical user interfaces, the underlying
commands are still available for power users and automated scripts.
A powerful GNU/Linux application harnesses the power of these APIs and commands
in its inner workings. GNU/Linux’s APIs provide access to sophisticated features
such as interprocess communication, multithreading, and high-performance
networking.And many problems can be solved simply by assembling existing commands
and programs using simple scripts.
GNU and Linux
Where did the name GNU/Liux come from? You’ve certainly heard of Linux before,
and you may have heard of the GNU Project.You may not have heard the name
GNU/Linux, although you’re probably familiar with the system it refers to.
Linux is named after Linus Torvalds, the creator and original author of the kernel
that runs a GNU/Linux system.The kernel is the program that performs the most
basic functions of an operating system: It controls and interfaces with the computer’s
hardware, handles allocation of memory and other resources, allows multiple programs
to run at the same time, manages the file system, and so on.
xxi
The kernel by itself doesn’t provide features that are useful to users. It can’t even
provide a simple prompt for users to enter basic commands. It provides no way for
users to manage or edit files, communicate with other computers, or write other programs.
These tasks require the use of a wide array of other programs, including command
shells, file utilities, editors, and compilers. Many of these programs, in turn, use
libraries of general-purpose functions, such as the library containing standard C library
functions, which are not included in the kernel.
On GNU/Linux systems, many of these other programs and libraries are software
developed as part of the GNU Project.1 A great deal of this software predates the
Linux kernel.The aim of the GNU Project is “to develop a complete UNIX-like
operating system which is free software” (from the GNU Project Web site,
The Linux kernel and software from the GNU Project has proven to be a powerful
combination. Although the combination is often called “Linux” for short, the complete
system couldn’t work without GNU software, any more than it could operate without
the kernel. For this reason, throughout this book we’ll refer to the complete system as
GNU/Linux, except when we are specifically talking about the Linux kernel.
The GNU General Public License
The source code contained in this book is covered by the GNU General Public License
(GPL), which is listed in Appendix F,“GNU General Public License.” A great deal of
free software, especially GNU/Linux software, is licensed under it. For instance, the
Linux kernel itself is licensed under the GPL, as are many other GNU programs and
libraries you’ll find in GNU/Linux distributions. If you use the source code in this
book, be sure to read and understand the terms of the GPL.
The GNU Project Web site includes an extensive discussion of the GPL
and other free software licenses.You can
find information about open source software licenses at
licenses/index.html.
Who Should Read This Book?
This book is intended for three types of readers:
You might be a developer already experienced with programming for the
GNU/Linux system, and you want to learn about some of its advanced features
and capabilities.You might be interested in writing more sophisticated programs
with features such as multiprocessing, multithreading, interprocess communication,
and interaction with hardware devices.You might want to improve your
programs by making them run faster, more reliably, and more securely, or by
designing them to interact better with the rest of the GNU/Linux system.
1. GNU is a recursive acronym: It stands for “GNU’s Not UNIX.”
xxii
n You might be a developer experienced with another UNIX-like system who’s
interested in developing GNU/Linux software, too.You might already be familiar
with standard APIs such as those in the POSIX specification.To develop
GNU/Linux software, you need to know the peculiarities of the system, its
limitations, additional capabilities, and conventions.
n You might be a developer making the transition from a non-UNIX environment,
such as Microsoft’s Win32 platform.You might already be familiar with
the general principles of writing good software, but you need to know the specific
techniques that GNU/Linux programs use to interact with the system and
with each other. And you want to make sure your programs fit naturally into the
GNU/Linux system and behave as users expect them to.
This book is not intended to be a comprehensive guide or reference to all aspects of
GNU/Linux programming. Instead, we’ll take a tutorial approach, introducing the
most important concepts and techniques, and giving examples of how to use them.
Section 1.5,“Finding More Information,” in Chapter 1, “Getting Started,” contains
references to additional documentation, where you can obtain complete details about
these and other aspects of GNU/Linux programming.
Because this is a book about advanced topics, we’ll assume that you are already
familiar with the C programming language and that you know how to use the standard
C library functions in your programs.The C language is the most widely used
language for developing GNU/Linux software; most of the commands and libraries
that we discuss in this book, and most of the Linux kernel itself, are written in C.
The information in this book is equally applicable to C++ programs because that
language is roughly a superset of C. Even if you program in another language, you’ll
find this information useful because C language APIs and conventions are the lingua
franca of GNU/Linux.
If you’ve programmed on another UNIX-like system platform before, chances are
good that you already know your way around Linux’s low-level I/O functions (open,
read, stat, and so on).These are different from the standard C library’s I/O functions
(fopen, fprintf, fscanf, and so on). Both are useful in GNU/Linux programming, and
we use both sets of I/O functions throughout this book. If you’re not familiar with
the low-level I/O functions, jump to the end of the book and read Appendix B,
“Low-Level I/O,” before you start Chapter 2,“Writing Good GNU/Linux Software.”
xxiii
This book does not provide a general introduction to GNU/Linux systems.
We assume that you already have a basic knowledge of how to interact with a
GNU/Linux system and perform basic operations in graphical and command-line
environments. If you’re new to GNU/Linux, start with one of the many excellent
introductory books, such as Michael Tolber’s Inside Linux (New Riders Publishing,
2001).
Conventions
This book follows a few typographical conventions:
n A new term is set in italics the first time it is introduced.
n Program text, functions, variables, and other “computer language” are set in a
fixed-pitch font—for example, printf (“Hello, world!\bksl n”).
n Names of commands, files, and directories are also set in a fixed-pitch font—for
example, cd /.
n When we show interactions with a command shell, we use % as the shell prompt
(your shell is probably configured to use a different prompt). Everything after
the prompt is what you type, while other lines of text are the system’s response.
For example, in this interaction