30-07-2012, 01:10 PM
Beginning Linux Programming
[Linux].WILEY-Beginning_Linux_Programming_Third_Edition.pdf (Size: 6.2 MB / Downloads: 182)
Linux Distributions
As we have already mentioned, Linux is actually just a kernel. You can obtain the sources for the kernel
to compile and install it and then obtain and install many other freely distributed software programs to
make a complete Linux. These installations are usually referred to as Linux systems, although they consist
of much more than just the kernel. Most of the utilities come from the GNU Project of the Free Software
Foundation.
As you can probably appreciate, creating a Linux system from just source code is a major undertaking.
Fortunately, many people have put together distributions (often called flavors), usually on CD-ROM, that
contain not just the kernel but also many other programming tools and utilities. These often include an
implementation of the X Window System, a graphical environment common on many UNIX systems.
The distributions usually come with a setup program and additional documentation (normally all on the
CD[s]) to help you install your own Linux system. Some well-known distributions (particularly on the
Intel x86 and Pentium families of processors) are Red Hat Linux, SuSE Linux, and Debian GNU/Linux,
but there are many others.
Programming Linux
Many people think that programming Linux means using C. It’s true that UNIX was originally written
in C and that the majority of UNIX applications are written in C, but C is not the only option available to
Linux programmers, or UNIX programmers for that matter. In the course of the book, we’ll introduce a
couple of the alternatives.
A vast range of programming languages are available for Linux systems, and many of them are free and
available on CD-ROM collections or from FTP archive sites on the Internet. Here’s a partial list of programming
languages available to the Linux programmer:
We’ll see how we can use a Linux shell (bash) to develop small- to medium-sized applications in
Chapter 2. For the rest of the book, we’ll mainly concentrate on C. We’ll direct our attention mostly
toward exploring the Linux programming interfaces from the perspective of the C programmer.
We will assume knowledge of the C programming language.
Linux Programs
Linux applications are represented by two special types of files: executables and scripts. Executable files
are programs that can be run directly by the computer; they correspond to Windows .exe files. Scripts
are collections of instructions for another program, an interpreter, to follow. These correspond to
Windows .bat or .cmd files, or interpreted BASIC programs.
Linux doesn’t require executables or scripts to have a specific filename or any extension whatsoever. File system
attributes, which we discuss in Chapter 2, are used to indicate that a file is a program that may be run.
In Linux, we can replace scripts with compiled programs (and vice versa) without affecting other programs
or the people who call them. In fact, at the user level, there is essentially no difference between the two.
In fact, the first version of UNIX was written in PDP 7 assembler language in 1969.
C was conceived by Dennis Ritchie around that time, and in 1973 he and Ken
Thompson rewrote essentially the entire UNIX kernel in C, quite a feat in the days
when system software was written in assembly language.
Getting Started
When we log in to a Linux system, we interact with a shell program (often bash) that runs programs in
the same way that the Windows command prompt does. It finds the programs we ask for by name by
searching for a file with the same name in a given set of directories. The directories to search are stored
in a shell variable, PATH, in much the same way as with Windows. The search path (to which we can
add) is configured by your system administrator and will usually contain some standard places where
system programs are stored. These include
/bin: Binaries, programs used in booting the system
/usr/bin: User binaries, standard programs available to users
/usr/local/bin: Local binaries, programs specific to an installation
An administrator’s login, such as root, may use a PATH variable that includes directories where system
administration programs are kept, such as /sbin and /usr/sbin.
Optional operating system components and third-party applications may be installed in subdirectories
of /opt, and installation programs might add to your PATH variable by way of user install scripts.
Note that Linux, like UNIX, uses the colon ( character to separate entries in the PATH variable, rather
than the semicolon ( that MS-DOS and Windows use. (UNIX chose : first, so ask Microsoft why
Windows is different, not why UNIX is different!) Here’s a sample
PATH variable:
a user’s home directory, and the X Window System.
Remember, Linux uses a forward slash (/) to separate directory names in a file name rather than the
backslash (\) of Windows. Again, UNIX got there first.
The C Compiler
On POSIX-compliant systems, the C compiler is called c89. Historically, the C compiler was simply
called cc. Over the years, different vendors have sold UNIX-like systems with C compilers with different
facilities and options, but often still called cc.
When the POSIX standard was prepared, it was impossible to define a standard cc command with
which all these vendors would be compatible. Instead, the committee decided to create a new standard
command for the C compiler, c89. When this command is present, it will always take the same options,
independent of the machine.
On Linux systems that do try to implement the standards, you might find that any or all of the commands
c89, cc, and gcc refer to the system C compiler, usually the GNU C compiler, or gcc. On UNIX systems,
the C compiler is almost always called cc.
Applications
Applications are usually kept in directories reserved for them. Applications supplied by the system for
general use, including program development, are found in /usr/bin. Applications added by system
administrators for a specific host computer or local network are often found in /usr/local/bin or /opt.
Administrators favor /usr/local, as it keeps vendor-supplied files and later additions separate from
the applications supplied by the system. Keeping /usr organized in this way may help when the time
comes to upgrade the operating system, since only /usr/local need be preserved. We recommend that
you compile your applications to run and access required files from the /usr/local hierarchy.
Additional features and programming systems may have their own directory structures and program
directories. Chief among these is the X Window System, which is commonly installed in the /usr/X11
directory. Alternative locations include /usr/X11R6 for Revision 6, also used by the XFree86 variant for
Intel processors distributed by the XFree consortium as well as many Linux distributions. Other UNIXlike
systems may choose different locations, such as /usr/openwin for Sun’s Open Windows provided
with Solaris.