CSCI2413 Lecture 6 Operating Systems Memory Management 2 phones off (please)

27
CSCI2413 Lecture 6 Operating Systems Memory Management 2 phones off (please)
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    214
  • download

    0

Transcript of CSCI2413 Lecture 6 Operating Systems Memory Management 2 phones off (please)

CSCI2413 Lecture 6

Operating Systems

Memory Management 2

phones off (please)

© De Montfort University, 2004

CSCI2413 - L6 2

Lecture Outline Virtual Memory

• Swapping

• Paging,

• thrashing

• Segmentation

Summery• Notes on swapping and thrashing

© De Montfort University, 2004 CSCI2413 - L6 3

Swapping

• Moving processes to and fro between main memory and hard disk is called swapping

• Roll out, roll in – swapping variant used for priority-based scheduling algorithms;

• lower-priority process is swapped out so higher-priority process can be loaded and executed.

• (see notes on page 25)

© De Montfort University, 2004 CSCI2413 - L6 4

Virtual Memory

• uses locality of reference in which most commonly used instructions and data are referenced:– instruction execution is localised either within loops or

heavily used subroutines,– data manipulation is on local variables or upon

tables or arrays of information. • Virtual memory can also be defined as:• A mapping from a virtual address space to a

physical address space.

© De Montfort University, 2004 CSCI2413 - L6 5

Why Virtual Memory?• Use Physical DRAM as a Cache for the Disk

– Address space of a process can exceed physical memory size

• Simplify Memory Management– Multiple processes resident in main memory.

• Each process with its own address space

• Provide Protection– One process can’t interfere with another.

• because they operate in different address spaces.

– User process cannot access privileged information• different sections of address spaces have different permissions.

© De Montfort University, 2004 CSCI2413 - L6 6

A System with Physical Memory Only• Examples:

– most Cray machines, early PCs, nearly all embedded systems, etc.

Addresses generated by the CPU correspond directly to bytes in physical memory

CPU

0:1:

N-1:

Memory

PhysicalAddresses

© De Montfort University, 2004 CSCI2413 - L6 7

0:1:

N-1:

A System with Virtual Memory (paging)

Address Translation: MMU converts virtual addresses to physical addresses via OS-managed lookup table (page table)

CPU

Memory

0:1:

P-1:

Page Table

Disk

VirtualAddresses

PhysicalAddresses

© De Montfort University, 2004 CSCI2413 - L6 8

Paging

• Partition memory into small equal-size blocks and divide each process into the same size blocks

• The blocks of a process are called pages and blocks of memory are called frames

• Operating system maintains a page table for each process– contains the frame location for each page in the

process– each memory reference consist of a page number

and offset within the page

© De Montfort University, 2004 CSCI2413 - L6 9

virtual page number page offset virtual address

physical page number page offset physical address0p–1

address translation

pm–1

n–1 0p–1p

Page offset bits don’t change as a result of translation

VM Address Translation• Parameters

– P = 2p = page size (bytes). – N = 2n = Virtual address limit– M = 2m = Physical address limit

© De Montfort University, 2004 CSCI2413 - L6 10

Paging …

• Each process has its own page table

• Each page table entry contains the frame number of the corresponding page in main memory

• A bit is needed to indicate whether the page is in main memory or not

© De Montfort University, 2004 CSCI2413 - L6 11

Page TablesPage TablesPage TablesPage Tables

Memory residentpage table

(physical page address)Physical Memory

Disk Storage(swap blocks)

Valid

1

1

111

1

10

0

0

Virtual PageNumber

© De Montfort University, 2004 CSCI2413 - L6 12

Execution of a Program

• Operating system brings into main memory a few blocks of the program

• An interrupt is generated when a block is needed that is

not in main memory - page fault

• Operating system places the process in a blocking state and a DMA is started to get the page from disk

• An interrupt is issued when disk I/O is complete which causes the OS to place the affected process in the Ready Queue

© De Montfort University, 2004 CSCI2413 - L6 13

Page Faults (like “Cache Misses”)• What if a block is on disk rather than in memory?

– Page table entry indicates virtual address not in memory– OS exception handler invoked to move data from disk into

memory• OS has full control over placement, etc.

CPU

Memory

Page Table

Disk

VirtualAddresses

PhysicalAddresses

CPU

Memory

Page Table

Disk

VirtualAddresses

PhysicalAddresses

Before fault After fault

© De Montfort University, 2004 CSCI2413 - L6 14

Thrashing

• Swapping out a piece of a process just before that piece is needed

• The processor spends most of its time swapping pieces rather than executing user instructions

(see notes on page 26)

© De Montfort University, 2004 CSCI2413 - L6 15

Replacement Policy

• Which page should be replaced?– Page that is least likely to be referenced in the near future

• Each page in memory usually has two bits associated with it:

• R   referenced bit: set when page is accessed (read/written)

• M  modified bit: set when page is written to

• These bits would be set by MMU when a page is read/written and

cleared by the OS - in particular the OS clears the R bits on every clock interrupt.

© De Montfort University, 2004 CSCI2413 - L6 16

Replacement …

• When a page fault occurs there are four possible scenarios:

• R = 0, M = 0: not referenced, not modified • R = 0, M = 1: not referenced, but modified • R = 1, M = 0: referenced, but not modified • R = 1, M = 1: referenced and modified  

© De Montfort University, 2004 CSCI2413 - L6 17

Replacement algorithm

• NRU, Simple to operated and adequate performance. •    If R = 0 and M = 0 (not referenced, not modified)

– swap one of these pages

•     else •         if R = 0, M = 1 (not referenced, but modified)

– swap one of these pages

•         else •              if R = 1, M = 0 (referenced, but not modified)

– swap one of these pages             else swap one of the R = 1, M = 1 pages

• Note that any page with M = 0 can be overwritten since that have not been written to since being read from disk.

© De Montfort University, 2004 CSCI2413 - L6 18

Replacement algorithm …

• FIFO: • Not very good - • pages are kept in a list and the one at the end of

the list is swapped out even if still in use.

• LRU: swap out or discard the page which has been least used (used by Atlas). Each page has a counter which is incremented by the MMU when the page is accessed. On a page fault swap page with lowest count.

© De Montfort University, 2004 CSCI2413 - L6 19

Replacement algorithms …

• NFU:• Similar to LRU but implemented in software;

• the OS adds the R bit to the page counter on each clock interrupt (then clears the R bit). On a page fault swap page with lowest count.

• (Read about problems of LRU and NFU in page 27)

© De Montfort University, 2004 CSCI2413 - L6 20

Segmentation• OS creates multiple virtual address spaces, each starting at an arbitrary

location and with arbitrary length.

• The start of a segment is virtual address 0.

• Each process can be assigned a different segment, independent of others.

• Relocation done at run time by the virtual memory mapping mechanism.

Segmentation is implemented in a manner much like paging, through a lookup table.

• The difference is that each segment descriptor in the table contains the base address of the segment and a length.

© De Montfort University, 2004 CSCI2413 - L6 21

Segmentation with Paging

• Some operating systems allow for the combination of segmentation with paging.

• If the size of a segment exceeds the size of main memory, the segment may be divided into equal size pages.

© De Montfort University, 2004 CSCI2413 - L6 22

Segmentation with paging …

The address consists of: segment number, page within the segment and offset within the page. The segment number is used to find the segment descriptor and the address within the segment is used to find the page frame and the offset within that page.

© De Montfort University, 2004 CSCI2413 - L6 23

Summary

• When swapping is used, the system can handle more processes than it has room for in memory … virtual memory concept.. and hence paging and segmentation

• When memory is full, a decision must be made as to which page or pages are to be replaced (replacement policy)

• http://www.cs.umass.edu/~weems/CmpSci535/535lecture9.html

© De Montfort University, 2004 CSCI2413 - L6 24

Example Page Sizes

© De Montfort University, 2004 CSCI2413 - L6 25

Swap file• A swap file (or swap space or, in Windows NT, a page) is a space on a

hard disk used as the virtual memory extension of a computer's real memory (RAM). Having a swap file allows your computer's operating system to pretend that you have more RAM than you actually do. The least recently used files in RAM can be "swapped out" to your hard disk until they are needed later so that new files can be "swapped in" to RAM. In larger operating systems (such as IBM's OS/390), the units that are moved are called pages and the swapping is called paging. One advantage of a swap file is that it can be organized as a single contiguous space so that fewer I/O operations are required to read or write a complete file.

• In general, Windows and Unix-based operating systems provide a default swap file of a certain size that the user or a system administrator can usually change.

© De Montfort University, 2004 CSCI2413 - L6 26

thrashing•

Thrashing is computer activity that makes little or no progress, usually because memory or other resources have become exhausted or too limited to perform needed operations. When this happens, a pattern typically develops in which a request is made of the operating system by a process or program, the operating system tries to find resources by taking them from some other process, which in turn makes new requests that can't be satisfied. In a virtual storage system (an operating system that manages its logical storage or memory in units called pages), thrashing is a condition in which excessive paging operations are taking place.

• A system that is thrashing can be perceived as either a very slow system or one that has come to a halt.

© De Montfort University, 2004 CSCI2413 - L6 27

LRU and NFU problem:

• Problem if page is referenced very heavily its counter is large even if it is no longer being accessed, i.e. the counter does not decrease. A modified LRU/NFU introduces aging by on every clock interrupt shifting the counter right one bit (dividing it by two) then adding the R bit. The penalty is that this takes processor power, i.e. if we had 1000 pages and a shift and add

R takes 2usec the total time would be 2msec