Class Notes
UNIT - 3
Logical and Physical Address in Operating
System
A logical address is generated by the CPU while a program is running. The
logical address is a virtual address as it does not exist physically, therefore, it
is also known as a Virtual Address. The physical address describes the
precise position of necessary data in a memory. Before they are used, the
MMU must map the logical address to the physical address. In operating
systems, logical and physical addresses are used to manage and access
memory. Here is an overview of each in detail.
2
What is a Logical Address?
A logical address, also known as a virtual address, is an address generated by
the CPU during program execution. It is the address seen by the process and
is relative to the program’s address space. The process accesses memory
using logical addresses, which are translated by the operating system into
physical addresses. An address that is created by the CPU while a program is
running is known as a logical address. Because the logical address is virtual—
that is, it doesn’t exist physically—it is also referred to as such. The CPU uses
this address as a reference to go to the actual memory location. All logical
addresses created from a program’s perspective are referred to as being in
the “logical address space”. This address is used as a reference to access the
physical memory location by CPU. The term Logical Address Space is used
for the set of all logical addresses generated by a program’s perspective.
What is a Physical Address?
A physical address is the actual address in the main memory where data is
stored. It is a location in physical memory, as opposed to a virtual address.
Physical addresses are used by the Memory Management Unit (MMU) to
translate logical addresses into physical addresses. The user must use the
corresponding logical address to go to the physical address rather than
directly accessing the physical address. For a computer program to function,
physical memory space is required. Therefore, the logical address and
physical address need to be mapped before the program is run.
The term “physical address” describes the precise position of necessary data
in a memory. Before they are used, the MMU must map the logical address to
3
the physical address. This is because the user program creates the logical
address and believes that the program is operating in this logical address.
However, the program requires physical memory to execute. All physical
addresses that match the logical addresses in a logical address space are
collectively referred to as the “physical address space”
The translation from logical to physical addresses is performed by the
operating system’s memory management unit (MMU) within the computer’s
hardware architecture. The MMU uses a page table to translate logical
addresses into physical addresses. The page table maps each logical page
number to a physical frame number. While the operating system plan this
process, it’s important to note that the MMU itself is a hardware component
separate from the software-based elements of the operating system.
Similarities Between Logical and Physical Addresses in the Operating
System
● Both logical and physical addresses are used to identify a specific
location in memory.
● Both logical and physical addresses can be represented in different
formats, such as binary, hexadecimal, or decimal.
● Both logical and physical addresses have a finite range, which is
determined by the number of bits used to represent them.
4
Important Points about Logical and Physical Addresses in Operating
Systems
● The use of logical addresses provides a layer of abstraction that
allows processes to access memory without knowing the physical
memory location.
● Logical addresses are mapped to physical addresses using a page
table. The page table contains information about the mapping
between logical and physical addresses.
● The MMU translates logical addresses into physical addresses using
the page table. This translation is transparent to the process and is
performed by hardware.
● The use of logical and physical addresses allows the operating
system to manage memory more efficiently by using techniques
such as paging and segmentation.
5
What is Memory Management Unit ?
The physical hardware of a computer that manages its virtual memory and
caching functions is called the memory management unit (MMU). The MMU is
sometimes housed in a separate Integrated Chip (IC), but it is typically found
inside the central processing unit (CPU) of the computer. The MMU receives
all inputs for data requests and decides whether to retrieve the data from
ROM or RAM storage.
6
Difference Between Logical address and Physical
Address
Parameter LOGICAL ADDRESS PHYSICAL ADDRESS
Basic generated by CPU location in a memory unit
Physical Address is set of
Logical Address Space is
all physical addresses
Address set of all logical addresses
mapped to the
Space generated by CPU in
corresponding logical
reference to a program.
addresses.
User can never view
User can view the logical
Visibility physical address of
address of a program.
program.
Generation generated by the CPU Computed by MMU
7
The user can use the The user can indirectly
Access logical address to access access physical address
the physical address. but not directly.
Logical address can be Physical address will not
Editable
change. change.
Also called virtual address. real address.
Segmentation in Operating System
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 Systems
● Virtual Memory Segmentation: Each process is divided into a
number of segments, but the segmentation is not done all at once.
8
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.
There is no simple relationship between logical addresses and physical
addresses in segmentation. A table stores the information about all such
segments and is called Segment Table.
What is Segment Table?
It maps a two-dimensional Logical address into a one-dimensional Physical
address. It’s each table entry has:
● Base Address: It contains the starting physical address where the
segments reside in memory.
● Segment Limit: Also known as segment offset. It specifies the length
of the segment.
The address generated by the CPU is divided into:
● Segment number (s): Number of bits required to represent the
segment.
● Segment offset (d): Number of bits required to represent the position
of data within a segment.
●
9
Advantages of Segmentation in Operating System
● Reduced Internal Fragmentation : Segmentation can reduce internal
fragmentation compared to fixed-size paging, as segments can be
sized according to the actual needs of a process. However, internal
fragmentation can still occur if a segment is allocated more space
than it is actually used.
● Segment Table consumes less space in comparison to Page table in
paging.
● As a complete module is loaded all at once, segmentation improves
CPU utilization.
● The user’s perception of physical memory is quite similar to
segmentation. Users can divide user programs into modules via
segmentation. These modules are nothing more than separate
processes’ codes.
● The user specifies the segment size, whereas, in paging, the
hardware determines the page size.
● Segmentation is a method that can be used to segregate data from
security operations.
● Flexibility: Segmentation provides a higher degree of flexibility than
paging. Segments can be of variable size, and processes can be
designed to have multiple segments, allowing for more fine-grained
memory allocation.
● Sharing: Segmentation allows for sharing of memory segments
between processes. This can be useful for inter-process
communication or for sharing code libraries.
10
● Protection: Segmentation provides a level of protection between
segments, preventing one process from accessing or modifying
another process’s memory segment. This can help increase the
security and stability of the system.
Disadvantages of Segmentation in Operating System
● External Fragmentation : As processes are loaded and removed from
memory, the free memory space is broken into little pieces, causing
external fragmentation. This is a notable difference from paging,
where external fragmentation is significantly lesser.
● Overhead is associated with keeping a segment table for each
activity.
● Due to the need for two memory accesses, one for the segment table
and the other for main memory, access time to retrieve the
instruction increases.
● Fragmentation: As mentioned, segmentation can lead to external
fragmentation as memory becomes divided into smaller segments.
This can lead to wasted memory and decreased performance.
● Overhead: Using a segment table can increase overhead and reduce
performance. Each segment table entry requires additional memory,
and accessing the table to retrieve memory locations can increase
the time needed for memory operations.
● Complexity: Segmentation can be more complex to implement and
manage than paging. In particular, managing multiple segments per
11
process can be challenging, and the potential for segmentation
faults can increase as a result.
Segmentation in Operating System
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 Systems
● 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.
There is no simple relationship between logical addresses and physical
addresses in segmentation. A table stores the information about all such
segments and is called Segment Table.
12
What is Segment Table?
It maps a two-dimensional Logical address into a one-dimensional Physical
address. It’s each table entry has:
● Base Address: It contains the starting physical address where the
segments reside in memory.
● Segment Limit: Also known as segment offset. It specifies the
length of the segment.
Segmentation is crucial in efficient memory management within an operating
system. For an in-depth understanding of memory management and other
critical OS topics, explore the
Translation of Two-dimensional Logical Address to Dimensional Physical
Address.
The address generated by the CPU is divided into:
● Segment number (s): Number of bits required to represent the
segment.
● Segment offset (d): Number of bits required to represent the
position of data within a segment.
13
Advantages of Segmentation in Operating System
● Reduced Internal Fragmentation : Segmentation can reduce
internal fragmentation compared to fixed-size paging, as segments
can be sized according to the actual needs of a process. However,
internal fragmentation can still occur if a segment is allocated more
space than it is actually used.
● Segment Table consumes less space in comparison to Page table in
paging.
● As a complete module is loaded all at once, segmentation improves
CPU utilization.
● The user’s perception of physical memory is quite similar to
segmentation. Users can divide user programs into modules via
segmentation. These modules are nothing more than separate
processes’ codes.
● The user specifies the segment size, whereas, in paging, the
hardware determines the page size.
● Segmentation is a method that can be used to segregate data from
security operations.
● Flexibility: Segmentation provides a higher degree of flexibility than
paging. Segments can be of variable size, and processes can be
designed to have multiple segments, allowing for more fine-grained
memory allocation.
14
● Sharing: Segmentation allows for sharing of memory segments
between processes. This can be useful for inter-process
communication or for sharing code libraries.
● Protection: Segmentation provides a level of protection between
segments, preventing one process from accessing or modifying
another process’s memory segment. This can help increase the
security and stability of the system.
Disadvantages of Segmentation in Operating System
● External Fragmentation : As processes are loaded and removed
from memory, the free memory space is broken into little pieces,
causing external fragmentation. This is a notable difference from
paging, where external fragmentation is significantly lesser.
● Overhead is associated with keeping a segment table for each
activity.
● Due to the need for two memory accesses, one for the segment
table and the other for main memory, access time to retrieve the
instruction increases.
● Fragmentation: As mentioned, segmentation can lead to external
fragmentation as memory becomes divided into smaller segments.
This can lead to wasted memory and decreased performance.
15
● Overhead: Using a segment table can increase overhead and reduce
performance. Each segment table entry requires additional memory,
and accessing the table to retrieve memory locations can increase
the time needed for memory operations.
● Complexity: Segmentation can be more complex to implement and
manage than paging. In particular, managing multiple segments per
process can be challenging, and the potential for segmentation
faults can increase as a result.
Page Replacement Algorithms in Operating
Systems
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 replacement becomes necessary when a
page fault occurs and no free page frames are in memory.
However, another page fault would arise if the replaced page is referenced
again. Hence it is important to replace a page that is not likely to be
referenced in the immediate future. Before proceeding to actual page
replacement algorithms, let’s first discuss Paging and Virtual Memory.
16
What is Paging?
Paging is a memory management technique operating systems use
operating systems to optimize computer memory usage. It divides memory
into fixed-size pages that are mapped to physical memory frames, reducing
fragmentation and improving system performance . This method allows for
more efficient use of memory, which is crucial for modern operating systems
and their ability to handle multitasking effectively.
What is 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, the 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.
What is Virtual Memory in OS?
Virtual memory in an operating system is a memory management technique
that creates an illusion of a large block of contiguous memory for users. It
uses both physical memory (RAM) and disk storage to provide a larger
virtual memory space, allowing systems to run larger applications and
17
handle more processes simultaneously. This helps improve system
performance and multitasking efficiency.
Understanding page replacement algorithms is critical for exams like GATE,
where operating systems are a key focus area. To deepen your knowledge
and enhance your exam preparation, consider enrolling in the GATE CS Self-
Paced Course . This course provides comprehensive coverage of operating
system concepts, including detailed explanations and examples of page
replacement algorithms, helping you to excel in your exams.
Page Replacement Algorithms
Page replacement algorithms are techniques used in operating systems to
manage memory efficiently when the virtual memory is full. When a new
page needs to be loaded into physical memory , and there is no free space,
these algorithms determine which existing page to replace.
If no page frame is free, the virtual memory manager performs a page
replacement operation to replace one of the pages existing in memory with
the page whose reference caused the page fault. It is performed as follows:
The virtual memory manager uses a page replacement algorithm to select
one of the pages currently in memory for replacement, accesses the page
table entry of the selected page to mark it as “not present” in memory, and
initiates a page-out operation for it if the modified bit of its page table entry
indicates that it is a dirty page.
18
Common Page Replacement Techniques
● First In First Out (FIFO)
● Optimal Page replacement
● Least Recently Used
● Most Recently Used (MRU)
operating systems use 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.
19
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.
20
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.
21
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.
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.
22
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.
Most Recently Used (MRU)
In this algorithm, page will be replaced which has been used recently.
Belady’s anomaly can occur in this algorithm.
23
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