Skip to main content

Beginning Linux Programming, 4th Edition

Beginning Linux Programming, 4th Edition

Neil Matthew, Richard Stones

ISBN: 978-1-118-05861-9

Apr 2011

816 pages

$25.99

Description

Beginning Linux Programming, Fourth Edition continues its unique approach to teaching UNIX programming in a simple and structured way on the Linux platform. Through the use of detailed and realistic examples, students learn by doing, and are able to move from being a Linux beginner to creating custom applications in Linux. The book introduces fundamental concepts beginning with the basics of writing Unix programs in C, and including material on basic system calls, file I/O, interprocess communication (for getting programs to work together), and shell programming. Parallel to this, the book introduces the toolkits and libraries for working with user interfaces, from simpler terminal mode applications to X and GTK+ for graphical user interfaces. Advanced topics are covered in detail such as processes, pipes, semaphores, socket programming, using MySQL, writing applications for the GNOME or the KDE desktop, writing device drivers, POSIX Threads, and kernel programming for the latest Linux Kernel.

Acknowledgements x

Foreword xxiii

Introduction xxv

Chapter 1: Getting Started 1

An Introduction to UNIX, Linux, and GNU 1

Programming Linux 4

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

The Shell as a Programming Language 23

Going Graphical — The dialog Utility 75

Putting It All Together 81

Summary 91

Chapter 3: Working with Files 93

Linux File Structure 94

System Calls and Device Drivers 96

Library Functions 97

Low-Level File Access 98

The Standard I/O Library 109

Formatted Input and Output 113

File and Directory Maintenance 120

Scanning Directories 122

Errors 127

The /proc File System 128

Advanced Topics: fcntl and mmap 132

Summary 135

Chapter 4: The Linux Environment 137

Program Arguments 137

Environment Variables 144

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

The termios Structure 184

Terminal Output 196

Detecting Keystrokes 205

Summary 209

Chapter 6: Managing Text-Based Screens with curses 211

Compiling with curses 212

Curses Terminology and Concepts 213

The Screen 216

The Keyboard 221

Windows 224

Subwindows 230

The Keypad 232

Using Color 235

Pads 238

The CD Collection Application 240

Summary 254

Chapter 7: Data Management 255

Managing Memory 255

File Locking 264

Databases 281

The CD Application 289

Summary 309

Chapter 8: MySQL 311

Installation 312

MySQL Administration 320

Accessing MySQL Data from C 335

The CD Database Application 358

Summary 375

Chapter 9: Development Tools 377

Problems of Multiple Source Files 377

The make Command and Makefiles 378

Source Code Control 392

Writing a Manual Page 406

Distributing Software 409

RPM Packages 413

Other Package Formats 424

Development Environments 424

Summary 427

Chapter 10: Debugging 429

Types of Errors 429

General Debugging Techniques 430

Debugging with gdb 437

More Debugging Tools 445

Assertions 452

Memory Debugging 453

Summary 459

Chapter 11: Processes and Signals 461

What Is a Process? 461

Process Structure 462

Starting New Processes 468

Signals 481

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

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

The Pipe Call 531

Parent and Child Processes 535

Named Pipes: FIFOs 540

The CD Database Application 553

Summary 575

Chapter 14: Semaphores, Shared Memory, and Message Queues 577

Semaphores 577

Shared Memory 586

Message Queues 594

The CD Database Application 599

IPC Status Commands 604

Summary 605

Chapter 15: Sockets 607

What Is a Socket? 608

Socket Connections 608

Network Information 624

Multiple Clients 632

Datagrams 642

Summary 644

Chapter 16: Programming GNOME Using GTK+ 645

Introducing X 645

Introducing GTK+ 648

Events, Signals, and Callbacks 655

Packing Box Widgets 658

GTK+ Widgets 661

GNOME Widgets 676

GNOME Menus 677

Dialogs 682

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

Dialogs 727

Menus and Toolbars with KDE 733

CD Database Application Using KDE/Qt 738

Summary 746

Chapter 18: Standards for Linux 747

The C Programming Language 748

Interfaces and the Linux Standards Base 751

The Filesystem Hierarchy Standard 755

Further Reading about Standards 758

Summary 759

Index 761

Beginning Linux Programming, 4th Edition
Zip file containing all the code for the book.
Download
Code Downloads
Code downloads for all chapters of this title are available here.
Download
ChapterPageDetailsDatePrint Run
252Error in Code
Try it Out: The Dot Command.
The first "echo $version", "ion" should be bold
14-Oct-2015

55Error in Text
Try It Out, section 2

Currently reads: ?export2?
Should read: ?./export2?
02/23/2012

55Error in Text
In the "How it works" section, second sentence:

HOW IT READS NOW:
...so when it subsequently invokes export1, the ...

HOW IT SHOULD READ:
...so when it subsequently invokes export2, the ...
12/28/2012

60Error in Text
trap INT

Should be:

trap - INT
9/17/09

60Error in Text
The executed script print out
-"press interrupt (CTRL-C) to interrupt ...."

Should be:
-"press interrupt (control-C) to interrupt ...."
03/21/2010

262Text correction: Error in Options under "The find Command"
The third entry in the table of Options near the bottom of the page, -maxdepths N
Should read -maxdepth N
3/12/14

68Error in Text
Section 4:

The line reading "Proceeding from the heat-oppressed brain" should be deleted

Or

Insert text:
the command 
$ grep -E [a-z]\{10\} words2.txt
should be replaced with
$ grep -E [A-Za-z]\{10\} words2.txt
02/23/2012

272Text Correction: Error in Code
CURRENTLY READS:
bar
fud
usr/bin/X11/startx
startx
/usr/local/etc
/usr
SHOULD READ:
bar
fud
usr/bin/X11/startx
startx
/usr/local/etc/
/usr/
10/02/14

290Error in Code
(a line of code in the first grey box on the page)
Currently reads:
num_tracks=$(wc -l $temp_file)
Should Read
num_tracks=$(wc -l <$temp_file)
17 June 2015

290Error in Code
(a line of code in the first grey box on the page)
Currently reads:
num_tracks=$(wc -l $temp_file)
Should Read:
num_tracks=$(wc -l <$temp_file)
17 June 2015

94Error in Text
Second paragraph in "Directories" section:

You can see the inode number for a file using ln -i.

Should be:

... using ls -i.
1/11/10

399Error in Code
code snippet at the top:
46 should be 47
14-Oct-2015

142Error in Text
Try It Out:
Text reading:
./longopt: invalid option -- q
should be deleted
02/23/2012

346Error in Text
Try It Out:
The output from the SELECT LAST_INSERT_ID(); command is given as:
14
15

To be consistent with the previous sequence of steps and results in the text, should be:
12
13
02/23/2012

347Error in Text
At the bottom of the page:
We inserted childno 6
We inserted childno 7

To be consistent with the sequence of steps and results in the text, this should be:
We inserted childno 14
We inserted childno 15
02/23/2012

363Error in Text
The line:
= cd.id AND track.track_id < 3

Should be:
= cd.id AND track.track_id < 3;

If the MySQL Query Browser is used, the semi-colon is not needed. It is required for the command-line client.
02/23/2012

386Error in Text
In the 3rd paragraph, the sentence:
Here you don't care too much about whether ensuring previous commands succeeded, so you can stick to the simpler form.
should be deleted.
02/23/2012

388-389Error in Code
The code download for Makefile6 in chapter 9 contains some additional lines for managing rpm files and manual pages which are not mentioned in the book text or used by the examples in the book. These additional lines can be ignored.
02/23/2012

416Error in Text
The sentences:
You can modify your earlier makefile, Makefile6, to add a new target to bundle the files into a tarball.
and
The final version of the makefile, simply called Makefile, follows:

should be replaced with:
You can modify your earlier makefile, Makefile5, to add a new target to bundle the files into a tarball.
and
The final version of the makefile, simply called Makefile, follows:
02/23/2012

487Error in Code
About 2/3 down the page:

CURRENTLY READS:
void (*) (int) sa_handler

SHOULD READ:
void (*sa_handler)(int)
10/9/2013

Chapter 6 Code fileError in Code
In line 18 of the "chapter06/multiw1.c" source code file:
Currently reads:
refreshing the actual screen once the logical screen has been filled:
Should read:
refreshing the actual screen once the logical screen has been filled: */

Corrected file posted
02/23/2012

Chapter 7 Code fileError in Code
Line 42 of the chapter07/app/cd_data.h source code file:

Currently reads:
/* two for simple data retrival */
Should read:
/* two for simple data retrieval */
02/23/2012

625Error in Code
CURRENTLY READS:
    if(argc == 1) {
        char myname[256];
        gethostname(myname, 255);
        host = myname;
    }


SHOULD READ:
    char myname[256];
    if(argc == 1) {
        gethostname(myname, 255);
        host = myname;
    }
3/26/2013

623Error in Text
First paragraph

Currently reads:
"However, the local address (the server socket) is given as 1574 (or you may see mvel-lm as a service name), but the port chosen in the example is 9734. Why are they different? The answer is that port numbers and addresses are communicated over socket interfaces as binary numbers. Different computers use different byte ordering for integers. For example, an Intel processor stores the 32-bit integer as four consecutive bytes in memory in the order 1-2-3-4, where 1 is the most significant byte. IBM PowerPC processors would store the integer in the byte order 4-3-2-1. If the memory used for integers were simply copied byte-by-byte, the two different computers would not be able to agree on integer values."

Should read:
"However, the local address (the server socket) is given as 1574 (or you may see mvel-lm as a service name), but the port chosen in the example is 9734. Why are they different? The answer is that port numbers and addresses are communicated over socket interfaces as binary numbers. Different computers use different byte ordering for integers. For example, an IBM PowerPC processor stores the 32-bit integer as four consecutive bytes in memory in the order 1-2-3-4, where 1 is the most significant byte. Intel processors would store the integer in the byte order 4-3-2-1. If the memory used for integers were simply copied byte-by-byte, the two different computers would not be able to agree on integer values."
01/02/2014

40Errata in Text
Grey tip box at top of page currently reads:

In general, if a loop should should always execute at least once, use a while loop; if it may not need to execute at all, use an until loop.

Should read:

In general, if a loop is expected to execute at least once use an until loop, if it may not need to execute at all use a while loop
05-Feb-16