eBook
Available on Compatible NOOK devices, the free NOOK App and in My Digital Library.
Related collections and offers
Overview
Product Details
| ISBN-13: | 9781118058619 |
|---|---|
| Publisher: | Wiley |
| Publication date: | 04/22/2011 |
| Sold by: | JOHN WILEY & SONS |
| Format: | eBook |
| Pages: | 816 |
| File size: | 5 MB |
About the Author
All of Neil’s "home" projects are developed using Linux. He says Linux is much easier because it supports quite a lot of features from other systems, so that both BSD- and System V-targeted programs will generally compile with little or no change.
Neil is currently working as an Enterprise Architect specializing in IT strategy at Celesio AG. He has a background in technical consultancy, software development techniques, and quality assurance. Neil has also programmed in C and C++ for real-time embedded systems.
Rick Stones started programming at school (more years ago than he cares to remember) on a 6502-powered BBC micro, which, with the help of a few spare parts, continued to function for the next 15 years. He graduated from Nottingham University with a degree in Electronic Engineering, but decided software was more fun.
Over the years he has worked for a variety of companies, from the very small with just a dozen employees, to the very large, including the IT services giant EDS. Along the way he has worked on a range of projects, from real-time communications to accounting systems, to very large help desk systems. He is currently working as an IT architect, acting as a technical authority on various major projects for a large pan-European company.
A bit of a programming linguist, he has programmed in various assemblers, a rather neat proprietary telecommunications language called SL-1, some FORTRAN, Pascal, Perl, SQL, and smidgeons of Python and C++, as well as C. (Under duress he even admits that he was once reasonably proficient in Visual Basic, but tries not to advertise this aberration.)
Read an Excerpt
Beginning Linux Programming
By Neil Matthew Richard Stones
John Wiley & Sons
ISBN: 0-7645-4497-7Chapter One
Getting StartedIn this chapter, we discover what Linux is and how it relates to its inspiration, UNIX. We'll take a guided tour of the facilities provided by a Linux development system, and we'll write and run the first program. Along the way, we'll be looking at
UNIX, Linux, and GNU
Programs and programming languages for Linux
How to locate development resources
Static and shared libraries
The UNIX philosophy
An Introduction to UNIX, Linux, and GNU
In recent years Linux has become a phenomenon. Hardly a day goes by without Linux cropping up in the media in some way. We've lost count of the number of applications that have been made available on Linux and the number of organizations that have adopted it.
Linux owes its success to systems and applications that preceded it: UNIX and GNU software. In this section we will look at how Linux came to be and what its roots are.
What Is UNIX?
The UNIX operating system was originally developed at Bell Laboratories, once part of the telecommunications giant AT&T. Designed in the 1970s for Digital Equipment PDP computers, UNIX has become a very popular multiuser, multitasking operating system for a wide variety of hardware platforms, from PC workstations to multiprocessor servers and supercomputers.
A Brief History of UNIX
Strictly, UNIX is a trademark administered by The Open Group, and it refers to a computer operating system that conforms to a particular specification. This specification, known as The Single UNIX Specification, defines the names of, interfaces to, and behaviors of all mandatory UNIX operating system functions. The specification is largely a superset of an earlier series of specifications, the P1003, or POSIX specifications, developed by the IEEE (Institute of Electrical and Electronic Engineers).
The UNIX source code belongs to SCO. Many UNIX-like systems are available commercially, such as SCO's Unixware, IBM's AIX, HP's HP-UX, and Sun's Solaris. Some have been made available for free, such as FreeBSD and Linux. Only a few systems currently conform to The Open Group specification, which allows them to be marketed with the name UNIX.
In the past, compatibility among different UNIX systems has been a real problem, although POSIX was a great help in this respect. These days, by following a few simple rules it is possible to create applications that will run on all UNIX and UNIX-like systems. More details on Linux and UNIX standards can be found in Chapter 19.
UNIX Philosophy
In the following chapters we hope to convey a flavor of Linux (and therefore UNIX) programming. Although programming in C is in many ways the same whatever the platform, it's true to say that UNIX developers have a special view of program and system development, and this view is equally applicable to Linux.
The UNIX operating system, and hence Linux, encourages a certain programming style. Following are a few characteristics shared by typical UNIX programs and systems:
Simplicity: Many of the most useful UNIX utilities are very simple and, as a result, small and easy to understand. "Small and Simple" is a good technique to learn. Larger, more complex systems are guaranteed to contain larger, more complex bugs, and debugging is a chore that we'd all like to avoid!
Focus: It's often better to make a program perform one task well than to throw in every feature along with the kitchen sink. A program with "feature bloat" can be difficult to use and difficult to maintain. Programs with a single purpose are easier to improve as better algorithms or interfaces are developed. In UNIX, small utilities are often combined to perform more demanding tasks when the need arises, rather than trying to anticipate a user's needs in one large program.
Reusable Components: Make the core of your application available as a library. Well-documented libraries with simple but flexible programming interfaces can help others to develop variations or apply the techniques to new application areas. Examples include the dbm database library, which is a suite of reusable functions rather than a single database management program.
Filters: Many UNIX applications can be used as filters. That is, they transform their input and produce output. As we'll see, UNIX provides facilities that allow quite complex applications to be developed from other UNIX programs by combining them in novel ways. Of course, this kind of reuse is enabled by the development methods that we've previously mentioned.
Open File Formats: The more successful and popular UNIX programs use configuration files and data files that are plain ASCII text. If this is an option for your program development, it's a good choice. It enables users to use standard tools to change and search for configuration items and to develop new tools for performing new functions on the data files. A good example of this is the ctags source code cross-reference system, which records symbol location information as regular expressions suitable for use by searching programs.
Flexibility: You can't anticipate exactly how ingeniously users will use your program. Try to be as flexible as possible in your programming. Try to avoid arbitrary limits on field sizes or number of records. If you can, write the program so that it's network-aware and able to run across a network as well as on a local machine. Never assume that you know everything that the user might want to do.
What Is Linux?
As you may already know, Linux is a freely distributed implementation of a UNIX-like kernel, the low-level core of an operating system. Because Linux takes the UNIX system as its inspiration, Linux and UNIX programs are very similar. In fact, almost all programs written for UNIX can be compiled and run on Linux. Also, some commercial applications sold for commercial versions of UNIX can run unchanged in binary form on Linux systems.
Linux was developed by Linus Torvalds at the University of Helsinki, with the help of UNIX programmers from across the Internet. It began as a hobby inspired by Andy Tanenbaum's Minix, a small UNIX-like system, but has grown to become a complete system in its own right. The intention is that the Linux kernel will not incorporate proprietary code but will contain nothing but freely distributable code.
Versions of Linux are now available for a wide variety of computer systems using many different types of CPUs, including PCs based on Intel x86 and compatible processors; workstations and servers using Sun SPARC, IBM PowerPC, and Intel Itanium; and even some handheld PDAs and the Sony Playstation 2. If it's got a processor, someone somewhere is trying to get Linux running on it!
The GNU Project and the Free Software Foundation
Linux owes its existence to the cooperative efforts of a large number of people. The operating system kernel itself forms only a small part of a usable development system. Commercial UNIX systems traditionally come bundled with applications that provide system services and tools. For Linux systems, these additional programs have been written by many different programmers and have been freely contributed.
The Linux community (together with others) supports the concept of free software, that is, software that is free from restrictions, subject to the GNU General Public License. Although there may be a cost involved in obtaining the software, it can thereafter be used in any way desired and is usually distributed in source form.
The Free Software Foundation was set up by Richard Stallman, the author of GNU Emacs, one of the best-known text editors for UNIX and other systems. Stallman is a pioneer of the free software concept and started the GNU Project (the name GNU stands for GNU's Not Unix), an attempt to create an operating system and development environment that would be compatible with UNIX but not suffer the restrictions of the proprietary UNIX name and source code. GNU may turn out to be very different from UNIX at the lowest level but still supports UNIX applications.
The GNU Project has already provided the software community with many applications that closely mimic those found on UNIX systems. All these programs, so-called GNU software, are distributed under the terms of the GNU General Public License (GPL), a copy of that may be found at gnu.org. This license embodies the concept of copyleft (a takeoff on "copyright"). Copyleft is intended to prevent others from placing restrictions on the use of free software.
A few major examples of software from the GNU Project distributed under the GPL follow:
GCC: The GNU Compiler Collection, containing the GNU C compiler
G++: A C++ compiler, included as part of GCC
GDB: A source code-level debugger GNU make: A version of UNIX make
Bison: A parser generator compatible with UNIX yacc bash: A command shell
GNU Emacs: A text editor and environment
Many other packages have been developed and released using free software principles and the GPL, including graphical image manipulation tools such as the Gimp, spreadsheets, source code control tools, compilers and interpreters, Internet tools, and two complete object-based environments: GNOME and KDE. We will discuss GNOME and KDE in Chapters 16 and 17.
There is now so much free software available that with the addition of the Linux kernel it could be said that the goal of a creating GNU, a free UNIX-like system, has been achieved with Linux. To recognize the contribution made by GNU software, many people now refer to Linux systems in general as GNU/Linux.
You can find out more about the free software concept at gnu.org.
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.
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:
/usr/local/bin:/bin:/usr/bin:.:/home/neil/bin:/usr/X11R6/bin
Here the PATH variable contains entries for the standard program locations, the current directory (.), 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.
Continues...
Excerpted from Beginning Linux Programming by Neil Matthew Richard Stones Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Table of Contents
Acknowledgements x
Foreword xxiii
Introduction xxv
Chapter 1: Getting Started 1
An Introduction to UNIX, Linux, and GNU 1
What Is UNIX? 1
What Is Linux? 3
The GNU Project and the Free Software Foundation 3
Linux Distributions 4
Programming Linux 4
Linux Programs 5
Text Editors 6
The C Compiler 7
Development System Roadmap 8
Getting Help 14
Summary 16
Chapter 2: Shell Programming 17
Why Program with a Shell? 18
A Bit of Philosophy 18
What Is a Shell? 19
Pipes and Redirection 21
Redirecting Output 21
Redirecting Input 22
Pipes 22
The Shell as a Programming Language 23
Interactive Programs 23
Creating a Script 24
Making a Script Executable 25
Shell Syntax 27
Variables 27
Conditions 31
Control Structures 34
Functions 46
Commands 49
Command Execution 68
Here Documents 73
Debugging Scripts 74
Going Graphical — The dialog Utility 75
Putting It All Together 81
Requirements 82
Design 82
Summary 91
Chapter 3: Working with Files 93
Linux File Structure 94
Directories 94
Files and Devices 95
System Calls and Device Drivers 96
Library Functions 97
Low-Level File Access 98
write 98
read 99
open 100
Initial Permissions 101
Other System Calls for Managing Files 106
The Standard I/O Library 109
fopen 110
fread 110
fwrite 111
fclose 111
fflush 111
fseek 112
fgetc, getc, and getchar 112
fputc, putc, and putchar 112
fgets and gets 113
Formatted Input and Output 113
printf, fprintf, and sprintf 113
scanf, fscanf, and sscanf 115
Other Stream Functions 117
Stream Errors 119
Streams and File Descriptors 119
File and Directory Maintenance 120
chmod 120
chown 120
unlink, link, and symlink 121
mkdir and rmdir 121
chdir and getcwd 122
Scanning Directories 122
opendir 123
readdir 123
telldir 123
seekdir 124
closedir 124
Errors 127
strerror 127
perror 127
The /proc File System 128
Advanced Topics: fcntl and mmap 132
fcntl 132
mmap 133
Summary 135
Chapter 4: The Linux Environment 137
Program Arguments 137
getopt 140
getopt_long 142
Environment Variables 144
Use of Environment Variables 146
The environ Variable 147
Time and Date 148
Temporary Files 156
User Information 158
Host Information 161
Logging 163
Resources and Limits 167
Summary 173
Chapter 5: Terminals 175
Reading from and Writing to the Terminal 175
Talking to the Terminal 180
The Terminal Driver and the General Terminal Interface 182
Overview 183
Hardware Model 183
The termios Structure 184
Input Modes 186
Output Modes 186
Control Modes 187
Local Modes 188
Special Control Characters 188
Terminal Speed 192
Additional Functions 192
Terminal Output 196
Terminal Type 197
Identify Your Terminal Type 197
Using terminfo Capabilities 200
Detecting Keystrokes 205
Virtual Consoles 207
Pseudo-Terminals 208
Summary 209
Chapter 6: Managing Text-Based Screens with curses 211
Compiling with curses 212
Curses Terminology and Concepts 213
The Screen 216
Output to the Screen 216
Reading from the Screen 217
Clearing the Screen 218
Moving the Cursor 218
Character Attributes 218
The Keyboard 221
Keyboard Modes 221
Keyboard Input 222
Windows 224
The WINDOW Structure 224
Generalized Functions 225
Moving and Updating a Window 225
Optimizing Screen Refreshes 229
Subwindows 230
The Keypad 232
Using Color 235
Redefining Colors 238
Pads 238
The CD Collection Application 240
Starting a New CD Collection Application 240
Looking at main 243
Building the Menu 243
Database File Manipulation 245
Querying the CD Database 250
Summary 254
Chapter 7: Data Management 255
Managing Memory 255
Simple Memory Allocation 256
Allocating Lots of Memory 257
Abusing Memory 260
The Null Pointer 261
Freeing Memory 262
Other Memory Allocation Functions 264
File Locking 264
Creating Lock Files 265
Locking Regions 268
Use of read and write with Locking 271
Competing Locks 276
Other Lock Commands 280
Deadlocks 280
Databases 281
The dbm Database 281
The dbm Routines 283
dbm Access Functions 283
Additional dbm Functions 287
The CD Application 289
Updating the Design 289
The CD Database Application Using dbm 290
Summary 309
Chapter 8: MySQL 311
Installation 312
MySQL Packages 312
Post-Install Configuration 314
Post-Installation Troubleshooting 319
MySQL Administration 320
Commands 320
Creating Users and Giving Them Permissions 325
Passwords 327
Creating a Database 328
Data Types 329
Creating a Table 330
Graphical Tools 333
Accessing MySQL Data from C 335
Connection Routines 337
Error Handling 341
Executing SQL Statements 342
Miscellaneous Functions 357
The CD Database Application 358
Creating the Tables 359
Adding Some Data 362
Accessing the Application Data from C 364
Summary 375
Chapter 9: Development Tools 377
Problems of Multiple Source Files 377
The make Command and Makefiles 378
The Syntax of Makefiles 378
Options and Parameters to make 379
Comments in a Makefile 382
Macros in a Makefile 382
Multiple Targets 384
Built-in Rules 387
Suffix and Pattern Rules 388
Managing Libraries with make 389
Advanced Topic: Makefiles and Subdirectories 391
GNU make and gcc 391
Source Code Control 392
RCS 393
SCCS 399
Comparing RCS and SCCS 399
CVS 400
CVS Front Ends 404
Subversion 405
Writing a Manual Page 406
Distributing Software 409
The patch Program 410
Other Distribution Utilities 411
RPM Packages 413
Working with RPM Package Files 414
Installing RPM Packages 415
Building RPM Packages 415
Other Package Formats 424
Development Environments 424
KDevelop 425
Other Environments 425
Summary 427
Chapter 10: Debugging 429
Types of Errors 429
General Debugging Techniques 430
A Program with Bugs 430
Code Inspection 433
Instrumentation 434
Controlled Execution 436
Debugging with gdb 437
Starting gdb 437
Running a Program 438
Stack Trace 438
Examining Variables 439
Listing the Program 440
Setting Breakpoints 441
Patching with the Debugger 444
Learning More about gdb 445
More Debugging Tools 445
Lint: Removing the Fluff from Your Programs 446
Function Call Tools 449
Execution Profiling with prof/gprof 451
Assertions 452
Memory Debugging 453
ElectricFence 454
valgrind 455
Summary 459
Chapter 11: Processes and Signals 461
What Is a Process? 461
Process Structure 462
The Process Table 463
Viewing Processes 463
System Processes 464
Process Scheduling 467
Starting New Processes 468
Waiting for a Process 475
Zombie Processes 477
Input and Output Redirection 479
Threads 480
Signals 481
Sending Signals 484
Signal Sets 489
Summary 493
Chapter 12: POSIX Threads 495
What Is a Thread? 495
Advantages and Drawbacks of Threads 496
A First Threads Program 497
Simultaneous Execution 501
Synchronization 503
Synchronization with Semaphores 503
Synchronization with Mutexes 508
Thread Attributes 512
Canceling a Thread 517
Threads in Abundance 520
Summary 524
Chapter 13: Inter-Process Communication: Pipes 525
What Is a Pipe? 525
Process Pipes 526
Sending Output to popen 528
Passing More Data 529
How popen Is Implemented 530
The Pipe Call 531
Parent and Child Processes 535
Reading Closed Pipes 536
Pipes Used as Standard Input and Output 537
Named Pipes: FIFOs 540
Accessing a FIFO 542
Advanced Topic: Client/Server Using FIFOs 549
The CD Database Application 553
Aims 554
Implementation 555
Client Interface Functions 558
The Server Interface, server.c 565
The Pipe 569
Application Summary 574
Summary 575
Chapter 14: Semaphores, Shared Memory, and Message Queues 577
Semaphores 577
Semaphore Definition 579
A Theoretical Example 579
Linux Semaphore Facilities 580
Using Semaphores 582
Shared Memory 586
shmget 588
shmat 588
shmdt 589
shmctl 589
Message Queues 594
msgget 594
msgsnd 595
msgrcv 595
msgctl 596
The CD Database Application 599
Revising the Server Functions 600
Revising the Client Functions 602
IPC Status Commands 604
Displaying Semaphore Status 604
Displaying Shared Memory Status 604
Displaying Message Queue Status 605
Summary 605
Chapter 15: Sockets 607
What Is a Socket? 608
Socket Connections 608
Socket Attributes 612
Creating a Socket 614
Socket Addresses 615
Naming a Socket 616
Creating a Socket Queue 617
Accepting Connections 617
Requesting Connections 618
Closing a Socket 619
Socket Communications 619
Host and Network Byte Ordering 622
Network Information 624
The Internet Daemon (xinetd/inetd) 629
Socket Options 631
Multiple Clients 632
select 635
Multiple Clients 638
Datagrams 642
Summary 644
Chapter 16: Programming GNOME Using GTK+ 645
Introducing X 645
X Server 646
X Client 646
X Protocol 646
Xlib 647
Toolkits 647
Window Managers 647
Other Ways to Create a GUI — Platform-Independent Windowing APIs 648
Introducing GTK+ 648
GLib Type System 649
GTK+ Object System 650
Introducing GNOME 651
Installing the GNOME/GTK+ Development Libraries 652
Events, Signals, and Callbacks 655
Packing Box Widgets 658
GTK+ Widgets 661
GtkWindow 662
GtkEntry 663
GtkSpinButton 666
GtkButton 668
GtkTreeView 672
GNOME Widgets 676
GNOME Menus 677
Dialogs 682
GtkDialog 682
Modal Dialog Box 684
Nonmodal Dialogs 685
GtkMessageDialog 686
CD Database Application 687
Summary 699
Chapter 17: Programming KDE Using Qt 701
Introducing KDE and Qt 701
Installing Qt 702
Signals and Slots 705
Qt Widgets 712
QLineEdit 712
Qt Buttons 716
QComboBox 721
QListView 724
Dialogs 727
QDialog 728
QMessageBox 730
QInputDialog 731
Using qmake to Simplify Writing Makefiles 733
Menus and Toolbars with KDE 733
CD Database Application Using KDE/Qt 738
MainWindow 738
AddCdDialog 742
LogonDialog 743
main.cpp 745
Summary 746
Chapter 18: Standards for Linux 747
The C Programming Language 748
A Brief History Lesson 748
The GNU Compiler Collection 749
gcc Options 749
Interfaces and the Linux Standards Base 751
LSB Standard Libraries 752
LSB Users and Groups 754
LSB System Initialization 754
The Filesystem Hierarchy Standard 755
Further Reading about Standards 758
Summary 759
Index 761