chapter 2 the os, the computer, and user programs copyright © 2008

33
Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Upload: james-jefferson

Post on 23-Dec-2015

255 views

Category:

Documents


13 download

TRANSCRIPT

Page 1: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Chapter 2

The OS, the Computer,and User Programs

Copyright © 2008

Page 2: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.2

Introduction

• Fundamental Principles of OS Operation• The Computer• OS Interaction with the Computer and User Programs

Page 3: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.3

Fundamental Principles of OS Operation

• The kernel of the OS is the collection of routines that form the core of the operating system– Implements control functions

– Set of services to user programs

– Exists in memory during operation of the OS

• An interrupt diverts the CPU to execution of the kernel code

• A software interrupt is used by programs to communicate their requests to the kernel

Page 4: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.4

Fundamental Principles of OS Operation (continued)

• CPU has two modes of operation:– Kernel mode

• CPU can execute all instructions• Kernel operates with CPU in this mode so that it can control

computer operations

– User mode• CPU cannot execute instructions that could interfere with other

programs or with the OS if used indiscriminately• CPU is put in this mode to execute user programs

Page 5: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.5

The Computer

Page 6: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.6

The Computer (continued)

• The CPU• Memory Management Unit (MMU)• Memory Hierarchy• Input/Output• Interrupts

Page 7: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.7

The CPU

• Two features of the CPU are visible to user programs or the OS:– General-purpose registers (GPRs)

• Also called program-accessible registers• Hold data, addresses, index values, or the stack pointer

during execution of a program

– Control registers• Contain information that controls or influences operation of

the CPU• Set of control registers is called the program status word

(PSW)

Page 8: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.8

Page 9: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.9

The CPU (continued)

• CPU can operate in two modes:– Kernel mode

• Can execute privileged instructions• OS puts CPU in kernel mode when it is executing

instructions in the kernel

– User mode• Cannot execute privileged instructions• OS puts CPU in user mode while executing user programs

• Mode (M) field of PSW contains 0 if CPU is in privileged mode and 1 if it is in user mode

Page 10: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.10

State of the CPU

• GPRs and PSW contain the information needed to know what the CPU is doing– State of the CPU

• Kernel saves state of CPU when it takes away the CPU from program– When program is to be resumed, it reloads the saved

CPU state into GPRs and PSW

Page 11: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.11

Example 2.1: State of the CPU (Contd)

Page 12: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.12

Memory Management Unit (MMU)

• Virtual memory is an illusion of memory that may be larger than the real memory of a computer– Implemented using noncontiguous memory allocation and

the MMU• CPU passes the address of data or instruction used in an

instruction to MMU– It is called the logical addresses

• MMU translates logical address to physical address

Page 13: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.13

Memory hierarchy

• The memory hierarchy provides a large and fast memory, at a low cost– It is an arrangement of several memories with different

access speeds and sizes• The CPU accesses only the fastest memory; i.e., the cache• If a required byte is not present in the memory being

accessed, it is loaded there from a slower memory

Page 14: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.14

Page 15: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.15

Memory Hierarchy (continued)

• When CPU performs a cache lookup, a cache hit or miss may occur– Hit ratio (h) of the cache is the fraction of bytes accessed

by the CPU that score a hit in the cache

tema = h × tcache + (1 – h) × (ttra + tcache)

= tcache + (1 – h) × ttra

where

tema = effective memory access time,

tcache = access time of cache, and

ttra = time taken to transfer a cache block from memory to

cache.

Page 16: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.16

Memory Hierarchy (continued)

• Operation of memory is analogous to operation of a cache– Blocks of bytes (pages) are transferred from disk to

memory or from memory to disk

– But,• Memory management and transfer of blocks between

memory and disk are performed by SW• In the cache, the transfer is performed by HW

• Memory hierarchy comprising MMU, memory, and the disk is called the virtual memory

Page 17: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.17

Memory Hierarchy (continued)

• Memory protection is implemented by checking whether a memory address used by a program lies outside the memory area allocated to it– Control registers used:

• base and size (also called limit)– Address of first byte = <base>

– Address of last byte = <base> +<size> – 1

Page 18: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.18

Example 2.2: Basics of Memory Protection

•interrupt• Execution of Load instruction causes protection violation

Page 19: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.19

Input/Output

Page 20: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.20

Interrupts

• An event is a situation that requires OS’s attention– Designer associates an interrupt with each event

• Purpose is to report occurrence of the event to OS and enable it to perform event handling actions

• Interrupt action saves CPU state and loads new contents into the PSW and GPRs– CPU starts executing instructions of an interrupt servicing

routine (ISR) in the kernel

Page 21: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.21

Interrupts (continued)

Page 22: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.22

Page 23: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.23

OS Interaction with the Computer and User Programs

• OS interacts with the computer to– know information about events, so that it can service

them– restore CPU state to resume a program after servicing an

interrupt• Programs need to use services of the OS for purposes

such as initiating an I/O operation– The method to cause an interrupt and pass requirement

to the OS is known as a System call• Will learn about:

– Controlling Execution of Programs– Interrupt Servicing– System Calls

Page 24: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.24

Controlling Execution of Programs

1. When user program starts, PSW should contain:a. Program counter (PC) field

b. Mode (M) field, set to user mode (1)

c. Memory protection information (MPI) field contains start address in memory and size of program

d. Interrupt mask (IM) field, set to enable interrupts

2. When program is interrupted, CPU state (PSW and GPRs) are saved• Program table or process control block (PCB)

3. When program is resumed, its CPU state is restored

Page 25: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.25

Interrupt Servicing

• Context save saves CPU state of the program• The scheduler selects a a program for execution

Page 26: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.26

Interrupt Servicing (continued)

Page 27: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008
Page 28: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.28

Interrupt Servicing (continued)

• Two approaches for nested interrupt servicing: – Disable nested interrupts through masking– Handle nested interrupts─preemptible kernel

Page 29: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.29

System Calls

Page 30: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.30

Example 2.4: System Call in a Hypothetical OS

• CPU provides the SI instruction to cause a software interrupt

• OS provides system call for obtaining current time– Code is 78: Instruction SI 78 causes a SW interrupt

• 78 is entered in IC field of PSW before it is saved• Interrupt vector for program contains aaa in PC

– CPU is switched to routine with start address aaa

– It finds that IC is 78 and determines that program needs time of day

• Time is returned to program in a standard location, typically a data register

Page 31: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.31

System Calls (continued)

Page 32: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.32

Summary

• An interrupt is a special signal sent to CPU to indicate occurrence of an event– It transfers control to the OS– A system call is a SW interrupt

• A program uses it to request OS services

• CPU’s control registers govern its functioning– Program status word (PSW) is the collection of these

• OS kernel saves CPU state when interrupt occurs– i.e., PSW and GPRs

• CPU has two modes of operation controlled by mode (M) field of PSW– User mode and kernel mode

Page 33: Chapter 2 The OS, the Computer, and User Programs Copyright © 2008

Operating Systems, by Dhananjay Dhamdhere Copyright © 2008 2.33

Summary (continued)

• Memory hierarchy provides the same effect as a fast and large memory, at a low cost– Contains:

• A very fast and small cache memory• A slower and larger RAM• A disk

• Effective memory access time depends on cache hit ratio

• I/O system uses direct memory access (DMA) to permit CPU and I/O system to operate independently