Operating Systems A Concept-Based Approach Dhananjay M. Dhamdhere ProfessorofComputerScience&Engineering IndianInstituteofTechnology,Bombay 02-M4363-DAS1.LaTeX: “fm” — 2007/11/29 — 12:31 — page i — #1 OPERATINGSYSTEMS:ACONCEPT-BASEDAPPROACH PublishedbyMcGraw-Hill,abusinessunitofTheMcGraw-HillCompanies,Inc.,1221AvenueoftheAmericas,NewYork, NY10020.Copyright©2009byTheMcGraw-HillCompanies,Inc.Allrightsreserved.Nopartofthispublicationmaybe reproducedordistributedinanyformorbyanymeans,orstoredinadatabaseorretrievalsystem,withoutthepriorwritten consentofTheMcGraw-HillCompanies,Inc.,including,butnotlimitedto,inanynetworkorotherelectronicstorageor transmission,orbroadcastfordistancelearning. Someancillaries,includingelectronicandprintcomponents,maynotbeavailabletocustomersoutsidetheUnitedStates. Thisbookisprintedonacid-freepaper. 1234567890DOC/DOC098 ISBN978–0–07–295769–3 MHID0–07–295769–7 GlobalPublisher:RaghothamanSrinivasan DirectorofDevelopment:KristineTibbetts FreelanceDevelopmentalEditor:MelindaBilecki ProjectCoordinator:MelissaM.Leick LeadProductionSupervisor:SandyLudovissy Designer:LaurieB.Janssen (USE)CoverImage:S.Solum/PhotoLink/GettyImages,RF Compositor:Newgen Typeface:10/12Times Printer:R.R.DonnelleyCrawfordsville,IN LibraryofCongressCataloging-in-PublicationData Dhamdhere,DhananjayM. Operatingsystems:aconcept-basedapproach/DhananjayM.Dhamdhere.–1sted. p.cm. Includesbibliographicalreferencesandindex. ISBN978–0–07–295769–3—ISBN0–07–295769–7(hardcopy:alk.paper)1.Operatingsystems (Computers)I.Title. QA76.76.O63D5 2009 005.4(cid:2)3–dc22 2007041510 www.mhhe.com 02-M4363-DAS1.LaTeX: “fm” — 2007/11/29 — 12:31 — page ii — #2 Dedication • TomywifeSnehalata 02-M4363-DAS1.LaTeX: “fm” — 2007/11/29 — 12:31 — page iii — #3 02-M4363-DAS1.LaTeX: “fm” — 2007/11/29 — 12:31 — page iv — #4 Brief Contents Preface xvi 1 • • Part Overview 1 1 Introduction 5 2 TheOS,theComputer,andUserPrograms 25 3 OverviewofOperatingSystems 51 4 StructureofOperatingSystems 80 2 • • Part Process Management 107 5 ProcessesandThreads 111 6 ProcessSynchronization 165 7 Scheduling 228 8 Deadlocks 277 9 MessagePassing 315 10 SynchronizationandSchedulinginMultiprocessorOperatingSystems 336 3 • • Part Memory Management 361 11 MemoryManagement 363 12 VirtualMemory 410 4 • • Part File Systems and I/O Management 477 13 FileSystems 479 14 ImplementationofFileOperations 543 15 SecurityandProtection 605 5 • • Part Distributed Operating Systems 649 16 DistributedOperatingSystems 653 17 TheoreticalIssuesinDistributedSystems 693 18 DistributedControlAlgorithms 714 19 RecoveryandFaultTolerance 743 20 DistributedFileSystems 760 21 DistributedSystemSecurity 785 v 02-M4363-DAS1.LaTeX: “contents” — 2007/11/29 — 15:07 — page v — #1 Contents 2 Preface xvi Chapter • The OS, the Computer, and User Programs 25 1 • Part • 2.1 FundamentalPrinciplesofOS Operation 25 Overview 1 2.2 TheComputer 26 2.2.1 TheCPU 27 2.2.2 MemoryManagementUnit 1 (MMU) 30 Chapter • 2.2.3 MemoryHierarchy 30 2.2.4 Input/Output 34 Introduction 5 2.2.5 Interrupts 36 1.1 AbstractViewsofanOperatingSystem 5 2.3 OSInteractionwiththeComputerand 1.2 GoalsofanOS 8 UserPrograms 39 1.2.1 EfficientUse 8 2.3.1 ControllingExecutionof 1.2.2 UserConvenience 9 Programs 39 1.2.3 Noninterference 10 2.3.2 InterruptServicing 40 1.3 OperationofanOS 10 2.3.3 SystemCalls 44 1.3.1 ProgramManagement 12 2.4 Summary 47 1.3.2 ResourceManagement 12 TestYourConcepts 48 1.3.3 SecurityandProtection 14 Exercises 49 1.4 PreviewoftheBook 15 Bibliography 49 1.4.1 IntroductiontoOperating Systems 16 1.4.2 ManagingUser Computations 17 3 1.4.3 ManagementofMemory 19 Chapter • 1.4.4 ManagementofFilesandI/O Devices 20 Overview of Operating Systems 51 1.4.5 DistributedOperating 3.1 ComputingEnvironmentsandNatureof Systems 21 Computations 51 1.5 Summary 22 3.2 ClassesofOperatingSystems 54 TestYourConcepts 23 3.3 Efficiency,SystemPerformance,andUser Exercises 23 Service 56 Bibliography 24 3.4 BatchProcessingSystems 58 vi 02-M4363-DAS1.LaTeX: “contents” — 2007/11/29 — 15:07 — page vi — #2 Contents vii 3.5 MultiprogrammingSystems 59 4.9 Summary 102 3.5.1 PriorityofPrograms 61 TestYourConcepts 103 3.6 Time-SharingSystems 65 Exercises 104 3.6.1 SwappingofPrograms 67 Bibliography 104 3.7 Real-TimeOperatingSystems 68 3.7.1 HardandSoftReal-Time Systems 69 2 • • 3.7.2 FeaturesofaReal-TimeOperating Part System 70 Process Management 107 3.8 DistributedOperatingSystems 71 3.8.1 SpecialTechniquesofDistributed 5 OperatingSystems 72 Chapter 3.9 ModernOperatingSystems 73 • 3.10 Summary 74 Processes and Threads 111 TestYourConcepts 75 5.1 ProcessesandPrograms 111 Exercises 76 5.1.1 WhatIsaProcess? 111 Bibliography 78 5.1.2 RelationshipsbetweenProcesses andPrograms 113 5.1.3 ChildProcesses 114 4 5.1.4 Concurrencyand Chapter Parallelism 116 • 5.2 ImplementingProcesses 117 Structure of Operating Systems 80 5.2.1 ProcessStatesandState 4.1 OperationofanOS 80 Transitions 119 4.2 StructureofanOperatingSystem 82 5.2.2 ProcessContextandtheProcess ControlBlock 123 4.2.1 PoliciesandMechanisms 82 5.2.3 ContextSave,Scheduling,and 4.2.2 PortabilityandExtensibilityof Dispatching 125 OperatingSystems 82 5.2.4 EventHandling 127 4.3 OperatingSystemswithMonolithic 5.2.5 Sharing,Communication,and Structure 84 Synchronizationbetween 4.4 LayeredDesignofOperating Processes 131 Systems 85 5.2.6 Signals 132 4.5 VirtualMachineOperatingSystems 88 5.3 Threads 134 4.6 Kernel-BasedOperatingSystems 92 5.3.1 POSIXThreads 138 4.6.1 EvolutionofKernel-Based 5.3.2 Kernel-Level,User-Level,and StructureofOperating HybridThreads 139 Systems 94 5.4 CaseStudiesofProcessesand 4.7 Microkernel-BasedOperating Threads 146 Systems 95 5.4.1 ProcessesinUnix 146 4.8 CaseStudies 97 5.4.2 ProcessesandThreadsin 4.8.1 ArchitectureofUnix 98 Linux 152 4.8.2 TheKernelofLinux 99 5.4.3 ThreadsinSolaris 154 4.8.3 TheKernelofSolaris 100 5.4.4 ProcessesandThreadsin 4.8.4 ArchitectureofWindows 101 Windows 157 02-M4363-DAS1.LaTeX: “contents” — 2007/11/29 — 15:07 — page vii — #3 viii Contents 5.5 Summary 160 6.9.4 Implementationof Semaphores 205 TestYourConcepts 161 6.10 Monitors 208 Exercises 161 6.10.1 MonitorsinJava 213 ClassProject:ImplementingaShell 162 6.11 CaseStudiesofProcess Bibliography 163 Synchronization 213 6.11.1 SynchronizationofPOSIX Threads 213 6 6.11.2 ProcessSynchronizationin Chapter Unix 215 • 6.11.3 ProcessSynchronizationin Process Synchronization 165 Linux 216 6.11.4 ProcessSynchronizationin 6.1 WhatIsProcessSynchronization? 165 Solaris 217 6.2 RaceConditions 166 6.11.5 ProcessSynchronizationin 6.3 CriticalSections 170 Windows 218 6.3.1 PropertiesofaCriticalSection 6.12 Summary 220 Implementation 172 TestYourConcepts 221 6.4 ControlSynchronizationandIndivisible Exercises 222 Operations 172 ClassProject1:Interprocess 6.5 SynchronizationApproaches 175 Communication 224 6.5.1 LoopingversusBlocking 175 ClassProject2:DiskScheduler 225 6.5.2 HardwareSupportforProcess Bibliography 226 Synchronization 177 6.5.3 AlgorithmicApproaches, SynchronizationPrimitives,and ConcurrentProgramming 7 Constructs 180 Chapter 6.6 StructureofConcurrentSystems 181 • 6.7 ClassicProcessSynchronization Scheduling 228 Problems 183 7.1 SchedulingTerminologyand 6.7.1 Producers−−Consumerswith Concepts 228 BoundedBuffers 183 7.1.1 FundamentalTechniquesof 6.7.2 ReadersandWriters 186 Scheduling 231 6.7.3 DiningPhilosophers 187 7.1.2 TheRoleofPriority 232 6.8 AlgorithmicApproachtoImplementing 7.2 NonpreemptiveSchedulingPolicies 233 CriticalSections 190 7.2.1 FCFSScheduling 233 6.8.1 Two-ProcessAlgorithms 190 7.2.2 ShortestRequestNext(SRN) 6.8.2 n-ProcessAlgorithms 194 Scheduling 234 6.9 Semaphores 197 7.2.3 HighestResponseRatioNext 6.9.1 UsesofSemaphoresinConcurrent (HRN)Scheduling 236 Systems 198 7.3 PreemptiveSchedulingPolicies 236 6.9.2 Producers−−ConsumersUsing 7.3.1 Round-RobinSchedulingwith Semaphores 201 Time-Slicing(RR) 237 6.9.3 Readers−−WritersUsing 7.3.2 LeastCompletedNext(LCN) Semaphores 204 Scheduling 240 02-M4363-DAS1.LaTeX: “contents” — 2007/11/29 — 15:07 — page viii — #4 Contents ix 7.3.3 ShortestTimetoGo(STG) 8.2.2 ModelingtheResourceAllocation Scheduling 241 State 281 7.4 SchedulinginPractice 242 8.3 HandlingDeadlocks 285 7.4.1 Long-,Medium-,andShort-Term 8.4 DeadlockDetectionandResolution 286 Schedulers 243 8.4.1 ADeadlockDetection 7.4.2 SchedulingDataStructuresand Algorithm 288 Mechanisms 246 8.4.2 DeadlockResolution 290 7.4.3 Priority-BasedScheduling 246 8.5 DeadlockPrevention 291 7.4.4 Round-RobinSchedulingwith 8.5.1 AllResourcesTogether 293 Time-Slicing 247 7.4.5 MultilevelScheduling 248 8.5.2 ResourceRanking 294 7.4.6 FairShareScheduling 249 8.6 DeadlockAvoidance 295 7.4.7 KernelPreemptibility 250 8.7 CharacterizationofResourceDeadlocksby 7.4.8 SchedulingHeuristics 250 GraphModels 301 7.4.9 PowerManagement 251 8.7.1 Single-Instance,Single-Request 7.5 Real-TimeScheduling 252 (SISR)Systems 301 7.5.1 ProcessPrecedencesandFeasible 8.7.2 Multiple-Instance,Single-Request (MISR)Systems 302 Schedules 252 7.5.2 DeadlineScheduling 254 8.7.3 Single-Instance,Multiple-Request (SIMR)Systems 303 7.5.3 RateMonotonicScheduling 257 8.7.4 Multiple-Instance, 7.6 CaseStudies 259 Multiple-Request(MIMR) 7.6.1 SchedulinginUnix 259 Systems 304 7.6.2 SchedulinginSolaris 262 8.7.5 ProcessesinDeadlock 305 7.6.3 SchedulinginLinux 263 8.8 DeadlockHandlinginPractice 306 7.6.4 SchedulinginWindows 264 8.8.1 DeadlockHandlinginUnix 307 7.7 PerformanceAnalysisofScheduling 8.8.2 DeadlockHandlingin Policies 266 Windows 308 7.7.1 PerformanceAnalysisthrough 8.9 Summary 309 Implementation 266 TestYourConcepts 310 7.7.2 Simulation 267 7.7.3 MathematicalModeling 268 Exercises 310 7.8 Summary 272 Bibliography 313 TestYourConcepts 272 9 Exercises 273 Chapter • Bibliography 275 Message Passing 315 9.1 OverviewofMessagePassing 315 8 9.1.1 DirectandIndirectNaming 317 Chapter • 9.1.2 BlockingandNonblocking Sends 318 Deadlocks 277 9.1.3 ExceptionalConditionsinMessage 8.1 WhatisaDeadlock? 277 Passing 319 8.2 DeadlocksinResourceAllocation 279 9.2 ImplementingMessagePassing 320 8.2.1 ConditionsforaResource 9.2.1 BufferingofInterprocess Deadlock 280 Messages 320 02-M4363-DAS1.LaTeX: “contents” — 2007/11/29 — 15:07 — page ix — #5