lect01

66
Introduction to Operating Systems Chapter 1 – 1.3 Chapter 1.5 – 1.9

Upload: vin-voro

Post on 13-Dec-2014

300 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Lect01

Introduction to Operating

SystemsSystems

Chapter 1 – 1.3

Chapter 1.5 – 1.9

Page 2: Lect01

Learning Outcomes

• High-level understand what is an operating

system and the role it plays

• A high-level understanding of the structure of operating systems, applications, and the operating systems, applications, and the relationship between them.

• Some knowledge of the services provided by operating systems.

• Exposure to some details of major OS concepts.

2

Page 3: Lect01

What is an Operating

System?

3

Page 4: Lect01

4

Page 5: Lect01

Viewing the Operating System as

an Abstract Machine

• Extends the basic hardware with added functionality

• Provides high-level abstractions• Provides high-level abstractions

– More programmer friendly

– Common core for all applications

• It hides the details of the hardware

– Makes application code portable

5

Page 6: Lect01

UsersDisk

Memory

CPU

6

CPU

Network

Bandwidth

Page 7: Lect01

Viewing the Operating System

as a Resource Manager

• Responsible for allocating resources to users

and processes

• Must ensure

– No Starvation

– Progress

– Allocation is according to some desired policy

• First-come, first-served; Fair share; Weighted fair share; limits (quotas), etc…

– Overall, that the system is efficiently used

7

Page 8: Lect01

Traditional View: the Operating

System as the Privileged Component

Requests (System Calls)

Applications Applications Applications

User Mode

8

Operating System

Privileged Mode

Hardware

Page 9: Lect01

Kernel

• Portion of the operating system that is running in privileged mode

• Usually resident in main memory• Usually resident in main memory

• Contains fundamental functionality– Whatever is required to implement other

services

– Whatever is required to provide security

• Contains most-frequently used functions

• Also called the nucleus or supervisor9

Page 10: Lect01

The Operating System is

Privileged

• Applications should not be able to interfere or bypass

the operating system

– OS can enforce the “extended machine”

– OS can enforce its resource allocation policies– OS can enforce its resource allocation policies

– Prevent applications from interfering with each other

10

Operating System

Applications Applications Applications

Privileged Mode

User Mode

Hardware

Page 11: Lect01

Structure of a Computer System

System Libraries

Application

System Libraries

Application

Memory

11

Operating System

Kernel Mode

User Mode

Device

Device

Page 12: Lect01

Structure of a Computer System

System Libraries

Application

Memory

12

OS

Kernel Mode

User Mode

Device

Device

OS interacts via load

and store instructions

to all memory, CPU

and device registers,

and interrupts

Page 13: Lect01

Structure of a Computer System

System Libraries

Application

Applications interact with

themselves and via

function calls to library

procedures

Memory

13

OS

System Libraries

Kernel Mode

User Mode

Device

Device

Page 14: Lect01

Structure of a Computer System

System Libraries

Application

Interaction via

System Calls

Memory

14

OS

Kernel Mode

User Mode

Device

Device

Page 15: Lect01

Privilege-less OS

• Some Embedded OSs have no

privileged component

– e.g. PalmOS, Mac OS 9, RTEMS

– Can implement OS functionality, – Can implement OS functionality, but cannot enforce it.

• All software runs together

• No isolation

• One fault potentially brings down entire

system

15

Page 16: Lect01

A note on System Libraries

System libraries are just that, libraries of support

functions (procedures, subroutines)

– Only a subset of library functions are actually systems calls

• strcmp(), memcpy(), are pure library functions• strcmp(), memcpy(), are pure library functions– manipulate memory within the application, or perform computation

• open(), close(), read(), write() are system calls– they cross the user-kernel boundary, e.g. to read from disk device

– Implemenation mainly focused on passing request to OS and returning result to application

– System call functions are in the library for convenience• try man syscalls on Linux

16

Page 17: Lect01

Operating System Objectives• Convenience

– Make the computer more convenient to use

• Abstraction– Hardware-independent programming model

• Efficiency– Allows the computer system to be used in an efficient manner– Allows the computer system to be used in an efficient manner

• Ability to evolve– Permit effective development, testing, and introduction of new

system functions without interfering with existing services

• Protection– allow only authorised access to data, computation, services, etc.

17

Page 18: Lect01

Services Provided by the

Operating System• Program execution

– Load a program and its data

• Access to I/O devices– Display, disk, network, printer, keyboard, camera, – Display, disk, network, printer, keyboard, camera,

etc.

• Controlled access to files– Access protection

• System access– User authentication

18

Page 19: Lect01

Services Provided by the

Operating System

• Error detection and response

– internal and external hardware errors

• memory error• memory error

• device failure

– software errors

• arithmetic overflow

• access forbidden memory locations

– operating system cannot grant request of

application19

Page 20: Lect01

Services Provided by the

Operating System

• Accounting

– collect statistics

– monitor performance– monitor performance

• diagnose lack of it

– used to anticipate future enhancements

– used for billing users

20

Page 21: Lect01

Operating System Software

• Fundamentally, OS functions the same way as ordinary computer software

– It is a program that is executed

(just like apps)

System Libraries

Application

(just like apps)

– It has more privileges

• Operating system relinquishes control of the processor to execute other programs

– Reestablishes control after

• System calls

• Interrupts (especially timer

interrupts)

21

Memory

OS

Kernel Mode

User Mode

Device

Device

Page 22: Lect01

Major OS Concepts

(Overview)

• Processes

• Concurrency and deadlocks

• Memory management• Memory management

• Files

• Scheduling and resource management

• Information Security and Protection

22

Page 23: Lect01

Processes• A program in execution

• An instance of a program running on a computer

• The entity that can be assigned to and executed on a processor

• A unit of resource ownership• A unit of resource ownership

23

Page 24: Lect01

Process• Consist of three segments– Text

• contains the code (instructions)

– Data

• Global variables

Stack

Gap

Memory

• Global variables

– Stack

• Activation records of procedure

• Local variables

• Note:– data can dynamically grow

up

– The stack can dynamically grow down

24

Data

Text

Page 25: Lect01

Process

• Consists of three components– An executable program

• text

– Associated data needed by the program– Associated data needed by the program• Data and stack

– Execution context of the program• All information the operating system needs to

manage the process– Registers, program counter, stack pointer, etc…

25

Page 26: Lect01

Multiple processes creates

concurrency issues

(a) A potential deadlock. (b) an actual deadlock.26

Page 27: Lect01

Memory Management• The view from thirty thousand feet

– Process isolation• Prevent processes from accessing each others data

– Automatic allocation and management• Don’t want users to deal with physical memory directly

– Protection and access control• Still want controlled sharing

– Long-term storage

– OS services• Virtual memory

• File system

27

Page 28: Lect01

Virtual Memory

• Allows programmers to address memory from a logical point of view– Gives apps the illusion of having RAM to

themselvesthemselves

– Logical addresses are independent of other processes

– Provides isolation of processes from each other

• Can overlap execution of one process while swapping in/out others.

28

Page 29: Lect01

Virtual Memory Addressing

29

Page 30: Lect01

File System

• Implements long-term store

• Information stored in named objects called filescalled files

30

Page 31: Lect01

Example File System

31

Page 32: Lect01

Information Protection and

Security

• Access control

– regulate user access to the system

– Involves authentication– Involves authentication

• Information flow control

– regulate flow of data within the system and

its delivery to users

32

Page 33: Lect01

Scheduling and Resource

Management

• Fairness

– give equal and fair access to all processes

• Differential responsiveness• Differential responsiveness

– discriminate between different classes of jobs

• Efficiency

– maximize throughput, minimize response time,

and accommodate as many uses as possible

33

Page 34: Lect01

Operating System

Structure?

34

Page 35: Lect01

Operating System Structure

• The layered approach

a) Processor allocation and multiprogramming

b) Memory Management

c) Devices

d) File system

e) Users

– Each layer depends on

the inner layers

35

a b c d e

Page 36: Lect01

Operating System

Structure

• In practice, layering is only a guide

– Operating Systems have many

interdependenciesinterdependencies

• Scheduling on virtual memory

• Virtual memory on I/O to disk

• VM on files (page to file)

• Files on VM (memory mapped files)

• And many more…

36

Page 37: Lect01

The Monolithic Operating

System Structure

• Also called the “spaghetti nest” approachapproach

– Everything is

tangled up with

everything else.

• Linux, Windows, ….

37

Page 38: Lect01

The Monolithic Operating

System Structure• However, some

reasonable structure usually prevails

38

Bowman, I. T., Holt, R. C., and Brewster, N. V. 1999. Linux as a case study: its extracted software architecture. In Proceedings of the 21st international Conference on Software Engineering (Los Angeles, California, United States, May 16 - 22, 1999). ICSE '99. ACM, New York, NY, 555-563. DOI= http://doi.acm.org/10.1145/302405.302691

Page 39: Lect01

39

Page 40: Lect01

Computer Hardware

ReviewReview

Chapter 1.4

Page 41: Lect01

Learning Outcomes

• Understand the basic components of computer hardware

– CPU, buses, memory, devices controllers, – CPU, buses, memory, devices controllers,

DMA, Interrupts, hard disks

• Understand the concepts of memory hierarchy and caching, and how they affect performance.

41

Page 42: Lect01

Operating Systems

• Exploit the hardware available

• Provide a set of high-level services that represent or are implemented by the hardware.represent or are implemented by the hardware.

• Manages the hardware reliably and efficiently

• Understanding operating systems requires a basic understanding of the underlying hardware

42

Page 43: Lect01

Basic Computer Elements

43

Page 44: Lect01

Basic Computer Elements• CPU

– Performs computations

– Load data to/from memory via system bus

• Device controllers– Control operation of their particular device

– Operate in parallel with CPU– Operate in parallel with CPU

– Can also load/store to memory (Direct Memory Access, DMA)

– Control register appear as memory locations to CPU

• Or I/O ports

– Signal the CPU with “interrupts”

• Memory Controller– Responsible for refreshing dynamic RAM

– Arbitrating access between different devices and CPU

44

Page 45: Lect01

The real world is logically similar,

but more complex

45

Page 46: Lect01

A Simple Model of CPU

Computation

• The fetch-execute cycle

46

Page 47: Lect01

A Simple Model of CPU

Computation• The fetch-execute cycle

– Load memory contents from

address in program counter

(PC) PC: 0x0300

CPU Registers

(PC)

• The instruction

– Execute the instruction

– Increment PC

– Repeat

47

PC: 0x0300

R1

SP: 0xcbf3

Status

Rn

Page 48: Lect01

A Simple Model of CPU

Computation• Stack Pointer

• Status Register– Condition codes

• Positive resultPC: 0x0300

CPU Registers

• Zero result

• Negative result

• General Purpose Registers– Holds operands of most instructions

– Enables programmers (compiler) to minimise memory references.

48

PC: 0x0300

R1

SP: 0xcbf3

Status

Rn

Page 49: Lect01

Privileged-mode Operation

• To protect operating system execution, two or more CPU modes of operation exist– Privileged mode (system-,

CPU Registers

Interrupt Mask

Exception Type

Others

MMU regs

– Privileged mode (system-, kernel-mode)

• All instructions and registers are available

– User-mode• Uses ‘safe’ subset of the

instruction set

– E.g. no disable interrupts instruction

• Only ‘safe’ registers are accessible

49

PC: 0x0300

R1

SP: 0xcbf3

Status

Rn

Others

Page 50: Lect01

‘Safe’ registers and

instructions

• Registers and instructions are safe if

– Only affect the state of the application itself

– They cannot be used to uncontrollably – They cannot be used to uncontrollably

interfere with

• The operating system

• Other applications

– They cannot be used to violate a correctly

implemented operating system.

50

Page 51: Lect01

Privileged-mode Operation

• The accessibility of addresses within an address space

Memory Address Space

Accessible only

to

Kernel-mode

0xFFFFFFFF

address space changes depending on operating mode

– To protect kernel code

and data

51

Accessible to

User- and

Kernel-mode

Kernel-mode

0x00000000

0x80000000

Page 52: Lect01

I/O and Interrupts• I/O events (keyboard, mouse, incoming network

packets) happen at unpredictable times

• How does the CPU know when to service an I/O event?

52

Page 53: Lect01

Interrupts

• An interruption of the normal sequence of

execution

• A suspension of processing caused by an event

external to that processing, and performed in

such a way that the processing can be resumed.

• Improves processing efficiency

– Allows the processor to execute other instructions

while an I/O operation is in progress

– Avoids unnecessary completion checking (polling)

53

Page 54: Lect01

Interrupt Cycle

• Processor checks for interrupts

• If no interrupts, fetch the next instruction

• If an interrupt is pending, divert to the

interrupt handlerinterrupt handler

54

Page 55: Lect01

Classes of Interrupts

• Program exceptions

(also called synchronous interrupts)

– Arithmetic overflow

– Division by zero

– Executing an illegal/privileged instruction

– Reference outside user’s memory space.

• Asynchronous (external) events

– Timer

– I/O

– Hardware or power failure 55

Page 56: Lect01

Interrupt Handler

• A software routine that determines the nature of the interrupt and performs whatever actions are needed.whatever actions are needed.

• Control is transferred to the handler by hardware.

• The handler is generally part of the operating system.

56

Page 57: Lect01

Simple Interrupt

User ModeApplication

57

Kernel Mode

Interrupt

Handler

Page 58: Lect01

Memory Hierarchy

• Going down the

hierarchy

– Decreasing cost per

bit

– Increasing capacity

– Increasing access – Increasing access

time

– Decreasing

frequency of access

to the memory by the

processor

• Hopefully

• Principle of locality!!!!!58

Page 59: Lect01

Memory Hierarchy• Rough (somewhat dated) approximation of

memory hierarchy

59

Page 60: Lect01

Cache

• Cache is fast memory placed between the CPU and main memory – 1 to a few cycles access time compared to RAM access time of tens –

hundreds of cycles

CPU

RegistersCache Main Memory

Word Transfer Block Transfer

• Holds recently used data or instructions to save memory accesses.• Matches slow RAM access time to CPU speed if high hit rate• Is hardware maintained and (mostly) transparent to software• Sizes range from few kB to several MB.• Usually a hierarchy of caches (2–5 levels), on- and off-chip.• Block transfers can achieve higher transfer bandwidth than single

words.– Also assumes probability of using newly fetch data is higher than the

probability of reuse ejected data.

60

Page 61: Lect01

Moving-Head Disk Mechanism

61

Page 62: Lect01

Example Disk Access Times

• Disk can read/write data relatively fast– 15,000 rpm drive - 80 MB/sec

– 1 KB block is read in 12 microseconds

• Access time dominated by time to locate the • Access time dominated by time to locate the head over data– Rotational latency

• Half one rotation is 2 milliseconds

– Seek time• Full inside to outside is 8 milliseconds

• Track to track .5 milliseconds

• 2 milliseconds is 164KB in “lost bandwidth”62

Page 63: Lect01

A Strategy: Avoid Waiting for

Disk Access

• Keep a subset of the disk’s data in memory

⇒ Main memory acts as a cache of disk ⇒ Main memory acts as a cache of disk contents

63

Page 64: Lect01

Two-level Memories and Hit

Rates

• Given a two-level memory,

– cache memory and main memory (RAM)

– main memory and disk– main memory and disk

what is the effective access time?

• Answer: It depends on the hit rate in the first level.

64

Page 65: Lect01

Effective Access Time

)1( 21

=

×−+×=eff THTHT

65

system of timeaccess effective

1memory in ratehit

2memory of timeaccess

1memory of timeaccess

2

1

=

=

=

=

effT

H

T

T

Page 66: Lect01

Example

• Cache memory access time 1ns

• Main memory access time 10ns

• Hit rate of 95%

66

9

99

9

105.1

)101010()95.01(

1095.0

−−

×=

×+×−

+×=effT