Sherry Dockins, MASA, LCPC,CADC,
Contents At a Glance
I Advanced UNIX Programming
1 Getting Started 3
2 Writing Good GNU/Linux
3 Processes 45
A Other Development Tools 259
B Low-Level I/O 281
C Table of Signals 301
D Online Resources 303
E Open Publication License
Version 1.0 305
F GNU General Public License 309
201 West 103rd Street, Indianapolis, Indiana 46290
limitations, additional capabilities, and conventions.
You might be a developer making the transition from a non-UNIX environment,
such as Microsofts Win32 platform.You might already be familiar with
the general principles of writing good software, but yo
path, strerror (error_code);
/* Dont end the program; perhaps give the user a chance to
choose another file. */
/* PATH contains an invalid memory address. This is probably a bug. */
/* Ran out of kernel memory.
filenames so that they dont collide.
The file permissions of the temporary file should be set in such a way that
unauthorized users cannot alter the programs execution by modifying or
replacing the temporary file.
Temporary filenames should be generated i
You can see that the debugger has stopped at the breakpoint.
You can step over the call to atoi using the next command:
9 printf (The reciprocal of 0 is %g\n, i, reciprocal (i);
If you want to see whats going on inside reciprocal,
/* Create the filename and file. The XXXXXX will be replaced with
characters that make the filename unique. */
char temp_filename = /tmp/temp_file.XXXXXX;
int fd = mkstemp (temp_filename);
/* Unlink the file immediately, so that it will be removed
D.1 General Information 303
D.2 Information About GNU/Linux
D.3 Other Sites 304
E Open Publication License
Version 1.0 305
I. Requirement on Both Unmodified and
Modified Versions 305
II. Copyright 306
III. Scope of License 306
2.1.6 The Environment
GNU/Linux provides each running program with an environment.The environment is
a collection of variable/value pairs. Both environment variable names and their values
are character strings. By convention, environment variable names ar
The error message indicates that even though libtest.a contains a definition of f, the
linker did not find it.Thats because libtest.a was searched when it was first encountered,
and at that point the linker hadnt seen any references to f.
On the other han
HIS CHAPTER SHOWS YOU HOW TO PERFORM THE BASIC
steps required to create a
C or C+ Linux program. In particular, this chapter shows you how to create and
modify C and C+ source code, compile that code, and debug the result. If youre
RUNNING INSTANCE OF A PROGRAM IS CALLED A PROCESS.
If you have two
terminal windows showing on your screen, then you are probably running the
same terminal program twiceyou have two terminal processes. Each terminal
window is probably running a shell;
example, cd /.
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 systems response.
degree from Stanford in 2000. His research interests center on
algorithm engineering, concentrating on flow and other combinatorial
algorithms. He works on GCC and scientific computing software.
Alex Samuel graduated from Harvard in 1995 with a degree in
upgrade, but probably easier to deploy. If you link dynamically, your programs will be
Listing 2.6 Continued
2.3 Writing and Using Libraries 37
smaller, easier to upgrade, but harder to deploy.This section explains how to link both
statically and dynamica
This functionality is available under Linux by using the dlopen function.You could
open a shared library named libtest.so by calling dlopen like this:
dlopen (libtest.so, RTLD_LAZY)
2.3 Writing and Using Libraries 43
(The second parameter is a flag that i
PostScript is a trademark of Adobe Systems, Inc.
Linux is a trademark of Linus Torvalds.
Warning and Disclaimer
This book is designed to provide information about
Advanced Linux Programming. Every effort has been made to
make this book as complete a
One library will often depend on another library. For example, many GNU/Linux
systems include libtif, a library that contains functions for reading and writing
image files in the TIFF format.This library, in turn, uses the libraries libjpeg (JPEG
% gdb reciprocal
When gdb starts
up, you should see the GDB prompt:
5.unless your programs always work the first time.
12 Chapter 1 Getting Started
The first step is to run your program inside the debugger. Just enter the command
and any program arg
accustomed to working with an Integrated Development Environment (IDE).You
add sources files to your project, and then the IDE builds your project automatically.
Although IDEs are available for Linux, this book doesnt discuss them. Instead, this
call entirely, so the expression will never be evaluated and
never be called.You should write this instead:
for (i = 0; i < 100; +i) cfw_
int status = do_something ();
assert (status = 0);
Another thing to bear in mind is that yo
Listing 2.6 shows an example of how you might write this function.
Listing 2.6 (readfile.c) Freeing Resources During Abnormal Conditions
Theres also one header file called
(see Listing 1.3).
Listing 1.3 (reciprocal.hpp) Header filereciprocal.hpp
extern C cfw_
extern double reciprocal (int i);
The first step is to turn the C
Print verbose messages
In addition, the program is to accept zero or more additional command-line
arguments, which are the names of input files.
To use getopt_long, you must provide two data structures.The first is a character
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
1.5.4 Source Code
This is Open Source, right? The final arbiter of how the system works is the system
source code itself, and luckily for Linux programmers, that source code is freely available.
Chances are, your Linux distribution includes full source co