Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 27

Chapter 9 and 10

Main Memory & Virtual


Memory

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Background

Program must be brought (from disk) into memory and


placed within a process for it to be run
Main memory and registers are only storage CPU can
access directly
Memory unit only sees a stream of addresses + read
requests, or address + data and write requests
Cache sits between main memory and CPU registers
Protection of memory required to ensure correct operation

Operating System Concepts – 10th Edition 9.2 Silberschatz, Galvin and Gagne ©2018
Base and Limit Registers
A pair of base and limit registers define the logical address space
CPU must check every memory access generated in user mode to be
sure it is between base and limit for that user

Operating System Concepts – 10th Edition 9.3 Silberschatz, Galvin and Gagne ©2018
Logical vs. Physical Address Space

The concept of a logical address space that is bound to a separate


physical address space is central to proper memory
management
Logical address – generated by the CPU; also referred to
as virtual address
Physical address – address seen by the memory unit
Logical and physical addresses are the same in compile-time
and load-time address-binding schemes; logical (virtual) and
physical addresses differ in execution-time address-binding
scheme
Logical address space is the set of all logical
addresses generated by a program
Physical address space is the set of all physical
addresses
generated by a program

Operating System Concepts – 10th Edition 9.4 Silberschatz, Galvin and Gagne ©2018
Memory-Management Unit (MMU)
Hardware device that at run time maps virtual to physical
address. Many methods possible, covered in the rest of this
chapter
To start, consider simple scheme where the value in the
relocation register is added to every address generated by a user
process at the time it is sent to memory
Base register now called relocation register
MS-DOS on Intel 80x86 used 4 relocation registers
The user program deals with logical addresses; it never sees the
real physical addresses
Logical address bound to physical addresses

Operating System Concepts – 10th Edition 9.5 Silberschatz, Galvin and Gagne ©2018
Dynamic relocation using a relocation register

Routine is not loaded until it is


called
Better memory-space utilization;
unused routine is never loaded
All routines kept on disk in
relocatable load format
Useful when large amounts of
code are needed to handle
infrequently occurring cases
No special support from the
operating system is required
Implemented through program
design
OS can help by providing libraries
to implement dynamic loading

Operating System Concepts – 10th Edition 9.6 Silberschatz, Galvin and Gagne ©2018
Swapping
A process can be swapped temporarily out of memory to a backing
store, and then brought back into memory for continued execution
Total physical memory space of processes can exceed
physical memory
Backing store – fast disk large enough to accommodate copies of
all memory images for all users; must provide direct access to these
memory images
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
Major part of swap time is transfer time; total transfer time is
directly proportional to the amount of memory swapped

Operating System Concepts – 10th Edition 9.7 Silberschatz, Galvin and Gagne ©2018
Context Switch Time including Swapping

If next processes to be put on CPU is not in memory, need to


swap out a process and swap in target process
Context switch time can then be very high
100MB process swapping to hard disk with transfer rate of
50MB/sec
Swap out time of 2000 ms
Plus swap in of same sized process
Total context switch swapping component time of 4000ms (4
seconds)
Can reduce if reduce size of memory swapped – by knowing
how much memory really being used
System calls to inform OS of memory use via
request_memory() and release_memory()

Operating System Concepts – 10th Edition 9.8 Silberschatz, Galvin and Gagne ©2018
Multiple-partition allocation
Multiple-partition allocation
Degree of multiprogramming limited by number of partitions
Variable-partition sizes for efficiency (sized to a given process’ needs)
Hole – block of available memory; holes of various size are scattered
throughout memory
When a process arrives, it is allocated memory from a hole large enough to
accommodate it
Process exiting frees its partition, adjacent free partitions combined
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)

Operating System Concepts – 10th Edition 9.9 Silberschatz, Galvin and Gagne ©2018
Dynamic Storage-Allocation Problem
How to satisfy a request of size n from a list of free holes?

First-fit: Allocate the first hole that is big enough

Best-fit: Allocate the smallest hole that is big enough; must


search entire list, unless ordered by size
Produces the smallest leftover hole

Worst-fit: Allocate the largest hole; must also search entire list
Produces the largest leftover hole

Operating System Concepts – 10th Edition 9.10 Silberschatz, Galvin and Gagne ©2018
First Fit vs. Best Fit vs. Worst Fit

Worst Fit
First Fit
Best Fit
Fragmentation
External Fragmentation – total memory space exists to
satisfy a request, but it is not contiguous
Internal Fragmentation – allocated memory may be slightly
larger than requested memory; this size difference is memory
internal to a partition, but not being used
First fit analysis reveals that given N blocks allocated, 0.5 N
blocks lost to fragmentation

Operating System Concepts – 10th Edition 9.12 Silberschatz, Galvin and Gagne ©2018
MM Techniques: Segmentation
Memory-management scheme that supports user view of memory A
program is a collection of segments
A segment is a logical unit such as:
main program procedure
function
method object
local variables, global variables common
block
stack symbol table
arrays

Operating System Concepts – 10th Edition 9.13 Silberschatz, Galvin and Gagne ©2018
User’s View of a Program Logical View of Segmentation

1 4

4
3 2

user space
p
h
y
s
i
Operating System Concepts – 10th Edition 9.14 Silberschatz, Galvin and Gagne ©2018
Segmentation Architecture
Logical address consists of a two tuple:
<segment-number, offset>,

Segment table – maps two-dimensional physical addresses; each


table entry has:
base – contains the starting physical address where the
segments reside in memory
limit – specifies the length of the segment

Operating System Concepts – 10th Edition 9.15 Silberschatz, Galvin and Gagne ©2018
Segmentation Hardware

Operating System Concepts – 10th Edition 9.16 Silberschatz, Galvin and Gagne ©2018
Example

Operating System Concepts – 10th Edition 9.17 Silberschatz, Galvin and Gagne ©2018
Example cont.

Operating System Concepts – 10th Edition 9.18 Silberschatz, Galvin and Gagne ©2018
Paging
Physical address space of a process can be noncontiguous;
process is allocated physical memory whenever the latter is
available
Avoids external fragmentation
Avoids problem of varying sized memory chunks
Divide physical memory into fixed-sized blocks called frames
Size is power of 2, 512 bytes, 4096 and ,..16 Mbytes Divide
logical memory into blocks of same size called pages Keep
track of all free frames
To run a program of size N pages, need to find N free frames and
load program
Set up a page table to translate logical to physical addresses
Still have Internal fragmentation

Operating System Concepts – 10th Edition 9.19 Silberschatz, Galvin and Gagne ©2018
Address Translation Scheme
Address generated by CPU is divided into:
Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit

page number page offset


p d
m -n n

Operating System Concepts – 10th Edition 9.20 Silberschatz, Galvin and Gagne ©2018
Paging Hardware

Operating System Concepts – 10th Edition 9.21 Silberschatz, Galvin and Gagne ©2018
Paging Model of Logical and Physical Memory

Operating System Concepts – 10th Edition 9.22 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 9.23 Silberschatz, Galvin and Gagne ©2018
Virtual Memory Concept
Virtual memory – separation of user logical memory from physical memory
Only part of the program needs to be in memory for execution
Logical address space can therefore be much larger than physical address space
Allows address spaces to be shared by several processes
Allows for more efficient process creation

Operating System Concepts – 10th Edition 9.24 Silberschatz, Galvin and Gagne ©2018
Page Fault

A page fault occurs when the CPU generates a logical address for a
page that is not in physical memory:

1. Operating system looks at another table to decide:


Invalid reference  abort
Just not in memory
2. Find free frame
3. Swap page into frame via scheduled disk operation
4. Reset tables to indicate page now in memory Set
validation bit = v
5. Restart the instruction that caused the page fault

Operating System Concepts – 10th Edition 9.25 Silberschatz, Galvin and Gagne ©2018
Page and Frame Replacement Algorithms

Frame-allocation algorithm determines


How many frames to give each process
Which frames to replace
Page-replacement algorithm
Want lowest page-fault rate on both first access and re-access
Evaluate algorithm by running it on a particular string of memory
references (reference string) and computing the number of page
faults on that string
String is just page numbers, not full addresses
Repeated access to the same page does not cause a page fault
Results depend on number of frames available
In all our examples, the reference string of referenced page
numbers is
7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

Operating System Concepts – 10th Edition 9.26 Silberschatz, Galvin and Gagne ©2018
First-In-First-Out (FIFO) Algorithm
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)

15 page faults

Self Study:
LIFO, LRU
(Moodle: TextBook)

Operating System Concepts – 10th Edition 9.27 Silberschatz, Galvin and Gagne ©2018

You might also like