Hello, welcome back.
So in this lesson, we're going to talk about context switching.
So earlier on, I mentioned that Real-Time operating system as a resource manager that guarantees the
meeting of deadlines.
And we posed this example of an autonomous vehicle system that has to take action, check the status
of three sensors that lie that everyday in the camera.
And we said each of them existed in a threat.
But what I didn't do is describe what a threat is.
So a thread you can think of a thread as just.
Your standard main function with a while, one loop.
And this is important because if you understand the war one look, you know, the anything that enters
the world, one doesn't come out because it's an infinite loop.
So if we have multiple threats and we are executing something in the wild, one loop of one threat,
how do we ever come out of that to enter the next threat?
So this is where context switching comes into question.
We're going to talk about context switching here.
So this just an overview I'm going to show.
Include some other videos that talk about these concepts.
I'm shooting this video again, some of you who may have seen the earlier versions of my old Archos
course, both the the free Archos and build your own Arktos.
We've spoken about this concept.
Now I'm shooting a new one this year and I'm summarizing some of the concepts.
So I'll still keep some of the old videos that go into details.
So the audio, the audio quality might be different because now I'm using a better microphone, so just
bear with it.
Yeah.
So anyway, we said you can think of a threat as your main function and the feature of the main function
is Itoi one, which is an infinite loop.
And we know what that means is if you enter the infinite loop, you never coming out unless you just
turn off your system.
So if we say each threat is a main function, then when we enter the while loop of one thread, how
do we come out and go to the next threat to execute its content?
This is where context switching comes to play, right?
We have this diagram here.
Over here we said we have we have safe context.
Let's start from thread one, save execution, state of the red one and then restore execution.
State of threat to.
We go to switch to to two and then save execution states of threat to restore execution, states of
threat one switch to threat one.
What we are trying to show here is that we have a system that has two threats.
Remember, one other thing.
When we use the word task and threat, the almost the same, they can be used interchangeably.
If you want a definition, a task.
A task becomes a threat when it is executing.
So you can think of a threat as a task which is being executed, so when you hear the two words, it's
safe to assume the same, right?
People have been arguing about the the correct definition for a while, but you can just assume the
same.
So let's assume we have in our firm where we have two tasks or two threats, the red one and threat
to.
How do we move from threat to threat to something known as context, which in.
We save.
Once we've executed, let's say we've executed the red one.
We want to go to threatener, we saved the execution states of the red one, and then we restore the
execution state of threat to and then we switch to threat to once we've finished working on, threat
to, we see if the execution state of threat to restore the execution states of the red one.
And then we switch to threat one.
What we mean by this.
Right.
So in a microcontroller in our cortex microcontroller that exist what is known as the register bank.
The register bank is just a set of registers, we have we have our issue, our one, our three all the
way to our fifteen and these registers, you can think of the register as a story.
And the story, just like a story, just allows quick access.
The quickest of all access is the the access of what is in your register.
It's quicker to access the register content way quicker down to access what is a memory.
So you can think of the registers as fast, fast, accessible storage as.
So during the execution of your program, even without an artist, the state of the program, let's
say the values of your local variables, the loop iterator, the state of the program is stored in these
registers and they are manipulated in these registers and the result is often produced.
So if you want to save the state of a particular program, you have to save the content of the registers.
So in Archos, whether you're building your own Archos or whether you are using an already existing
archer such as Free Artus, what you always do is you would have to allocate some space in memory to
be saving the content of the register.
So what we would do is when we are creating our own Archos, we would have to allocate a stack size.
When we are using already made our tools, such as free tools or micro, we would have to define the
size of stock we are using.
So in this example, we are seeing Sèvres execution states of threat to.
Was this what this diagram is trying to depict?
Is that the content of the registers?
Are you through AR 15?
The content is collected and saved onto the stack.
The stack is just a memory space that we shall declare.
We shall see.
Give us a stack of one hundred byte and then the content of the register whenever we whenever we reach
the point where the execution of state of threat to is to be saved is going to be taking everything
in, in our cortex and register bank, it's going to be taken and stored in this stack that we've allocated.
This is what we called this is what we call save in the context.
Right.
So we said in our diagram, we said save the execution state of threat to.
Once we've done that, we see restored execution, state of the red one.
So once we've saved this, assuming that we've already saved the the execution states of the red one,
we would restore execution.
State of the red one.
Over here, so as we sift through a two.
We would have to restore execution stage of thread one.
Right.
So what this means is that we are assuming we've already saved through Red One onto its stock, so our
program started by running thread one.
And the other question you would ask is, OK, when do we know it's time to save and it's not time to
save?
The default mode of the the most popular way of running an artist is using time scheduling method known
as the known as Round-Robin Scheduling.
And what this means is that each thread executes for a set amount of time.
And when that time expires, automatically the system would move to the next thread.
The thread scheduler would automatically switch from the current threat to the next one.
So let's see if that time amount is 10, MS, 10 milliseconds after ten milliseconds, we would save
if we are currently executing the red one after ten milliseconds, we would save the the the execution
state of the red one onto its stock and then restore the execution state of threat to.
So if we are run in the round robling, round robin schedule an algorithm, this is how it would take
place.
That's just an example.
OK, but there are other scheduling algorithms out there.
So let's start again.
We said, OK, we we are currently executing three, two with executed threat, two for ten milliseconds,
which is the time Quanta or the Round-Robin time slice that each threat has to execute.
After ten milliseconds, we save the execution state of threat two.
So we take the content in our register bank and save it onto the threat to stack that we are located.
After we've done that, because we have just two threads, thread one and three, two, we will go to
the stack of thread one and then take it content and put it back into our Coatex and registers of we
are ready to execute the red one now.
This is known as restoring the execution state of the red one.
So then once we've restored the execution status of the red one, we are going to execute the red one
for ten milliseconds.
And once that 10 milliseconds expires, then we save the execution states of Red one back onto its stack
and then we restore the execution state of threat to because we are back to executing through it, too.
And then this process continues, this loopier, it continues.
So then we have the two threats to main functions executing a SCIF execute and in parallel.
So when this happens, it happened so fast that we assume that happening at the same time.
But it's just context switch.
And this is known as context switching.
So if you're asked what is context switching contexts, which involves saving the execution states of
currently executing threat and restoring the execution state of the next threat to be executed.
As simple as that, if you have any questions, of course you can live it in the questions analysis
area.
OK, so now let's give let's talk about the differences between a threat of interrupt service routine
and then our busy wait system.
So we should do that in the next lesson, which will give the differences between the way we often write
our sequential code, comparing that to a real time operating system and then comparing that to having
an interrupt service routine, which I shall explain in the next lesson.
So I'll see you.
If you have any questions, just leave them the questions analysis area.
If you're flying into the course useful, just leave a review or a recommendation.
I'll see you later.