Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 22

Logical and Physical Address in Operating System

Logical Address is generated by CPU while a program is running. The logical address is virtual address as it does
not exist physically, therefore, it is also known as Virtual Address. This address is used as a reference to access the
physical memory location by CPU.

Physical Address identifies a physical location of required data in a memory. The user never directly deals
with the physical address but can access by its corresponding logical address.
The user program generates the logical address and thinks that the program is running in this logical
address but the program needs physical memory for its execution, therefore, the logical address must be
mapped to the physical address by MMU before they are used.
Differences Between Logical and Physical Address in Operating System
1.The basic difference between Logical and physical address is that Logical address is
generated by CPU in perspective of a program whereas the physical address is a location
that exists in the memory unit.
2.Logical Address Space is the set of all logical addresses generated by CPU for a
program whereas the set of all physical address mapped to corresponding logical
addresses is called Physical Address Space.
3.The logical address does not exist physically in the memory whereas physical address
is a location in the memory that can be accessed physically.
4.The logical address is generated by the CPU while the program is running whereas the
physical address is computed by the Memory Management Unit (MMU).
Virtual Memory
• Virtual Memory is a storage scheme that provides user an illusion of having a very big
main memory. This is done by treating a part of secondary memory as the main
memory.
• In this scheme, User can load the bigger size processes than the available main
memory by having the illusion that the memory is available to load the process.
•The ability to load only the portions of processes that were actually needed ( and
only when they were needed ) has several benefits: Programs could be written for a much
larger address space ( virtual memory space ) than physically exists on the computer.
•Because each process is only using a fraction of their total address space, there is more
memory left for other programs, improving CPU utilization and system throughput.
•Less I/O is needed for swapping processes in and out of RAM, speeding things up.
• Virtual Memory is a storage allocation scheme in which secondary memory can be
addressed as though it were part of main memory.
• 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.
• 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 main memory such that it occupies different places in main
memory at different times during the course of execution.
• A process may be broken into number of pieces and these pieces need not be
continuously located in the main memory during execution.
• 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.
Advantages of Virtual Memory
1.The degree of Multiprogramming will be increased.
2.User can run large application with less real RAM.
3.There is no need to buy more memory RAMs.
Disadvantages of Virtual Memory
4.The system becomes slower since swapping takes time.
5.It takes more time in switching between applications.
6.The user will have the lesser hard disk space for its use.
Definition of Paging
Paging is a memory management scheme. Paging allows a process to be stored in a memory
in a non-contiguous manner. Storing process in a non-contiguous manner solves the problem
of external fragmentation.
For implementing paging the physical and logical memory spaces are divided into the same
fixed-sized blocks. These fixed-sized blocks of physical memory are called frames, and the fixed-
sized blocks of logical memory are called pages.

When a process needs to be executed the process pages from logical memory space are
loaded into the frames of physical memory address space. Now the address generated
by CPU for accessing the frame is divided into two parts i.e. page number and page offset.

•Paging is a memory management scheme that allows processes physical memory


to be discontinuous, and which eliminates problems with fragmentation by
allocating memory in equal sized blocks known as pages.
The basic idea behind paging is to divide physical memory into a number of equal sized
blocks called frames, and to divide a programs logical memory space into blocks of the
same size called pages.
Any page ( from any process ) can be placed into any available frame.
The page table is used to look up what frame a particular page is stored in at the
moment.
The page table uses page number as an index; each process has its separate page table
that maps logical address to the physical address. The page table contains base address
of the page stored in the frame of physical memory space. The base address defined by
page table is combined with the page offset to define the frame number in physical
memory where the page is stored.
Page 0 Frame 0

Page 1 frame1

Page 2 Frame 2

Page 3

Page offset is number of bits required to represent particular


word in a page . Frame 5

frame 6

•Consider the following micro example, in which a process has 16 bytes of logical memory, mapped in 4 byte pages into
32 bytes of physical memory. ( Presumably some other processes would be consuming the remaining 16 bytes of physical
memory. )
•A logical address consists of two parts: A page number in which the address resides, and an offset from
the beginning of that page. ( The number of bits in the page number limits how many pages a single
process can address. The number of bits in the offset determines the maximum size of each page, and
should correspond to the system frame size. )
•The page table maps the page number to a frame number, to yield a physical address which also has two
parts: The frame number and the offset within that frame. The number of bits in the frame number
determines how many frames the system can address, and the number of bits in the offset determines the
size of each frame.
Demand Paging
•The basic idea behind demand paging is that when a process is swapped in, its pages are not swapped in all at once.
Rather they are swapped in only when the process needs them. ( on demand. ) This is termed a lazy swapper,

Transfer of a paged memory to contiguous disk space


•The basic idea behind paging is that when a process is swapped in, the pager only loads into memory those pages that
it expects the process to need .
•Pages that are not loaded into memory are marked as invalid in the page table, using the invalid bit.
1. if a page is needed that was not originally
loaded up, then a page fault trap is generated,
which must be handled in a series of steps: The
memory address requested is first checked, to
make sure it was a valid memory request.
2.If the reference was invalid, the process is
terminated. Otherwise, the page must be paged
in.
3.A free frame is located, possibly from a free-
frame list.
4.A disk operation is scheduled to bring in the
necessary page from disk.
5.When the I/O operation is complete, the
process's page table is updated with the new
frame number, and the invalid bit is changed to
indicate that this is now a valid page reference.
6.The instruction that caused the page fault
must now be restarted from the beginning,
Segmentation
•Most users ( programmers ) do not think of their programs as existing in one continuous linear address space.
•Rather they tend to think of their memory in multiple segments, each dedicated to a particular use, such as
code, data, the stack, the heap, etc.
•Memory segmentation supports this view by providing addresses with a segment number ( mapped to a
segment base address ) and an offset from the beginning of that segment.
•For example, a C compiler might generate 5 segments for the user code, library code, global ( static )
variables, the stack, and the heap
Segmentation Hardware
•A segment table maps segment-offset addresses to physical addresses, and
simultaneously checks for invalid addresses, using a system similar to the page tables and
relocation base registers discussed previously.
Paging and Segmentation: Advantages and Disadvantages

Paging Advantages
•On the programmer level, paging is a transparent function and does not require intervention.
•No external fragmentation.
•No internal fragmentation on updated OS’s.
•Frames do not have to be contiguous.
Paging Disadvantages
•Paging causes internal fragmentation on older systems.
•Longer memory lookup times than segmentation;
•Segmentation Advantages
•No internal fragmentation.
•Segment tables consumes less space compared to page tables.
•Average segment sizes are larger than most page sizes, which allows segments to store more process data.
•Less processing overhead.
•Simpler to relocate segments than to relocate contiguous address spaces on disk.
•Segment tables are smaller than page tables, and takes up less memory.
Segmentation Disadvantages
•Uses legacy technology in x86-64 servers.
•Linux only supports segmentation in 80x86 microprocessors: states that paging simplifies memory management by using the same set of linear
addresses.
•Porting Linux to different architectures is problematic because of limited segmentation support.
•Requires programmer intervention.
•Subject to serious external fragmentation.
Key Differences: Paging and Segmentation

Size:

•Paging: Fixed block size for pages and frames. Computer hardware determines page/frame sizes.
•Segmentation: Variable size segments are user-specified.
Fragmentation:

•Paging: Older systems were subject to internal fragmentation by not allocating entire pages to memory. Modern OS’s
no longer have this problem.
•Segmentation: Segmentation leads to external fragmentation.
Tables:

•Paging: Page tables direct the MMU to page location and status. This is a slower process than segmentation tables
•Segmentation: Segmentation tables contain segment ID and information, and are faster than direct paging table
lookups.
Availability:

•Paging: Widely available on CPUs and as MMU chips.


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 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 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 :
•First In First Out (FIFO)
•Optimal Page replacement –
•Least Recently Used –
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.
It is very important to have the optimal frame allocation and page replacement algorithm.
Frame allocation is all about how many frames are to be allocated to the process
while the page replacement is all about determining the page number which needs
to be replaced in order to make space for the requested page.
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.

Consider a reference string: 4, 7, 6, 1, 7, 6, 1, 2, 7, 2. the number of frames in the memory is 3. Find out the number of
page faults respective to:
FIFO

•Advantages –
• It is simple and easy to understand & implement.
•Disadvantages –
• The process effectiveness is low.
• When we increase the number of frames while using FIFO, we are giving more memory to processes.
So, page fault should decrease, but here the page faults are increasing. This problem is called as
Belady’s Anomaly.
• Every frame needs to be taken account off.

https://www.youtube.com/watch?v=l7HoguhFVQ4

You might also like