61
1 Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Outline

Embed Size (px)

DESCRIPTION

Outline. Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation. When OS Involves Paging?. Process creation Process execution Page faults Process termination. - PowerPoint PPT Presentation

Citation preview

Page 1: Outline

1

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Page 2: Outline

2

When OS Involves Paging? Process creation Process execution Page faults Process termination

Page 3: Outline

3

Paging in Process Creation Determine (initial) size of program

and data Assign appropriate page frames

Create page table Process is running page table in

memory Create swap area

For the pages swapped out Record info about page table and

swap area in process table

Page 4: Outline

4

Paging During Process Running When a process is scheduled for

execution Reset MMU Flush TLB (translation Lookaside Buffer) Copy or point to new process’ page

table Bring some or all of new process’ pages

into memory Reduce the number of page faults

Page 5: Outline

5

Paging When Page Faults Read registers to identify virtual

address causing the page fault Locate the page needed on disk Find available page frame

Evict an old page if necessary Read in the page Execute the faulting instruction

again

Page 6: Outline

6

Paging When Process Exits

Release page table

Release pages and swap area on disk If some pages are shared by other

processes, keep them.

Page 7: Outline

7

Swap Area Swap area: reserved for processes’ pages A chunk of pages reserved on disk, same

size of # of pages of the process Each process has its own swap area, recorded

in process table Initialize swap area before a process runs

Copy entire process image, or Load the process in memory and let it be

paged out as needed

Page 8: Outline

8

Swap Area for Growing Processes Process may grow after starting

Data area and stack may grow Reserve separate swap areas for text

(program), data, and stack Reserve nothing in advance

Allocate disk space when pages swapped out De-allocate when page swapped in again Have to keep track of pages on disk A disk address per page, costly!

Page 9: Outline

9

Comparison of Two Methods

Paging to a static swap area Backing up pages dynamically

Page 10: Outline

10

Outline Basic memory management Swapping Virtual memory Page replacement algorithms Modeling page replacement algorithms Design issues for paging systems Implementation issues Segmentation

Page 11: Outline

11

A Motivating Example Many tables when compiling a program

Symbol table: names and attributes of variables Constant table: integer/floating-point constants Parse tree: syntactic analysis Stack: for procedure calls within the compiler

Each table needs contiguous chunks of virtual address space. But tables grow/shrink as compilation proceeds. How to manage space for these tables?

Page 12: Outline

12

With One-Dimensional Address Take space from tables

with an excess of room Tedious work

Free programmers from managing expanding and contracting tables

Segments: many completely independent address spaces

Call stack

Parse tree

Constant table

Source text

Symbol table

Virtual address space

Page 13: Outline

13

Segments Segment: a two dimensional memory

Each segment has a linear sequence of address (0 to some maximum)

Different segments may have different lengths Segment lengths may change during

execution Different segments can grow/shrink

independently Address: segment number + address

within the segment (offset)

Page 14: Outline

14

Multiple Segments in A Process

Segments are logical entities Programmers are aware of them. A segment may contain a procedure, or

an array, but not a mixture of different types.

Facilitate separate protection.

Source text Constant

s

Parse tree

Call stack

Symbol table

Page 15: Outline

15

Paging Vs. Segmentation

Consideration Paging SegmentationAware by programmers? NO Yes# of linear address spaces 1 Many

Total address space > physical memory? Yes Yes

Distinguish and separately protect procedures and data?

No Yes

Accommodate fluctuating tables? No Yes

Facilitate sharing among procedures? No Yes

Why is this technique for?

Get larger linear space than that of physical memory

1. Break programs/data into logically independent address space2. Aid sharing and protection

Page 16: Outline

16

Implementing Pure Segmentation

External fragmentation

Time

Page 17: Outline

17

Segmentation With Paging: MULTICS

For large segments, only the “working set” should be kept in memory Paging segments: segment has own page table

Each program has a segment table One entry (descriptor) per segment Segment table is itself a segment If (part of) a segment is in memory, its page table

must be in memory Address: segment # + virtual page # +

offset Segment # page table Page table + virtual page # page frame

address Page frame address + offset physical address

Page 18: Outline

18

The MULTICS virtual memory

……Segment 2 descriptorSegment 1 descriptorSegment 0 descriptor

……Page 2 entryPage 1 entryPage 0 entry

Page table for segment 2

……Page 2 entryPage 1 entryPage 0 entry

Page table for segment 0

Page frame

Segment tableSegment descriptor contains the memory address of the page table.

#segment

#page

offsetVirtual Address

Page frame

Page 19: Outline

19

Summary Fixed partitions

Multiple queues Vs. single queue Degree of multiprogramming Relocation and protection

Swapping Virtual memory Vs. physical memory Bitmap and linked list Holes

Page 20: Outline

20

Summary (Cont.) Virtual memory

Pages Vs. page frames Page tables Page replacement algorithms (aging and

WSClock) Modeling paging systems

Stack algorithms Predict page faults using distance string

Page 21: Outline

21

Summary (Cont.) Design issues

Local Vs. global allocations Load control to reduce thrashing Shared pages

Implementation issues Page fault handling and swap area

Segmentation Each segment has its own address space Advantages Pure segmentation and segmentation with paging

Page 22: Outline

CMPT 300: Operating System

Chapter 5Input/Output

Page 23: Outline

23

Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

Page 24: Outline

24

Block and Character Devices Block devices: store info in fixed-size

blocks Examples: disks Each block has its own address Each block can be read/written independently Data are read/write in the units of block

Character devices: no block structure, deliver/accept a stream of characters Examples: keyboard, printers, network, mice Not addressable, no seek operation

Devices not in the classification: clock

Page 25: Outline

25

Huge Range in Speeds Many orders of magnitude in data rates

Keyboard: 10 bytes/sec Mouse: 100 bytes/sec Laser printer: 100Kb/sec IDE disk: 5Mb/sec PCI bus: 528Mb/sec

Challenge: how to design a general structure to control various I/O devices? Multi-bus system

Page 26: Outline

26

Structure of I/O Units A mechanical component: the device

itself Disk: plates, heads, motors, arm, etc. Monitor: tube, screen, etc.

An electronic component: device controller, adaptor Disk: issuing commands to mechanical

components, assembling, checking and transferring data

Monitor: read characters to be displayed and generate electrical signals to modulate the CRT beam

Page 27: Outline

27

Mechanical / Electronic Components

Bus

CPU Memory Video controller

Keyboard controller

Floppy disk

controller

Hard disk controller

Monitor KeyboardFloppy

diskHard disk

Mechanical components

Electronic components

Page 28: Outline

28

Device Controller Registers in I/O controllers

CPU writes commands into registers CPU reads states of devices from

registers Data buffer for transferring data How CPU distinguishes different

registers and data buffers? I/O port number Memory-mapped I/O

Page 29: Outline

29

I/O Port Number Each control register is

assigned a unique I/O port number (8-/16-bit integer)

Instruction IN and OUT IN REG, PORT OUT PORT, REG

Separated address spaces IN R0, 4 and MOV R0, 4

are completely different.

0xFFFF…

0

Memory

I/O ports

Page 30: Outline

30

Memory-Mapped I/O Map all control registers into

memory space Usually at the top of the address

space Each register is assigned a unique

memory address, e.g., 0xFF10 Hybrid scheme (used in Pentium)0xFFFF…

0

0xFFFF…

0Memory

I/O portsMemory-mapped Hybrid

I/O buffered data

Page 31: Outline

31

Pros & Cons of Memory-Mapped I/O Advantages

Easier for programming Easier to protect and share I/O devices Save time of accessing control registers

Disadvantages Caching a control register is disastrous

Disable caching for selected pages I/O devices cannot see the memory

addresses with separate buses for memory and I/O devices (see next slide)

Page 32: Outline

CPU Main Memory

I/O

CPU Main Memory

I/O

All addresses go here

CPU reads/writes of memory go over this high-bandwidth bus

Single-bus

Dual-bus

Page 33: Outline

33

Interrupts

CPU

Bus

Interrupt controller

Disk

1. Device finishes a work

Keyboard

ClockPrinter2. Controller

issues interrupt

3. CPU acks interrupt

Page 34: Outline

34

Interrupt Processing I/O devices raise interrupt by asserting a

signal on a bus line assigned Multiple interrupts the one with high

priority goes first Interrupt controller interrupts CPU

Put device # on address lines Device # check interrupt vector table

for interrupt handler (a program) Enable interrupts shortly after the handler

starts

Page 35: Outline

35

Direct Memory Access (DMA)

Request data from I/O without DMA Device controller reads data from device It interrupts CPU when a byte/block of data

available CPU reads controller’s buffer into main memory Too many interruptions, expensive

DMA: direct memory access A DMA controller with registers read/written by CPU CPU programs the DMA: what to transfer where

Source, destination and size DMA interrupts CPU only after all the data are

transferred.

Page 36: Outline

Operations of DMA

CPU

DMA controller

Disk controller

Main memory

Bus

Address

CountControl

Drive1. CPU programs the DMA and controller

Buffer

2. DMA requires transfer to memory

3. Data transferred

4. Ack

Interrupt when done

Page 37: Outline

37

Transfer Modes Word-at-a-time (cycle stealing)

DMA controller acquires the bus, transfer one word, and releases the bus

CPU waits for bus if data is transferring Cycle stealing: steal an occasional bus cycle

from CPU once in a while Burst mode

DMA holds the bus until a series of transfers complete

More efficient since acquiring bus takes time Block the CPU from using bus for a substantial

time

Page 38: Outline

38

Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

Page 39: Outline

39

Issues of The I/O Software Device independence Uniform naming Error handling Buffering Others

Page 40: Outline

40

How to Perform I/O? Programmed I/O (Polling/Busy Waiting)

Acquire I/O device (e.g., printer) Blocked if it is being used by another process

Copy the buffer from use space to kernel space Step 1: keep checking the state until it is ready. Step 2: send one character to printer. Go to

Step 1. Release the I/O.

The CPU do all the work Simple Waste a lot of CPU time on busy waiting

Page 41: Outline

41

An Examplecopy_from_user(buffer, p, count);for (i=0; i<count; i++){ while (*printer_status_reg!=READY); *printer_data_register=p[i];}return_to_user();

// p is the kernel buffer// loop on every character// loop until ready// output one character

Q: Why does OS copy the buffer from user space to kernel space?

Page 42: Outline

42

Interrupt-Driven I/OPrint system callcopy_from_user(buffer, p, count);enable_interrupts();while (*printer_status_reg!=READY);*printer_data_register=p[0];scheduler(); Interrupt service procedure

if (count==0){ unblock_user(); } else { *printer_data_register=p[I]; count--; i++; } acknowledge_interrupt(); return_from_interrupt();

Interrupt occurs on every character!

Page 43: Outline

43

I/O Using DMA Too many interrupts in interrupt-

driven I/O DMA reduces # of interrupts from

1/char to 1/buffer printedPrint system call copy_from_user(buffer, p, count); set_up_DMA_controller(); scheduler(); Interrupt service procedure

acknowledge_interrupt(); unblock_user(); return_from_interrupt();

Page 44: Outline

44

Outline Principles of I/O hardware Principles of I/O software I/O software layers Disks Clocks

Page 45: Outline

45

Layers Overview

User-level I/O softwareDevice-independent I/O

softwareDevice drivers

Interrupt handlers

Hardware

Page 46: Outline

46

Interrupt Handlers Hide I/O interrupts deep in OS

Device driver starts I/O and blocks (e.g., down a mutex)

Interrupt wakes up driver Process an interrupt

Save registers ( which to where?) Set up context (TLB, MMU, page table) Run the handler (usually the handler will be blocked) Choose a process to run next Load the context for the newly selected process Run the process

Take considerable number of CPU instructions

Page 47: Outline

47

Device Drivers Device-specific code for controlling I/O

devices Written by manufacture, delivered along

with device One driver for one (class) device(s)

Position: part of OS kernel, below the rest of OS

Interfaces for rest of OS Block device and character device have

different interfaces

Page 48: Outline

48

Logical Position of Device Drivers

User program

Rest of the OS

Printer driver

Printer controller

printer

User space

Kernel space

Hardware

Devices

Page 49: Outline

49

How to Install a Driver? Re-compile and re-link the kernel

Drivers and OS are in a single binary program UNIX systems Often run by computer centers, devices rarely

change Dynamically loaded during OS initialization

Windows system Devices often change Difficult to obtain source code Users don’t know how to compile OS

Page 50: Outline

50

Functions of Device Drivers Accept abstract read/write requests

Error checking, parameter converting Check status, initialize device, if

necessary Issue a sequence of commands

May block and wait for interrupt Check error, return data

Other issues: re-entrant, up-call, etc.

Page 51: Outline

51

Device-Independent I/O Software Why device-

independent I/O software? Perform I/O

functions common to all devices

Provide a uniform interface to user-level software

Functions in device-independent software

Uniform interfacing for devices drivers

Buffering Error reporting Allocating and

releasing dedicated devices

Providing a device-independent block sizeUser-level I/O software

Device-independent I/O software

Device driversInterrupt handlers

Hardware

Page 52: Outline

52

Uniform Interfacing for Device Drivers New device modify OS, no good Provide the same interface for all drivers

Easy to plug a new driver In reality, not absolutely identical, but most

functions are common Name I/O devices in a uniform way

Mapping symbolic device names onto the proper driver

Treat device name as file name in UNIX E.g., hard disk /dev/disk0 is a special file. Its i-node

contains the major device number, which is used to locate the appropriate driver, and minor device number.

Page 53: Outline

53

Buffering for Input Motivation: consider a process that wants

to read data from a modem User process handles one character at a time. It blocks if a character is not available Each arriving character causes an interrupt User process is unblocked and reads the

character. Try to read another character and block

again. Many short runs in a process: inefficient!

Overhead of context switching

Page 54: Outline

54

Buffering in User Space

User space

Kernel space

Buffering in user space

Set a buffer in user process’ spaceUser process is waked up only if the buffer is filled up by interrupt service procedure. More efficient.Can the buffer be paged out?

Yes: where to put the next character? No: the pool of available pages shrink

Page 55: Outline

55

Buffering in Kernel Two buffers: one in kernel and one in

user Interrupt handler puts characters into

the buffer in kernel space This buffer is locked

When full, copy the kernel buffer to user buffer Bring in the user buffer if necessary Where to store the new arrived characters

during page loading?User spaceKernel space

Buffering in kernel

Page 56: Outline

56

Double Buffering in Kernel Two kernel buffers When the first one fills up, but

before it has been emptied, the second one is used.

Buffers are used in turn: while one is being copied to user space, the other is accumulating new input

User spaceKernel space

Double buffering

Page 57: Outline

57

Downside of Data Buffering Many sequential buffering slow

down transmission

Process A

1

2Network controller

Network

4

5

3

User space

Kernel space

Process B

What’s it for?

Page 58: Outline

58

Handling I/O Errors Programming errors: ask for something

impossible E.g. writing a keyboard, reading a printer Invalid parameters, like buffer address Report an error code to caller

Actual I/O error E.g. write a damaged disk block Handled by device driver and/or device-

independent software System error

E.g. root directory or free block list is destroyed display message, terminate system

Page 59: Outline

59

Allocating Dedicated Devices Before using a device, make the

system call open When the device is unavailable

The call fails, or The caller is blocked and put on a

queue Release the device by making the close system call

Page 60: Outline

60

User-Space I/O Software Libraries: interface between user

programs and system calls E.g., write(fd, buffer, nbytes),

printf/scanf Spooling

User-level, controlled by a daemon Eliminated unnecessarily

waiting/deadlockUser-level I/O softwareDevice-independent I/O

softwareDevice drivers

Interrupt handlersHardware

Page 61: Outline

61

Summary: I/O Software

User-level I/O software Make I/O call; format I/O; spooling

Device-independent OS software

Naming, protection, blocking, buffering, allocation

Device drivers Setup device registers; check status

Interrupt handlers Wake up driver when I/O completed

Hardware Perform I/O operation

I/O request I/O reply