4For Doris and Helen.www.it-ebooks.info
ptg ❖ For Doris and Helen. ❖ www.it-ebooks.info
Contents at a Glance1 Introductionto the Linux Kernel12 GettingStarted with theKernel 113ProcessManagement234 ProcessScheduling415SystemCalls696 Kernel Data Structures857 Interrupts and Interrupt Handlers1138 BottomHalvesandDeferringWork1339 AnIntroductiontoKernelSynchronization1611o KernelSynchronizationMethods17511 TimersandTimeManagement20712MemoryManagement23113TheVirtualFilesystem26114 The Block /0 Layer28915TheProcessAddressSpace:30516ThePageCacheandPageWriteback32317 Devices andModules633718Debugging36319Portability3792oPatches,Hacking,andtheCommunity395Bibliography407Index411www.it-ebooks.info
ptg Contents at a Glance 1 Introduction to the Linux Kernel 1 2 Getting Started with the Kernel 11 3 Process Management 23 4 Process Scheduling 41 5 System Calls 69 6 Kernel Data Structures 85 7 Interrupts and Interrupt Handlers 113 8 Bottom Halves and Deferring Work 133 9 An Introduction to Kernel Synchronization 161 10 Kernel Synchronization Methods 175 11 Timers and Time Management 207 12 Memory Management 231 13 The Virtual Filesystem 261 14 The Block I/O Layer 289 15 The Process Address Space 305 16 The Page Cache and Page Writeback 323 17 Devices and Modules 337 18 Debugging 363 19 Portability 379 20 Patches, Hacking, and the Community 395 Bibliography 407 Index 411 www.it-ebooks.info
Table of Contents1 Introduction to the Linux Kernel1History of Unix1AlongCameLinus:Introductionto Linux3OverviewofOperatingSystemsandKernels4.LinuxVersus ClassicUnixKernels6LinuxKernel Versions8TheLinuxKernelDevelopmentCommunity10BeforeWe Begin10112Getting Started withtheKernel11ObtainingtheKernelSourceUsing Git11Installing theKernel Source12UsingPatches12TheKernel SourceTree12Building the Kernel 13Configuring the Kernel1415Minimizing Build NoiseSpawningMultipleBuild JobsS1616InstallingtheNewKernel16ABeastof aDifferentNatureNo libcorStandardHeaders17GNUC18InlineFunctions61819InlineAssemblyBranchAnnotation19NoMemoryProtection20No (Easy) Use of Floating Point20Small,Fixed-SizeStack2021Synchronization and Concurrency21ImportanceofPortabilityConclusion 21www.it-ebooks.info
ptg Table of Contents 1 Introduction to the Linux Kernel 1 History of Unix 1 Along Came Linus: Introduction to Linux 3 Overview of Operating Systems and Kernels 4 Linux Versus Classic Unix Kernels 6 Linux Kernel Versions 8 The Linux Kernel Development Community 10 Before We Begin 10 2 Getting Started with the Kernel 11 Obtaining the Kernel Source 11 Using Git 11 Installing the Kernel Source 12 Using Patches 12 The Kernel Source Tree 12 Building the Kernel 13 Configuring the Kernel 14 Minimizing Build Noise 15 Spawning Multiple Build Jobs 16 Installing the New Kernel 16 A Beast of a Different Nature 16 No libc or Standard Headers 17 GNU C 18 Inline Functions 18 Inline Assembly 19 Branch Annotation 19 No Memory Protection 20 No (Easy) Use of Floating Point 20 Small, Fixed-Size Stack 20 Synchronization and Concurrency 21 Importance of Portability 21 Conclusion 21 www.it-ebooks.info
Contentsvili3ProcessManagement23TheProcess23Process Descriptorandthe Task Structure24AllocatingtheProcessDescriptor:25Storing theProcess Descriptor26Process State27Manipulatingthe Current Process State29ProcessContext29TheProcessFamilyTree29Process Creation3131Copy-on-WriteForking32vfork()33The Linux ImplementationofThreads33Creating Threads34KernelThreads35ProcessTermination36RemovingtheProcessDescriptor37TheDilemma oftheParentless Task38Conclusion40ProcessScheduling41441MultitaskingLinux'sProcessScheduler42Policy43I/O-Bound VersusProcessor-BoundProcesses643ProcessPriority44Timeslice4545TheSchedulingPolicyinAction46The LinuxSchedulingAlgorithm46SchedulerClasses47Process Scheduling inUnix SystemsFair Scheduling4850TheLinuxSchedulingImplementationTime Accounting 5050TheScheduler Entity StructureTheVirtual Runtime51www.it-ebooks.info
ptg viii Contents 3 Process Management 23 The Process 23 Process Descriptor and the Task Structure 24 Allocating the Process Descriptor 25 Storing the Process Descriptor 26 Process State 27 Manipulating the Current Process State 29 Process Context 29 The Process Family Tree 29 Process Creation 31 Copy-on-Write 31 Forking 32 vfork() 33 The Linux Implementation of Threads 33 Creating Threads 34 Kernel Threads 35 Process Termination 36 Removing the Process Descriptor 37 The Dilemma of the Parentless Task 38 Conclusion 40 4 Process Scheduling 41 Multitasking 41 Linux’s Process Scheduler 42 Policy 43 I/O-Bound Versus Processor-Bound Processes 43 Process Priority 44 Timeslice 45 The Scheduling Policy in Action 45 The Linux Scheduling Algorithm 46 Scheduler Classes 46 Process Scheduling in Unix Systems 47 Fair Scheduling 48 The Linux Scheduling Implementation 50 Time Accounting 50 The Scheduler Entity Structure 50 The Virtual Runtime 51 www.it-ebooks.info
ContentsixProcessSelection52PickingtheNextTask53AddingProcessestotheTree54RemovingProcessesfromtheTree56TheSchedulerEntryPoint57SleepingandWakingUp58Wait Queues58Waking Up61PreemptionandContextSwitching62UserPreemption62KernelPreemption 63Real-Time SchedulingPolicies64Scheduler-RelatedSystemCalls65Scheduling Policy and Priority-RelatedSystemCalls66ProcessorAffinitySystemCalls6666YieldingProcessorTimeConclusion675SystemCalls69CommunicatingwiththeKernel6970APIs,POSIX,and theC LibrarySyscalls71672SystemCall NumbersSystemCall Performance72System Call Handler73DenotingtheCorrectSystemCall73ParameterPassing74SystemCall Implementation74ImplementingSystemCalls74Verifying theParameters75System Call Context78Final StepsinBindingaSystemCall7981AccessingtheSystemCallfromUser-SpaceWhyNottoImplementaSystemCall82Conclusion83www.it-ebooks.info
ptg Contents ix Process Selection 52 Picking the Next Task 53 Adding Processes to the Tree 54 Removing Processes from the Tree 56 The Scheduler Entry Point 57 Sleeping and Waking Up 58 Wait Queues 58 Waking Up 61 Preemption and Context Switching 62 User Preemption 62 Kernel Preemption 63 Real-Time Scheduling Policies 64 Scheduler-Related System Calls 65 Scheduling Policy and Priority-Related System Calls 66 Processor Affinity System Calls 66 Yielding Processor Time 66 Conclusion 67 5 System Calls 69 Communicating with the Kernel 69 APIs, POSIX, and the C Library 70 Syscalls 71 System Call Numbers 72 System Call Performance 72 System Call Handler 73 Denoting the Correct System Call 73 Parameter Passing 74 System Call Implementation 74 Implementing System Calls 74 Verifying the Parameters 75 System Call Context 78 Final Steps in Binding a System Call 79 Accessing the System Call from User-Space 81 Why Not to Implement a System Call 82 Conclusion 83 www.it-ebooks.info