Professional Documents
Culture Documents
Paging
Need of Paging
• Segmentation:
• It logically chops the address space into variable-sized
pieces(segments)
• The free space itself gets divided in fragments of variable
sizes.
• Thus, while allocating space for a new process, one needs to
consider the sizes of available memory chunks.
pages.
• When a process is to be
executed, its pages are
loaded into any available
memory frames from their
Paging
Basic Method of Implementing “Paging”
The mechanism of paging requires three basic elements to store the information for mapping
• Page Table
• Page Table Base Register
• Translation Look Aside Buffer
Page Table
• Approach 2:
• Instead of flushing the TLB,
• Some systems provide to address space identifier (ASID) field in the TLB to identify “to which
process this entry belongs to”.
• The ASID is a process identifier (PID), but usually it has fewer bits (e.g., 8 bits for the ASID
versus 32 bits for a PID).
Paging Vs. Segmentation
S. No. Paging Segmentation
1. In paging, program is divided into fixed or In segmentation, program is divided into variable size
mounted size pages. sections.
6. In paging, logical address is split into page Here, logical address is split into section number and
number and page offset. section offset.
Paging comprises a page table which While segmentation also comprises the segment table
7. encloses the base address of every page. which encloses segment number and segment offset.
Paging Vs. Segmentation
S. No. Paging Segmentation
8. Page table is employed to keep up the Section Table maintains the section data.
page data.
In paging, operating system must In segmentation, operating system maintain a list of
9.
maintain a free frame list. holes in main memory.
10. Paging is invisible to the user. Segmentation is visible to the user.
11. In paging, processor needs page number, In segmentation, processor uses segment number, offset
offset to calculate absolute address. to calculate full address.
12. It is hard to allow sharing of procedures Facilitates sharing of procedures between the processes.
between processes.
In paging, a programmer cannot
13 efficiently handle data structure. It can efficiently handle data structures.
Hybrid Approach: Paging and Segments
• Segmentation can be combined with Paging to get the best features out of both the techniques.
• Segmented Paging: The main memory is divided into variable size segments which are further
divided into fixed size pages.
• Pages are smaller than segments.
• Each Segment has a page table which means every program has multiple page tables.
• The logical address is represented as Segment Number (base address), Page number and
page offset.
• Segment Number → It points to the appropriate Segment Number.
• Page Number → It Points to the exact page within the segment
Hybrid Approach: Paging and Segments
Hybrid Approach: Paging and Segments
Step-01:
• CPU generates a
logical address
consisting of three
parts-
1. Segment Number
2. Page Number
3. Page Offset Page Table
Base Address
Hybrid Approach: Paging and Segments
Step-02:
• For the generated
segment number,
corresponding entry
is located in the
segment table.
• Segment table
provides the frame Page Table
number of the frame Base Address
storing the page table
of the referred
segment.
E.g.
1. The address space of a process is small and completely fits into physical memory.
2. Address space of every running process fits into memory.
We will now relax these big assumptions, and assume that we wish to support many
concurrently-running processes having large address spaces.
OS uses some more mechanisms to deal with this actual situation.
Those we re going to study next.
Swap Space
• OS reserve some space on the hard disk for moving pages back and forth.
• This space is known as swap space, because we swap pages out of main memory to it and swap
pages into main memory from it.
• Thus, we will simply assume that the OS can read from and write to the swap space, in page-
sized units.
• To do so, the OS will need to remember the disk address of a given page.
Swap Space
• How using swap space allows the system to pretend that main memory is larger than it actually
is??
• A typical scenario:
• Three processes (Proc 0, Proc 1, and Proc 2) are actively sharing physical/main memory;
• Each of the three, however, only have some of their valid pages in main memory, with the rest
located in swap space on disk.
• A fourth process (Proc 3) has all of its pages swapped out to disk, and thus clearly isn’t currently
running.
Present Bit
• How using swap space allows the system to pretend that main memory is larger than it actually
is??
• Thus, OS swaps the pages between main memory and swap space as and when needed.
• For this purpose “Present bit” in Page Table Entry is used.
• For all the pages that are present in the main memory, this bit will be set to 1 and the bit will be 0
for all the pages which are absent.
• If some page is not present in the main memory then it is called page fault.
Page replacement control flow
• When present bit=0, it means
page is not present in main
memory. This situation is called
as “Page Fault”.
• Particular page is loaded in man
memory and PTE is updated i.e.
present bit is set to 1.
What If Memory Is Full?
• In the process described above, we assumed there is plenty of free space available in main memory
to load a page from swap space.
• But this not be the case; memory may be full (or close to it).
• Here, the OS might like to first page out one or more pages to make room for the new page(s) the
OS is about to bring in.
• The process of picking a page to kick out, or replace is known as the page-replacement policy.
• First In First Out
• Optimal Page Replacement
• Least recently used
The Linux Virtual Memory System.
Page allocator: Each zone has its own allocator, which is responsible for allocating and freeing all
physical pages for the zone and is capable of allocating ranges of physically contiguous pages on
request.
The Linux Virtual Memory System.
Virtual Memory:
• The Linux virtual memory system is responsible for maintaining the address space accessible to
each process.
• It creates pages of virtual memory on demand and manages loading those pages from disk and
swapping them back out to disk as required.
• Under Linux, the virtual memory manager maintains two separate views of a process’s address
space:
1. Logical view
2. Physical view
The Linux Virtual Memory System.
Virtual Memory:
1. Logical view
• It describes instructions that the virtual memory system has received concerning the layout of
the address space.
• In this view, the address space consists of a set of nonoverlapping regions, each region
representing a continuous, page-aligned subset of the address space.
2. Physical view
• This view is stored in the hardware page tables for the process.
• The page table entries identify the exact current location of each page of virtual memory
The Linux Virtual Memory System.
Swapping and Paging:
• An important task for a virtual memory system is to relocate pages of memory from physical
memory out to disk when that memory is needed.
• Early UNIX systems performed this relocation by swapping out the contents of entire processes
at once, but modern versions of UNIX rely more on paging—the movement of individual pages
of virtual memory between physical memory and disk
The paging system can be divided into two sections:
1. Policy algorithm: decides which pages to write out to backing store and when to write them.
2. Paging mechanism: carries out the transfer and pages data back into physical memory when
they are needed again.
The Linux Virtual Memory System.
• Kernel Virtual Memory:
• Linux reserves for its own internal use a constant, architecture-dependent region of the virtual
address space of every process.
• The page-table entries that map to these kernel pages are marked as protected, and not visible or
modifiable when the processor is running in user mode.
The Linux Virtual Memory System.
• Execution and Loading of User Programs:
• The execution of user programs is triggered by a call to the exec() system call.
• This exec() call commands the kernel to run a new program within the current process, completely
overwriting the current execution context with the initial context of the new program.
• The first job of this system service is to verify that the calling process has permission rights to the file
• being executed.
• Once that matter has been checked, the kernel invokes a loader routine to start running the
program.
The Linux Virtual Memory System.
• Mapping of Programs into Memory
• Under Linux, the binary loader does not load a binary file into physical memory.
• Rather, the pages of the binary file are mapped into regions of virtual memory.
• Only when the program tries to access a given page will a page fault result in the loading of that
page into physical memory using demand paging.
• It is the responsibility of the kernel’s binary loader to set up the initial memory mapping.
The Linux Virtual Memory System.
• Static and Dynamic Linking
• Once the program has been loaded and has started running, all the necessary contents of the binary
file have been loaded into the process’s virtual address space.
• Linux implements dynamic linking in user mode through a special linker library.
• Every dynamically linked program contains a small, statically linked function that is called when the
program starts.
• This static function just maps the link library into memory and runs the code that the function
contains.