Understanding The LINUX - Lagout

3y ago
180 Views
29 Downloads
5.35 MB
944 Pages
Last View : Today
Last Download : 2m ago
Upload by : Kamden Hassan
Transcription

Understanding theLINUXKERNEL

Other Linux resources from O’ReillyRelated titlesLinux BooksResource CenterBuilding Embedded LinuxSystemsLinux Device DriversLinux in a NutshellLinux NetworkAdministrator’s GuideLinux Pocket GuideLinux Security Cookbook Linux Server Hacks Linux Server SecurityRunning LinuxSELinuxUnderstanding LinuxNetwork Internalslinux.oreilly.com is a complete catalog of O’Reilly’s books onLinux and Unix and related technologies, including samplechapters and code examples.ONLamp.com is the premier site for the open source web platform: Linux, Apache, MySQL, and either Perl, Python, or PHP.ConferencesO’Reilly brings diverse innovators together to nurture the ideasthat spark revolutionary industries. We specialize in documenting the latest tools and systems, translating the innovator’sknowledge 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. Conductsearches across more than 1,000 books. Subscribers can zero inon 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.

Understanding theLINUXKERNELTHIRD EDITIONDaniel P. Bovet and Marco CesatiBeijing Cambridge Farnham Köln Paris Sebastopol Taipei Tokyo

Understanding the Linux Kernel, Third Editionby Daniel P. Bovet and Marco CesatiCopyright 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 editionsare 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 OramProduction Editor:Darren KellyProduction Services:Amy ParkerCover Designer:Edie FreedmanInterior Designer:David FutatoPrinting 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 ofO’Reilly Media, Inc. The Linux series designations, Understanding the Linux Kernel, Third Edition, theimage 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 astrademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of atrademark 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 authorsassume no responsibility for errors or omissions, or for damages resulting from the use of theinformation contained herein.ISBN-10: 0-596-00565-2ISBN-13: 978-0-596-00565-8[M][9/07]

Table of ContentsPreface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi1. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1Linux Versus Other Unix-Like KernelsHardware DependencyLinux VersionsBasic Operating System ConceptsAn Overview of the Unix FilesystemAn Overview of Unix Kernels267812192. Memory Addressing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35Memory AddressesSegmentation in HardwareSegmentation in LinuxPaging in HardwarePaging in Linux35364145573. Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79Processes, Lightweight Processes, and ThreadsProcess DescriptorProcess SwitchCreating ProcessesDestroying Processes79811021141264. Interrupts and Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131The Role of Interrupt SignalsInterrupts and Exceptions132133v

Nested Execution of Exception and Interrupt HandlersInitializing the Interrupt Descriptor TableException HandlingInterrupt HandlingSoftirqs and TaskletsWork QueuesReturning from Interrupts and Exceptions1431451481511711801835. Kernel Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189How the Kernel Services RequestsSynchronization PrimitivesSynchronizing Accesses to Kernel Data StructuresExamples of Race Condition Prevention1891942172226. Timing Measurements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227Clock and Timer CircuitsThe Linux Timekeeping ArchitectureUpdating the Time and DateUpdating System StatisticsSoftware Timers and Delay FunctionsSystem Calls Related to Timing Measurements2282322402412442527. Process Scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258Scheduling PolicyThe Scheduling AlgorithmData Structures Used by the SchedulerFunctions Used by the SchedulerRunqueue Balancing in Multiprocessor SystemsSystem Calls Related to Scheduling2582622662702842908. Memory Management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294Page Frame ManagementMemory Area ManagementNoncontiguous Memory Area Management2943233429. Process Address Space . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351The Process’s Address SpaceThe Memory DescriptorMemory Regionsvi Table of Contents352353357

Page Fault Exception HandlerCreating and Deleting a Process Address SpaceManaging the Heap37639239510. System Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398POSIX APIs and System CallsSystem Call Handler and Service RoutinesEntering and Exiting a System CallParameter PassingKernel Wrapper Routines39839940140941811. Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420The Role of SignalsGenerating a SignalDelivering a SignalSystem Calls Related to Signal Handling42043343945012. The Virtual Filesystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456The Role of the Virtual Filesystem (VFS)VFS Data StructuresFilesystem TypesFilesystem HandlingPathname LookupImplementations of VFS System CallsFile Locking45646248148349550551013. I/O Architecture and Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519I/O ArchitectureThe Device Driver ModelDevice FilesDevice DriversCharacter Device Drivers51952653654055214. Block Device Drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 560Block Devices HandlingThe Generic Block LayerThe I/O SchedulerBlock Device DriversOpening a Block Device File560566572585595Table of Contents vii

15. The Page Cache . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599The Page CacheStoring Blocks in the Page CacheWriting Dirty Pages to DiskThe sync( ), fsync( ), and fdatasync() System Calls60061162262916. Accessing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 631Reading and Writing a FileMemory MappingDirect I/O TransfersAsynchronous I/O63265766867117. Page Frame Reclaiming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 676The Page Frame Reclaiming AlgorithmReverse MappingImplementing the PFRASwapping67668068971218. The Ext2 and Ext3 Filesystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 738General Characteristics of Ext2Ext2 Disk Data StructuresExt2 Memory Data StructuresCreating the Ext2 FilesystemExt2 MethodsManaging Ext2 Disk SpaceThe Ext3 Filesystem73874175075375575776619. Process Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775PipesFIFOsSystem V IPCPOSIX Message Queues77678778980620. Program Execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 808Executable FilesExecutable FormatsExecution DomainsThe exec Functionsviii Table of Contents809824827828

A. System Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 835B. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 842Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 852Source Code Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 905Table of Contents ix

PrefaceIn the spring semester of 1997, we taught a course on operating systems based onLinux 2.0. The idea was to encourage students to read the source code. To achievethis, we assigned term projects consisting of making changes to the kernel and performing tests on the modified version. We also wrote course notes for our studentsabout a few critical features of Linux such as task switching and task scheduling.Out of this work—and with a lot of support from our O’Reilly editor Andy Oram—came the first edition of Understanding the Linux Kernel at the end of 2000, whichcovered Linux 2.2 with a few anticipations on Linux 2.4. The success encountered bythis book encouraged us to continue along this line. At the end of 2002, we came outwith a second edition covering Linux 2.4. You are now looking at the third edition,which covers Linux 2.6.As in our previous experiences, we read thousands of lines of code, trying to makesense of them. After all this work, we can say that it was worth the effort. We learneda lot of things you don’t find in books, and we hope we have succeeded in conveyingsome of this information in the following pages.The Audience for This BookAll people curious about how Linux works and why it is so efficient will find answershere. After reading the book, you will find your way through the many thousands oflines of code, distinguishing between crucial data structures and secondary ones—inshort, becoming a true Linux hacker.Our work might be considered a guided tour of the Linux kernel: most of the significant data structures and many algorithms and programming tricks used in the kernelare discussed. In many cases, the relevant fragments of code are discussed line byline. Of course, you should have the Linux source code on hand and should be willing to expend some effort deciphering some of the functions that are not, for sake ofbrevity, fully described.xiThis is the Title of the Book, eMatter EditionCopyright 2007 O’Reilly & Associates, Inc. All rights reserved.

On another level, the book provides valuable insight to people who want to knowmore about the critical design issues in a modern operating system. It is not specifically addressed to system administrators or programmers; it is mostly for people whowant to understand how things really work inside the machine! As with any goodguide, we try to go beyond superficial features. We offer a background, such as thehistory of major features and the reasons why they were used.Organization of the MaterialWhen we began to write this book, we were faced with a critical decision: should werefer to a specific hardware platform or skip the hardware-dependent details andconcentrate on the pure hardware-independent parts of the kernel?Others books on Linux kernel internals have chosen the latter approach; we decidedto adopt the former one for the following reasons: Efficient kernels take advantage of most available hardware features, such asaddressing techniques, caches, processor exceptions, special instructions, processor control registers, and so on. If we want to convince you that the kernelindeed does quite a good job in performing a specific task, we must first tellwhat kind of support comes from the hardware. Even if a large portion of a Unix kernel source code is processor-independentand coded in C language, a small and critical part is coded in assembly language. A thorough knowledge of the kernel, therefore, requires the study of afew assembly language fragments that interact with the hardware.When covering hardware features, our strategy is quite simple: only sketch the featuresthat are totally hardware-driven while detailing those that need some software support. In fact, we are interested in kernel design rather than in computer architecture.Our next step in choosing our path consisted of selecting the computer system todescribe. Although Linux is now running on several kinds of personal computers andworkstations, we decided to concentrate on the very popular and cheap IBM-compatible personal computers—and thus on the 80 86 microprocessors and on some support chips included in these personal computers. The term 80 86 microprocessorwill be used in the forthcoming chapters to denote the Intel 80386, 80486, Pentium,Pentium Pro, Pentium II, Pentium III, and Pentium 4 microprocessors or compatiblemodels. In a few cases, explicit references will be made to specific models.One more choice we had to make was the order to follow in studying Linux components. We tried a bottom-up approach: start with topics that are hardwaredependent and end with those that are totally hardware-independent. In fact, we’llmake many references to the 80 86 microprocessors in the first part of the book,while the rest of it is relatively hardware-independent. Significant exceptions aremade in Chapter 13 and Chapter 14. In practice, following a bottom-up approachis not as simple as it looks, because the areas of memory management, processxii PrefaceThis is the Title of the Book, eMatter EditionCopyright 2007 O’Reilly & Associates, Inc. All rights reserved.

management, and filesystems are intertwined; a few forward references—that is,references to topics yet to be explained—are unavoidable.Each chapter starts with a theoretical overview of the topics covered. The material isthen presented according to the bottom-up approach. We start with the data structures needed to support the functionalities described in the chapter. Then we usually move from the lowest level of functions to higher levels, often ending by showinghow system calls issued by user applications are supported.Level of DescriptionLinux source code for all supported architectures is contained in more than 14,000 Cand assembly language files stored in about 1000 subdirectories; it consists ofroughly 6 million lines of code, which occupy over 230 megabytes of disk space. Ofcourse, this book can cover only a very small portion of that code. Just to figure outhow big the Linux source is, consider that the whole source code of the book you arereading occupies less than 3 megabytes. Therefore, we would need more than 75books like this to list all code, without even commenting on it!So we had to make some choices about the parts to describe. This is a rough assessment of our decisions: We describe process and memory management fairly thoroughly. We cover the Virtual Filesystem and the Ext2 and Ext3 filesystems, althoughmany functions are just mentioned without detailing the code; we do not discuss other filesystems supported by Linux. We describe device drivers, which account for roughly 50% of the kernel, as faras the kernel interface is concerned, but do not attempt analysis of each specificdriver.The book describes the official 2.6.11 version of the Linux kernel, which can bedownloaded from the web site http://www.kernel.org.Be aware that most distributions of GNU/Linux modify the official kernel to implement new features or to improve its efficiency. In a few cases, the source code provided by your favorite distribution might differ significantly from the one describedin this book.In many cases, we show fragments of the original code rewritten in an easier-to-readbut less efficient way. This occurs at time-critical points at which sections of programs are often written in a mixture of hand-optimized C and assembly code. Onceagain, our aim is to provide some help in studying the original Linux code.While discussing kernel code, we often end up describing the underpinnings of manyfamiliar features that Unix programmers have heard of and about which they may becurious (shared and mapped memory, signals, pipes, symbolic links, and so on).Preface This is the Title of the Book, eMatter EditionCopyright 2007 O’Reilly & Associates, Inc. All rights reserved.xiii

Overview of the BookTo make life easier, Chapter 1, Introduction, presents a general picture of what isinside a Unix kernel and how Linux competes against other well-known Unix systems.The heart of any Unix kernel is memory management. Chapter 2, Memory Addressing,explains how 80 86 processors include special circuits to address data in memory andhow Linux exploits them.Processes are a fundamental abstraction offered by Linux and are introduced inChapter 3, Processes. Here we also explain how each process runs either in an unprivileged User Mode or in a privileged Kernel Mode. Transitions between User Mode andKernel Mode happen only through well-established hardware mechanisms called interrupts and exceptions. These are introduced in Chapter 4, Interrupts and Exceptions.In many occasions, the kernel has to deal with bursts of interrupt signals coming fromdifferent devices and processors. Synchronization mechanisms are needed so that allthese requests can be serviced in an interleaved way by the kernel: they are discussed inChapter 5, Kernel Synchronization, for both uniprocessor and multiprocessor systems.One type of interrupt is crucial for allowing Linux to take care of elapsed time; further details can be found in Chapter 6, Timing Measurements.Chapter 7, Process Scheduling, explains how Linux executes, in turn, every activeprocess in the system so that all of them can progress toward their completions.Next we focus again on memory. Chapter 8, Memory Management, describes thesophisticated techniques required to handle the most precious resource in the system (besides the processors, of course): available memory. This resource must begranted both to the Linux kernel and to the user applications. Chapter 9, ProcessAddress Space, shows how the kernel copes with the requests for memory issued bygreedy application programs.Chapter 10, System Calls, explains how a process running in User Mode makesrequests to the kernel, while Chapter 11, Signals, describes how a process may sendsynchronization signals to other processes. Now we are ready to move on to anotheressential topic, how Linux implements the filesystem. A series of chapters cover thistopic. Chapter 12, The Virtual Filesystem, introduces a general layer that supportsmany different filesystems. Some Linux files are special because they provide trapdoors to reach hardware devices; Chapter 13, I/O Architecture and Device Drivers,and Chapter 14, Block Device Drivers, offer insights on these special files and on thecorresponding hardware device drivers.Another issue to consider is disk access time; Chapter 15, The Page Cache, showshow a clever use of RAM reduces disk accesses, therefore improving system performance significantly. Building on the material covered in these last chapters, we cannow explain in Chapter 16, Accessing Files, how user applications access normalfiles. Chapter 17, Page Frame Reclaiming, completes our discussion of Linux memory management and explains the techniques us

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 Books Resource Center linux.oreilly.comis a complete catalog of O’Reilly’s books on Linux and Unix and related technologies .

Related Documents:

May 02, 2018 · D. Program Evaluation ͟The organization has provided a description of the framework for how each program will be evaluated. The framework should include all the elements below: ͟The evaluation methods are cost-effective for the organization ͟Quantitative and qualitative data is being collected (at Basics tier, data collection must have begun)

Silat is a combative art of self-defense and survival rooted from Matay archipelago. It was traced at thé early of Langkasuka Kingdom (2nd century CE) till thé reign of Melaka (Malaysia) Sultanate era (13th century). Silat has now evolved to become part of social culture and tradition with thé appearance of a fine physical and spiritual .

On an exceptional basis, Member States may request UNESCO to provide thé candidates with access to thé platform so they can complète thé form by themselves. Thèse requests must be addressed to esd rize unesco. or by 15 A ril 2021 UNESCO will provide thé nomineewith accessto thé platform via their émail address.

̶The leading indicator of employee engagement is based on the quality of the relationship between employee and supervisor Empower your managers! ̶Help them understand the impact on the organization ̶Share important changes, plan options, tasks, and deadlines ̶Provide key messages and talking points ̶Prepare them to answer employee questions

Dr. Sunita Bharatwal** Dr. Pawan Garga*** Abstract Customer satisfaction is derived from thè functionalities and values, a product or Service can provide. The current study aims to segregate thè dimensions of ordine Service quality and gather insights on its impact on web shopping. The trends of purchases have

Other Linux resources from O’Reilly Related titles Building Embedded Linux Systems Linux Device Drivers Linux in a Nutshell Linux Pocket Guide Running Linux Understanding Linux Network Internals Understanding the Linux Kernel Linux Books Resource Center linu

Chính Văn.- Còn đức Thế tôn thì tuệ giác cực kỳ trong sạch 8: hiện hành bất nhị 9, đạt đến vô tướng 10, đứng vào chỗ đứng của các đức Thế tôn 11, thể hiện tính bình đẳng của các Ngài, đến chỗ không còn chướng ngại 12, giáo pháp không thể khuynh đảo, tâm thức không bị cản trở, cái được

FEMINIST CRITICISM: AN INTRODUCTION SANDEEP KUMAR SHARMA Research Scholar Department of English Punjabi University, Patiala (Punjab) INDIA Feminist criticism began as a kind of revolution against the traditional literary criticism which was male-centred that considered women's writing as inferior. A feeling prevailed among the traditional literary critics that women were incapable of any .