Transcription of Linux System Programming - Institut Gaspard Monge
1 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.
2 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.
3 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 2 of 396 Linux System Programming by Robert Love Copyright 2007 O'Reilly Media, Inc.
4 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 ( ). For more information, contact our corporate/institutional sales department: (800) 998-9938 or 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.
5 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.
6 ISBN-10: 0-596-00958-5 ISBN-13: 978-0-596-00958-8 [M] Linux System Programming 3 of 396 Table of Contents Linux System Foreword ..10 Audience and Contents of This Versions Covered in This Conventions Used in This Book ..13 Safari Books Using Code Examples ..15 How to Contact Us ..15 Chapter 1. Introduction and Essential System System Calls ..18 The C The C APIs and ABIs ..19 APIs ..19 POSIX and SUS C Language Linux and the Standards ..22 This Book and the Concepts of Linux Programming ..23 Files and the Users and Groups.
7 29 Signals ..31 Interprocess Communication ..31 Error Handling ..31 Getting Started with System Programming ..35 Chapter 2. File Opening Files ..36 The open( ) System Call ..36 Owners of New Files ..39 Permissions of New The creat( ) Return Values and Error Codes ..43 Reading via read( )..43 Return Reading All the Bytes ..44 Nonblocking Other Error Values ..45 Size Limits on read( ) ..46 Writing with write( ) ..46 Partial Append Mode ..47 Nonblocking Writes ..48 Other Error Codes ..48 Size Limits on write( ).
8 49 Behavior of write( ) ..49 Synchronized I/O ..50 fsync( ) and fdatasync( )..50 Linux System Programming 4 of 396 sync( )..52 The O_SYNC O_DSYNC and O_RSYNC ..53 Direct Closing Error Values ..54 Seeking with lseek( ) ..55 Seeking Past the End of a Error Values ..56 Limitations ..57 Positional Reads and Writes ..57 Error Values ..58 Truncating Multiplexed select( )..60 poll( ) ..65 poll( ) Versus select( ) ..69 Kernel The Virtual The Page Cache ..71 Page Conclusion ..73 Chapter 3. Buffered User-Buffered Block Size.
9 75 Standard I/O ..75 File Pointers ..76 Opening Files ..76 Modes ..76 Opening a Stream via File Descriptor ..78 Closing Streams ..78 Closing All Streams ..78 Reading from a Stream ..79 Reading a Character at a Reading an Entire Reading Binary Issues of Alignment ..81 Writing to a Stream ..82 Writing a Single Writing a String of Writing Binary Sample Program Using Buffered I/O ..83 Seeking a Obtaining the Current Stream Flushing a Errors and End-of-File ..86 Obtaining the Associated File Descriptor ..86 Controlling the Thread Safety.
10 88 Manual File Locking ..89 Unlocked Stream Operations ..90 Critiques of Standard Conclusion ..91 Chapter 4. Advanced File I/O ..92 Scatter/Gather I/O ..92 readv( ) and writev( )..93 Optimizing the Count ..95 The Event Poll Interface ..97 Creating a New Epoll Instance ..98 Linux System Programming 5 of 396 Controlling Waiting for Events with Epoll .. 102 Edge- Versus Level-Triggered 104 Mapping Files into Memory .. 104 mmap( ) .. 104 Protection Flags, Architectures, and 105 munmap( ).. 111 Mapping 112 Advantages of mmap( ).