Linux System Programming Focuses on Everything Above the Kernel, Where Applications Such As Apache, Bash, Cp, Vim, Emacs, Gcc, Gdb, Glibc, Ls, Mv, and X Exist
Total Page:16
File Type:pdf, Size:1020Kb
Overview This book is about writing software that makes the most effective use of the system you're running on -- code that interfaces directly with the kernel and core system libraries, including the shell, text editor, compiler, debugger, core utilities, and system daemons. The majority of both Unix and Linux code is still written at the system level, and Linux System Programming focuses on everything above the kernel, where applications such as Apache, bash, cp, vim, Emacs, gcc, gdb, glibc, ls, mv, and X exist. Written primarily for engineers looking to program (better) at the low level, this book is an ideal teaching tool for any programmer. Even with the trend toward high-level development, either through web software (such as PHP) or managed code (C#), someone still has to write the PHP interpreter and the C# virtual machine. Linux System Programming gives you an understanding of core internals that makes for better code, no matter where it appears in the stack. Debugging high-level code often requires you to understand the system calls and kernel behavior of your operating system, too. Key topics include: • An overview of Linux, the kernel, the C library, and the C compiler • Reading from and writing to files, along with other basic file I/O operations, including how the Linux kernel implements and manages file I/O • Buffer size management, including the Standard I/O library • Advanced I/O interfaces, memory mappings, and optimization techniques • The family of system calls for basic process management • Advanced process management, including real-time processes • File and directories-creating, moving, copying, deleting, and managing them • Memory management -- interfaces for allocating memory, managing the memory you have, and optimizing your memory access • Signals and their role on a Unix system, plus basic and advanced signal interfaces • Time, sleeping, and clock management, starting with the basics and continuing through POSIX clocks and high resolution timers With Linux System Programming , you will be able to take an in-depth look at Linux from both a theoretical and an applied perspective as you cover a wide range of programming topics. Linux System Programming Linux System Programming by Robert Love Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( safari.oreilly.com ). For more information, contact our corporate/institutional sales department: (800) 998-9938 or [email protected] . Editor: Andy Oram Production Editor: Sumita Mukherji Copyeditor: Rachel Head Proofreader: Sumita Mukherji Indexer: John Bickelhaupt Cover Designer: Karen Montgomery Interior Designer: David Futato Illustrator: Jessamyn Read Printing History: September 2007: First Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are registered trademarks of O'Reilly Media, Inc. The Linux series designations, Linux System Programming , images of the man in the flying machine, and related trade dress are trademarks of O'Reilly Media, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. While every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN-10: 0-596-00958-5 ISBN-13: 978-0-596-00958-8 [M] 2 of 396 Linux System Programming Table of Contents Linux System Programming...............................................................................2 Foreword ................................................................................................................ 10 Preface................................................................................................................... 10 P3.1. Audience and Assumptions............................................................................ 11 P3.2. Contents of This Book................................................................................... 11 P3.3. Versions Covered in This Book....................................................................... 13 P3.4. Conventions Used in This Book ...................................................................... 13 P3.5. Safari® Books Online ................................................................................... 15 P3.6. Using Code Examples ................................................................................... 15 P3.7. How to Contact Us ....................................................................................... 15 P3.8. Acknowledgments........................................................................................ 16 Chapter 1. Introduction and Essential Concepts ........................................................... 17 1.1. System Programming..................................................................................... 17 1.1.1. System Calls .......................................................................................... 18 1.1.2. The C Library.......................................................................................... 19 1.1.3. The C Compiler....................................................................................... 19 1.2. APIs and ABIs ............................................................................................... 19 1.2.1. APIs ...................................................................................................... 19 1.2.2. ABIs ...................................................................................................... 20 1.3. Standards..................................................................................................... 20 1.3.1. POSIX and SUS History............................................................................ 21 1.3.2. C Language Standards............................................................................. 21 1.3.3. Linux and the Standards .......................................................................... 22 1.3.4. This Book and the Standards .................................................................... 22 1.4. Concepts of Linux Programming ...................................................................... 23 1.4.1. Files and the Filesystem........................................................................... 23 1.4.2. Processes............................................................................................... 28 1.4.3. Users and Groups ................................................................................... 29 1.4.4. Permissions............................................................................................ 30 1.4.5. Signals .................................................................................................. 31 1.4.6. Interprocess Communication .................................................................... 31 1.4.7. Headers ................................................................................................. 31 1.4.8. Error Handling ........................................................................................ 31 1.5. Getting Started with System Programming ....................................................... 35 Chapter 2. File I/O ................................................................................................... 36 2.1. Opening Files ................................................................................................ 36 2.1.1. The open( ) System Call .......................................................................... 36 2.1.2. Owners of New Files ................................................................................ 39 2.1.3. Permissions of New Files .......................................................................... 40 2.1.4. The creat( ) Function............................................................................... 42 2.1.5. Return Values and Error Codes ................................................................. 43 2.2. Reading via read( )........................................................................................ 43 2.2.1. Return Values......................................................................................... 43 2.2.2. Reading All the Bytes .............................................................................. 44 2.2.3. Nonblocking Reads.................................................................................. 45 2.2.4. Other Error Values .................................................................................. 45 2.2.5. Size Limits on read( ) .............................................................................. 46 2.3. Writing with write( ) ...................................................................................... 46 2.3.1. Partial Writes.......................................................................................... 47 2.3.2. Append Mode ......................................................................................... 47 2.3.3. Nonblocking Writes ................................................................................