Sie sind auf Seite 1von 183

OPEN SOURCE LECTURE NOTES Operating System Design and Implementation

Anthony Aaby

DRAFT Version: α 1. 0 EDITED: November 10, 2006

2

This work is licensed

cc 2006 by

Anthony A. Aaby Walla Walla College 204 S. College Ave. College Place, WA 99324 E-mail: aabyan@wwc.edu

under the Creative Commons Attribution-NonCommercial License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/ 1.0, or send a letter to Creative Commons, 559 Natha n Abbot Way, Stanford, California 94305, USA.

This book is distributed in the hope it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particu- lar purpose. You may copy, modify, and distribute this book for the cost of reproduction provided the above creative commons notice remains intact. No explicit permission is required from the author for reproduction of this book in any medium, physical or electronic.

The most current version of this text and L A T E Xsource is available at:

Source: http://www.cs.wwc.edu/~aabyan/LN/OS/osnotes.tar.gz Postscript: http://www.cs.wwc.edu/~aabyan/LN/OS/osln.ps DVI: http://www.cs.wwc.edu/~aabyan/LN/OS/osln.dvi PDF: http://www.cs.wwc.edu/~aabyan/LN/OS/osln.pdf HTML: http://www.cs.wwc.edu/~aabyan/LN/OS/osln/index.html

Contents

Preface

11

Assignments

13

I Introduction

15

1 OS1: Overview of Operating Systems

 

17

1.1 A Computer System

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

18

1.2 Role and Purpose of the OS

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

18

1.2.1 Provide a service for clients - a virtual machine (top-down

view)

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

19

1.2.2 Resource manager (bottom-up view)

 

19

1.2.3 The general functions of an operating system .

.

.

.

.

.

.

20

1.3 History of OS Development

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

1.4 Functionality of a Typical OS

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

21

1.5 Mechanisms to Support Client-Server Models, Hand-held Devices

22

1.6 Design Issues

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

23

1.7 Influences of Security, Networking, Multimedia, Windows

 

24

2 OS2: Operating System Principles

 

25

2.1 Elements

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

26

2.2 Structuring Methods

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

26

2.2.1 Monolithic systems .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

2.2.2 Layered systems

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

2.2.3 Virtual machines

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

2.2.4 Exokernels

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

27

2.2.5 Client-server

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

2.2.6 OS Research

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

2.3 Abstractions, processes, and resources

 

28

2.3.1 Resources

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

28

2.3.2 Processes

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

2.3.3 Threads

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

2.3.4 Objects

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

3

4

CONTENTS

2.4 Concepts of APIs .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

2.5 Application needs and evolution of techniques

 

29

2.6 Device Organization

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

2.7 Interrupts: methods and implementations

 

29

2.8 User and System State .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

29

II Processes

 

31

3 OS3: Concurrency

 

33

4 Processes

35

4.1 Process Concept

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

35

4.1.1

Definition

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

35

4.1.2 Goal/Rationale

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

36

4.1.3 Design

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

36

4.1.4 Implementation .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

37

4.2 Threads

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

4.2.1 Goal/Rationale

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

4.2.2 Thread Structure .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

39

4.2.3 Design

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

40

4.2.4 Implementation .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

40

4.2.5

Solaris 2

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

40

4.3 Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

40

5 Deadlock

41

5.1 Basic Concepts

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

41

5.1.1

Resource-Allocation Graph

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

5.2 Methods for handling deadlocks

 

42

5.3 Ignore/Ostrich Algorithm

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

5.4 Deadlock Prevention

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

5.5 Deadlock Avoidance

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

5.5.1 Safe State .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

42

5.5.2 Resource-Allocation Graph Algorithm

 

43

5.5.3 Banker’s Algorithm

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

5.6 Deadlock Detection and Recovery

 

43

5.7 Combined Approach to Deadlock Handling

 

43

5.8 Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

43

6 Synchronization and Communication

 

45

6.1 Basic Concepts

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

45

6.2 Software Solutions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

46

6.2.1 Two-Process ”Solutions” .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

46

6.2.2 Multple-Process Solutions

 

48

6.3 Hardware Solutions .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

48

6.4 Semaphores

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

49

CONTENTS

5

6.4.1 Definition

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

49

6.4.2 Goal/Rationale

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.4.3

Design .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.4.4

Implementation .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.4.5

Usage

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.4.6 Deadlocks and starvation

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.4.7 Binary Semaphores

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

50

6.5 Classical Problems of Synchronization

 

51

6.5.1 Bounded Buffer

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

51

6.5.2 Dining Philosophers

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

51

6.5.3 Readers and Writers

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

51

6.5.4 Sleeping Barber

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

51

6.6 Critical Regions

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

51

6.7 Monitors

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

6.7.1

Example: Solaris 2

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

6.8 Message Passing

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

52

6.9 Atomic Transactions

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

54

6.10 Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

54

7 OS4 - Scheduling and dispatch

 

55

8 CPU Scheduling

 

57

8.1 Ontology: Basic Concepts

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

57

8.2 Values - Scheduling Criteria (Goals)

 

60

8.3 Methods - Scheduling Algorithms

 

60

8.3.1 First-Come, First-Served (FCFS)

 

60

8.3.2 Shortest-Job-First (SJF)- shortest process next (SPN)

 

60

8.3.3 Priority Scheduling .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

61

8.3.4 Round-Robin (RR) .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

61

8.3.5 Multilevel Queue Scheduling

 

62

8.3.6 Multilevel Feedback Queue Scheduling

 

62

8.4 Multiple-Processor Scheduling

 

64

8.5 Real-time Scheduling

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

64

8.6 Algorithm Evaluation

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

64

8.6.1 Deterministic modeling

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

64

8.6.2 Queueing models

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

8.6.3 Simulations

 

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

8.6.4 Implementation .

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

8.7 Exercises

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

.

65

III

I/O

9 OS6 - Device management

67

69

6

CONTENTS

10

I/O Devices and Systems

 

71

Devices as files

10.1 .

.

.

.

.