You are on page 1of 22

CS211 OS

March 2-11 (SP-21)


B.Tech (CS)
Memory Management
• Memory management is the functionality of an operating
system which handles or manages primary memory and moves
processes back and forth between main memory and disk
during execution.
• Memory management keeps track of each and every memory
location, regardless of either it is allocated to some process or it
is free.
• It checks how much memory is to be allocated to processes. It
decides which process will get memory at what time.
• It tracks whenever some memory gets freed or unallocated and
correspondingly it updates the status.
Main Memory

• Operating system occupies a


fixed portion
• Rest of the space is available
for user processes
• Process address space consists
of its PCB, program, data and
stack.
Address Binding
Compile Time:
• If it is known in advance that where the process will be placed in memory then
absolute code can be generated at the time of compilation.
• If at some later time starting location changes then it will be necessary to
recompile this code.
Load Time:
• If it is not known at compile time that at which particular location the process will
reside in memory when the compiler will generate relocatable code to find the
address.
• In this case final binding is delayed until load time.
• In this case absolute address will be generated by the loader at the load time
Execution Time (Run Time):
• If the process can be moved during its execution from one memory segment to
another then address binding must be performed at run time.
• A special hardware-MMU (Memory Management Unit) is used to
generate physical addresses.
Protection and Relocation
Memory Partitioning Scheme
• In this type of allocation, main memory is divided into a
number of partitions where each partition should contain
only one process.
• When a partition is free, a process is selected from the input
queue and is loaded into the free partition.
• When the process terminates, the partition becomes available
for another process.
Multiple-partition Allocation
Multiple Fixed Partitioning (MFT)
• Fixed-sized partitions of same or
different sizes.

Internal fragmentation
• Memory block assigned to process is
bigger.
• Some portion of memory is left
unused, as it cannot be used by another
process.
Multiple Variable Partitioning (MVT)
• Initially all memory space is available as one large block.
• When a process needs memory a partition is created equal to
the process size and is allocated to the process.
• The partition space becomes free when the process
completes.
• Adjacent free spaces are merged together.
Fragmentation
External fragmentation
• Total memory space is enough to satisfy a request or to reside
a process in it, but it is not contiguous, so it cannot be used.
• As processes are loaded and removed from memory, the free
memory space is broken into little pieces.
• It happens after sometimes that processes cannot be
allocated to memory blocks considering their small size and
free space remains unused.
Placement Algorithms
If there is more than one free block of memory of sufficient size,
then the operating system must decide which free block to allocate
by using three different placement algorithms-
• Best-fit:- It chooses the block, that is closest in size to the given
request from the beginning to the ending free blocks. This strategy
produces the smallest left over hole.
• First-fit:- It begins to scan memory from the beginning and
chooses the first available block which is large enough. Searching
can start either at the beginning of the set of blocks or where the
previous first-fit search ended. We can stop searching as soon as
we find a free block that is large enough.
• Worst-fit:- It allocates the largest block. This strategy produces the
largest leftover hole, which may be more useful than the smaller
leftover hole from a best-fit approach.
Paging
The basic process of paging takes place in the following way:
• Processes are divided up into fixed page sizes.
• These pages are assigned to frames (blocks of memory).
• The operating system manages allocation and de-allocation of
pages in memory as required by the process.
• Pages are not necessarily contiguously assigned, they can be
anywhere in memory.
• Page size is power of two.
• The size of the process is measured in the number of pages.
An example to illustrate division of
memory space into frames
Memory Size – 256Bytes, Page Frame size- 4 Bytes
Memory Locations Data
Frame 1
000000 00 a
000000 01 b
000000 10 c
000000 11 d
Frame 2
000001 00 e
000001 01 f
000001 10 g
000001 11 h
Addresses
• Page address is called logical address and represented by
page number and the offset.
• Logical Address = Page number + Page offset
• Frame address is called physical address and represented by a
frame number and the offset.
• Physical Address = Frame number + Page offset
• A data structure called page table is used to keep track of the
relation between a page of a process to a frame in physical
memory.
• For given logical address space 2m and page size 2n
Logical Address is represented as- Page number Page offset
p d
Address Translation
Practice Assignment
Main-memory = 232 B = 4 GB ƒ
Process memory = 512 MB ƒ
Page size = 4K ƒ

Compute
1. Total number of frames in memory?
2. Total number of pages in the process?
3. Number of bits required for offset and page number?
Page Table Hardware Support (TLB)
Inverted Page Table
Practice Assignment
• Logical address space- 4GB
• Page size- 4 KB
• Size of 1 entry in page table- 32bits
• Compute
a. Maximum number of entries in one page
b. Total number of pages in the logical address space
c. Number of pages required for the page table
d. Number of pages left for processes
Solution
• Maximum no. of entries in one page = 210
• Total no. of pages = 232 / 212 = 220
• No. of pages for page table = 220 / 210 = 210
• No. of pages for processes = 220 - 210
Multi-level Paging

You might also like