MemoryManagement PDF
MemoryManagement PDF
Memory Management
Reading: Chapter 8 of Textbook
The program and data of a process must be in main memory for the
process to execute.
How to keep the track of processes currently being executed?
Which processes to load when memory space is available?
How to load the processes that are larger than main memory?
How do processes share the main memory?
OS component that is responsible for handling these issues is a
memory manager.
By Bishnu Gautam 1
Advanced Operating System Memory Management
2
By Bishnu Gautam
Advanced Operating System Memory Management
Sharing
• Allow several processes to access the same portion of memory (allow to
share data).
• Better to allow each process (person) access to the same copy of the
program rather than have their own separate copy.
By Bishnu Gautam 3
Advanced Operating System Memory Management
CPU compare every address generated in user mode with the base registers. Any
attempt by a program executing in user mode to access operating-system memory or
other users' memory results in a trap to the operating system, which treats the attempt
as a fatal error
By Bishnu Gautam 4
Advanced Operating System Memory Management
Hardware device that maps virtual to physical address at run time is Memory
Management Unit (MMU)
By Bishnu Gautam 5
Advanced Operating System Memory Management
Swapping
In multiprogramming environment, some times there is no enough memory to hold
all processes that is being executed.
How to handle this problem? – swapping.
A process can be swapped temporarily out of memory to a backing store, and then
brought back into memory for continued execution
both the memory and CPU.
By Bishnu Gautam 6
Advanced Operating System Memory Management
Memory Allocation
How to allocate the space for a process as much as they need?
Two Strategies:
Fixed size partitions and variable size partitions
Variable Partitions
When processes arrive, they are given as much storage as they need.
By Bishnu Gautam 7
Advanced Operating System Memory Management
Memory Allocation
When processes finish, they leave holes in main memory; OS fills
these holes with another processes from the input queue of
processes.
How to satisfy a request of size n from a list of the free holes
(Partition Selection Problem)?
Situation: Multiple memory holes are large enough to contain a process, OS
must select which hole the process will be loaded into.
There are many solutions to this problem – Partition Selection
Algorithms or Placement Algorithms
By Bishnu Gautam 8
Advanced Operating System Memory Management
Memory Allocation
Input queue
PA 25K PB 15K PC 20 K PD 20 K
OS OS OS OS OS
PA 25K PA 25K Hole PC 20K PC 20K
Free PD 20K
Free
Free Free Free
By Bishnu Gautam 9
Advanced Operating System Memory Management
Memory Fragmentation
When holes given to other process they may again partitioned, the remaining
holes gets smaller eventually becoming too small to hold new processes –
waste of memory occur – memory fragmentation.
By Bishnu Gautam 11
Advanced Operating System Memory Management
Memory Compaction
By moving processes in memory, the memory holes can be
collected into a single section of unallocated space –
memory compaction.
OS
Problems: PC 20K
Not possible in absolute translation. Compaction is possible
only if relocation is dynamic, and is done at execution time PD 20K
If it possible, it is highly expensive – it requires a lots of
CPU time; Free
Eg: On a 256-MB machine that can copy 4 bytes in 40 nsec, it takes about 2.7
sec to compact all memory.
It stops every thing when compaction – not good solution
Memory compaction
Alternate solutions: Allows the noncontiguous allocations
e.g Paging, segmentation
By Bishnu Gautam 12
Advanced Operating System Memory Management
Paging
The logical address space (process) is divided up into fixed sized blocks called
pages and the corresponding same size block in main memory is called frames
When a process is to be executed, its pages are loaded into any available
memory frames from the backing store.
The size of the pages is determined by the hardware, normally from 512 bytes to
16MB (in power of 2).
Paging permits the physical address space of process to be noncontiguous.
Traditionally, support for paging has been handled by hardware, but the recent design
have implemented by closely integrating the hardware and OS.
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
By Bishnu Gautam 13
Advanced Operating System Memory Management
Address Translation
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
If the size of the logical address space is 2^m and page size is 2^n, then the
high-order m - n bits of logical address designate page number, and n lower
order bits designate the page offset.
Present/absent bit keeps the track of which pages are physically present in
memory.
By Bishnu Gautam 14
Advanced Operating System Memory Management
Address Translation
Traditionally, paging has been handled by hardware. However, recent designs
have implemented paging by closely integrating the hardware and operating
system, especially on 64-bit microprocessors.
By Bishnu Gautam 15
Advanced Operating System Memory Management
By Bishnu Gautam 16
Advanced Operating System Memory Management
Paging Fragmentation
No external fragmentation: any free frame can be allocated to a process that
needs it. However, it may have some internal fragmentation
In the worst case, a process would need n pages plus 1 byte. It would be allocated
n + 1 frames, resulting in internal fragmentation of almost an entire frame.
By Bishnu Gautam 17
Advanced Operating System Memory Management
Page Tables
A page table is allocated for each process, stores the number of frames
allocated for each page.
The purpose of the page table is to map virtual pages into pages frames. This
function of page table can be represented in mathematical notation as:
page_frame = page_table(page_number)
The virtual page number is used as an index into the page table to find the
corresponding page frame.
18
Advanced Operating System Memory Management
By Bishnu Gautam 19
Advanced Operating System Memory Management
By Bishnu Gautam 21
Advanced Operating System Memory Management
By Bishnu Gautam 23
Advanced Operating System Memory Management
p1 p2 d
10 10 12
The top level have 1024 entries, corresponding to P1. At mapping, it first extracts the
P1 and uses this value as an index into the top level page table. Each of these entries
have again 1024 entries, the resulting address of top-level yields the address or page
frame number of second-level page table.
By Bishnu Gautam 24
Advanced Operating System Memory Management
The hash value is the virtual-page number. Each entry in the hash
table contains a linked list of elements that hash to the same
location.
The virtual address is hashed into the hash table, if there is match
the corresponding page frame is used, if not, subsequent entries
in the linked list are searched.
By Bishnu Gautam 25
Advanced Operating System Memory Management
By Bishnu Gautam 26
Advanced Operating System Memory Management
Decreases memory needed to store each page table, but increases time needed
to search the table when a page reference occurs
By Bishnu Gautam 27
Advanced Operating System Memory Management
Present/absent bit: If present/absent bit is present, the virtual addresses is mapped to the
corresponding physical address. If present/absent is absent the trap is occur called page fault.
Protection bit: Tells what kinds of access are permitted read, write or read only.
Modified bit (dirty bit): Identifies the changed status of the page since last access; if it is
modified then it must be rewritten back to the disk.
Referenced bit: set whenever a page is referenced; used in page replacement.
Caching disabled: used for that system where the mapping into device register rather than
memory.
Frame number: The goal is to locate this value.
By Bishnu Gautam 29
Advanced Operating System Memory Management
Advantages/Disadvantages of Paging
Advantages:
Fast to allocate and free:
Allocate: keep free list of free pages, grab first page in
the list.
Free: Add pages to free list.
Easy to swap-out memory to disk.
Frame size matches disk page size.
Swap-out only necessary pages.
Easy to swap-in back from disk.
Disadvantages:
Additional memory reference.
Page table are kept in memory.
Internal fragmentation: process size does not match allocation
size.
By Bishnu Gautam 30
Advanced Operating System Memory Management
Segmentation
What happens if program increase their size in their execution?
How to manage expanding and contracting tables?
How to protect only data from the program?
How to share data to other program or functions?
The general solution of these issues is to provide the machine with many
completely independent address spaces, called segments.
Segmentation
Independent Logical Address
Segmentation Hardware
By Bishnu Gautam 33
Advanced Operating System Memory Management
Example of Segmentation
The segment number used as index into the segment table. The
offset d of the logical address must be between 0 and the segment
limit. If not ,trap occur, if it is legal it is added to the segment base
to produce the address in the physical memory.
By Bishnu Gautam 34
Advanced Operating System Memory Management
By Bishnu Gautam 35
Advanced Operating System Memory Management
By Bishnu Gautam 36
Advanced Operating System Memory Management
Multics:
It has 256K independent segments, and each up to 64K. The page
size is 1K or small.
By Bishnu Gautam 37
Advanced Operating System Memory Management
Home Works
HW #7
1. Problems 8.1 – 8.11, 8.14, 8.16, 8.18, 8.19, 8.21 & 8.22 from Textbook
2. How fragmentation occur? Discuss the techniques that manage the
fragmentation.
3. Why are page sizes always a power of 2?
4. On a simple paging system with 2^24 bytes of physical memory, 256
pages of logical address space, and a page size of 2^10 bytes, how many
bits are in a logical address?
5. Describe, how TLB increase performance in paging.
By Bishnu Gautam 38
Advanced Operating System Memory Management
By Bishnu Gautam 40
Advanced Operating System Memory Management
Demand Paging
Pages requested by CPU – Demand pages
With demand-paged virtual memory, pages are only loaded when they are
demanded during program execution; pages that are never accessed are thus
never loaded into physical memory.
Demand paging strategy:
• Less I/O needed, no unnecessary I/O
• Less memory needed
• Faster response
• More users
By Bishnu Gautam 41
Advanced Operating System Memory Management
Page Fault
What happens if the process tries to access a page that was not brought into memory?
Access to a page marked invalid causes a Page Fault. (1)
The paging hardware, in translating the address through the page table, will
notice that the invalid bit is set, causing a trap to the operating system.
2. Operating system looks at
another table to decide:
• Invalid reference abort
• Just not in memory
3. Get empty frame
4. Swap page into frame via
scheduled disk operation
5. Reset tables to indicate page
now in memory. Set validation
bit = v
6. Restart the instruction that
caused the page fault
Steps in handling a page fault
By Bishnu Gautam 42
Advanced Operating System Memory Management
Page Replacement
What Happens if There is no Free Frame?
By Bishnu Gautam 43
Advanced Operating System Memory Management
No of page fault = 15
Advantages: Easy to understand and program.
Distributes fair chance to all.
Problems: FIFO is likely to replace heavily (or constantly) used pages and they are
still needed for further processing.
By Bishnu Gautam 44
Advanced Operating System Memory Management
No of page fault: 9
No of page fault: 12
Advantages: Excellent, efficient is close to the optimal algorithm.
Problems: Difficult to implement exactly. How to find good heuristic?
If it is implemented as linked list, updating list in every reference is not a way making system
fast!
The Alternate implementation is by hardware primitives, it requires a time-of-use field in page
table and a logical clock or counter in the CPU.
By Bishnu Gautam 46
Advanced Operating System Memory Management
By Bishnu Gautam 47
Advanced Operating System Memory Management
By Bishnu Gautam 49
Advanced Operating System Memory Management
Home Works
HW#8:
1. Problem 9.1 – 9.5, 9.8 – 9.10 & 9.18 of Textbook
2. Under what circumstances do page fault occur? Describe the action taken by
the OS when a page fault occurs.
3. Given references to the following pages by a program,
1,2,3,4,2,1,5,6,2,1,2,3,7,6,3,2,1,2,3,6.
How many page faults will occur if the program has three page frames
available to it and uses:
a) FIFO replacement?
b) LRU replacement?
c) Optimal replacement?
(Remember that all frames are initially empty)
By Bishnu Gautam 51