unit 2 OS
unit 2 OS
Memory Management
What is Main Memory?
The main memory is central to the operation of a Modern Computer. Main Memory is a large
array of words or bytes, ranging in size from hundreds of thousands to billions. Main memory
is a repository of rapidly available information shared by the CPU and I/O devices. Main
memory is the place where programs and information are kept when the processor is
effectively utilizing them. Main memory is associated with the processor, so moving
instructions and information into and out of the processor is extremely fast. Main memory is
also known as RAM (Random Access Memory). This memory is volatile. RAM loses its data
when a power interruption occurs.
Main Memory
Address Binding :
The Association of program instruction and data to the actual physical memory locations is
called the Address Binding. Let’s consider the following example given below for better
understanding.
Consider a program P1 has the set of instruction such that I1, I2, I3, I4, and program counter
value is 10, 20, 30, 40 respectively.
Program P1
I1 --> 10
I2 --> 20
I3 --> 30
I4 --> 40
Note :
The majority of the Operating System practically implement dynamic loading, dynamic
linking, dynamic address binding. For example – Windows, Linux, Unix all popular OS.
Memory management keeps track of the status of each memory location, whether it is
allocated or free. It allocates the memory dynamically to the programs at their request and
frees it for reuse when it is no longer needed. Memory management meant to satisfy some
requirements that we should keep in mind.
These Requirements of memory management are:
1. Relocation – The available memory is generally shared among a number of
processes in a multiprogramming system, so it is not possible to know in advance
which other programs will be resident in main memory at the time of execution of
this program. Swapping the active processes in and out of the main memory
enables the operating system to have a larger pool of ready-to-execute process.
When a program gets swapped out to a disk memory, then it is not always possible
that when it is swapped back into main memory then it occupies the previous
memory location, since the location may still be occupied by another process. We
may need to relocate the process to a different area of memory. Thus there is a
possibility that program may be moved in main memory due to swapping.
The figure depicts a process image. The process image is occupying a continuous
region of main memory. The operating system will need to know many things
including the location of process control information, the execution stack, and the
code entry. Within a program, there are memory references in various instructions and
these are called logical addresses.
After loading of the program into main memory, the processor and the operating
system must be able to translate logical addresses into physical addresses. Branch
instructions contain the address of the next instruction to be executed. Data reference
instructions contain the address of byte or word of data referenced.
For example, multiple processes may use the same system file and it is natural to load one
copy of the file in main memory and let it be shared by those processes. It is the task of
Memory management to allow controlled access to the shared areas of memory without
compromising the protection. Mechanisms are used to support relocation supported sharing
capabilities.
Protection – There is always a danger when we have multiple programs at the same time
as one program may write to the address space of another program. So every process must be
protected against unwanted interference when other process tries to write in a process
whether accidental or incidental. Between relocation and protection requirement a trade-off
occurs as the satisfaction of relocation requirement increases the difficulty of satisfying the
protection requirement.
Prediction of the location of a program in main memory is not possible, that’s why it is
impossible to check the absolute address at compile time to assure protection. Most of the
programming language allows the dynamic calculation of address at run time. The memory
protection requirement must be satisfied by the processor rather than the operating system
because the operating system can hardly control a process when it occupies the processor.
Thus it is possible to check the validity of memory references.
Contiguous Memory Allocation
The main memory should accommodate both the operating system and the different client
processes. Therefore, the allocation of memory becomes an important task in the operating
system. The memory is usually divided into two partitions: one for the resident operating
system and one for the user processes. We normally need several user processes to reside in
memory simultaneously. Therefore, we need to consider how to allocate available memory to
the processes that are in the input queue waiting to be brought into memory. In adjacent
memory allotment, each process is contained in a single contiguous segment of memory.
Memory Allocation
To gain proper memory utilization, memory allocation must be allocated efficiently. One of
the simplest methods for allocating memory is to divide memory into several fixed-sized
partitions and each partition contains exactly one process. Thus, the degree of
multiprogramming is obtained by the number of partitions.
Multiple partition allocation: In this method, a process is selected from the input
queue and loaded into the free partition. When the process terminates, the partition
becomes available for other processes.
Fixed partition allocation: In this method, the operating system maintains a table
that indicates which parts of memory are available and which are occupied by
processes. Initially, all memory is available for user processes and is considered
one large block of available memory. This available memory is known as a
“Hole”. When the process arrives and needs memory, we search for a hole that is
large enough to store this process. If the requirement is fulfilled then we allocate
memory to process, otherwise keeping the rest available to satisfy future requests.
While allocating a memory sometimes dynamic storage allocation problems
occur, which concerns how to satisfy a request of size n from a list of free holes.
There are some solutions to this problem:
First Fit
In the First Fit, the first available free hole fulfil the requirement of the process allocated.
First Fit
Here, in this diagram, a 40 KB memory block is the first available free hole that can store
process A (size of 25 KB), because the first two blocks did not have sufficient memory space.
Best Fit
In the Best Fit, allocate the smallest hole that is big enough to process requirements. For this,
we search the entire list, unless the list is ordered by size.
Best Fit
Here in this example, first, we traverse the complete list and find the last hole 25KB is the
best suitable hole for Process A(size 25KB). In this method, memory utilization is maximum
as compared to other memory allocation techniques.
Worst Fit
In the Worst Fit, allocate the largest available hole to process. This method produces the
largest leftover hole.
Worst Fit
Here in this example, Process A (Size 25 KB) is allocated to the largest available memory
block which is 60KB. Inefficient memory utilization is a major issue in the worst fit.
Fragmentation
Fragmentation is defined as when the process is loaded and removed after execution from
memory, it creates a small free hole. These holes can not be assigned to new processes
because holes are not combined or do not fulfill the memory requirement of the process. To
achieve a degree of multiprogramming, we must reduce the waste of memory or
fragmentation problems. In the operating systems two types of fragmentation:
1. Internal fragmentation: Internal fragmentation occurs when memory blocks are
allocated to the process more than their requested size. Due to this some unused
space is left over and creating an internal fragmentation problem.Example:
Suppose there is a fixed partitioning used for memory allocation and the different
sizes of blocks 3MB, 6MB, and 7MB space in memory. Now a new process p4 of
size 2MB comes and demands a block of memory. It gets a memory block of 3MB
but 1MB block of memory is a waste, and it can not be allocated to other
processes too. This is called internal fragmentation.
2. External fragmentation: In External Fragmentation, we have a free memory block,
but we can not assign it to a process because blocks are not contiguous. Example:
Suppose (consider the above example) three processes p1, p2, and p3 come with
sizes 2MB, 4MB, and 7MB respectively. Now they get memory blocks of size
3MB, 6MB, and 7MB allocated respectively. After allocating the process p1
process and the p2 process left 1MB and 2MB. Suppose a new process p4 comes
and demands a 3MB block of memory, which is available, but we can not assign it
because free memory space is not contiguous. This is called external
fragmentation.
Both the first-fit and best-fit systems for memory allocation are affected by external
fragmentation. To overcome the external fragmentation problem Compaction is used. In the
compaction technique, all free memory space combines and makes one large block. So, this
space can be used by other processes effectively.
Another possible solution to the external fragmentation is to allow the logical address space
of the processes to be noncontiguous, thus permitting a process to be allocated physical
memory wherever the latter is available.
Paging
Paging is a memory management scheme that eliminates the need for a contiguous allocation
of physical memory. This scheme permits the physical address space of a process to be non-
contiguous.
Logical Address or Virtual Address (represented in bits): An address generated by
the CPU.
Logical Address Space or Virtual Address Space (represented in words or bytes):
The set of all logical addresses generated by a program.
Physical Address (represented in bits): An address actually available on a memory
unit.
Physical Address Space (represented in words or bytes): The set of all physical
addresses corresponding to the logical addresses.
Example:
If Logical Address = 31 bits, then Logical Address Space = 231 words = 2 G
words (1 G = 230)
If Logical Address Space = 128 M words = 27 * 220 words, then Logical Address
= log2 227 = 27 bits
If Physical Address = 22 bits, then Physical Address Space = 222 words = 4 M
words (1 M = 220)
If Physical Address Space = 16 M words = 24 * 220 words, then Physical Address
= log2 224 = 24 bits
The mapping from virtual to physical address is done by the memory management unit
(MMU) which is a hardware device and this mapping is known as the paging technique.
The Physical Address Space is conceptually divided into several fixed-size blocks,
called frames.
The Logical Address Space is also split into fixed-size blocks, called pages.
Page Size = Frame Size
Paging
The address generated by the CPU is divided into:
Page Number(p): Number of bits required to represent the pages in Logical
Address Space or Page number
Page Offset(d): Number of bits required to represent a particular word in a page
or page size of Logical Address Space or word number of a page or page offset.
The hardware implementation of the page table can be done by using dedicated registers. But
the usage of the register for the page table is satisfactory only if the page table is small. If the
page table contains a large number of entries then we can use TLB(translation Look-aside
buffer), a special, small, fast look-up hardware cache.
The TLB is an associative, high-speed memory.
Each entry in TLB consists of two parts: a tag and a value.
When this memory is used, then an item is compared with all tags simultaneously.
If the item is found, then the corresponding value is returned.
Segmentation
A process is divided into Segments. The chunks that a program is divided into which are not
necessarily all of the exact sizes are called segments. Segmentation gives the user’s view of
the process which paging does not provide. Here the user’s view is mapped to physical
memory.
Types of Segmentation in Operating System
Virtual Memory Segmentation: Each process is divided into a number of
segments, but the segmentation is not done all at once. This segmentation may or
may not take place at the run time of the program.
Simple Segmentation: Each process is divided into a number of segments, all of
which are loaded into memory at run time, though not necessarily contiguously.
Virtual Memory
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed
as though it were part of the main memory. The addresses a program may use to reference
memory are distinguished from the addresses the memory system uses to identify physical
storage sites and program-generated addresses are translated automatically to the
corresponding machine addresses.
The size of virtual storage is limited by the addressing scheme of the computer system and
the amount of secondary memory available not by the actual number of main storage
locations.
It is a technique that is implemented using both hardware and software. It maps memory
addresses used by a program, called virtual addresses, into physical addresses in computer
memory.
1. All memory references within a process are logical addresses that are dynamically
translated into physical addresses at run time. This means that a process can be
swapped in and out of the main memory such that it occupies different places in
the main memory at different times during the course of execution.
2. A process may be broken into a number of pieces and these pieces need not be
continuously located in the main memory during execution. The combination of
dynamic run-time address translation and the use of a page or segment table
permits this.
If these characteristics are present then, it is not necessary that all the pages or segments are
present in the main memory during execution. This means that the required pages need to be
loaded into memory whenever required. Virtual memory is implemented using Demand
Paging or Demand Segmentation.
Demand Paging
The process of loading the page into memory on demand (whenever a page fault occurs) is
known as demand paging. The process includes the following steps are as follows:
Demand Paging
1. If the CPU tries to refer to a page that is currently not available in the main
memory, it generates an interrupt indicating a memory access fault.
2. The OS puts the interrupted process in a blocking state. For the execution to
proceed the OS must bring the required page into the memory.
3. The OS will search for the required page in the logical address space.
4. The required page will be brought from logical address space to physical address
space. The page replacement algorithms are used for the decision-making of
replacing the page in physical address space.
5. The page table will be updated accordingly.
6. The signal will be sent to the CPU to continue the program execution and it will
place the process back into the ready state.
Hence whenever a page fault occurs these steps are followed by the operating system and the
required page is brought into memory.
Advantages of Virtual Memory
More processes may be maintained in the main memory: Because we are
going to load only some of the pages of any particular process, there is room for
more processes. This leads to more efficient utilization of the processor because it
is more likely that at least one of the more numerous processes will be in the ready
state at any particular time.
A process may be larger than all of the main memory: One of the most
fundamental restrictions in programming is lifted. A process larger than the main
memory can be executed because of demand paging. The OS itself loads pages of
a process in the main memory as required.
It allows greater multiprogramming levels by using less of the available (primary)
memory for each process.
It has twice the capacity for addresses as main memory.
It makes it possible to run more applications at once.
Users are spared from having to add memory modules when RAM space runs out,
and applications are liberated from shared memory management.
When only a portion of a program is required for execution, speed has increased.
Memory isolation has increased security.
It makes it possible for several larger applications to run at once.
Memory allocation is comparatively cheap.
It doesn’t require outside fragmentation.
It is efficient to manage logical partition workloads using the CPU.
Automatic data movement is possible.
Page Fault Service Time: The time taken to service the page fault is called page fault
service time. The page fault service time includes the time taken to perform all the above six
steps.
Let Main memory access time is: m
Page fault service time is: s
Page fault rate is : p
Then, Effective memory access time = (p*s) + (1-p)*m
In an operating system that uses paging for memory management, a page replacement
algorithm is needed to decide which page needs to be replaced when a new page comes in.
Page Fault: A page fault happens when a running program accesses a memory page that is
mapped into the virtual address space but not loaded in physical memory. Since actual
physical memory is much smaller than virtual memory, page faults happen. In case of a page
fault, Operating System might have to replace one of the existing pages with the newly
needed page. Different page replacement algorithms suggest different ways to decide which
page to replace. The target for all algorithms is to reduce the number of page faults.
Page Replacement Algorithms:
1. 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.
Example 1: Consider page reference string 1, 3, 0, 3, 5, 6, 3 with 3 page frames.Find the
number of page faults.
Initially, all slots are empty, so when 1, 3, 0 came they are allocated to the empty slots —> 3
Page Faults.
when 3 comes, it is already in memory so —> 0 Page Faults. Then 5 comes, it is not
available in memory so it replaces the oldest page slot i.e 1. —>1 Page Fault. 6 comes, it is
also not available in memory so it replaces the oldest page slot i.e 3 —>1 Page Fault.
Finally, when 3 come it is not available so it replaces 0 1 page fault.
Belady’s anomaly proves that it is possible to have more page faults when increasing the
number of page frames while using the First in First Out (FIFO) page replacement algorithm.
For example, if we consider reference strings 3, 2, 1, 0, 3, 2, 4, 3, 2, 1, 0, 4, and 3 slots, we
get 9 total page faults, but if we increase slots to 4, we get 10-page faults.
2. Optimal Page replacement: In this algorithm, pages are replaced which would not be
used for the longest duration of time in the future.
Example-2: Consider the page references 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frame. Find number of page fault.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already there so —> 0 Page fault. when 3 came it will take the place of 7 because it is
not used for the longest duration of time in the future.—>1 Page fault. 0 is already there so
—> 0 Page fault. 4 will takes place of 1 —> 1 Page Fault.
Now for the further page reference string —> 0 Page fault because they are already available
in the memory.
Optimal page replacement is perfect, but not possible in practice as the operating system
cannot know future requests. The use of Optimal Page replacement is to set up a benchmark
so that other replacement algorithms can be analyzed against it.
3. Least Recently Used: In this algorithm, page will be replaced which is least recently
used.
Example-3: Consider the page reference string 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 3 with 4 page
frames. Find number of page faults.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already their so —> 0 Page fault. when 3 came it will take the place of 7 because it is
least recently used —>1 Page fault
0 is already in memory so —> 0 Page fault.
4 will takes place of 1 —> 1 Page Fault
Now for the further page reference string —> 0 Page fault because they are already available
in the memory.
4. Most Recently Used (MRU): In this algorithm, page will be replaced which has been used
recently. Belady’s anomaly can occur in this algorithm.
Initially, all slots are empty, so when 7 0 1 2 are allocated to the empty slots —> 4 Page
faults
0 is already their so–> 0 page fault
when 3 comes it will take place of 0 because it is most recently used —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 4 comes it will take place of 0 —>1 Page fault
2 is already in memory so —> 0 Page fault
when 3 comes it will take place of 2 —>1 Page fault
when 0 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 0 —>1 Page fault
when 2 comes it will take place of 3 —>1 Page fault
when 3 comes it will take place of 2 —>1 Page fault