So welcome back, guys.
Until now, we have just written one single standalone API, which is used to send messages from userspace
to kernel space.
Now we are in a process to write full fledged userspace program.
So before writing any code, let us discuss the design model or thread model of our user space program.
We have already discussed about it briefly.
Now let us discuss with the help of a flowchart so that it would be easier for us to implement our Userspace
program.
So you can see our userspace program will start with the execution from Main, and the next thing that
we will going to do is to create a netlink socket.
In order to create this netlink socket, we will going to use the same protocol number which we used
while creating a netlink socket in the kernel space.
So the reserved number that we have been using is 31 and after that we will going to bind the socket.
So when we will be discussing the code, I will discuss that.
How to bind the Netlink socket, What needs to be done in order to bind the netlink socket.
And we bind against what?
And then in the fourth step we will going to start a separate thread.
And we have already discussed that.
Why we will be starting a separate thread.
We will start a separate thread so that our userspace program can independently receive messages from
the kernel space, while the main thread can independently send message to the kernel space.
So you can see that in the main thread.
Finally we get entered into the main menu and we will add more and more options to this main menu as
we add more features to our user space program.
So the very first option is the grid kernel option, which will simply send a text message to a kernel
space, more precisely saying to our Linux kernel module.
So grid kernel API will eventually invoke the API which we have already written.
Send netlink message to kernel.
Right.
And once the sending of the message is finished, we again come back and we again run this main menu
again.
So this main menu will run in an infinite loop.
Right now, coming to the thread that we have launched from the main thread in our userspace application.
The other thread will have a responsibility to actually receive the messages from the kernel space.
Right.
So the flowchart on the right hand side actually represents the code flow of the separate receiver thread.
So you can see that writing a logic for the receiver thread is pretty much simple and easy.
All we have to do is to call, receive message system call and this receive message system call is a
blocking system call.
It means that receiver thread will stay blocked on this receive message system call until it receives
some data on this netlink socket.
Right.
So this is a blocking system call.
I hope you have some background on socket programming and understand what are blocking or non-blocking
system calls.
And once our userspace program receives a message from the kernel space, our userspace program will
get unblocked from this receive message system, call and resume its execution further.
So we will process the message, what we have received from the kernel space and we again go back and
then block on the receive message system call.
So this portion of the code is also running in infinite loop, right?
So overall picture is that that our user space application is a two threaded application.
The first thread is a main thread itself and it has the responsibility to continuously send messages
to the kernel space through main menu.
Right.
So main menu runs in an infinite loop and then we have our receiver thread which also runs in an infinite
loop and it stay blocked on the received master system call until it receives a message from the kernel
space.
Once it receives a message, we will process the message and again we go back and block on the received
message system call.
So the thread model is fairly simple and easy.
So now in the next lecture videos we will going to implement this flowchart step by step.
I would like to encourage you to implement this flowchart by yourself and feel free to skip the lecture
videos if you are able to implement this by yourself.