Beginning Linux Programming

Beginning Linux Programming

Beginning Linux Programming

Beginning Linux Programming

eBook

$25.00 

Available on Compatible NOOK Devices and the free NOOK Apps.
WANT A NOOK?  Explore Now

Related collections and offers

LEND ME® See Details

Overview

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.

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

Neil Matthew has been interested in and has programmed computers since 1974. A mathematics graduate from the University of Nottingham, Neil is just plain keen on programming languages and likes to explore new ways of solving computing problems. He’s written systems to program in BCPL, FP (Functional Programming), Lisp, Prolog, and a structured BASIC. He even wrote a 6502 microprocessor emulator to run BBC microcomputer programs on UNIX systems. In terms of UNIX experience, Neil has used almost every flavor since the late 1970s, including BSD UNIX, AT&T System V, Sun Solaris, IBM AIX, many others, and of course Linux. He can claim to have been using Linux since August 1993 when he acquired a floppy disk distribution of Soft Landing (SLS) from Canada, with kernel version 0.99.11. He’s used Linux-based computers for hacking C, C++, Icon, Prolog, Tcl, and Java at home and at work.
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-7


Chapter One

Getting Started

In 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

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

From the B&N Reads Blog

Customer Reviews