Hello and welcome to this video session.
In today's video session, we will discuss more on the internal working of the system call.
In previous video session, we had discussed the basics of library function and the system call.
Now in this session we will see the internal working of the system call.
Before we begin with the explanation of the system call working, we shall note that every system call
has a unique number associated with it, and the and the system recognizes the system call by its unique
system call number.
Now let's say that we have written a user code and we have a system call called as write.
So when we call the system call right there is a wrapper function present in the glibc.
For the write function, the arguments from the user code are passed on to this wrapper function.
And now in the wrapper function, the wrapper function raises a interrupt and this interrupt is specific
to the hardware which is being used, and as soon as the interrupt is being raised, the execution switches
from the user mode to the kernel mode.
Before switching the mode, the wrapper function writes the system call number onto a specific CPU register.
Now, once the system switches from the user mode to the kernel mode, it enters the trap handler and
it begins to execute the code present in the trap handler.
In the trap handler, the trap handler looks for the system call number which has raised the interrupt,
and from there it calls the specific system call definition.
So before switching to the kernel mode from the user mode, the arguments that are passed from the user
code is being carried from the wrapper to the kernel stack, and the argument from the kernel stack
reaches to the specific.
system call, and in the specific system call, the action is being taken.
So in this case, the right action takes place of writing the user given string onto the screen, and
once the right action is finished, it returns back to the trap handler and which in turn returns to
the wrapper function.
And finally the result is been returned to the user code.
So here we can see whenever the system call is being called, the system switches from the user mode
to the kernel mode because of the interrupt or the exception being raised by the wrapper function,
and once the system switches to the kernel mode, it executes the specific system call because of the
system call number stored in the CPU register, and performs the action and returns back to the trap
handler and finally reaches reaches to the user code.