Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 104 Introduction to Computer Science and Graphics Problems

Similar presentations

Presentation on theme: "CS 104 Introduction to Computer Science and Graphics Problems"— Presentation transcript:

1 CS 104 Introduction to Computer Science and Graphics Problems
Operating Systems (3) Memory Management 10/07 ~ 10/10/2008 Yang Song (Prepared by Yang Song and Dr. Hung)

2 The Fundamental Functions
Process Management Memory Management File Management Input / Output Networking Utility Services

3 Memory Management Memory management and CPU scheduling are perhaps the two most central tasks of an OS memory consists of a large array of words, each with its own address both program and data are stored in memory CPU fetch-execute cycle fetch next instruction from memory (based on program counter) decode the instruction possibly fetch operands from memory execute the instruction store the result in memory Memory is given an address and it returns a value it does not distinguish between instructions and data it does not care how the address was arrived at

4 Address Binding Users do not (cannot?) think about real addresses
user processes go through several steps before execution (e.g., compilation, loading, linking, …) addresses may be represented in different ways during these steps (symbol, relocatable addr, …) Instructions/data may be bound to real addresses: at compile time: compiler substitutes actual addresses in executable used in MS-DOS at load time: compiler generates relocatable code, final binding is delayed until load time once loaded in memory and process starts, bindings are locked in – not generally used at execution time: if process can be moved during its execution, then binding must be delayed until run time requires special hardware support – used in most general-purpose OS's

5 Logical vs. Physical Address
The concept of a logical address space that is mapped to a physical address space is central to memory management CPU works in a logical (virtual) address space users, processes, instruction operands, program counter refer to logical addresses memory is a physical address space real memory accesses require real addresses Memory Management Unit (MMU) maps logical addresses to physical addresses implemented in hardware for example, might have a relocation register that provides the base address for a process' block of physical memory

6 Contiguous Memory Allocation
Main memory must accommodate both the OS and user processes usually divided into two partitions: OS & interrupt vector in low memory, user processes in high memory contiguous memory allocation places each process in a single, contiguous section of memory OPTION 1: single partition (all processes share same memory space) relocation-register scheme used to protect user processes from each other, and from changing OS code and data relocation register contains smallest physical address; limit register contains range of logical addresses – each logical address must be less than the limit register

7 Contiguous Memory Allocation
OPTION 2: multiple partitions (each process has its own partition) process memory time P1 600K 10 P2 1000K 5 P3 300K 20 P4 700K 8 P5 500K 15 OS 400K OS P2 400K 2560K 1000K 2000K 2300K P1 P3 OS 400K 2560K 1000K 2000K 2300K P1 P3 OS P4 400K 2560K 1000K 2000K 2300K P1 P3 1700K

8 Dynamic Storage Allocation Problem
as processes free up holes in memory and new processes request memory, how do you allocate partitions? first-fit: allocate the first hole that is big enough best-fit: allocate the smallest hole that is big enough must search the entire list of holes unless ordered by size produces the smallest leftover hole worst-fit: allocate the largest hole available also must search the entire list of holes produces the largest leftover hole process memory time P1 600K 10 P2 1000K 5 P3 300K 20 P4 200K 8 P5 100K 15 OS 400K 2560K 1000K 2000K 2300K P1 P3 first-fit is fast best-fit tends to work better for storage utilization

9 Fragmentation With dynamic storage allocation, fragmentation can be a problem there may be many small holes, none big enough to fit a process compaction is needed to coalesce holes (similar to defragging a hard drive) note: compaction is possible only if bindings are done at execution time process memory time P1 600K 10 P2 1000K 5 P3 300K 20 P4 1200K 8 OS 400K 2560K 1000K 2000K 2300K P1 P3 1300K

10 Paging Paging is one solution to the external fragmentation problem
Non-contiguous storage allocation – a process is allocated physical memory wherever it is available divide the physical memory into fixed-size blocks called frames (size is a power of 2, generally between 512 and 8K bytes) divide logical memory into blocks of the same size called pages to run a process of size n pages, need to find n free frames and load Paging requires hardware support every logical address generated by the CPU is divided into: page number (p): used as an index into a page table containing the base address of each page in physical memory page offset (d): combined with base address to determine the physical memory address

11 Paging Examples abstract example: tiny (but concrete) example:
page size = 4 bytes physical memory = 32 bytes abstract example: 4 pages in logical address space page table maps to physical frames

12 Page Fault If the process references a logical page that
is not in physical memory: memory management unit checks page table page fault exception is raised due to invalid bit OS must locate logical page in secondary memory schedules a disk operation to swap into memory at disk interrupt, swaps page into a free frame sets valid bit in page table restarts instruction

13 Page Replacement Algorithms
Page replacement completes separation between logical & physical memory large virtual memory can be provided on a smaller physical memory choices as to which pages are swapped in and out of memory have significant impact on effective access time we want page replacement algorithms that minimize the page fault rate

14 First-In-First-Out (FIFO) Algorithm
Select as victim the page that has been in memory the longest i.e., treat frame set as a queue, replace pages in FIFO order simple, but not very effective in practice doesn't favor pages that are used often (e.g., contains frequently used variable) example: suppose 3 pages can be in memory at a time per process process references pages: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 requires 15 page faults

15 Optimal Algorithm Select as victim the page that will not be used for the longest time Same reference string requires only 9 page faults (compared with 15 for FIFO)

16 Least Recently Used (LRU) Algorithm
OPT is not practical since it requires future knowledge However, we can approximate OPT under certain assumptions if we use the recent past as an approximation of the near future, then will select as victim the page that has been used least recently LRU works well in practice

Download ppt "CS 104 Introduction to Computer Science and Graphics Problems"

Similar presentations

Ads by Google