Hello and welcome.
In this video session, we are going to discuss the virtual memory management and the page table of
a process.
The topic.
Virtual memory and memory management in general is a very important topic in Linux system programming.
Many modern kernels has a concept of virtual memory, and Linux also has similar techniques in which
it can efficiently handle the CPU and the Ram.
Ram is also called as the physical memory.
The physical memory is a place where instructions fetched from secondary memory, like hard disk, is
stored, and the CPU can access the instructions from the physical memory.
The physical memory is limited in size and there are many processes running.
Hence the virtual memory concept arrives.
Let's understand the concept of virtual memory.
Let's assume we have a Ram of one GB and we have say four processes running.
Each process has a memory space of four GB.
Now if all four process has to run in memory then physical memory should be four cross four.
That is 16 GB in total.
Now assume ten process running in physical memory.
In this case, the total physical memory size would be ten into four.
That is 40 GB.
Hence, to run ten process of four GB each we need physical memory of 40 GB.
But practically we cannot have such a huge size of Ram.
Hence, the concept of virtual memory arises, where we store only few parts of process memory in Ram,
and remaining process memory is stored in secondary memory and on need basis.
The process memory is loaded to Ram, so all this is done by the swapper process in background and user
gets a feeling that all four GB process memory is running in Ram.
Now in virtual memory.
Each process has its own memory space.
On a 32 bit system.
The address space can map up to four GB.
That is two raise to power 32.
Each process can have its own memory space of four GB.
Each process has a private memory space.
This means memory of one process cannot be directly accessed by other process, and the same way each
process maintains its private data.
As we have discussed in previous video sessions, there are different memory segments of a process like
text segment initialized and uninitialized data segment.
Stack segment and the heap segment.
Usually on a four GB memory space.
The first three GB is a user space and the top one GB is the kernel space.
Let's see this using a diagram.
This diagram shows the virtual memory of a process.
This memory space is from
00000000020XFFFFFFFF.
That means four GB memory space.
But in reality, each process cannot have all four GB memory in physical memory or Ram, also called
as main memory.
Only few parts of process memory is present on Ram.
This is the concept of virtual memory, where even though the physical memory or Ram cannot hold full
four GB process memory of all running process, only some part of process memory is loaded in Ram,
and remaining process memory is wiped in or swapped out between main memory or Ram and secondary memory.
Now the kernel has top one GB, so the address from
0XC000000020XFFFFFFFF
is one GB.
This one.
GB is the kernel space and remaining lower.
Three GB is user space.
In the three GB user space we have.
Start of stack.
As the program executes.
Each function has its own stack frame.
So here the main function has a stack frame, and when main calls some other function, the stack frame
of that function is occupied onto the stack, and the top of the stack is called a stack pointer, and
it always points to the top of the stack.
Now, at some point when this function returns back to the calling function, this top stack frame is
deleted.
And now the stack pointer points to this point, which is nothing but the stack frame of calling function.
Also there is a heap memory.
This address can be considered as a start of heap and the heap grows upwards whereas stack grows downwards.
So stack grows downwards towards lower address memory from upper address memory and heap grows from
lower address memory to higher address memory.
Here.
From this point to this point, there is some heap memory that has been allocated during dynamic memory
allocation, and the space between stack and the heap is an allocated memory.
And this memory is being used by both stack as well as the heap.
But the user program should make sure that the stack does not cross over the heap.
And if it crosses, stack overflow occurs.
Then we have the data segment.
From this point to here, we have the initialized data segment.
Then from here to here we have an initialized data segment.
And from this point from here to here we have text segment or program segment.
Now let's discuss the concept of page frame.
A virtual memory of each process can be split into small fixed size unit called as the pages.
Usually a page by default is four KB, but this page size can be configurable to different sizes.
Now we have this process memory which is divided into fixed memory size called as frames.
And we have similarly the physical memory, which is also divided in series of page frames.
This means the page frame size is constant for both process and the physical memory at any given point
of time.
Only few pages of a process can be present in the physical memory.
Each process.
Page moment.
Page memory has a ID starting from zero, and the pages which are present in physical memory is entered
in the table called page table.
Kernel maintains a page table for every process.
And the page which are present on the physical memory are called as resident sets.
The copies of unused pages whose entry is not present in the page table of a process are maintained
in swamp area.
We will see more on page table in next slide.
Usually a disk is used for a swap area, and the memory keeps swapping between the main memory and the
swap memory based on the scheduling algorithm.
So now, as we have discussed, the page frame is a configurable parameter and it generally vary from
four KB or eight KB or 16 KB.
In Linux, the default page frame size is four kb.
Now let's see what is a page table in this figure?
We can see some important concepts.
In the center of the diagram we have physical memory, which is the Ram, and this Ram is divided into
page frames or of fixed memory say four kilobyte.
So page frame zero is four kilobyte.
Page frame one is four kilobyte.
Now on the right hand side and the left hand side we see process A and process B.
So this process has completely four GB of virtual memory space, and process B also has four GB of virtual
memory space.
But here we have shown a small part of memory.
In this case we have taken page zero, one, two, three and four for process E and page 012 for process
B.
We have a fixed frame size of four kb, both in process as well as the physical memory.
And this represents the swap memory, or usually the hard disk or the secondary memory where the swap
in and swap out occurs.
Now there is a page table maintained by kernel for every process.
The main intention of the page table is to specify which page of a given process is loaded onto the
physical memory, and where it is loaded in physical memory.
As as we have discussed earlier.
A process has different page frames, and few page frames are loaded onto the physical memory, and
not all page frames are loaded onto the physical memory.
In this case, we have.
Page zero is loaded onto page frame three.
So this page zero of process A is completely present in page three of physical Ram.
Page one is loaded onto page four of physical Ram.
Page two of process A is loaded onto page five of main memory or physical memory.
And page three is loaded onto zero page.
Location of Ram.
At this given point of time.
Page number 0123 of process E or loaded in Ram.
As we can see, there is no entry for Process A's page four in page table.
This means this page memory is not present in the physical memory, and it is stored in swap area or
secondary memory.
Similarly, process B we can take three pages as an example here.
Page zero.
Page one.
And page two.
Page.
Zeroth entry is present in the page table of process B, and page zero of process B is loaded onto page
one of physical Ram, and page one of process B is loaded onto page two of physical Ram, and page two
of process B is loaded on page six of physical Ram.
Now this is a page table, and page table is applicable per process as discussed.
So each process has a page table entry where it has entry of the pages of the process that are loaded
onto the physical memory.
So now in this case the page four of process B is not present in page table.
This means process B's page four is present on memory.
Now let's see what is a page fault.
Page fault occurs when a process tries to access a page that is not currently present in the physical
memory.
A page fault occurs.
Let's see this with an example.
Let's say process A process is instruction which is present in process.
A's page four is to be executed by CPU.
The kernel first checks if page four is already present in Ram by checking the process A's page table
entry.
If page four is not present in its page table.
A page fault occurs and the process execution is suspended, and the page four is loaded from the swap
area to the main memory.
And once it is loaded onto the main memory, an entry of this page is made in the page table of process
A.
So in this video session, we have learned the virtual memory of a process.
In next video session, let's discuss command line argument of a process.
Thank you.