(2) beginning linux-programming-third-edition.9780764544972.24103(1)


Published on


Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

(2) beginning linux-programming-third-edition.9780764544972.24103(1)

  1. 1. Beginning Linux®Programming Third Edition Neil Matthew Richard Stones
  2. 2. Beginning Linux®Programming Third Edition Neil Matthew Richard Stones
  3. 3. Beginning Linux® ProgrammingThird EditionPublished byWiley Publishing, Inc.10475 Crosspoint BoulevardIndianapolis, IN 46256Copyright © 2004 by Wiley Publishing, Inc. All rights reserved.Published simultaneously in CanadaNo part of this publication may be reproduced, stored in a retrieval system, or transmitted in any formor by any means, electronic, mechanical, photocopying, recording, scanning, or otherwise, except aspermitted under Section 107 or 108 of the 1976 United States Copyright Act, without either the priorwritten permission of the Publisher, or authorization through payment of the appropriate per-copy feeto the Copyright Clearance Center, Inc., 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax(978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department,Wiley Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317)572-4447, E-mail: permcoordinator@wiley.com.LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: WHILE THE PUBLISHER AND AUTHORHAVE USED THEIR BEST EFFORTS IN PREPARING THIS BOOK, THEY MAKE NO REPRESENTA-TIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THECONTENTS OF THIS BOOK AND SPECIFICALLY DISCLAIM ANY IMPLIED WARRANTIES OFMERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. NO WARRANTY MAY BECREATED OR EXTENDED BY SALES REPRESENTATIVES OR WRITTEN SALES MATERIALS.THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR YOUR SIT-UATION. YOU SHOULD CONSULT WITH A PROFESSIONAL WHERE APPROPRIATE. NEITHERTHE PUBLISHER NOR AUTHOR SHALL BE LIABLE FOR ANY LOSS OF PROFIT OR ANY OTHERCOMMERCIAL DAMAGES, INCLUDING BUT NOT LIMITED TO SPECIAL, INCIDENTAL, CON-SEQUENTIAL, OR OTHER DAMAGES.For general information on our other products and services please contact our Customer Care Depart-ment within the United States at (800) 762-2974, outside the United States at (317) 572-3993 or fax (317)572-4002.Trademarks: Wiley, Wrox, the Wrox logo, and related trade dress are trademarks or registered trade-marks of John Wiley & Sons, Inc. and/or its affiliates in the United States and other countries, and maynot be used without written permission. Linux is a trademark of Linus Torvalds. All other trademarksare the property of their respective owners. Wiley Publishing, Inc., is not associated with any product orvendor mentioned in this book.Wiley also publishes its books in a variety of electronic formats. Some content that appears in printmay not be available in electronic books.Library of Congress Control Number: 2003115911ISBN: 0-7645-4497-7Printed in the United States of America10 9 8 7 6 5 4 3 2 13B/RX/RS/QT/IN
  4. 4. About Neil Matthew 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. Neil 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. Most of Neil’s “home” projects were originally developed using SCO UNIX, but they’ve all ported to Linux with little or no trouble. 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. As the head of software and principal engineer at Camtec Electronics in the 1980s, Neil programmed in C and C++ for real-time embedded systems. Since then he’s worked on software development tech- niques and quality assurance. After a spell as a consultant with Scientific Generics he is currently work- ing as a systems architect with Celesio AG. Neil is married to Christine and has two children, Alexandra and Adrian. He lives in a converted barn in Northamptonshire, England. His interests include solving puzzles by computer, music, science fiction, squash, mountain biking, and not doing it yourself.About Rick Stones Rick 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 employ- ees, 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, very large help desk systems, and more recently as the technical authority on a large EPoS and retail central systems program. 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.) Rick lives in a village in Leicestershire, England, with his wife Ann, children Jennifer and Andrew, and two cats. Outside work his main interest is classical music, especially early religious music, and he even does his best to find time for some piano practice. He is currently trying to learn to speak German.
  5. 5. CreditsAuthors Vice President and Executive PublisherNeil Matthew Bob IpsenRichard Stones Vice President and PublisherExecutive Editor Joseph B. WikertDebra Williams Cauley Executive Editorial DirectorDevelopment Editor Mary BednarekJames H. Russell Project CoordinatorsProduction Editor Regina SnyderPamela Hanley April FarlingTechnical Editors Graphics and Production SpecialistsArthur Griffith Steved AranyTobias DiPasquale Beth Brooks Carrie FosterCopy Editor Lauren GoddardPublication Services Kristin McMullan Janet SeibSenior Production ManagerFred Bernardi Quality Control Technicians Laura AlbertEditorial Manager Andy HollandbeckMary Beth Wakefield Rob SpringerVice President & Executive Group Publisher Proofreading and IndexingRichard Swadley TECHBOOKS Production Services
  6. 6. Contents Foreword by Alan Cox xxxi Introduction xxxiiiChapter 1: Getting Started 1 An Introduction to UNIX, Linux, and GNU 1 What Is UNIX? 1 A Brief History of UNIX 2 UNIX Philosophy 2 What Is Linux? 3 The GNU Project and the Free Software Foundation 3 Linux Distributions 4 Programming Linux 5 Linux Programs 5 The C Compiler 6 Try It Out—Our First Linux C Program 7 Development System Roadmap 8 Applications 8 Header Files 8 Library Files 9 Static Libraries 10 Try It Out—Static Libraries 10 Shared Libraries 12 Getting Help 13 Try It Out—Manual Pages and info 14 Summary 15Chapter 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 23
  7. 7. Contents The Shell as a Programming Language 23 Interactive Programs 23 Creating a Script 25 Making a Script Executable 26 Shell Syntax 27 Variables 28 Quoting 28 Try It Out—Variables 29 Environment Variables 29 Parameter Variables 30 Try It Out—Parameter and Environment Variables 31 Conditions 32 The test, or [, Command 32 Control Structures 35 if 35 Try It Out—Using the if Command 35 elif 36 Try It Out—Doing Further Checks with an elif 36 A Problem with Variables 36 for 37 Try It Out—for Loop with Fixed Strings 38 Try It Out—for Loop with Wildcard Expansion 38 while 39 Try It Out—Here We Go Again, Again 40 until 41 case 41 Try It Out—Case I: User Input 42 Try It Out—Case II: Putting Patterns Together 42 Try It Out—Case IIl: Executing Multiple Statements 43 Lists 44 Try It Out—AND Lists 45 Try It Out—OR Lists 46 Statement Blocks 47 Functions 47 Try It Out—A Simple Function 47 Try It Out—Returning a Value 49 Commands 50 break 50 The : Command 51viii
  8. 8. Contents continue 51 The . Command 52 Try It Out—The Dot Command 53 echo 53 eval 54 exec 54 exit n 55 export 55 Try It Out—Exporting Variables 56 expr 56 printf 57 return 58 set 58 shift 59 trap 60 Try It Out—Trapping Signals 60 unset 62 Two More Useful Commands and Regular Expressions 62 Try It Out—find with Tests 64 Try It Out—Basic grep Usage 66 Try It Out—Regular Expressions 68 Command Execution 69 Arithmetic Expansion 70 Parameter Expansion 70 Try It Out—Parameter Processing 72 Here Documents 73 Try It Out—Using Here Documents 73 Try It Out—Another Use for a Here Document 74 Debugging Scripts 75Going Graphical—The Dialog Utility 76 Try It Out—Using the dialog Utility 78 Try It Out 79Putting It All Together 80 Requirements 81 Design 81 Try It Out—A CD Application 83 Notes on the Application 90Summary 90 ix
  9. 9. ContentsChapter 3: Working with Files 91 Linux File Structure 92 Directories 92 Files and Devices 93 /dev/console 93 /dev/tty 93 /dev/null 94 System Calls and Device Drivers 94 Library Functions 95 Low-Level File Access 96 write 96 read 97 open 98 Initial Permissions 100 umask 100 close 101 ioctl 102 Try It Out—A File Copy Program 102 Other System Calls for Managing Files 104 lseek 104 fstat, stat, and lstat 104 dup and dup2 106 The Standard I/O Library 107 fopen 108 fread 108 fwrite 109 fclose 109 fflush 109 fseek 110 fgetc, getc, and getchar 110 fputc, putc, and putchar 110 fgets and gets 111 Formatted Input and Output 111 printf, fprintf, and sprintf 112 scanf, fscanf, and sscanf 114 Other Stream Functions 116 Try It Out—Another File Copy Program 116 Stream Errors 117 Streams and File Descriptors 118 File and Directory Maintenance 118 chmod 118 chown 119x
  10. 10. Contents unlink, link, and symlink 119 mkdir and rmdir 119 chdir and getcwd 120 Scanning Directories 120 opendir 121 readdir 121 telldir 122 seekdir 122 closedir 122 Try It Out—A Directory-Scanning Program 122 Errors 125 strerror 125 perror 126 The /proc File System 126 Advanced Topics: fcntl and mmap 130 fcntl 130 mmap 131 Try It Out—Using mmap 132 Summary 134Chapter 4: The Linux Environment 135 Program Arguments 135 Try It Out—Program Arguments 137 getopt 138 Try It Out—getopt 139 getopt_long 140 Environment Variables 142 Try It Out—getenv and putenv 143 Use of Environment Variables 144 The environ Variable 145 Try It Out—environ 145 Time and Date 146 Try It Out—time 146 Try It Out—gmtime 148 Try It Out—ctime 150 Try It Out—strftime and strptime 152 Temporary Files 153 Try It Out—tmpnam and tmpfile 154 User Information 155 Try It Out—User Information 156 Host Information 158 Try It Out—Host Information 159 xi
  11. 11. Contents Logging 160 Try It Out—syslog 162 Try It Out—logmask 163 Resources and Limits 164 Try It Out—Resource Limits 167 Summary 169Chapter 5: Terminals 171 Reading from and Writing to the Terminal 171 Try It Out—Menu Routines in C 172 Canonical versus Non-Canonical Modes 173 Handling Redirected Output 174 Try It Out—Checking for Output Redirection 175 Talking to the Terminal 176 Try It Out—Using /dev/tty 176 The Terminal Driver and the General Terminal Interface 178 Overview 178 Hardware Model 179 The termios Structure 180 Input Modes 181 Output Modes 182 Control Modes 183 Local Modes 183 Special Control Characters 184 Characters 185 The TIME and MIN Values 185 Accessing Terminal Modes from the Shell 186 Setting Terminal Modes from the Command Prompt 187 Terminal Speed 187 Additional Functions 188 Try It Out—A Password Program with termios 189 Try It Out—Reading Each Character 190 Terminal Output 191 Terminal Type 191 Identify Your Terminal Type 192 Using terminfo Capabilities 195 Try It Out—Total Terminal Control 198 Detecting Keystrokes 200 Try It Out—Your Very Own kbhit 200 Virtual Consoles 202 Pseudo Terminals 204 Summary 204xii
  12. 12. ContentsChapter 6: Managing Text-Based Screens with curses 205 Compiling with curses 206 Concepts 207 Try It Out—A Simple curses Program 208 Initialization and Termination 210 Output to the Screen 210 Reading from the Screen 211 Clearing the Screen 211 Moving the Cursor 212 Character Attributes 212 Try It Out—Moving, Inserting, and Attributes 212 The Keyboard 214 Keyboard Modes 215 Keyboard Input 215 Try It Out—Keyboard Modes and Input 216 Windows 217 The WINDOW Structure 217 Generalized Functions 218 Moving and Updating a Window 218 Try It Out—Multiple Windows 219 Optimizing Screen Refreshes 222 Subwindows 223 Try It Out—Subwindows 223 The Keypad 225 Try It Out—Using the Keypad 226 Color 227 Try It Out—-Colors 229 Redefining Colors 230 Pads 230 Try It Out—Using a Pad 231 The CD Collection Application 233 Try It Out—A New CD Collection Application 233 Try It Out—Looking at main 235 Try It Out—The Menu 236 Try It Out—Database File Manipulation 238 Try It Out—Querying the CD Database 243 Summary 247Chapter 7: Data Management 249 Managing Memory 249 Simple Memory Allocation 250 Try It Out—Simple Memory Allocation 250 xiii
  13. 13. Contents Allocating Lots of Memory 251 Try It Out—Asking for All Physical Memory 251 Try It Out—Available Memory 252 Abusing Memory 254 Try It Out—Abuse Your Memory 254 The Null Pointer 255 Try It Out—Accessing a Null Pointer 255 Freeing Memory 256 Try It Out—Freeing Memory 257 Other Memory Allocation Functions 257 File Locking 258 Creating Lock Files 258 Try It Out—Creating a Lock File 259 Try It Out—Cooperative Lock Files 260 Locking Regions 262 The F_GETLK Command 263 The F_SETLK Command 264 The F_SETLKW Command 264 Use of read and write with Locking 264 Try It Out—Locking a File with fcntl 265 Try It Out—Testing Locks on a File 267 Competing Locks 270 Try It Out—Competing Locks 270 Other Lock Commands 273 Deadlocks 273 Databases 274 The dbm Database 274 Introduction to dbm 274 Getting dbm 275 Troubleshooting and Reinstalling dbm 275 The dbm Routines 276 dbm Access Functions 277 dbm_open 277 dbm_store 277 dbm_fetch 277 dbm_close 278 Try It Out—A Simple dbm Database 278 Additional dbm Functions 280 dbm_delete 281 dbm_error 281xiv
  14. 14. Contents dbm_clearerr 281 dbm_firstkey and dbm_nextkey 281 Try It Out—Retrieving and Deleting 281 The CD Application 282 Updating the Design 283 The CD Database Application Using dbm 283 Try It Out—cd_data.h 284 Try It Out—app_ui.c 285 Try It Out—cd_access.c 295 Summary 302Chapter 8: MySQL 303 Installation 304 Precompiled Packages 304 Post-Install Configuration 306 Post-Installation Troubleshooting 310 MySQL Administration 310 Commands 310 myisamchk 311 mysql 311 mysqladmin 312 mysqlbug 313 mysqlimport 314 mysqlshow 314 Creating Users and Giving Them Permissions 314 grant 314 revoke 316 Passwords 316 Creating a Database 317 Data Types 318 Boolean 318 Character 318 Number 318 Temporal 319 Creating a Table 319 Graphical Tools 322 Accessing MySQL Data from C 325 Connection Routines 326 Error Handling 330 xv
  15. 15. Contents Executing SQL Statements 331 SQL Statements That Return No Data 331 Discovering What You Inserted 334 Try It Out 334 Try It Out 335 Statements That Return Data 336 Processing Returned Data 340 Miscellaneous Functions 346 The CD Database Application 347 Creating the Tables 348 Adding Some Data 350 Try It Out 352 Accessing the Application Data from C 353 Interface Definition 353 Test Application Interface 354 Implementing the Interface 355 Summary 363Chapter 9: Development Tools 365 Problems of Multiple Source Files 365 The make Command and Makefiles 366 The Syntax of Makefiles 366 Options and Parameters to make 367 Dependencies 367 Rules 368 Try It Out—A Simple Makefile 368 Comments in a Makefile 370 Macros in a Makefile 370 Try It Out—A Makefile with Macros 370 Multiple Targets 372 Try It Out—Multiple Targets 372 Built-in Rules 374 Suffix and Pattern Rules 375 Managing Libraries with make 376 Try It Out—Managing a Library 376 Advanced Topic: Makefiles and Subdirectories 378 GNU make and gcc 378 Try It Out—gcc -MM 379 Source Code Control 379 RCS 380 The rcs Command 380 The ci Command 381xvi
  16. 16. Contents The co Command 381 The rlog Command 382 The rcsdiff Command 383 Identifying Revisions 383 Try It Out—GNU make with RCS 385 The ident Command 385 Try It Out—ident 385 SCCS 386 Comparing RCS and SCCS 386 CVS 386 Using CVS Locally 387 Accessing CVS over a Network 390 gCVS 390 BitKeeper 391 Writing a Manual Page 391 Distributing Software 395 The patch Program 395 Other Distribution Utilities 397 RPM Packages 399 Working with RPM Package Files 400 Installing RPM Packages 400 Building RPM Packages 401 Gathering the Software 401 Creating an RPM Spec File 403 Building an RPM Package with rpmbuild 407 Other Package Formats 410 Development Environments 410 xwpe 410 C-Forge 411 KDevelop 412 Other Environments 414 Summary 415Chapter 10: Debugging 417 Types of Errors 417 General Debugging Techniques 418 A Program with Bugs 418 Code Inspection 421 Instrumentation 422 Try It Out—Debug Information 423 Debugging without Recompiling 424 Controlled Execution 424 xvii
  17. 17. Contents Debugging with gdb 425 Starting gdb 425 Running a Program 426 Stack Trace 427 Examining Variables 427 Listing the Program 428 Setting Breakpoints 429 Patching with the Debugger 432 Learning More about gdb 433 More Debugging Tools 434 Lint: Removing the Fluff from Your Programs 434 Function Call Tools 436 ctags 436 cxref 436 cflow 437 Execution Profiling with prof/gprof 438 Assertions 439 Try It Out—assert 439 Memory Debugging 440 ElectricFence 441 Try It Out—ElectricFence 441 valgrind 442 Try It Out—valgrind 442 Summary 444Chapter 11: Processes and Signals 445 What Is a Process? 445 Process Structure 446 The Process Table 447 Viewing Processes 447 System Processes 449 Process Scheduling 450 Starting New Processes 451 Try It Out—system 452 Replacing a Process Image 453 Try It Out—execlp 454 Duplicating a Process Image 455 Try It Out—fork 457 Waiting for a Process 458 Try It Out—wait 458 Zombie Processes 460 Try It Out—Zombies 460xviii
  18. 18. Contents Input and Output Redirection 462 Try It Out—Redirection 462 Threads 463 Signals 463 Try It Out—Signal Handling 465 Sending Signals 467 Try It Out—An Alarm Clock 468 A Robust Signals Interface 470 Try It Out—sigaction 470 Signal Sets 471 sigaction Flags 473 Common Signal Reference 474 Summary 476Chapter 12: POSIX Threads 477 What Is a Thread? 477 Advantages and Drawbacks of Threads 478 A First Threads Program 479 Try It Out—A Simple Threaded Program 481 Simultaneous Execution 483 Try It Out—Simultaneous Execution of Two Threads 483 Synchronization 485 Synchronization with Semaphores 485 Try It Out—A Thread Semaphore 486 Synchronization with Mutexes 490 Try It Out—A Thread Mutex 490 Thread Attributes 494 Try It Out—Setting the Detached State Attribute 495 Thread Attributes—Scheduling 497 Try It Out—Scheduling 497 Canceling a Thread 498 Try It Out—Canceling a Thread 499 Threads in Abundance 501 Try It Out—Many Threads 501 Summary 504Chapter 13: Inter-Process Communication: Pipes 505 What Is a Pipe? 505 Process Pipes 506 popen 506 pclose 507 Try It Out—Reading Output from an External Program 507 xix
  19. 19. Contents Sending Output to popen 508 Try It Out—Sending Output to an External Program 508 Passing More Data 509 Try It Out—Reading Larger Amounts of Data from a Pipe 509 How popen Is Implemented 510 Try It Out—popen Starts a Shell 510 The Pipe Call 511 Try It Out—The pipe Function 512 Try It Out—Pipes across a fork 513 Parent and Child Processes 514 Try It Out—Pipes and exec 515 Reading Closed Pipes 516 Pipes Used as Standard Input and Output 517 File Descriptor Manipulation by close and dup 517 Try It Out—Pipes and dup 518 Named Pipes: FIFOs 520 Try It Out—Creating a Named Pipe 521 Accessing a FIFO 522 Try It Out—Accessing a FIFO File 522 Opening a FIFO with open 523 Try It Out—Opening FIFO Files 524 O_RDONLY and O_WRONLY with No O_NONBLOCK 525 O_RDONLY with O_NONBLOCK and O_WRONLY 526 Reading and Writing FIFOs 526 Try It Out—Inter-Process Communication with FIFOs 527 Advanced Topic: Client/Server Using FIFOs 529 Try It Out—An Example Client/Server Application 530 The CD Database Application 533 Aims 534 Implementation 534 Try It Out—The Header File, cliserv.h 536 Client Interface Functions 538 Try It Out—The