Paging in Operating Systems: A Comprehensive Guide
SPaging is a method of gaining access to data more
quickly. When a program requires a page, it is available in the main memory
because the OS copies a set number of pages from the storage device into the
main memory. Paging permits a process’s physical address space to be
noncontiguous. Paging refers to a memory management strategy that does away
with the need for the allocation of contiguous physical memory.
What is Paging in
the OS?
Paging is a storage
mechanism used in OS to retrieve processes from secondary storage to the main
memory as pages. The primary concept behind paging is to break each process
into individual pages. Thus the primary memory would also be separated into
frames.
One page of the
process must be saved in one of the given memory frames. These pages can be
stored in various memory locations, but finding contiguous frames/holes is
always the main goal. Process pages are usually only brought into the main
memory when they are needed; else, they are stored in the secondary storage.
The frame sizes may
vary depending on the OS. Each frame must be of the same size. Since the pages
present in paging are mapped on to the frames, the page size should be similar
to the frame size.
Example
Assuming that the
main memory is 16 KB and the frame size is 1 KB, the main memory will be
partitioned into a collection of 16 1 KB frames. P1, P2, P3, and P4 are the
four processes in the system, each of which is 4 KB in size. Each process is
separated into 1 KB pages, allowing one page to be saved in a single frame.
Because all of the
frames are initially empty, the pages of the processes will be stored in a
continuous manner. The graphic below depicts frames, pages, and the mapping
between them.
Consider the case
when P2 and P4 are shifted to the waiting state after a period of time. Eight
frames are now empty, allowing other pages to be loaded in their stead. Inside
the ready queue is the process P5, which is 8 KB (8 pages) in size.
Given that we have
8 noncontiguous frames accessible in memory, paging allows us to store the
process in many locations. As a result, we can load the process P5 page instead
of P2 and P4.
Memory Management Unit
The Memory
Management Unit (MMU) is responsible for converting logical addresses to
physical addresses. The physical address refers to the actual address of a
frame in which each page will be stored, whereas the logical address refers to
the address that is generated by the CPU for each page.
When the CPU
accesses a page using its logical address, the OS must first collect the
physical address in order to access that page physically. There are two
elements to the logical address:
- Page
number
- Offset
The OS’s memory
management unit must convert the page numbers to the frame numbers.
Examples
Let’s say the CPU requests
the 10th word of the 4th page of process P3 in the image above. Because page
number 4 of process P1 is stored at frame number 9, the physical address will
be returned as the 10th word of the 9th frame.
Let’s consider
another example:
- If
the physical address is 12 bits, then the physical address space would be
4 K words
- If
the logical address is 13 bits, then the logical address space would be 8
K words
- If
the page size is equal to the frame size, which is equal to 1 K words
(assumption),
Then:
The address
generated by the CPU is divided into the following:
- Page
offset(d): It refers to the number of bits necessary to represent a
certain word on a page, page size in Logical Address Space, or page word
number or page offset.
- Page
number(p): It is the number of bits needed to represent the pages in the
Logical Address Space or the page number.
The Physical
Address is divided into the following:
- Frame
offset(d): It refers to the number of bits necessary to represent a
certain word in a frame, or the Physical Address Space frame size, the
word number of a frame, or the frame offset.
- Frame
number(f): It’s the number of bits needed to indicate a frame of the
Physical Address Space or a frame number.
Dedicated registers
can be used to implement the page table in hardware. However, using a register
for the page table is only useful if the page table is tiny. We can employ TLB
(translation look-aside buffer), a particular, tiny, fast look-up hardware cache
if the page table has a significant number of entries.
- The
TLB is a high-speed, associative memory.
- LB
entries are made up of two parts: a value and a tag.
- When
this memory is accessed, an item is compared to all tags at the same time.
- If
the object is located, the value associated with it is returned.
m = main memory
access time
In case the page
table is kept in the main memory,
then the effective
access time would be = m(page table) + m(page in page table)
what is Paging in
the OS?
Before directly delving into the concept of paging, it is essential to
understand the memory management in an operating system. In brief, memory
management is a method used by operating systems to manage and organize the
memory resources of a computer. This includes memory allocation/deallocation,
mapping, protection, and fragmentation.
Having an idea of the meaning of memory management, now we can better
understand the paging in OS. So, paging is a memory management method used in
an operating system. We all know computers use several programs to run a
variety of tasks. And what paging does is that it tells the program how to use
the memory.
To understand this, let’s consider an example of a computer program that
is used to edit the images. Now, before performing any further task, the
program is first divided into different sections called pages. These pages are
used by the OS to manage its memory. In order to do so, the OS assigns frames
to its pages.
What is Paging
Protection?
Paging protection is a technique used for the purpose of security and isolation
of applications and processes running on the computer. It also aids in managing
virtual memory, thereby allowing the use of memory resources efficiently. That
is to say, when a process attempts to access a memory location, the associated
protection bits in the page table entry for the requested address are checked
by the hardware. If the process is trying to perform an unpermitted operation
by the protection bits, an interrupt is generated.
Memory Management
Unit
Memory Management Unit, also widely known as MMU, is a component found
in the central processing unit of a computer that manages virtual memory and
physical memory. When a program runs, it creates virtual addresses to access
memory locations without knowing the actual physical location of the
information. When the CPU tries to access memory using a virtual address, the
MMU translates it to the physical address. So, it is primarily used to
translate virtual memory addresses to physical memory addresses.
This can be clearly understood with the help of the following example.
Imagine you're using a web browser that's running multiple tabs with various
websites. Each tab represents a different program. As you switch between tabs,
the operating system and MMU work together to manage memory. When you click on
a tab, the MMU translates the tab's virtual memory addresses into actual
physical memory locations. If another program is using a lot of memory, the MMU
might move some of its data to disk to make room for the active tab's data in
RAM. This swapping happens transparently, allowing your tabs to run smoothly
even
Advantages and
Disadvantages of Paging
Paging in operating systems offers streamlined memory management but
presents challenges like internal fragmentation and the overhead of managing
page tables. Here are some of the key advantages and disadvantages of paging in
the OS.
Advantages of
Paging
· Paging offers simplified memory management so that the programs need not
worry about the physical memory addresses.
· It allows efficient memory usage.
· Aids in eliminating external fragmentation.
· Provides memory protection by preventing unauthorized access.
· The mapping between virtual and physical addresses is quite simple.
Disadvantages of
Paging
· Since the pages are of fixed size, there is a possibility for internal
fragmentation.
· Page table overhead arises in systems that require large memory.
· Complex page replacement.
· An additional layer of memory access is created by paging.
· Too many pages in a physical memory at the same time lead to thrashing.
Practice Problems on Paging in OS
1. The logical memory
gets broken into various blocks of similar sizes known as _______.
a) pages
b) backing store
c) frames
d) none of the
above
Answer- (a) pages
2. The physical memory
gets broken into various fixed-sized blocks known as _________.
a) pages
b) backing store
c) frames
d) none of the
above
Answer- (c) frames
3. Which of these is
used in the form of an index in a given page table?
a) page offset
b) frame offset
c) frame bit
d) page number
Answer- (d) page number
4. The smaller page
tables can be implemented in the form of a set of which of these?
a) registers
b) counters
c) stacks
d) queues
Answer- (a) registers
5. Paging helps in
increasing the ______ time.
a) execution
b) context – switch
c) waiting
d) all of the above
Answer- (b) context – switch
Frequently Asked Questions on Paging in OS
Q1
Why is paging used?
Paging is a method
of gaining access to data more quickly. When a program requires a page, it is
available in the main memory because the OS copies a set number of pages from
the storage device into the main memory. Paging permits a process’s physical address
space to be noncontiguous.
Q2
What are paging and frames?
Paging is a method
of memory management. External Fragmentation is removed using the concept of
paging. The process is divided into ‘Pages’, and the Main Memory is divided
into ‘Frames’ so that a part of the process (a page) can be contained in a
frame (part of the main memory).
Q3
What is the difference between a page
and a frame?
A page (often known
as a memory page, a logical page, or a virtual page) is a contiguous virtual
memory block with a set length. The frame (often known as a memory frame, page
frame, or physical page) is basically a fixed-length RAM block, i.e. physical memory.
Q4
What is a page in OS?
A page, also known
as a memory page or virtual page, refers to a contiguous block of virtual
memory with a defined length that is described by one single entry in a page
table. In a virtual memory OS, it is the smallest data unit for memory
management.
Comments
Post a Comment