Professional Documents
Culture Documents
MEMORY
WHAT IS VIRTUAL
MEMORY?
Background
Code needs to be in memory to execute, but entire
program rarely used
Error code, unusual routines, large data
structures
Entire program could not needed at the same time
Consider ability to execute partial-loaded program
Program no longer constrained by limits of
physical memory
Programs could be larger than physical memory
VIRTUAL MEMORY
a technique that allows the
execution of process that are not
completely in the main memory.
A separation of user logical
memory from physical memory
30 bit ram
address
space(1gb)
30 bit ram
address
space(1gb)
Program
0
30 bit ram
address
space(1gb)
Program
0
Program
1
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
30 bit ram
address
space(1gb)
MAP
3
Program
4
Program
5
DISK
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
30 bit ram
address
space(1gb)
Program
0
MAP
3
Program
4
Program
5
DISK
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
30 bit ram
address
Program
space(1gb)
1
Program
0
MAP
3
Program
4
Program
5
DISK
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
MAP
30 bit ram
address
Program
space(1gb)
1
Program
0
Program
2
3
Program
4
Program
5
DISK
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
MAP
30 bit ram
address
Program
space(1gb)
1
Program
0
Program
2
3
Program
4
Program
5
DISK
With virtual
memory
30 bit ram
address
space(1gb)
Program
0
Program
1
Pr0gram
2
????
Crash if we try
to access more
ram than we
have
32 bit program
address space
(4gb)
Program
0
Program
1
Program
2
Program
30 bit ram
address
Program
space(1gb)
1
MAP
3
Program
4
Program
5
Program
3
Program
2
Program 0
DISK
Virtual Address
Space
MAX
Stack
heap
Data
0
Code
Virtual Address
Space
MAX
Stack
Stack
Stack
Shared
library
Shared
pages
Shared
library
heap
heap
heap
Data
Data
Data
Code
Code
Code
Demand Paging
Pages are loaded only when they are demanded during
program execution.
Uses lazy swapper
Demand Paging
Pages are loaded only when they are demanded during
program execution.
Uses lazy swapper
pager is the correct term not swapper
Demand Paging
Pages are loaded only when they are demanded during
program execution.
Uses lazy swapper
pager is the correct term not swapper
Manipulates the
entire process
Demand Paging
Pages are loaded only when they are demanded during
program execution.
Uses lazy swapper
pager is the correct term not swapper
Manipulates the
entire process
Demand Paging
Pages are loaded only when they are demanded during
program execution.
Uses lazy swapper
pager is the correct term not swapper
Manipulates the
entire process
Demand Paging
The hardware to support demand paging is the same as
the hardware for paging and swapping:
Page table
Secondary memory
Demand Paging
The hardware to support demand paging is the same as
the hardware for paging and swapping:
Page table. This table has the ability to mark an
entry invalid through a validinvalid bit or a
special value of protection bits.
Secondary memory
Demand Paging
The hardware to support demand paging is the same as
the hardware for paging and swapping:
Page table. This table has the ability to mark an
entry invalid through a validinvalid bit or a
special value of protection bits.
Secondary memory. This memory holds those
pages that are not present in main memory. The
secondary memory is usually a high-speed disk. It
is known as the swap device, and the section of
disk used for this purpose is known as swap
space.
Demand Paging
Transfer of memory to contiguous disk
space
10
11
12
13
14
15
Swap out
Progra
mA
Swap in
Progra
mB
Main memory
Demand Paging
Benefits
Less I/O needed
Less memory needed
More users
Demand Paging
Valid and Invalid
Bit Scheme
When bit is set to valid
the associated page
is both legal and in memory.
Demand Paging
Valid and Invalid
Bit Scheme
When bit is set to valid (v)
the associated page
in memory.
When bit is set to invalid (i)
the page is not in memory
5
6
7
g
h
Logical
memor
y
Validinvali
frame d bit
1
2
0
1
2
3
4 a
6
7
8
9
10
11
5
6
7
i
Page table
physica
lmemo
ry
Page fault
1. OS looks at another table to decide
.
Invalid reference =>abort
. Just not in memory
2.
3.
4.
5.
6.
Page is on
backing store
OS
2
trap
reference
1
Load M
i
v
Restart
instruction
Free
frame
Page table
Reset page
table
Bring in
missing
page
Physical memory
Copy on Write
Copy-on-Write (COW) allows both parent and child
processes to initially share the same pages in memory
If either process modifies a shared page, only then is the
page copied
COW allows more efficient process creation as only
modified pages are copied
Free pages are allocated from a pool using a technique
called zero-fill-on demand. Zero-fill-on-demand pages
have been zeroed-out before
being allocated, thus erasing the previous contents.
Pag
eB
Pag
eC
Physical Memory
Process 2
Pag
eB
Pag
eC
Copy
of
page
C
Process 2
PAGE REPLACEMENT
Basic to demand paging
Completes separation between logical memory and physical memory
Condition on existence of demand paging
No demand paging:
User addresses are mapped into physical addresses
All pages of a process still must be in physical memory
With demand paging:
The size of the logical address space is no longer constrained by physical
memory
Two major problems to solve in order to implement demand paging:
Frame-allocation algorithm
Must decide how many frames to allocate in each process
Must select the frames that are to be replaced
Page-replacement algorithm
Want lowest page-fault rate on first access and re-access
Evaluated by running on a reference string
Write the victim frame to the disk; change the page and frame tables
Read the desired page into a newly freed frame; change the page and frame
tables
Continues the use process from where the page fault occurred
If bit is set:
Page has been modified since it was read from the disk
BELADYS ANOMALY
For some page-replacement algorithms, the page-fault may increase as the number
of allocated frames increases
Resource bit
Associated with each entry in the page table
Initially bits are 0
As user executes, the bit associated with each page referenced is set to 1
ADDITIONAL-REFERENCE-BITS ALGORITHM
SECOND-CHANCE ALGORITHM
When page has been selected, inspect its reference bit
If value is 0:
Proceed to replace page
If value is 1:
Give page a second chance and move on to the next FIFO page
When page gets second chance:
Reference bit is cleared
Arrival time is set to current time
CLOCK ALGORITHM
Second-Chance algorithm implemented using a circular queue
A pointer indicates which page to replace next
When a frame is needed, the pointer advance until it finds a page with a 0
reference bit
Clears reference bit as it advances
Once a victim page is found, the page is replaced, and the new page is inserted in
the circular queue in that position
Based on the argument that the page with the smallest count was probably just
brought in and has yet to be used
PAGE-BUFFERING ALGORITHMS
Maintain a list of modified pages
Modifies page is selected and is written to the desk
Modify bit is reset
Modification to keep a pool of free frames but to remember which page was in
each frame
Old page can be reused directly from the free-frame pool if it is needed before
the frame is reused
No I/O needed
When page fault occurs:
First check if desired page in in the free-frame pool
Otherwise, select a free frame and read into it
Page is quickly retrieved from the free-frame pool, and no I/O is necessary
Free-frame provides protection against the relatively poor, but simple, FIFO
replace algorithm
Raw Disk
Disk partition as a large sequential array of logical blocks without any filesystem data structures
Raw I/O
I/O to Raw Disk
Bypasses all the file-allocation, file names, and directories
ALLOCATION OF FRAMES
Simplest solution of a single user system is to place the free frames in a free-frame
list the process which will then generate page faults will then get the free frames.
When the free-frame list is exhausted a page-replacement algorithm would be
used to select one of the in-memory pages to be replaced with the next one, and so
on. When the process terminates the frames will then be put back to the freeframes list.
There are variations on this strategy. One is keeping a certain number of frames in
the free-frame list so that there is always a free frame for when a page fault is
generated.
ALLOCATION OF FRAMES
fixed allocation
priority allocation
FIXED ALLOCATION
Equal Allocation If there are n processes and m free frames then each process
should get m/n frames and keep extra frames for a free frame buffer pool.
S si
m total number of frames
s
ai allocation for pi i m
S
PRIORITY ALLOCATION
It uses the same equation as the proportional allocation but instead of s i = size of pi
instead si = priority of pi.
It also enables a higher priority process to take frames from a lower priority process
when it generates a page fault.
PAGE REPLACEMENT
There are two general types of Page replacement algorithms Global allocation and Local
allocation.
Global allocation allows processes to select for replacement from anywhere in the system.
Allows for more throughput overall but is less consistent on completion times of
individual processes.
Local allocation only allows replacement pages from its own page pool
It can be also done by scheduling the thread such that it is done on the same system
board as its required memory.
When possible schedule all threads of a process and allocate all memory for that process within
the lgroup
THRASHING
When there is a lack of pages for a process its page fault rate increases.
If the process then spends more time page faulting than executing then this activity
is called thrashing.
This happens when a process page faults to get a page so it starts to take pages
away from other processes but these processes needed those pages so they also
take away pages from other processes and so on and so forth.
This leads to low CPU utilization and the OS thinking it needs to increase the degree
of multiprogramming and thus adds another process to continue the thrashing.
THRASHING
One can limit the effects of thrashing by using Local Allocation since the processes
then cannot take away pages from other processes.
However the processes page faulting is still page faulting and will still continue to do so
until they are completed so they will tie up page fault queues.
One can also use the locality model which states that as a processes executes it
should move from a group of pages that are to used together, known as a locality,
to another. Localities may overlap.
PAGE
FAULT
FREQUENCY
A more direct
approach
in solving thrashing
It uses the page fault rate of a process. An allowable range for page fault rate is
chosen.
If a process has a higher page fault rate than the allowable then a frame is
allocated to it.
If a process has a lower page fault rate than the allowable then a frame is taken
from it.
Some OS use memory mapped files for standard I/O i.e. Solaris
Processes can explicitly request memory mapping a file using mmap() system call
In Solaris even without using mmap() files are memory mapped but is mapped into
kernel address space instead of user address space
mmap() can also be used to implement shared memory by memory mapping the
same file
EXAMPLE PROGRAM
#include <windows.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
HANDLE hFile, hMapFile;
LPVOID lpMapAddress;
hFile = CreateFile("temp.txt", /* file name */
GENERICREAD | GENERICWRITE, /* read/write
access */
0, /* no sharing of the file */
NULL, /* default security */
OPENALWAYS, /* open new or existing file */
FILEATTRIBUTENORMAL, /* routine file
attributes */
NULL); /* no file template */
hMapFile = CreateFileMapping(hFile, /* file
handle */
MEMORY-MAPPED I/O
In the case of OSs that uses Memory mapped I/O instead of mapping the file to the
user address space it is mapped to the kernel address space
Usually a range of memory addresses is set aside just for this. Usually contiguous
especially for device I/Os
Usually implemented for devices with fast response times like video controllers.
ALLOCATING KERNEL
MEMORY
KERNEL MEMORY
PRIMARY REASONS
BUDDY SYSTEM
First Strategy
BUDDY SYSTEM
Allocates memory from a fixed-size segment
consisting of physically contiguous pages.
Memory is allocated using a power-of-2
allocator
Satisfies requests in units sized as a power
of 2
A request in units not appropriately sized is
rounded up to the next higher power of 2
BUDDY SYSTEM
Initial memory segment size = 256 KB
Kernel request = 21 KB of memory
256 KB
128 KB
AL
64 KB
BL
32
KB
CL
32
KB
CR
128 KB
AR
64 KB
BR
Buddy system
allocation
SLAB ALLOCATION
Second Strategy
SLAB ALLOCATION
Slab made up of one or more physically contiguous
pages
Cache consists of one or more
slabs
o There is a single cache for each unique
kernel data structure
SLAB ALLOCATION
free
The objects which
are initially
marked as free
are allocated to
the cache.
When a new object
for a kernel data
structure is needed,
the slab allocator can
assign any free
object from the
cache to satisfy the
request.
use
The object
assigned from the
cache is marked
as use.
struct task_struct
SLAB ALLOCATOR
Slab started in Solaris, now wide-spread for both kernel
mode and user memory in various OSes.
Linux 2.2 had SLAB, now has both SLOB and SLUB
allocators.
SLOB (Simple List of Blocks) designed for system with
limited memory
It works by maintaining 3 lists of objects: small, medium &
large
SLUB
int[128,128] data;
Program 1
stored data[0][0],data[0][1],...,data[0]
[127], data[1][0], data[1][1],...,
...data[127][127]
If the operating system allocates fewer than 128 frames to the entire
program, then its execution will result in 128 x 128 = 16,384 page faults
Program 2
for (i = 0; i < 128; i++)
for (j = 0; j < 128; j++)
data[i,j] = 0;
This code zeros all the words on one page before starting the next page,
reducing into
128 page faults
To write a
block on
tape:
bloc
k copy
syste
mwrite
tap
e
Pages containing the
block
lock
memor
y
Working-set minimum
Minimum number of pages the process is guaranteed to
have in memory.
If sufficient memory is available, a process may be assigned as
many pages as
Working-set maximum
When the amount of memory falls below the threshold, the virtual
memory manager uses a tactic
knownworking-set
as
automatic
to restore the value above trimming
the threshold.
Removes pages from processes that have pages in excess of their working set
minimum
HOMEWORK
1. When is a process considered to be thrashing?
2. How does memory mapping speed up the system?
3. Consider having m = 90 free frames, s0 = 5 , s1 = 15 and s2 = 35 if you are using
proportional allocation algorithm how many frames will be allocated to p 0, p1, and
p2?
4. Given a reference string: 1, 3, 7, 5, 2, 1 ,0, 2, 4, 3, 0, 6, 7, 1, 3, 1, 2, 5, 6, 3, 7, 5, 1,
3,
and memory with the size of 4 frames, show how many page faults occur using
clock LRU algorithm. (Show solution)