www.it-ebooks.info www.it-ebooks.info Understanding the LINUX KERNEL www.it-ebooks.info Other Linux resources from O’Reilly Related titles Linux Books Resource Center Building Embedded Linux Systems Linux Device Drivers Linux in a Nutshell Linux Network Administrator’s Guide Linux Pocket Guide Linux Security Cookbook™ Linux Server Hacks™ Linux Server Security Running Linux SELinux Understanding Linux Network Internals 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 platform: 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 documenting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches Visit conferences.oreilly.com for our upcoming events Safari Bookshelf (safari.oreilly.com) is the premier online reference 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 simply flip to the page you need Try it today for free www.it-ebooks.info Understanding the LINUX KERNEL THIRD EDITION Daniel P Bovet and Marco Cesati Beijing • Cambridge • Farnham • Köln • Paris • www.it-ebooks.info Sebastopol • Taipei • Tokyo Understanding the Linux Kernel, Third Edition by Daniel P Bovet and Marco Cesati Copyright © 2006 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: Darren Kelly Production Services: Amy Parker Cover Designer: Edie Freedman Interior Designer: David Futato Printing History: November 2000: First Edition December 2002: Second Edition November 2005: Third Edition Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’Reilly Media, Inc The Linux series designations, Understanding the Linux Kernel, Third Edition, the image of a man with a bubble, 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 authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein ISBN-10: 0-596-00565-2 ISBN-13: 978-0-596-00565-8 [M] [9/07] www.it-ebooks.info Table of Contents Preface xi Introduction Linux Versus Other Unix-Like Kernels Hardware Dependency Linux Versions Basic Operating System Concepts An Overview of the Unix Filesystem An Overview of Unix Kernels 12 19 Memory Addressing 35 Memory Addresses Segmentation in Hardware Segmentation in Linux Paging in Hardware Paging in Linux 35 36 41 45 57 Processes 79 Processes, Lightweight Processes, and Threads Process Descriptor Process Switch Creating Processes Destroying Processes 79 81 102 114 126 Interrupts and Exceptions 131 The Role of Interrupt Signals Interrupts and Exceptions 132 133 v www.it-ebooks.info Nested Execution of Exception and Interrupt Handlers Initializing the Interrupt Descriptor Table Exception Handling Interrupt Handling Softirqs and Tasklets Work Queues Returning from Interrupts and Exceptions 143 145 148 151 171 180 183 Kernel Synchronization 189 How the Kernel Services Requests Synchronization Primitives Synchronizing Accesses to Kernel Data Structures Examples of Race Condition Prevention 189 194 217 222 Timing Measurements 227 Clock and Timer Circuits The Linux Timekeeping Architecture Updating the Time and Date Updating System Statistics Software Timers and Delay Functions System Calls Related to Timing Measurements 228 232 240 241 244 252 Process Scheduling 258 Scheduling Policy The Scheduling Algorithm Data Structures Used by the Scheduler Functions Used by the Scheduler Runqueue Balancing in Multiprocessor Systems System Calls Related to Scheduling 258 262 266 270 284 290 Memory Management 294 Page Frame Management Memory Area Management Noncontiguous Memory Area Management 294 323 342 Process Address Space 351 The Process’s Address Space The Memory Descriptor Memory Regions vi | Table of Contents www.it-ebooks.info 352 353 357 Page Fault Exception Handler Creating and Deleting a Process Address Space Managing the Heap 376 392 395 10 System Calls 398 POSIX APIs and System Calls System Call Handler and Service Routines Entering and Exiting a System Call Parameter Passing Kernel Wrapper Routines 398 399 401 409 418 11 Signals 420 The Role of Signals Generating a Signal Delivering a Signal System Calls Related to Signal Handling 420 433 439 450 12 The Virtual Filesystem 456 The Role of the Virtual Filesystem (VFS) VFS Data Structures Filesystem Types Filesystem Handling Pathname Lookup Implementations of VFS System Calls File Locking 456 462 481 483 495 505 510 13 I/O Architecture and Device Drivers 519 I/O Architecture The Device Driver Model Device Files Device Drivers Character Device Drivers 519 526 536 540 552 14 Block Device Drivers 560 Block Devices Handling The Generic Block Layer The I/O Scheduler Block Device Drivers Opening a Block Device File 560 566 572 585 595 Table of Contents www.it-ebooks.info | vii 15 The Page Cache 599 The Page Cache Storing Blocks in the Page Cache Writing Dirty Pages to Disk The sync( ), fsync( ), and fdatasync() System Calls 600 611 622 629 16 Accessing Files 631 Reading and Writing a File Memory Mapping Direct I/O Transfers Asynchronous I/O 632 657 668 671 17 Page Frame Reclaiming 676 The Page Frame Reclaiming Algorithm Reverse Mapping Implementing the PFRA Swapping 676 680 689 712 18 The Ext2 and Ext3 Filesystems 738 General Characteristics of Ext2 Ext2 Disk Data Structures Ext2 Memory Data Structures Creating the Ext2 Filesystem Ext2 Methods Managing Ext2 Disk Space The Ext3 Filesystem 738 741 750 753 755 757 766 19 Process Communication 775 Pipes FIFOs System V IPC POSIX Message Queues 776 787 789 806 20 Program Execution 808 Executable Files Executable Formats Execution Domains The exec Functions viii | 809 824 827 828 Table of Contents www.it-ebooks.info ... Nutshell Linux Network Administrator’s Guide Linux Pocket Guide Linux Security Cookbook™ Linux Server Hacks™ Linux Server Security Running Linux SELinux Understanding Linux Network Internals linux. oreilly.com... respect, Linux is comparable with the other Unix-like operating systems Reading this book and studying the Linux kernel, therefore, may help you understand the other Unix variants, too The 2.6... O’Reilly editor Andy Oram— came the first edition of Understanding the Linux Kernel at the end of 2000, which covered Linux 2.2 with a few anticipations on Linux 2.4 The success encountered by this