Memory management is one of the most complex activity done by Linux kernel. It has various concepts/issues associated with it.
This article is part of our on-going UNIX kernel overview series.
In the previous article of the kernel series, we discussed about the UNIX process overview, and Reentrant Kernels.
In this article we will try to touch base on virtual memory and demand paging as these are some of the important concepts related to memory management.
Virtual memory makes your system appear as if it has more memory than it actually has. This may sound interesting and may prompt one to as how is this possible. So, lets understand the concept.
Theoretically we can consider a page table to contain the following information :
In the image below, VPFN stands for Virtual page frame number, and PFN indicates the physical page frame number.
It may happen that a processor goes to a processes page table entry with a virtual page frame number and finds the entry as invalid. In this case it is the processor’s responsibility to pass the control to kernel and ask it to fix the problem. Different processors pass the control in different ways but this phenomenon is known as a ‘page fault’. But if the entry was valid then processor takes the physical page frame number, multiplies with the size of the page to get the base address of the physical page and then adds the offset to get to the exact physical address.
So now we understand that through the concept of virtual memory, each process thinks that it has all range of virtual address at its disposal and hence this concepts make the system appear as if it has more physical memory than actually available.
While in case ’2′, as already explained, the physical page corresponding to the virtual address is not yet loaded into physical memory. In this case also a page fault is generated and the kernel then tries to bring the required memory page into physical memory from hard disk.
Since this operation of bringing a page from hard disk into physical memory is time consuming so by this time a context switch between processes happens and some other process is brought into execution. Meanwhile the page of the earlier process is brought into physical memory and the page tables are updated and then this process is brought back into execution again from the same instruction that caused the ‘page fault’.
This is known as demand paging where all the memory pages corresponding to a process are not present in the physical memory at any given time. This saves the physical memory from clogging up with non-required memory pages while when necessary these pages can be brought into physical memory through page fault (as explained above).
This article is part of our on-going UNIX kernel overview series.
In the previous article of the kernel series, we discussed about the UNIX process overview, and Reentrant Kernels.
In this article we will try to touch base on virtual memory and demand paging as these are some of the important concepts related to memory management.
Virtual Memory
The concept of virtual memory is one of the very powerful aspects of memory management. Since the initial era of computers the need of memory more than the existing physical memory has been felt. Over the years, many solutions were used to overcome this issue and the most successful of them has been the concept of virtual memory.Virtual memory makes your system appear as if it has more memory than it actually has. This may sound interesting and may prompt one to as how is this possible. So, lets understand the concept.
- To start, we must first understand that virtual memory is a layer of memory addresses that map to physical addresses.
- In virtual memory model, when a processor executes a program instruction, it reads the instruction from virtual memory and executes it.
- But before executing the instruction, it first converts the virtual memory address into physical address.
- This conversion is done based on the mapping of virtual to physical addresses that is done based on the mapping information contained in the page tables (that are maintained by OS).
- An offset (Lowest 12 bits)
- A virtual page frame number (rest of the bits)
Theoretically we can consider a page table to contain the following information :
- A flag that describes whether the entry is valid or not
- The physical page frame number as described by this entry
- Access information regarding the page (like read-only, read-write etc)
In the image below, VPFN stands for Virtual page frame number, and PFN indicates the physical page frame number.
It may happen that a processor goes to a processes page table entry with a virtual page frame number and finds the entry as invalid. In this case it is the processor’s responsibility to pass the control to kernel and ask it to fix the problem. Different processors pass the control in different ways but this phenomenon is known as a ‘page fault’. But if the entry was valid then processor takes the physical page frame number, multiplies with the size of the page to get the base address of the physical page and then adds the offset to get to the exact physical address.
So now we understand that through the concept of virtual memory, each process thinks that it has all range of virtual address at its disposal and hence this concepts make the system appear as if it has more physical memory than actually available.
Demand Paging
In the previous sectioned we learned that if the processor goes to the processes page table with a virtual page frame number for which no entry was present in the table then two cases arise.- Either the process has tried to access an invalid memory address
- The physical page corresponding to the virtual address was not loaded into physical memory
While in case ’2′, as already explained, the physical page corresponding to the virtual address is not yet loaded into physical memory. In this case also a page fault is generated and the kernel then tries to bring the required memory page into physical memory from hard disk.
Since this operation of bringing a page from hard disk into physical memory is time consuming so by this time a context switch between processes happens and some other process is brought into execution. Meanwhile the page of the earlier process is brought into physical memory and the page tables are updated and then this process is brought back into execution again from the same instruction that caused the ‘page fault’.
This is known as demand paging where all the memory pages corresponding to a process are not present in the physical memory at any given time. This saves the physical memory from clogging up with non-required memory pages while when necessary these pages can be brought into physical memory through page fault (as explained above).
No comments:
Post a Comment