Unit -3
Memory Management strategies: Introduction, Swapping, Contiguous memory
allocation, Paging, Segmentation. Virtual Memory Management: Introduction, Demand
paging, copy on-write, Page replacement, Frame allocation, Thrashing, Memory-mapped
files, Kernel memory allocation.
Swapping:
● The system maintains a ready queue consisting of all processes whose memory images are
on the backing store or in memory and are ready to run.
● Whenever the CPU scheduler decides to execute a process, it calls the dispatcher.
● The dispatcher checks to see whether the next process in the queue is in memory.
● If it is not, and if there is no free memory region, the dispatcher swaps out a process currently
in memory and swaps in the desired process.
● A process with dynamic memory requirements will need to issue system calls (request
memory() and release memory()) to inform the operating system of its changing memory
needs.
Schematic View of Swapping
Contiguous memory allocation
● In Contiguous memory allocation which is a memory management technique, whenever
there is a request by the user process for the memory then a single section of the
contiguous memory block is given to that process according to its requirement.
● Contiguous Memory allocation is achieved just by dividing the memory into the fixed-sized
partition.
The memory can be divided either in the fixed-sized partition or in the variable-sized partition
in order to allocate contiguous space to user processes
Fixed Size Partition:
● In this scheme, the system divides the memory into fixed-size partitions.
● The partitions may or may not be the same size.
● The size of each partition is fixed as indicated by the name of the technique and it cannot be
changed.
● In this partition scheme, each partition may contain exactly one process.
● There is a problem that this technique will limit the degree of multiprogramming because
the number of partitions will basically decide the number of processes.
● Whenever any process terminates then the partition becomes available for another
process.
Variable Size Partition
● In this partitioning, scheme allocation is done dynamically.
● The size of the partition is not declared initially.
● Whenever any process arrives, a partition of size equal to the size of the process is created
and then allocated to the process.
● Thus the size of each partition is equal to the size of the process.
● As partition size varies according to the need of the process so in this partition scheme
there is no internal fragmentation.
Segmentation
● Segmentation is a memory management technique in which, the memory is divided into
the variable size parts.
● Each part is known as segment which can be allocated to a process.
● The details about each segment are stored in a table called as segment table.
● Segment table is stored in one (or many) of the segments.
Segment table contains mainly two information about segment:
1. Base: It is the base address of the segment
2. Limit: It is the length of the segment.
Segmentation Hardware
Limit Register: Specifies the size of the range
Base address: A base address is an address serving as a reference point
Offset : Denotes the number of address locations added to a base address in order to get to a
specific absolute address, sometimes called as relative address
Segmentation Hardware
● The Segment number is mapped to the segment table.
● The limit of the respective segment is compared with the offset.
● If the offset is less than the limit Register then the address is valid otherwise it throws an
error as the address is invalid.
● In the case of valid address, the base address of the segment is added to the offset to get
the physical address of actual word in the main memory.
Advantages of Segmentation
1. No internal fragmentation
2. Average Segment Size is larger than the actual page size.
3. Less overhead
4. It is easier to relocate segments than entire address space.
5. The segment table is of lesser size as compare to the page table in paging.
Paging
● The basic method for implementing paging involves breaking physical memory into fixed-
sized blocks called frames and breaking logical memory into blocks of the same size called
pages.
● When a process is to be executed, its pages are loaded into any available memory frames
from their source (a file system or the backing store).
● The backing store is divided into fixed-sized blocks that are the same size as the memory
frames or clusters of multiple frames.
● For example, the logical address space is now totally separate from the physical address
space, so, if a process can have a logical 64-bit address space even though the system has
less than 264 bytes of physical memory(18446744073709551616) size.
● Every address generated by the CPU is divided into two parts: a page number (p) and a page
offset (d).
● The page number is used as an index into a page table.
● The page table contains the base address of each page in physical memory.
● This base address is combined with the page offset to define the physical memory address
that is sent to the memory unit.
• If the size of the logical address space is 2m, and a page size is 2n bytes, then the high-order
m − n bits of a logical address designate the page number, and the n low-order bits
designate the page offset. Thus, the logical address is as follows:
Where p is an index into the page table and d is the displacement within the page.
Paging Example
● Let us consider the main memory size 16 Kb and Frame size is 1 KB therefore the main
memory will be divided into the collection of 16 frames of 1 KB each.
● There are 4 processes in the system that is P1, P2, P3 and P4 of 4 KB each. Each process is
divided into pages of 1 KB each so that one page can be stored in one frame.
● Initially, all the frames are empty therefore pages of the processes will get stored in the
contiguous way.
● Let us consider that, P2 and P4 are moved to waiting state after some time. Now, 8 frames
become empty and therefore other pages can be loaded in that empty place.
● The process P5 of size 8 KB (8 pages) is waiting inside the ready queue.
● We have 8 noncontiguous frames available in the memory and paging provides the
flexibility of storing the process at the different places. Therefore, we can load the pages of
process P5 in the place of P2 and P4.
Paging Example
Logical address, n = 2 and m = 4
Page size =4 bytes, 1 byte = 8 bits, 24=16 bytes, ie 0 – 15 bytes shown in logical memory.
Physical memory is of 32 bytes (page size * no of bits)(4 * 8) = 32 bytes, ie it will be divided
into 8 pages)ranging from 0 to 32 bytes, with equivalent partitions from
0,4,8,12,16,20,24,28.
Paging Hardware Support
● The hardware implementation of the page table can be done in several ways.
● In the simplest case, the page table is implemented as a set of dedicated registers.
● These registers should be built with very high-speed logic to make the paging-address
translation efficient.
● The page table thus consists of eight entries that are kept in fast registers.
● The use of registers for the page table is satisfactory if the page table is reasonably small (for
example, 256 entries – 2 8 = 256).
● Most contemporary computers (of some generation), however, allow the page table to be
very large (for example, 1 million entries).
● For these machines, they use fast registers to implement the page table is not feasible.
● Rather, the page table is kept in main memory, and a page-table base register (PTBR) points
to the page table.
● Changing page tables requires changing only this one register, substantially reducing
context-switch time.
● The standard solution to this problem is to use a special, small, fast lookup hardware cache
called a translation look-aside buffer (TLB).
● The TLB is associative, high-speed memory.
● Each entry in the TLB consists of two parts: a key (or tag) and a value.
● When the associative memory is presented with an item, the item is compared with all keys
simultaneously.
● If the item is found, the corresponding value field is returned.
● The search is fast; a TLB lookup in modern hardware is part of the instruction pipeline,
essentially adding no performance penalty.
● The TLB is used with page tables in the following way.
● The TLB contains only a few of the page-table entries.
● When a logical address is generated by the CPU, its page number is presented to the TLB.
● If the page number is found, its frame number is immediately available and is used to access
memory. (repeated until found)
● If the page number is not in the TLB (known as a TLB miss), a memory reference to the page
table must be made.
● Depending on the CPU, this may be done automatically in hardware or via an interrupt to
the operating system.
● When the frame number is obtained, we can use it to access memory (Figure 8.14).
● In addition, we add the page number and frame number to the TLB, so that they will be
found quickly on the next reference.
● If the TLB is already full of entries, an existing entry must be selected for replacement.
● Replacement policies range from least recently used (LRU) through round-robin to
random.
Protection
● Memory protection in a paged environment is accomplished by protection bits associated
with each frame.
● Normally, these bits are kept in the page table.
● One bit can define a page to be read –write or read-only.
● Every reference to memory goes through the page table to find the correct frame number.
● At the same time that the physical address is being computed, the protection bits can be
checked to verify that no writes are being made to a read-only page.
● An attempt to write to a read-only page causes a hardware trap to the operating system (or
memory-protection violation).
● We can create hardware to provide read-only, read –write, or execute-only protection; or,
by providing separate protection bits for each kind of access.
● One additional bit is generally attached to each entry in the page table: a valid – invalid bit.
● When this bit is set to valid, the associated page is in the process’s logical address space
and is thus a legal (or valid) page.
● When the bit is set to invalid, the page is not in the process’s logical address space. Illegal
addresses are trapped by use of the valid –invalid bit.
Structure of a Page Table
● Multilevel Paging is a paging scheme which consist of two or more levels of page tables in a
hierarchical manner. It is also known as hierarchical paging.
● The entries of the level 1 page table are pointers to a level 2 page table and entries of the
level 2 page tables are pointers to a level 3 page table and so on.
● The entries of the last level page table are stores actual frame information. Level 1 contain
single page table and address of that table is stored in PTBR (Page Table Base Register).
● In multilevel paging whatever may be levels of paging all the page tables will be stored in
main memory.
● So it requires more than one memory access to get the physical address of page frame.
● One access for each level needed.
● Each page table entry except the last level page table entry contains base address of the
next level page table.
Virtual Memory
● A computer can address more memory than the amount physically installed on the system.
● This extra memory is actually called virtual memory and it is a section of a hard disk that's
set up to emulate the computer's RAM.
● The main visible advantage of this scheme is that programs can be larger than physical
memory.
● Virtual memory serves two purposes. First, it allows us to extend the use of physical
memory by using disk.
● Second, it allows us to have memory protection, because each virtual address is translated
to a physical address.
Working
● In modern word, virtual memory has become quite common these days.
● In this scheme, whenever some pages needs to be loaded in the main memory for the
execution and the memory is not available for those many pages, then in that case, instead
of stopping the pages from entering in the main memory, the OS search for the RAM area
that are least used in the recent times or that are not referenced and copy that into the
secondary memory to make the space for the new pages in the main memory.
● Since all this procedure happens automatically, therefore it makes the computer feel like it
is having the unlimited RAM.
Advantages and Disadvantages
Advantages Dis Advantages
The degree of Multiprogramming will be The system becomes slower since swapping takes
increased. time.
User can run large application with less real RAM. It takes more time in switching between
applications.
There is no need to buy more memory RAMs. The user will have the lesser hard disk space for its
use.
Demand Paging
● According to the concept of Virtual Memory, in order to execute some process, only a part
of the process needs to be present in the main memory, which means that only a few pages
will only be present in the main memory at any time.
● However, deciding, which pages need to be kept in the main memory and which need to be
kept in the secondary memory, is going to be difficult because we cannot say in advance
that a process will require a particular page at particular time.
● Therefore, to overcome this problem, there is a concept called Demand Paging is
introduced or Lazy Evaluation or Lazy Swapper”.
● It similar to the Paging.
● It suggests keeping all pages of the frames in the secondary memory until they are
required.
● In other words, it says that do not load any page in the main memory until it is required.
● Whenever any page is referred for the first time in the main memory, then that page will be
found in the secondary memory.
● After that, it may or may not be present in the main memory depending upon the page
replacement algorithm.
Demand Paging Working
● Demand paging system is totally depend on the page table(p,f) implementation because
page table helps to maps logical memory to physical memory.
● Bitwise operators are implemented in the page table to indication that pages are valid or
invalid.
● All valid pages are existed into Primary Memory, and other side invalid pages are existed
into Secondary Memory.
● Now all process go ahead to access all pages, then some things will be happened. Such as –
• Attempt to currently access page.
• If page is ok (Valid), then all processing instructions work as normal.
• If, any one page is found as invalid, then page- fault issue is arise.
• Now memory reference is determined that valid reference is existed on the
auxiliary memory (secondary storage areas) or not. If not existed, then process is
terminated, otherwise needed pages are paged in.
• Now disk operations are implemented to fetch the required page into Primary
Memory.
Demand Paging Working... Page Fault
● Access to a page marked invalid causes a page fault.
● The paging hardware, in translating the address through the page table, will notice that the
invalid bit(i) is set, causing a trap to the operating system.
● This trap is the result of the operating system’s failure to bring the desired page into
memory.
Procedure
○ We check an internal table (usually kept with the process control block) for this
process to determine whether the reference was a valid or an invalid memory
access.
○ If the reference was invalid, we terminate the process. If it was valid but we have
not yet brought in that page, we now page it in.
○ We find a free frame (by taking one from the free-frame list, for example).
○ We schedule a disk operation to read the desired page into the newly allocated
frame. When the disk read is complete, we modify the internal table kept with the
process and the page table to indicate that the page is now in memory.
○ We restart the instruction that was interrupted by the trap. The process can now
access the page as though it had always been in memory
Copy on Write
● Copy on Write or simply COW is a resource management technique.
● One of its main use is the implementation of the fork system call in which it shares the
virtual memory(pages) of the OS.
● In UNIX like OS, fork() system call creates a duplicate process of the parent process which is
called as the child process.
Working
The idea behind a copy-on-write is that when a parent process creates a child process then both of
these processes initially will share the same pages in memory, and these shared pages will be
marked as copy-on-write which means that if any of these processes will try to modify the shared
pages then only a copy of these pages will be created and the modifications will be done on the
copy of pages by that process and thus not affecting the other process.
Page Replacement
● The Page replacement algorithm decides which memory page is to be replaced. The
process of replacement is sometimes called swap out or write to disk.
● Page replacement is done when the requested page is not found in the main memory (page
fault).
● There are two main aspects of virtual memory, Frame allocation and Page Replacement.
● Frame Allocation: How many frames are to be allocated to the process.
● Page Replacement: Which page is to be replaced.
Types of Page Replacement Algorithms
1. Optimal Page Replacement algorithm → this algorithms replaces the page which will not
be referred for so long in future.
2. Least recent used (LRU) page replacement algorithm → this algorithm replaces the page
which has not been referred for a long time.
3. FIFO → in this algorithm, a queue is maintained. The page which is assigned the frame first
will be replaced first.
First in First Out (FIFO)
● This is the simplest page replacement algorithm.
● In this algorithm, the operating system keeps track of all pages in the memory in a queue,
the oldest page is in the front of the queue.
● When a page needs to be replaced page in the front of the queue is selected for removal.
For Solving the Problems they will provide a reference string and the number of frames, for example
it will be in the following mode
Reference string : 1,3,0,3,5,6,3
Page frames : 3
Page Replacement(LRU)
Page Replacement (Optimal)
Allocation of Frames
● An important aspect of operating systems, virtual memory is implemented using demand
paging.
● Demand paging necessitates the development of a page replacement and a frame
allocation algorithm.
● Frame allocation algorithms are used if you have multiple processes; it helps decide how
many frames to allocate to each process.
Strategies for the allocation of frames
● You cannot allocate more than the total number of available frames.
● At least a minimum number of frames should be allocated to each process.
Allocation Algorithms
1. Equal Allocation: The easiest way to split m frames among n processes is to give everyone
an equal share, m/n frames. For instance, if there are 93 frames and 5 processes, each
process will get 18 frames (93/5). The 3 leftover frames can be used as a free-frame buffer
pool. This scheme is called equal allocation.
2. Proportional Allocation: We allocate available memory to each process according to its size. Let
the size of the virtual memory for process pi be si, and define S = ∑si, if the total number of
available frames is m, we allocate ai frames to process pi, where ai is approximately ai = si/S × m.
We must adjust each ai to be an integer that is greater than the minimum number of frames
required by the instruction set, with a sum not exceeding m.
Global vs Local Allocation
The number of frames allocated to a process can also dynamically change depending on whether
you have used global replacement or local replacement for replacing pages in case of a page fault.
● Local replacement: When a process needs a page which is not in the memory, it can bring
in the new page and allocate it a frame from its own set of allocated frames only.
● Global replacement: When a process needs a page which is not in the memory, it can bring
in the new page and allocate it a frame from the set of all frames, even if that frame is
currently allocated to some other process; that is, one process can take a frame from
another.
Thrashing
● Thrashing is a state in which the CPU performs 'productive' work less, and 'swapping' more.
● The CPU is busy in swapping pages so much that it can not respond to users' programs and
interrupts as much as required.
● Thrashing occurs when there are too many pages in memory, and each page refers to
another page.
Techniques to Handle Thrashing
● Working Set Model
● Page Fault Frequency
Working Set Model
● The working-set model is based on the assumption of locality(Local Replacement).
● This model uses a parameter,⌂ , to define the working-set window.(WS)
● The idea is to examine the most recent ⌂ page references.
● The set of pages in the most recent ⌂ page references is the working set .
● If a page is in active use, it will be in the working set.
● If it is no longer being used, it will drop from the working set ⌂ time units after its last
reference.
● Thus, the working set is an approximation of the program’s locality.
Page Fault Frequency
● The working-set model is successful, and knowledge of the working set can be useful for
pre-paging ,but it seems a clumsy way to control thrashing.
● A strategy that uses the page-fault frequency (PFF) takes a more direct approach.
● Thrashing has a high page-fault rate. Thus, we want to control the page-fault rate.
● When it is too high, we know that the process needs more frames.
● Conversely, if the page-fault rate is too low, then the process may have too many frames.
● We can establish upper and lower bounds on the desired page-fault rate .
● If the actual page-fault rate exceeds the upper limit, we allocate that process to another
frame.
● If the page-fault rate falls below the lower limit, we remove a frame from the process.
● Thus, we can directly measure and control the page-fault rate to prevent thrashing.
Memory Mapped Files
• Memory mapping refers to process ability to access files on disk the same way it accesses
dynamic memory.
• It is obvious that accessing RAM is much faster than accessing disk via read and write
system calls.
• This technique saves user applications IO overhead (excess or indirect computation) and
buffering.
• Memory mapping a file is accomplished by mapping a disk block to a page (or pages) in
memory.
• Initial access to the file proceeds through ordinary demand paging, resulting in a page fault.
• Subsequent reads and writes to the file are handled as routine memory accesses using
read() and write().
• Note that write() to the file mapped in memory are not necessarily immediate
(synchronous) writes to the file on disk.
• Some systems may choose to update the physical file when the operating system
periodically checks whether the page in memory has been modified.
• When the file is closed, all the memory mapped data are written back to disk and removed
from the virtual memory of the process.
• Some operating systems provide memory mapping only through a specific system call and
use the standard system calls to perform all other file I/O.
• However, some systems choose to memory-map a file regardless of whether the file was
specified as memory-mapped.
● Multiple processes may be allowed to map the same file concurrently, to allow sharing of
data.
● Writes by any of the processes modify the data in virtual memory and can be seen by all
others that map the same section of the file.
● The memory-mapping mmap() system calls can also support copy-on-write functionality,
allowing processes to share a file in read-only mode but to have their own copies of any data
they modify.
● So that access to the shared data is coordinated, the processes involved might use one of
the mechanisms for achieving mutual exclusion.
● The memory-mapped file serves as the region of shared memory between the
communicating processes
Allocating Kernel Memory
● Generally the Main Memory, is divided into two parts (i). Operating System and (ii). User
Space.
● The operating system is the system which can also be called as Kernel, in which it contains
many device driver programs ,etc.
● For that kernel we need to allocate the memory.
● For that we have two approaches
(i). Buddy System
(ii.) Slab Allocation
Buddy System
● The Buddy System uses power of 2 Allocator
● Memory is always allocated as a power of 2 ( 4K, 8K, 16K, etc )
● If a block of the correct size is not currently available, then one is formed by splitting the
next larger block in two, forming two matched buddies.
● Free lists are maintained for every size block.
● If the necessary block size is not available upon request, a free block from the next largest
size is split into two buddies of the desired size.
Slab Allocation
● A second strategy for allocating kernel memory is known as slab allocation.
● It eliminates fragmentation caused by allocations and deallocations.
● This method is used to retain allocated memory that contains a data object of a certain type
for reuse upon subsequent allocations of objects of the same type.
● In slab allocation memory chunks suitable to fit data objects of certain type or size are pre-
allocated.
● Cache does not free the space immediately after use although it keeps track of data which
are required frequently so that whenever request is made the data will reach very fast.
● Slab – A slab is made up of one or more physically contiguous pages. The slab is the actual
container of data associated with objects of the specific kind of the containing cache.
● Cache – Cache represents a small amount of very fast memory. A cache consists of one or
more slabs. There is a single cache for each unique kernel data structure.
Implementation
• The slab allocation algorithm uses caches to store kernel objects.
• When a cache is created a number of objects which are initially marked as free are allocated
to the cache.
• The number of objects in the cache depends on size of the associated slab.
• Initially all objects in the cache are marked as free.
• When a new object for a kernel data structure is needed, the allocator can assign any free
object from the cache to satisfy the request.
• The object assigned from the cache is marked as used.
In linux, a slab may in one of three possible states:
• Full – All objects in the slab are marked as used
• Empty – All objects in the slab are marked as free
• Partial – The slab consists of both
• The slab allocator first attempts to satisfy the request with a free object in a partial slab.
• If none exists, a free object is assigned from an empty slab.
• If no empty slabs are available, a new slab is allocated from contiguous physical pages and
assigned to a cache.