Example: tourism industry

Linux System Programming

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.

• 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

Tags:

  Programming, Library, Directories

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Transcription of Linux System Programming

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. 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.

2 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.

3 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. 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.

4 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] Linux System Programming 3 of 396 Table of Contents Linux System Foreword.

5 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 ..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.

6 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( ) ..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 ..75 Standard I/O ..75 File Pointers ..76 Opening Files ..76 Modes ..76 Opening a Stream via File Descriptor.

7 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 ..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.

8 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( ) .. 113 Disadvantages of mmap( ) .. 114 Resizing a Mapping .. 114 Changing the Protection of a 116 Synchronizing a File with a Mapping .. 116 Giving Advice on a 118 120 Advice for Normal File I/O .. 121 The posix_fadvise( ) System Call .. 121 The readahead( ) System Call .. 124 Advice Is 124 Synchronized, Synchronous, and Asynchronous Operations .. 125 Asynchronous I/O .. 126 I/O Schedulers and I/O 127 Disk Addressing .. 128 The Life of an I/O 128 Helping Out Reads .. 129 Selecting and Configuring Your I/O 131 Optimizing I/O Performance .. 131 137 Chapter 5. Process 138 The Process 138 Process ID Allocation.

9 139 The Process Hierarchy .. 139 pid_t .. 139 Obtaining the Process ID and Parent Process 139 Running a New Process .. 140 The Exec Family of Calls ..140 The fork( ) System 144 Terminating a 147 Other Ways to Terminate .. 148 atexit( ).. 149 on_exit( ) .. 150 SIGCHLD .. 150 Waiting for Terminated Child Processes .. 150 Waiting for a Specific Process .. 153 Even More Waiting Versatility .. 155 BSD Wants to Play: wait3( ) and wait4( ).. 158 Launching and Waiting for a New 160 Zombies .. 161 Users and Groups .. 162 Real, Effective, and Saved User and Group IDs .. 162 Changing the Real or Saved User or Group ID .. 163 Changing the Effective User or Group 164 Changing the User and Group IDs, BSD Style .. 164 Changing the User and Group IDs, HP-UX Style .. 165 Preferred User/Group ID 165 Support for Saved User 165 Obtaining the User and Group 166 Sessions and Process Groups.

10 166 Session System 168 Linux System Programming 6 of 396 Process Group System Calls .. 170 Obsolete Process Group 171 172 173 Chapter 6. Advanced Process Management .. 175 Process Scheduling .. 175 Big-Oh 176 176 I/O- Versus Processor-Bound 177 Preemptive Scheduling .. 177 Threading .. 178 Yielding the Processor .. 178 Legitimate Uses .. 179 Yielding, Past and Present .. 179 Process Priorities .. 180 nice( ) .. 181 getpriority( ) and setpriority( ) ..182 I/O 183 Processor 184 sched_getaffinity() and sched_setaffinity( ).. 184 Real-Time 187 Hard Versus Soft Real-Time 187 Latency, Jitter, and 188 Linux 's Real-Time Linux Scheduling Policies and Priorities .. 189 Setting Scheduling Parameters .. 192 sched_rr_get_interval( ) .. 195 Precautions with Real-Time 197 Determinism.


Related search queries