1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu LINUX System Programming doc

390 1.5K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Cấu trúc

  • Linux System Programming

    • Table of Contents

    • Foreword

    • Preface

      • Audience and Assumptions

      • Contents of This Book

      • Versions Covered in This Book

      • Conventions Used in This Book

      • Safari® Books Online

      • Using Code Examples

      • How to Contact Us

      • Acknowledgments

    • Introduction and Essential Concepts

      • System Programming

        • System Calls

          • Invoking system calls

        • The C Library

        • The C Compiler

      • APIs and ABIs

        • APIs

        • ABIs

      • Standards

        • POSIX and SUS History

        • C Language Standards

        • Linux and the Standards

        • This Book and the Standards

      • Concepts of Linux Programming

        • Files and the Filesystem

          • Regular files

          • Directories and links

          • Hard links

          • Symbolic links

          • Special files

          • Filesystems and namespaces

        • Processes

          • Threads

          • Process hierarchy

        • Users and Groups

        • Permissions

        • Signals

        • Interprocess Communication

        • Headers

        • Error Handling

      • Getting Started with System Programming

    • File I/O

      • Opening Files

        • The open() System Call

          • Flags for open()

        • Owners of New Files

        • Permissions of New Files

        • The creat() Function

        • Return Values and Error Codes

      • Reading via read()

        • Return Values

        • Reading All the Bytes

        • Nonblocking Reads

        • Other Error Values

        • Size Limits on read()

      • Writing with write()

        • Partial Writes

        • Append Mode

        • Nonblocking Writes

        • Other Error Codes

        • Size Limits on write()

        • Behavior of write()

      • Synchronized I/O

        • fsync( ) and fdatasync()

          • Return values and error codes

        • sync()

        • The O_SYNC Flag

        • O_DSYNC and O_RSYNC

      • Direct I/O

      • Closing Files

        • Error Values

      • Seeking with lseek()

        • Seeking Past the End of a File

        • Error Values

        • Limitations

      • Positional Reads and Writes

        • Error Values

      • Truncating Files

      • Multiplexed I/O

        • select()

          • Return values and error codes

          • select() example

          • Portable sleeping with select()

          • pselect()

        • poll()

          • Return values and error codes

          • poll() example

          • ppoll()

        • poll( ) Versus select()

      • Kernel Internals

        • The Virtual Filesystem

        • The Page Cache

        • Page Writeback

      • Conclusion

    • Buffered I/O

      • User-Buffered I/O

        • Block Size

      • Standard I/O

        • File Pointers

      • Opening Files

        • Modes

      • Opening a Stream via File Descriptor

      • Closing Streams

        • Closing All Streams

      • Reading from a Stream

        • Reading a Character at a Time

          • Putting the character back

        • Reading an Entire Line

          • Reading arbitrary strings

        • Reading Binary Data

      • Writing to a Stream

        • Writing a Single Character

        • Writing a String of Characters

        • Writing Binary Data

      • Sample Program Using Buffered I/O

      • Seeking a Stream

        • Obtaining the Current Stream Position

      • Flushing a Stream

      • Errors and End-of-File

      • Obtaining the Associated File Descriptor

      • Controlling the Buffering

      • Thread Safety

        • Manual File Locking

        • Unlocked Stream Operations

      • Critiques of Standard I/O

      • Conclusion

    • Advanced File I/O

      • Scatter/Gather I/O

        • readv( ) and writev()

          • Return values

          • writev() example

          • readv() example

          • Implementation

      • The Event Poll Interface

        • Creating a New Epoll Instance

        • Controlling Epoll

        • Waiting for Events with Epoll

        • Edge- Versus Level-Triggered Events

      • Mapping Files into Memory

        • mmap()

          • The page size

          • Return values and error codes

          • Associated signals

        • munmap()

        • Mapping Example

        • Advantages of mmap()

        • Disadvantages of mmap()

        • Resizing a Mapping

          • Return values and error codes

        • Changing the Protection of a Mapping

          • Return values and error codes

        • Synchronizing a File with a Mapping

          • Return values and error codes

        • Giving Advice on a Mapping

          • Return values and error codes

      • Advice for Normal File I/O

        • The posix_fadvise() System Call

          • Return values and error codes

        • The readahead() System Call

          • Return values and error codes

        • Advice Is Cheap

      • Synchronized, Synchronous, and Asynchronous Operations

        • Asynchronous I/O

          • Thread-based asynchronous I/O

      • I/O Schedulers and I/O Performance

        • Disk Addressing

        • The Life of an I/O Scheduler

        • Helping Out Reads

          • The Deadline I/O Scheduler

          • The Anticipatory I/O Scheduler

          • The CFQ I/O Scheduler

          • The Noop I/O Scheduler

        • Selecting and Configuring Your I/O Scheduler

        • Optimizing I/O Performance

          • Scheduling I/O in user space

      • Conclusion

    • Process Management

      • The Process ID

        • Process ID Allocation

        • The Process Hierarchy

        • pid_t

        • Obtaining the Process ID and Parent Process ID

      • Running a New Process

        • The Exec Family of Calls

          • The rest of the family

          • Error values

        • The fork() System Call

          • Copy-on-write

          • vfork()

      • Terminating a Process

        • Other Ways to Terminate

        • atexit()

        • on_exit()

        • SIGCHLD

      • Waiting for Terminated Child Processes

        • Waiting for a Specific Process

        • Even More Waiting Versatility

        • BSD Wants to Play: wait3( ) and wait4()

        • Launching and Waiting for a New Process

        • Zombies

      • Users and Groups

        • Real, Effective, and Saved User and Group IDs

        • Changing the Real or Saved User or Group ID

        • Changing the Effective User or Group ID

        • Changing the User and Group IDs, BSD Style

        • Changing the User and Group IDs, HP-UX Style

        • Preferred User/Group ID Manipulations

        • Support for Saved User IDs

        • Obtaining the User and Group IDs

      • Sessions and Process Groups

        • Session System Calls

        • Process Group System Calls

        • Obsolete Process Group Functions

      • Daemons

      • Conclusion

    • Advanced Process Management

      • Process Scheduling

        • Big-Oh Notation

        • Timeslices

        • I/O- Versus Processor-Bound Processes

        • Preemptive Scheduling

        • Threading

      • Yielding the Processor

        • Legitimate Uses

        • Yielding, Past and Present

      • Process Priorities

        • nice()

        • getpriority( ) and setpriority()

        • I/O Priorities

      • Processor Affinity

        • sched_getaffinity() and sched_setaffinity()

      • Real-Time Systems

        • Hard Versus Soft Real-Time Systems

        • Latency, Jitter, and Deadlines

        • Linux’s Real-Time Support

        • Linux Scheduling Policies and Priorities

          • The first in, first out policy

          • The round-robin policy

          • The normal policy

          • The batch scheduling policy

          • Setting the Linux scheduling policy

        • Setting Scheduling Parameters

          • Error codes

          • Determining the range of valid priorities

        • sched_rr_get_interval()

          • Error codes

        • Precautions with Real-Time Processes

        • Determinism

          • Prefaulting data and locking memory

          • CPU affinity and real-time processes

      • Resource Limits

        • The Limits

          • Default limits

        • Setting and Retrieving Limits

          • Error codes

    • File and Directory Management

      • Files and Their Metadata

        • The Stat Family

        • Permissions

        • Ownership

        • Extended Attributes

          • Keys and values

          • Extended attribute namespaces

          • Extended attribute operations

      • Directories

        • The Current Working Directory

          • Obtaining the current working directory

          • Changing the current working directory

        • Creating Directories

        • Removing Directories

        • Reading a Directory’s Contents

          • Reading from a directory stream

          • Closing the directory stream

          • System calls for reading directory contents

      • Links

        • Hard Links

        • Symbolic Links

        • Unlinking

      • Copying and Moving Files

        • Copying

        • Moving

      • Device Nodes

        • Special Device Nodes

        • The Random Number Generator

      • Out-of-Band Communication

      • Monitoring File Events

        • Initializing inotify

        • Watches

          • Adding a new watch

          • Watch masks

        • inotify Events

          • Reading inotify events

          • Linking together move events

        • Advanced Watch Options

        • Removing an inotify Watch

        • Obtaining the Size of the Event Queue

        • Destroying an inotify Instance

    • Memory Management

      • The Process Address Space

        • Pages and Paging

          • Sharing and copy-on-write

        • Memory Regions

      • Allocating Dynamic Memory

        • Allocating Arrays

        • Resizing Allocations

        • Freeing Dynamic Memory

        • Alignment

          • Allocating aligned memory

          • Other alignment concerns

      • Managing the Data Segment

      • Anonymous Memory Mappings

        • Creating Anonymous Memory Mappings

        • Mapping /dev/zero

      • Advanced Memory Allocation

        • Fine-Tuning with malloc_usable_size() and malloc_trim()

      • Debugging Memory Allocations

        • Obtaining Statistics

      • Stack-Based Allocations

        • Duplicating Strings on the Stack

        • Variable-Length Arrays

      • Choosing a Memory Allocation Mechanism

      • Manipulating Memory

        • Setting Bytes

        • Comparing Bytes

        • Moving Bytes

        • Searching Bytes

        • Frobnicating Bytes

      • Locking Memory

        • Locking Part of an Address Space

        • Locking All of an Address Space

        • Unlocking Memory

        • Locking Limits

        • Is a Page in Physical Memory?

      • Opportunistic Allocation

        • Overcommitting and OOM

    • Signals

      • Signal Concepts

        • Signal Identifiers

        • Signals Supported by Linux

      • Basic Signal Management

        • Waiting for a Signal, Any Signal

        • Examples

        • Execution and Inheritance

        • Mapping Signal Numbers to Strings

      • Sending a Signal

        • Permissions

        • Examples

        • Sending a Signal to Yourself

        • Sending a Signal to an Entire Process Group

      • Reentrancy

        • Guaranteed-Reentrant Functions

      • Signal Sets

        • More Signal Set Functions

      • Blocking Signals

        • Retrieving Pending Signals

        • Waiting for a Set of Signals

      • Advanced Signal Management

        • The siginfo_t Structure

        • The Wonderful World of si_code

      • Sending a Signal with a Payload

        • Example

      • Conclusion

    • Time

      • Time’s Data Structures

        • The Original Representation

        • And Now, Microsecond Precision

        • Even Better: Nanosecond Precision

        • Breaking Down Time

        • A Type for Process Time

      • POSIX Clocks

        • Time Source Resolution

      • Getting the Current Time of Day

        • A Better Interface

        • An Advanced Interface

        • Getting the Process Time

      • Setting the Current Time of Day

        • Setting Time with Precision

        • An Advanced Interface for Setting the Time

      • Playing with Time

      • Tuning the System Clock

      • Sleeping and Waiting

        • Sleeping with Microsecond Precision

        • Sleeping with Nanosecond Resolution

        • An Advanced Approach to Sleep

        • A Portable Way to Sleep

        • Overruns

        • Alternatives to Sleeping

      • Timers

        • Simple Alarms

        • Interval Timers

        • Advanced Timers

          • Creating a timer

          • Arming a timer

          • Obtaining the expiration of a timer

          • Obtaining the overrun of a timer

          • Deleting a timer

    • GCC Extensions to the C Language

      • GNU C

      • Inline Functions

      • Suppressing Inlining

      • Pure Functions

      • Constant Functions

      • Functions That Do Not Return

      • Functions That Allocate Memory

      • Forcing Callers to Check the Return Value

      • Marking Functions As Deprecated

      • Marking Functions As Used

      • Marking Functions or Parameters As Unused

      • Packing a Structure

      • Increasing the Alignment of a Variable

      • Placing Global Variables in a Register

      • Branch Annotation

      • Getting the Type of an Expression

      • Getting the Alignment of a Type

      • The Offset of a Member Within a Structure

      • Obtaining the Return Address of a Function

      • Case Ranges

      • Void and Function Pointer Arithmetic

      • More Portable and More Beautiful in One Fell Swoop

    • Bibliography

      • Books on the C Programming Language

      • Books on Linux Programming

      • Books on the Linux Kernel

      • Books on Operating System Design

    • Index

Nội dung

www.it-ebooks.info LINUX System Programming www.it-ebooks.info Other Linux resources from O’Reilly Related titles Building Embedded Linux Systems Designing Embedded Hardware Linux Device Drivers Linux Kernel in a Nutshell Programming Embedded Systems Running Linux Understanding Linux Network Internals Understanding the Linux Kernel Linux Books Resource Center linux.oreilly.com is a complete catalog of O’Reilly’s books on Linux and Unix and related technologies, including sample chapters and code examples. ONLamp.com is the premier site for the open source web plat- form: Linux, Apache, MySQL and either Perl, Python, or PHP. Conferences O’Reilly brings diverse innovators together to nurture the ideas that spark revolutionary industries. We specialize in document- ing the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit con- ferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searches across more than 1,000 books. Subscribers can zero in on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. www.it-ebooks.info LINUX System Programming Robert Love Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo www.it-ebooks.info 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 corporate@oreilly.com. 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. This book uses RepKover ™ , a durable and flexible lay-flat binding. ISBN-10: 0-596-00958-5 ISBN-13: 978-0-596-00958-8 [M] www.it-ebooks.info v Table of Contents Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi 1. Introduction and Essential Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 System Programming 1 APIs and ABIs 4 Standards 6 Concepts of Linux Programming 9 Getting Started with System Programming 22 2. File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Opening Files 24 Reading via read( ) 29 Writing with write( ) 33 Synchronized I/O 37 Direct I/O 40 Closing Files 41 Seeking with lseek( ) 42 Positional Reads and Writes 44 Truncating Files 45 Multiplexed I/O 47 Kernel Internals 57 Conclusion 61 www.it-ebooks.info vi | Table of Contents 3. Buffered I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 User-Buffered I/O 62 Standard I/O 64 Opening Files 65 Opening a Stream via File Descriptor 66 Closing Streams 67 Reading from a Stream 67 Writing to a Stream 70 Sample Program Using Buffered I/O 72 Seeking a Stream 74 Flushing a Stream 75 Errors and End-of-File 76 Obtaining the Associated File Descriptor 77 Controlling the Buffering 77 Thread Safety 79 Critiques of Standard I/O 81 Conclusion 82 4. Advanced File I/O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Scatter/Gather I/O 84 The Event Poll Interface 89 Mapping Files into Memory 95 Advice for Normal File I/O 108 Synchronized, Synchronous, and Asynchronous Operations 111 I/O Schedulers and I/O Performance 114 Conclusion 125 5. Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 The Process ID 126 Running a New Process 129 Terminating a Process 136 Waiting for Terminated Child Processes 139 Users and Groups 149 Sessions and Process Groups 154 Daemons 159 Conclusion 161 www.it-ebooks.info Table of Contents | vii 6. Advanced Process Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162 Process Scheduling 162 Yielding the Processor 166 Process Priorities 169 Processor Affinity 172 Real-Time Systems 176 Resource Limits 190 7. File and Directory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196 Files and Their Metadata 196 Directories 212 Links 223 Copying and Moving Files 228 Device Nodes 231 Out-of-Band Communication 233 Monitoring File Events 234 8. Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 The Process Address Space 243 Allocating Dynamic Memory 245 Managing the Data Segment 255 Anonymous Memory Mappings 256 Advanced Memory Allocation 260 Debugging Memory Allocations 263 Stack-Based Allocations 264 Choosing a Memory Allocation Mechanism 268 Manipulating Memory 269 Locking Memory 273 Opportunistic Allocation 277 9. Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 Signal Concepts 280 Basic Signal Management 286 Sending a Signal 291 Reentrancy 293 Signal Sets 295 Blocking Signals 296 www.it-ebooks.info viii | Table of Contents Advanced Signal Management 298 Sending a Signal with a Payload 305 Conclusion 306 10. Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308 Time’s Data Structures 310 POSIX Clocks 313 Getting the Current Time of Day 315 Setting the Current Time of Day 318 Playing with Time 320 Tuning the System Clock 321 Sleeping and Waiting 324 Timers 330 Appendix. GCC Extensions to the C Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339 Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 www.it-ebooks.info ix Foreword There is an old line that Linux kernel developers like to throw out when they are feel- ing grumpy: “User space is just a test load for the kernel.” By muttering this line, the kernel developers aim to wash their hands of all responsi- bility for any failure to run user-space code as well as possible. As far as they’re concerned, user-space developers should just go away and fix their own code, as any problems are definitely not the kernel’s fault. To prove that it usually is not the kernel that is at fault, one leading Linux kernel developer has been giving a “Why User Space Sucks” talk to packed conference rooms for more than three years now, pointing out real examples of horrible user- space code that everyone relies on every day. Other kernel developers have created tools that show how badly user-space programs are abusing the hardware and drain- ing the batteries of unsuspecting laptops. But while user-space code might be just a “test load” for kernel developers to scoff at, it turns out that all of these kernel developers also depend on that user-space code every day. If it weren’t present, all the kernel would be good for would be to print out alternating ABABAB patterns on the screen. Right now, Linux is the most flexible and powerful operating system that has ever been created, running everything from the tiniest cell phones and embedded devices to more than 70 percent of the world’s top 500 supercomputers. No other operating system has ever been able to scale so well and meet the challenges of all of these dif- ferent hardware types and environments. And along with the kernel, code running in user space on Linux can also operate on all of those platforms, providing the world with real applications and utilities people rely on. In this book, Robert Love has taken on the unenviable task of teaching the reader about almost every system call on a Linux system. In so doing, he has produced a tome that will allow you to fully understand how the Linux kernel works from a user-space perspective, and also how to harness the power of this system. www.it-ebooks.info [...]... with traditional Unix systems, Linux is rife with additional system calls, different behavior, and new features System Programming Traditionally speaking, all Unix programming is system- level programming Historically, Unix systems did not include many higher-level abstractions Even programming in a development environment such as the X Window System exposed in full view the core Unix system API Consequently,... this book is a book on Linux 1 www.it-ebooks.info programming in general But note that this book does not cover the Linux programming environment—there is no tutorial on make in these pages What is covered is the system programming API exposed on a modern Linux machine System programming is most commonly contrasted with application programming System- level and application-level programming differ in... from application programming to system programming (or vice versa) is not hard Even when the application lives very high up the stack, far from the lowest levels of the system, knowledge of system programming is important And the same good practices are employed in all forms of programming The last several years have witnessed a trend in application programming away from system- level programming and... the root of the filesystem on the CD accessible at that mount point The first filesystem mounted is located in the root of the namespace, /, and is called the root filesystem Linux systems always have a root filesystem Mounting other filesystems at other mount points is optional Filesystems usually exist physically (i.e., are stored on disk), although Linux also supports virtual filesystems that exist... writing system software; others spend only part of their time on this task There is no programmer, however, who does not benefit from some understanding of system programming Whether it is the programmer’s raison d’être, or merely a foundation for higher-level concepts, system programming is at the heart of all software that we write In particular, this book is about system programming on Linux Linux... Unix-like system, written from scratch by Linus Torvalds, and a loose-knit community of hackers around the globe Although Linux shares the goals and ideology of Unix, Linux is not Unix Instead, Linux follows its own course, diverging where desired, and converging only where practical Generally, the core of Linux system programming is the same as on any other Unix system Beyond the basics, however, Linux. .. the system- level interface, and how do I write system- level applications in Linux? What exactly do the kernel and the C library provide? How do I write optimal code, and what tricks does Linux provide? What neat system calls are provided in Linux compared to other Unix variants? How does it all work? Those questions are at the center of this book There are three cornerstones to system programming in Linux: ... importantly, the Linux kernel guarantees the stability of system calls Once a system call is implemented in a stable version of the Linux kernel, it is set in stone Among the various Linux distributions, the Linux Standard Base (LSB) standardizes much of the Linux system The LSB is a joint project of several Linux vendors under the auspices of the Linux Foundation (formerly the Free Standards Group) The... that you are familiar with the Linux environment: I presume that you can get around in a shell, use basic commands, and compile a simple C program This is not an overview of Linux, or its programming environment, but rather of the “stuff” that forms the basis of Linux system programming Files and the Filesystem The file is the most basic and fundamental abstraction in Linux Linux follows the everything-is-a-file... different Linux distributions and hardware types It will allow you to understand how Linux works and how to take advantage of its flexibility In the end, this book teaches you how to write code that doesn't suck, which is the best thing of all —Greg Kroah-Hartman x | Foreword www.it-ebooks.info Preface This book is about system programming specifically, system programming on Linux System programming . Kroah-Hartman www.it-ebooks.info xi Preface This book is about system programming specifically, system programming on Linux. System programming is the practice of writing system software, which. Embedded Hardware Linux Device Drivers Linux Kernel in a Nutshell Programming Embedded Systems Running Linux Understanding Linux Network Internals Understanding the Linux Kernel Linux

Ngày đăng: 17/02/2014, 23:20

TỪ KHÓA LIÊN QUAN

w