The fruit of silence is PRAYER. The fruit of prayer is FAITH. The fruit of faith is LOVE. The fruit of love is SERVICE. The fruit of service is PEACE. MOTHER TERESAMemory Management
Memory Management without swaping or paging:
Multiprogramming
Advantages:
Example:
if n process in memory
CPU is not utilized if all processes are waiting
Thus CPU utilization α = 1 - Pwait = 1 - pn
e.g p = 1/2
| n | &alpha |
|---|---|
| 1 | 1/2 ( 50% ) |
| 2 | 3/4 ( 75% ) |
| 3 | 7/8 ( 87.5% ) |
| .. | .. |
| 10 | 99.9% |
moving processes from main memory to disk and back
variable partition
|
|
memory compaction -- combine all holes into one big hole ( it may require a lot of time; mainframe has special hardware to handle compaction )
consider a hole ~ 10244 bytes
next process requires 10242 bytes
2 bytes left, overhead to keep track of the 2 bytes is much larger than 2 bytes
so better allocate the 2 bytes to the process → internal fragmentation
Memory allocation with swapping
Use a linked list to handle dynamic storage allocation
A set of holes with various sizes scattered throughout memory
| page 64K virtual address space |
32K main mem page frame | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
Thus, MMU maps the address to 20K + 10 and put it on the bus
CPU generates a trap
put a page frame back on disk
and put required page to main memory
e.g. want to access location 8196
8196 = 8192 + 4 = 8K + 4 so it is in page 2
Virtual Address
| page # | 4K address | |||
| 0 0 1 0 | 0 . . . . . . 0 1 0 0 | |||
|
||||
| → |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
In paging, user's view of memory are separated from actual physical memory
user's view of memory -- a collection of variable-sized segments, e.g. stacks, data, symbol table, main program, functions, ...

Segmentation is a memory-management scheme that supports its user view of memory; a logical memory is a collection of segments:
Implementation of segment tables:
replace the page that will not be used for the longest period of time
guarantees lowest page fault but almost impossible to implement
mainly for comparison
associate with each page 2 bits:
R is set on any read/write to the page
M is set when the page is written
| class | functions | R | M |
|---|---|---|---|
| 0 | not referenced, not modified | 0 | 0 |
| 1 | not referenced, modified | 0 | 1 |
| 2 | referenced, not modified | 1 | 0 |
| 3 | referenced, modified | 1 | 1 |
NRU removes a page at random from the lowest nonempty class
easy to understand
efficient to implement
replace the oldest page
second chance
Belady's anomaly
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
select the page that has not been used for the longest time
expensive, need a linked list of all pages in memory
clock algorithm -- an efficient way to approximate LRU
uses circular queue and reference bits as mechanisms
current | new value
ref. bit | ref. bit action
-----------+------------------------------------------------------
|
0 | 0 replace this page, move pointer forward
|
1 | 0 skip this page for now, continue search

two-handed clock uses both reference and modified bits
current | new value
ref. mod. | ref. mod. actions
------------+------------------------------------------------------
|
0 0 | 0 0 replace this page (when any scheduled
| write back on it is completed), move
| pointer forward
|
0 1 | 0 0 clean the page (i.e., write back), skip
| this page for now, continue search
|
1 0 | 0 0 skip this page for now, continue search
|
1 1 | 0 1 skip this page for now, continue search
demand paging -- pages are loaded on demand only, not in advance; only the pages that are needed to run the program before the program will be swapped out, will be swapped in; Through this, swapping time and amount of physical memory required can be decreased.

locality -- processes tend to reference storage in nonuniform highly localized pattern
Working set -- the set of pages that a process is currently using
Denning
W( Δ , t ) -- at time t, the set consists of all pages used by most recent memory reference
Δ -- working set window
if two many processes are running, page faults may occur in every few instructions; this occurs when the total size of all working set exceeds the number of frames → thrashing

to avoid thrashing, we can keep track of each process's working set
and make sure that it is in memory before letting the process in
( prepaging )
A process will never be executed unless its working set is resident in main memory. Pages outside the working set may be discarded at any time
use the recent needs of a process to predict its future needs
aging bit -- if a page is not referenced for a certain amount of time, it will be dropped from the working set
Total number of frames D = ∑iWi
if D > m ( number of frames ), thrashing occurs
local -- each process process is allocated a fixed amount of memory
global -- dynamically allocate page frames among the runnable processes
in general global works better
figure
on average, half of the final page is wasted ( internal fragmentation )
if n segments, page size = p bytes,
But small pages => large page table
suppose
page size = p bytes
wasted memory in each page = p / 2
average process size = s bytes
number of pages needed per process = s / p
total table space = s x e / p
| total overhead H = |
s x e p |
+ | p 2 |
Minimize
| 0 = |
d H d p |
= - |
s x e p2 |
+ | 1 2 |
=> p = ( 2se ) ½
if s = 32K, e = 8 bytes, p ~ ( 2 x 32K x 8 ) ½ ~ 724 ( bytes )
~ 512, 1 K, 2K, 4K