Chapter 9
Chapter 9
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
Chapter-9 1
Contiguous Allocation
Main memory must support both OS and user processes
Resident operating system, usually held in low memory with interrupt vector
• Relocation registers used to protect user processes from each other, and from
changing operating-system code and data
Can then allow actions such as kernel code being transient and kernel
changing size
Chapter-9 2
Variable Partition
• Multiple-partition allocation
Best-fit: Allocate the smallest hole that is big enough; must search entire list,
unless ordered by size
Worst-fit: Allocate the largest hole; must also search entire list
First-fit and best-fit better than worst-fit in terms of speed and storage utilization
Fragmentation
External Fragmentation – total memory space exists to satisfy a request, but it
is not contiguous
First fit analysis reveals that given N blocks allocated, 0.5 N blocks lost to
fragmentation
Chapter-9 3
• 1/3 may be unusable -> 50-percent rule
Shuffle memory contents to place all free memory together in one large block
I/O problem
Paging
• Physical address space of a process can be noncontiguous; process is allocated
physical memory whenever the latter is available
To run a program of size N pages, need to find N free frames and load program
Chapter-9 4
Chapter-9 5
Paging -- Calculating internal
fragmentation
Page size = 2,048 bytes
Chapter-9 6
Implementation of Page Table
• Page table is kept in main memory
On a TLB miss, value is loaded into the TLB for faster access next time
Chapter-9 7
Effective Access Time
Hit ratio – percentage of times that a page number is found in the TLB
An 80% hit ratio means that we find the desired page number in the TLB 80% of
the time.
Memory Protection
• Memory protection implemented by associating protection bit with each frame to
indicate if read-only or read-write access is allowed
“valid” indicates that the associated page is in the process’ logical address
space, and is thus a legal page
“invalid” indicates that the page is not in the process’ logical address space
Chapter-9 8
Shared Pages
• Shared code
One copy of read-only (reentrant) code shared among processes (i.e., text
editors, compilers, window systems)
The pages for the private code and data can appear anywhere in the logical
address space
Chapter-9 9
Structure of the Page Table
• Memory structures for paging can get huge using straight-forward methods
If each entry is 4 bytes > each process 4 MB of physical address space for
the page table alone
• One simple solution is to divide the page table into smaller units
Hierarchical Paging
Chapter-9 10
Chapter-9 11
Hashed Page Tables
Chapter-9 12
Common in address spaces > 32 bits
Each element contains (1) the virtual page number (2) the value of the mapped
page frame (3) a pointer to the next element
Virtual page numbers are compared in this chain searching for a match
• If a match is found, the corresponding physical frame is extracted
Similar to hashed but each entry refers to several pages (such as 16) rather
than 1
Especially useful for sparse address spaces (where memory references are
non-contiguous and scattered)
Entry consists of the virtual address of the page stored in that real memory
location, with information about the process that owns that page
Chapter-9 13
Decreases memory needed to store each page table, but increases time needed
to search the table when a page reference occurs
Use hash table to limit the search to one — or at most a few — page-table
entries
• TLB can accelerate access
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
Chapter-9 14
Major part of swap time is transfer time; total transfer time is directly proportional
to the amount of memory swapped
Does the swapped out process need to swap back in to same physical
addresses?
• Modified versions of swapping are found on many systems (i.e., UNIX, Linux, and
Windows)
Chapter-9 15
If next processes to be put on CPU is not in memory, need to swap out a
process and swap in target process
• 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()
• Other constraints as well on swapping
• Pending I/O – can’t swap out as I/O would occur to wrong process
• Or always transfer I/O to kernel space, then to I/O device
Chapter-9 16
• Android terminates apps if low free memory, but first writes application
state to flash for fast restart
Chapter-9 17