1
00:00:00,750 --> 00:00:01,780
Hello, welcome back.

2
00:00:02,250 --> 00:00:10,050
In this lesson, we're going to see how to change the priority of a task during runtime, I'll make

3
00:00:10,050 --> 00:00:13,980
a copy of our last project, number four, top priority here.

4
00:00:16,080 --> 00:00:17,990
Copy and then paste over here.

5
00:00:19,570 --> 00:00:22,480
And then I'll call this number five.

6
00:00:32,850 --> 00:00:35,840
Change in priority in runtime, I'll call this.

7
00:00:39,050 --> 00:00:39,670
OK.

8
00:00:40,840 --> 00:00:43,630
So I'm going to open this and then.

9
00:00:45,410 --> 00:00:54,500
I'm going to open or may not see, right, so let's see in our task creation, we have Gnarl here and

10
00:00:54,500 --> 00:00:57,050
now here we made use of this Fastenal.

11
00:00:57,050 --> 00:00:58,150
We know this now.

12
00:00:58,190 --> 00:01:05,090
As for the the task parameter, if we have any task parameter, this where we pass it, this second

13
00:01:05,090 --> 00:01:08,780
null is for a variable to hold the task.

14
00:01:08,780 --> 00:01:11,320
Hondo, we've never used the task Hondo.

15
00:01:11,450 --> 00:01:18,920
However, in this lesson we're going to introduce the tasks the task handle the task undo allows us

16
00:01:18,920 --> 00:01:22,460
to configure the task during runtime.

17
00:01:22,460 --> 00:01:23,960
We need a handle of the task.

18
00:01:24,140 --> 00:01:27,590
You can think of something you hold in order to control something.

19
00:01:27,770 --> 00:01:32,350
So we need a handle of the task to be able to configure it or make changes to it.

20
00:01:32,690 --> 00:01:34,070
So I'm going to come over here.

21
00:01:35,760 --> 00:01:40,020
And then I'm going to I'm going to have the task Hondo here.

22
00:01:45,330 --> 00:01:54,330
Task Hondo, let's see, I'm going to see task, I'm going to create a task Hondo type.

23
00:02:02,200 --> 00:02:11,860
And then I call this blue Hondo for blue Tosk, red Honda for Red Tosk.

24
00:02:13,110 --> 00:02:15,930
And then we have green Honda.

25
00:02:24,000 --> 00:02:32,550
OK, so I'm going to pass these condos to the various tasks, so over here, when we created Blue Task,

26
00:02:33,210 --> 00:02:34,050
I'll copy this.

27
00:02:34,560 --> 00:02:38,910
The last document is supposed to be the position of the ampersand over here.

28
00:02:39,150 --> 00:02:43,550
And then there's the name of the variable essentially parsing the address here.

29
00:02:43,920 --> 00:02:45,900
So Red would have this.

30
00:02:49,080 --> 00:02:52,880
And then green controversy would have this.

31
00:02:57,900 --> 00:03:00,490
OK, so we have the task, hundreds now.

32
00:03:01,110 --> 00:03:05,460
Now we come to our task functions, we said we want to change the priority of the tasks.

33
00:03:06,000 --> 00:03:13,530
We're going to start off the C, we're going to start off by let's say we give Blue a priority of two

34
00:03:13,920 --> 00:03:16,200
rights during creation.

35
00:03:16,200 --> 00:03:18,090
Blue has a priority of two.

36
00:03:18,540 --> 00:03:21,230
Red has a priority of two as well.

37
00:03:21,480 --> 00:03:23,760
And then let's say Green has.

38
00:03:24,780 --> 00:03:30,450
Greenhaus, a priority of one, so we know what's going to happen, essentially based on our previous

39
00:03:30,450 --> 00:03:37,620
experiment, when we run the program, we're going to have just blue and red executing and green won't

40
00:03:37,620 --> 00:03:39,430
have any time at all to execute.

41
00:03:39,930 --> 00:03:43,780
OK, so what are we going to do is we know blue is going to be executed.

42
00:03:43,860 --> 00:03:52,740
We're going to go into the task function of blue and then we would use that to set the priority of green

43
00:03:52,740 --> 00:03:53,200
higher.

44
00:03:53,700 --> 00:03:56,720
So over here, when we start off, green has a priority of one.

45
00:03:57,390 --> 00:03:59,640
So we would go inside.

46
00:04:01,010 --> 00:04:08,980
We'll go inside one of the execution, was it a blue or red and then change the priority of green shirt

47
00:04:09,150 --> 00:04:11,330
that so I'll come over here to red.

48
00:04:11,750 --> 00:04:18,430
So red increment, it's variable and then let's say it delays for a while.

49
00:04:19,610 --> 00:04:22,550
It will delay for let's say, I'll say four over here.

50
00:04:24,830 --> 00:04:31,730
I'll put a local variable here and I say, for I equals zero,

51
00:04:36,380 --> 00:04:39,560
sorry about the composition in the microphone equals zero.

52
00:04:40,460 --> 00:04:44,390
I less than, let's say, a value such as seven hundred thousand.

53
00:04:47,720 --> 00:04:54,830
I plus plus there's a reason I'm not using delay functions, actually delay functions here because.

54
00:04:57,310 --> 00:05:03,520
When we get to where the task goes to sleep, we would use the delay, you use the delay to essentially

55
00:05:03,520 --> 00:05:10,080
put the task to sleep and by putting it to sleep, the scheduler removes it from running states.

56
00:05:10,390 --> 00:05:11,280
We don't want that.

57
00:05:11,320 --> 00:05:14,310
We just want we want to simulate delay.

58
00:05:14,320 --> 00:05:15,780
We don't want to put it to sleep.

59
00:05:15,790 --> 00:05:17,830
That is why we use in for loops here.

60
00:05:18,100 --> 00:05:24,230
You would understand when we get to the bit where we are, we put in the task to sleep, OK?

61
00:05:24,370 --> 00:05:32,980
So over here we delay for this amount and the function for the priority is the V task priority set function.

62
00:05:33,790 --> 00:05:35,410
And this function takes the argument.

63
00:05:35,410 --> 00:05:40,450
The first argument is the handle of the task, whose priority you wish to set.

64
00:05:40,750 --> 00:05:45,850
And the second argument is the new priority level you want to assign to that task.

65
00:05:46,180 --> 00:05:48,010
So come over here, I'll say.

66
00:05:49,930 --> 00:05:54,700
The task priority said.

67
00:05:55,820 --> 00:06:04,610
And then I want to said this to green task, so I use the green Honda and then I want to assign priority

68
00:06:04,610 --> 00:06:06,680
three to it like this.

69
00:06:07,380 --> 00:06:10,850
OK, so let's run this and see what we have.

70
00:06:14,090 --> 00:06:15,200
Click over here to build.

71
00:06:16,980 --> 00:06:19,500
OK, that's an issue, but as I say.

72
00:06:20,820 --> 00:06:21,720
The task.

73
00:06:24,840 --> 00:06:26,790
We top priority set.

74
00:06:29,470 --> 00:06:34,270
What does it say to function as a green Honda, Undeclared says?

75
00:06:36,110 --> 00:06:38,940
OK, I'm using a cocktail here, it should be a small H.

76
00:06:39,470 --> 00:06:40,370
Click here to build.

77
00:06:42,380 --> 00:06:44,150
OK, it's both successfully.

78
00:06:46,830 --> 00:06:48,270
Click here to get onto the board.

79
00:06:50,940 --> 00:06:52,620
Yes, well, we need number five here.

80
00:06:54,160 --> 00:06:54,810
OK.

81
00:06:59,300 --> 00:07:00,230
Click here to switch.

82
00:07:02,620 --> 00:07:09,400
OK, so let's see what's going to happen or what we expect to happen with created three tasks, the

83
00:07:09,400 --> 00:07:13,750
first task as priority of to the second highest priority of two.

84
00:07:13,990 --> 00:07:16,780
The third one is the green task, the highest priority of one.

85
00:07:16,960 --> 00:07:20,200
So when we start, we're going to have just these two tasks running.

86
00:07:20,590 --> 00:07:27,480
But inside a red task, we call a function to set the priority of green task to three.

87
00:07:27,940 --> 00:07:31,520
So all of a sudden the priority of green becomes the highest.

88
00:07:32,170 --> 00:07:33,670
So let's see what happens.

89
00:07:33,820 --> 00:07:34,600
Click to run.

90
00:07:36,980 --> 00:07:37,570
OK.

91
00:07:39,010 --> 00:07:42,100
So that is what we ended up with these two Froese.

92
00:07:43,130 --> 00:07:45,320
Essentially so red.

93
00:07:46,450 --> 00:07:47,650
OK, imposed this.

94
00:07:49,380 --> 00:07:56,100
So blue started blue and red, they started with the same priority level, so they were sharing the

95
00:07:56,100 --> 00:08:02,250
time blue was expected to execute for X amount of time, say ten milliseconds.

96
00:08:02,610 --> 00:08:08,700
After ten milliseconds, red would execute for ten milliseconds and then blue blue would execute again

97
00:08:08,700 --> 00:08:13,410
for ten milliseconds and then ready to execute again for ten milliseconds, then to continue going back

98
00:08:13,410 --> 00:08:18,660
and forth like that because they have equal priority and then the other one has a lower priority.

99
00:08:18,840 --> 00:08:26,490
But this will happened blue finish executing for the assigned amount of time or known as the time cancer.

100
00:08:26,820 --> 00:08:34,530
But when Red tried to consume all of it time, when Red started executing its first executing its first

101
00:08:34,530 --> 00:08:39,480
execution, the first line is to increment the variable and then wait for a bit.

102
00:08:39,840 --> 00:08:46,080
And then the second line is to execute this instruction, which essentially said to the priority of

103
00:08:46,440 --> 00:08:47,470
green to three.

104
00:08:47,610 --> 00:08:53,790
So when that happened, red was taken off the color red was taken from the time time was taken from

105
00:08:53,790 --> 00:08:54,110
red.

106
00:08:54,630 --> 00:08:58,260
So red was moved out of the running states.

107
00:08:58,260 --> 00:09:00,900
All of a sudden Green had the highest priority.

108
00:09:01,110 --> 00:09:05,560
So Green started executing and green is going to execute forever.

109
00:09:05,560 --> 00:09:11,060
So because now green house priority of three, the rest just have priority of two.

110
00:09:11,250 --> 00:09:13,550
So you see just green task executing.

111
00:09:13,980 --> 00:09:19,790
So we've essentially changed the priority of green during runtime, right?

112
00:09:19,830 --> 00:09:20,520
Very easy.

113
00:09:20,520 --> 00:09:21,360
Very straightforward.

114
00:09:21,600 --> 00:09:22,800
These are just the basics.

115
00:09:22,800 --> 00:09:29,600
It's important to to get familiar with the basics, every aspect of it, before you start building a

116
00:09:29,850 --> 00:09:31,230
full project out of them.

117
00:09:31,830 --> 00:09:33,120
So, yeah.

118
00:09:36,170 --> 00:09:42,500
So we've seen how a task can can change the priority of another task.

119
00:09:42,920 --> 00:09:46,280
What if a task wants to change its own priority?

120
00:09:47,780 --> 00:09:48,560
How about that?

121
00:09:49,700 --> 00:09:52,160
So what are we going to do known as?

122
00:09:54,690 --> 00:09:56,010
I'm going to comment this out.

123
00:09:57,540 --> 00:10:03,570
Well, I could have created a new project for this so that you have the two projects for revision,

124
00:10:03,570 --> 00:10:07,370
but it's such a tiny change, I think you would remember.

125
00:10:07,710 --> 00:10:08,820
So I comment this out.

126
00:10:09,810 --> 00:10:10,440
And.

127
00:10:11,480 --> 00:10:12,860
What I'm going to do is.

128
00:10:15,190 --> 00:10:16,240
I'm going to start off.

129
00:10:19,630 --> 00:10:24,640
You know what, you know what's just bear with me, I think for your revision, I've done this thing

130
00:10:24,640 --> 00:10:25,890
for four years.

131
00:10:26,650 --> 00:10:29,740
It's important for students to have these projects separate.

132
00:10:29,750 --> 00:10:36,070
I'm going to create a new project just to demonstrate that I'll save this as it was, and then I'm going

133
00:10:36,070 --> 00:10:37,240
to come over here.

134
00:10:38,070 --> 00:10:39,510
And then creates a new project.

135
00:10:42,430 --> 00:10:44,580
OK, so come over here.

136
00:10:46,700 --> 00:10:49,090
Copy and then paste this over here.

137
00:10:49,950 --> 00:10:50,730
Number six.

138
00:10:52,930 --> 00:10:55,000
And then change in own priorities.

139
00:10:55,040 --> 00:10:55,630
I call this.

140
00:11:13,420 --> 00:11:15,010
OK, so I'm going to open this.

141
00:11:20,520 --> 00:11:20,890
Right.

142
00:11:20,910 --> 00:11:27,900
So what are we going to do here is over here, I'm going to start off by keeping blue.

143
00:11:28,740 --> 00:11:31,140
Blue is going to be created with priority number two.

144
00:11:31,140 --> 00:11:35,700
The other one red who have priority one in green will have priority one.

145
00:11:35,880 --> 00:11:38,460
So when we start just blue be executed.

146
00:11:38,880 --> 00:11:44,000
But what are we going to do is we're going to let Blue drop down its priority when the program starts.

147
00:11:44,010 --> 00:11:45,930
I'm going to clean this from the previous projects.

148
00:11:46,440 --> 00:11:52,560
So I'm going to go to the the task function of blue and I'm going to say sets priority of blue to one.

149
00:11:52,890 --> 00:11:55,410
So blue is going to increment just one.

150
00:11:55,770 --> 00:12:01,140
Its profile would increment just once and then we're going to drop the priority of blue to one.

151
00:12:01,140 --> 00:12:08,190
And when the priority of blue drops to want everyone to have access to time, everyone would run for

152
00:12:08,190 --> 00:12:09,270
equal amount of time.

153
00:12:09,750 --> 00:12:10,260
Let's see.

154
00:12:10,530 --> 00:12:13,770
So to do this, we use the same priority V task.

155
00:12:17,960 --> 00:12:18,950
Priority said.

156
00:12:22,380 --> 00:12:30,000
The first argument is now, and the second argument is the priority level, so if you want to change

157
00:12:30,000 --> 00:12:35,850
the priority of the task from which you are calling this function, you pass null.

158
00:12:37,490 --> 00:12:43,310
So I can either personally hear or I can pause blue Honda, remember Blue House to I can see blue Honda,

159
00:12:43,460 --> 00:12:45,760
but because I'm in blue, I can simply say no.

160
00:12:46,220 --> 00:12:48,290
Okay, I'll click here to run.

161
00:12:49,950 --> 00:12:55,530
And then click here to do it on our click here to build and then this one to run, this number six.

162
00:12:58,120 --> 00:12:59,680
I say, OK, over here.

163
00:13:04,260 --> 00:13:04,800
Switch.

164
00:13:07,860 --> 00:13:08,580
So let's see.

165
00:13:10,870 --> 00:13:16,210
OK, so as you can see, they're all running, but we know how we started, it started with blue.

166
00:13:17,820 --> 00:13:18,540
It started with.

167
00:13:24,510 --> 00:13:30,990
It started with blue incrementing once and then setting the priority for the rest.

168
00:13:32,310 --> 00:13:32,750
Right.

169
00:13:35,510 --> 00:13:36,890
But it looks as if.

170
00:13:38,160 --> 00:13:41,820
These two red and green are running more than blue.

171
00:13:43,060 --> 00:13:43,690
The see?

172
00:13:48,530 --> 00:13:56,180
OK, so I suppose what happened is when the priority of blue dropped, the priority of blue became the

173
00:13:56,720 --> 00:14:05,540
the same priority as red and green, but blue went to the back of the queue, so red and green had to

174
00:14:05,540 --> 00:14:08,810
execute before blue had its chance to execute.

175
00:14:08,840 --> 00:14:11,090
So that is why I would see this disparity here.

176
00:14:11,510 --> 00:14:11,930
Right.

177
00:14:12,530 --> 00:14:13,490
Because it's counting.

178
00:14:13,670 --> 00:14:15,620
It's yeah, it's cumulative.

179
00:14:16,010 --> 00:14:20,630
But now they are all executed for equal amount of time because they have equal priority.

180
00:14:21,110 --> 00:14:21,500
Right.

181
00:14:21,770 --> 00:14:26,540
So this is a with changing TASC priorities will conclude the lesson here.

182
00:14:26,540 --> 00:14:31,460
If you have any questions at all, you should leave them in the questions and answers area and I'll

183
00:14:31,460 --> 00:14:32,030
see you later.

184
00:14:32,360 --> 00:14:38,670
Finding the course useful or if you have any feedback at all, do leave it in the in the review area.

185
00:14:39,020 --> 00:14:40,310
I'll see you have a nice to.
