1
00:00:00,760 --> 00:00:01,900
Hello, welcome back.

2
00:00:02,850 --> 00:00:06,600
So in this lesson, we're going to talk about context switching.

3
00:00:07,580 --> 00:00:16,280
So earlier on, I mentioned that Real-Time operating system as a resource manager that guarantees the

4
00:00:16,280 --> 00:00:17,390
meeting of deadlines.

5
00:00:19,070 --> 00:00:26,900
And we posed this example of an autonomous vehicle system that has to take action, check the status

6
00:00:26,900 --> 00:00:29,820
of three sensors that lie that everyday in the camera.

7
00:00:29,840 --> 00:00:31,910
And we said each of them existed in a threat.

8
00:00:32,420 --> 00:00:35,840
But what I didn't do is describe what a threat is.

9
00:00:37,250 --> 00:00:40,670
So a thread you can think of a thread as just.

10
00:00:42,080 --> 00:00:45,560
Your standard main function with a while, one loop.

11
00:00:46,500 --> 00:00:53,250
And this is important because if you understand the war one look, you know, the anything that enters

12
00:00:53,250 --> 00:00:56,780
the world, one doesn't come out because it's an infinite loop.

13
00:00:57,450 --> 00:01:04,980
So if we have multiple threats and we are executing something in the wild, one loop of one threat,

14
00:01:05,190 --> 00:01:08,940
how do we ever come out of that to enter the next threat?

15
00:01:10,000 --> 00:01:13,540
So this is where context switching comes into question.

16
00:01:13,920 --> 00:01:15,830
We're going to talk about context switching here.

17
00:01:16,390 --> 00:01:19,210
So this just an overview I'm going to show.

18
00:01:20,160 --> 00:01:25,140
Include some other videos that talk about these concepts.

19
00:01:25,620 --> 00:01:33,540
I'm shooting this video again, some of you who may have seen the earlier versions of my old Archos

20
00:01:33,540 --> 00:01:36,900
course, both the the free Archos and build your own Arktos.

21
00:01:37,200 --> 00:01:38,830
We've spoken about this concept.

22
00:01:38,850 --> 00:01:43,530
Now I'm shooting a new one this year and I'm summarizing some of the concepts.

23
00:01:43,530 --> 00:01:47,670
So I'll still keep some of the old videos that go into details.

24
00:01:48,270 --> 00:01:54,630
So the audio, the audio quality might be different because now I'm using a better microphone, so just

25
00:01:54,630 --> 00:01:55,220
bear with it.

26
00:01:55,230 --> 00:01:55,580
Yeah.

27
00:01:56,040 --> 00:02:02,820
So anyway, we said you can think of a threat as your main function and the feature of the main function

28
00:02:02,820 --> 00:02:04,980
is Itoi one, which is an infinite loop.

29
00:02:05,250 --> 00:02:11,130
And we know what that means is if you enter the infinite loop, you never coming out unless you just

30
00:02:11,130 --> 00:02:12,020
turn off your system.

31
00:02:12,690 --> 00:02:18,890
So if we say each threat is a main function, then when we enter the while loop of one thread, how

32
00:02:18,900 --> 00:02:22,590
do we come out and go to the next threat to execute its content?

33
00:02:23,460 --> 00:02:27,050
This is where context switching comes to play, right?

34
00:02:27,930 --> 00:02:29,330
We have this diagram here.

35
00:02:30,090 --> 00:02:36,060
Over here we said we have we have safe context.

36
00:02:36,270 --> 00:02:42,570
Let's start from thread one, save execution, state of the red one and then restore execution.

37
00:02:42,570 --> 00:02:43,790
State of threat to.

38
00:02:45,990 --> 00:02:52,680
We go to switch to to two and then save execution states of threat to restore execution, states of

39
00:02:52,680 --> 00:02:54,810
threat one switch to threat one.

40
00:02:55,470 --> 00:03:01,030
What we are trying to show here is that we have a system that has two threats.

41
00:03:01,590 --> 00:03:03,150
Remember, one other thing.

42
00:03:03,150 --> 00:03:09,690
When we use the word task and threat, the almost the same, they can be used interchangeably.

43
00:03:10,200 --> 00:03:12,420
If you want a definition, a task.

44
00:03:13,610 --> 00:03:16,790
A task becomes a threat when it is executing.

45
00:03:17,860 --> 00:03:24,370
So you can think of a threat as a task which is being executed, so when you hear the two words, it's

46
00:03:24,370 --> 00:03:26,940
safe to assume the same, right?

47
00:03:26,950 --> 00:03:31,930
People have been arguing about the the correct definition for a while, but you can just assume the

48
00:03:32,010 --> 00:03:32,320
same.

49
00:03:32,830 --> 00:03:39,910
So let's assume we have in our firm where we have two tasks or two threats, the red one and threat

50
00:03:39,910 --> 00:03:40,230
to.

51
00:03:41,510 --> 00:03:46,070
How do we move from threat to threat to something known as context, which in.

52
00:03:46,900 --> 00:03:47,830
We save.

53
00:03:48,690 --> 00:03:52,560
Once we've executed, let's say we've executed the red one.

54
00:03:53,710 --> 00:03:59,160
We want to go to threatener, we saved the execution states of the red one, and then we restore the

55
00:03:59,160 --> 00:04:06,210
execution state of threat to and then we switch to threat to once we've finished working on, threat

56
00:04:06,210 --> 00:04:12,390
to, we see if the execution state of threat to restore the execution states of the red one.

57
00:04:12,390 --> 00:04:13,830
And then we switch to threat one.

58
00:04:14,520 --> 00:04:15,690
What we mean by this.

59
00:04:17,590 --> 00:04:18,050
Right.

60
00:04:18,730 --> 00:04:25,540
So in a microcontroller in our cortex microcontroller that exist what is known as the register bank.

61
00:04:26,520 --> 00:04:32,940
The register bank is just a set of registers, we have we have our issue, our one, our three all the

62
00:04:32,940 --> 00:04:37,920
way to our fifteen and these registers, you can think of the register as a story.

63
00:04:38,550 --> 00:04:42,360
And the story, just like a story, just allows quick access.

64
00:04:42,360 --> 00:04:47,510
The quickest of all access is the the access of what is in your register.

65
00:04:48,060 --> 00:04:53,640
It's quicker to access the register content way quicker down to access what is a memory.

66
00:04:53,640 --> 00:04:59,280
So you can think of the registers as fast, fast, accessible storage as.

67
00:05:00,480 --> 00:05:06,630
So during the execution of your program, even without an artist, the state of the program, let's

68
00:05:06,630 --> 00:05:13,890
say the values of your local variables, the loop iterator, the state of the program is stored in these

69
00:05:13,890 --> 00:05:18,870
registers and they are manipulated in these registers and the result is often produced.

70
00:05:19,320 --> 00:05:27,210
So if you want to save the state of a particular program, you have to save the content of the registers.

71
00:05:28,480 --> 00:05:34,510
So in Archos, whether you're building your own Archos or whether you are using an already existing

72
00:05:34,510 --> 00:05:42,010
archer such as Free Artus, what you always do is you would have to allocate some space in memory to

73
00:05:42,010 --> 00:05:45,250
be saving the content of the register.

74
00:05:46,370 --> 00:05:52,100
So what we would do is when we are creating our own Archos, we would have to allocate a stack size.

75
00:05:53,460 --> 00:06:00,810
When we are using already made our tools, such as free tools or micro, we would have to define the

76
00:06:00,810 --> 00:06:02,310
size of stock we are using.

77
00:06:03,280 --> 00:06:07,300
So in this example, we are seeing Sèvres execution states of threat to.

78
00:06:07,960 --> 00:06:10,210
Was this what this diagram is trying to depict?

79
00:06:10,210 --> 00:06:13,580
Is that the content of the registers?

80
00:06:13,630 --> 00:06:15,220
Are you through AR 15?

81
00:06:15,790 --> 00:06:18,820
The content is collected and saved onto the stack.

82
00:06:19,390 --> 00:06:21,940
The stack is just a memory space that we shall declare.

83
00:06:21,940 --> 00:06:22,370
We shall see.

84
00:06:22,390 --> 00:06:29,860
Give us a stack of one hundred byte and then the content of the register whenever we whenever we reach

85
00:06:29,860 --> 00:06:36,010
the point where the execution of state of threat to is to be saved is going to be taking everything

86
00:06:36,010 --> 00:06:42,640
in, in our cortex and register bank, it's going to be taken and stored in this stack that we've allocated.

87
00:06:43,100 --> 00:06:46,750
This is what we called this is what we call save in the context.

88
00:06:47,770 --> 00:06:48,230
Right.

89
00:06:48,580 --> 00:06:55,450
So we said in our diagram, we said save the execution state of threat to.

90
00:06:55,450 --> 00:06:59,460
Once we've done that, we see restored execution, state of the red one.

91
00:06:59,740 --> 00:07:06,730
So once we've saved this, assuming that we've already saved the the execution states of the red one,

92
00:07:07,150 --> 00:07:09,010
we would restore execution.

93
00:07:09,010 --> 00:07:09,970
State of the red one.

94
00:07:10,930 --> 00:07:14,020
Over here, so as we sift through a two.

95
00:07:15,580 --> 00:07:18,730
We would have to restore execution stage of thread one.

96
00:07:21,940 --> 00:07:22,370
Right.

97
00:07:22,720 --> 00:07:29,710
So what this means is that we are assuming we've already saved through Red One onto its stock, so our

98
00:07:29,710 --> 00:07:31,990
program started by running thread one.

99
00:07:32,530 --> 00:07:38,410
And the other question you would ask is, OK, when do we know it's time to save and it's not time to

100
00:07:38,410 --> 00:07:38,770
save?

101
00:07:39,850 --> 00:07:47,920
The default mode of the the most popular way of running an artist is using time scheduling method known

102
00:07:47,920 --> 00:07:50,430
as the known as Round-Robin Scheduling.

103
00:07:50,440 --> 00:07:55,270
And what this means is that each thread executes for a set amount of time.

104
00:07:55,540 --> 00:08:01,440
And when that time expires, automatically the system would move to the next thread.

105
00:08:01,540 --> 00:08:06,540
The thread scheduler would automatically switch from the current threat to the next one.

106
00:08:07,450 --> 00:08:14,020
So let's see if that time amount is 10, MS, 10 milliseconds after ten milliseconds, we would save

107
00:08:14,410 --> 00:08:21,530
if we are currently executing the red one after ten milliseconds, we would save the the the execution

108
00:08:21,530 --> 00:08:26,530
state of the red one onto its stock and then restore the execution state of threat to.

109
00:08:27,670 --> 00:08:32,920
So if we are run in the round robling, round robin schedule an algorithm, this is how it would take

110
00:08:32,920 --> 00:08:33,290
place.

111
00:08:33,310 --> 00:08:34,390
That's just an example.

112
00:08:34,660 --> 00:08:38,270
OK, but there are other scheduling algorithms out there.

113
00:08:38,500 --> 00:08:39,330
So let's start again.

114
00:08:39,340 --> 00:08:47,050
We said, OK, we we are currently executing three, two with executed threat, two for ten milliseconds,

115
00:08:47,050 --> 00:08:52,030
which is the time Quanta or the Round-Robin time slice that each threat has to execute.

116
00:08:52,360 --> 00:08:56,550
After ten milliseconds, we save the execution state of threat two.

117
00:08:56,560 --> 00:09:03,850
So we take the content in our register bank and save it onto the threat to stack that we are located.

118
00:09:04,920 --> 00:09:10,710
After we've done that, because we have just two threads, thread one and three, two, we will go to

119
00:09:10,710 --> 00:09:18,810
the stack of thread one and then take it content and put it back into our Coatex and registers of we

120
00:09:18,810 --> 00:09:20,950
are ready to execute the red one now.

121
00:09:21,150 --> 00:09:24,680
This is known as restoring the execution state of the red one.

122
00:09:25,080 --> 00:09:30,660
So then once we've restored the execution status of the red one, we are going to execute the red one

123
00:09:30,660 --> 00:09:31,860
for ten milliseconds.

124
00:09:31,980 --> 00:09:40,140
And once that 10 milliseconds expires, then we save the execution states of Red one back onto its stack

125
00:09:40,470 --> 00:09:46,800
and then we restore the execution state of threat to because we are back to executing through it, too.

126
00:09:47,040 --> 00:09:50,760
And then this process continues, this loopier, it continues.

127
00:09:51,060 --> 00:09:58,010
So then we have the two threats to main functions executing a SCIF execute and in parallel.

128
00:09:58,500 --> 00:10:03,240
So when this happens, it happened so fast that we assume that happening at the same time.

129
00:10:03,600 --> 00:10:05,730
But it's just context switch.

130
00:10:05,730 --> 00:10:07,430
And this is known as context switching.

131
00:10:08,250 --> 00:10:15,690
So if you're asked what is context switching contexts, which involves saving the execution states of

132
00:10:15,960 --> 00:10:22,470
currently executing threat and restoring the execution state of the next threat to be executed.

133
00:10:22,470 --> 00:10:26,850
As simple as that, if you have any questions, of course you can live it in the questions analysis

134
00:10:26,850 --> 00:10:27,210
area.

135
00:10:27,620 --> 00:10:37,290
OK, so now let's give let's talk about the differences between a threat of interrupt service routine

136
00:10:37,290 --> 00:10:39,540
and then our busy wait system.

137
00:10:40,200 --> 00:10:46,380
So we should do that in the next lesson, which will give the differences between the way we often write

138
00:10:46,380 --> 00:10:52,500
our sequential code, comparing that to a real time operating system and then comparing that to having

139
00:10:52,500 --> 00:10:56,210
an interrupt service routine, which I shall explain in the next lesson.

140
00:10:56,220 --> 00:10:56,730
So I'll see you.

141
00:10:56,730 --> 00:10:59,620
If you have any questions, just leave them the questions analysis area.

142
00:11:00,350 --> 00:11:04,530
If you're flying into the course useful, just leave a review or a recommendation.

143
00:11:04,740 --> 00:11:05,570
I'll see you later.
