0:07
hello everyone welcome to controllers
0:11
Tech this video is part 2 of mutex in
0:15
stm32 and today we will look at the
0:18
priority inversion and priority
0:20
inheritance this video is the
0:23
continuation of my last video so if you
0:27
haven't watched my last video yet I will
0:29
advise you to do that first
0:32
well let's resume from where we left off
0:35
I will open my last project let's
0:39
uncomment this task now we need all
0:43
three tasks in this video we will see
0:47
the difference between binary semaphore
0:49
and mutex so let's create a binary
0:54
semaphore first of all we need to create
0:57
the semaphore handler and let's call it
1:09
in this function instead of taking mutex
1:12
we will acquire the binary semaphore I
1:19
will change this delay position and
1:22
increase it to five seconds
1:34
now just like we create the mutex we
1:37
will create the binary semaphore also if
1:41
the semaphore gets created successfully
1:43
we will print this string after creating
1:52
binary semaphore it must be first given
1:55
before taking it let's make some small
2:00
changes in the higher task and the lower
2:09
I will create a new medium priority task
2:17
this task will print this string and it
2:21
does not need any semaphore or mutex to
2:40
and I am giving it the highest
2:42
suspension time let's build this code
2:50
few warnings are there we will debug the
3:11
let the program run for a while I will
3:15
explain what is going on here
3:34
okay let's see now the semaphore and
3:38
mutex both are created the high priority
3:42
task will run first acquire the
3:44
semaphore print the string release the
3:47
semaphore and exit the medium priority
3:51
task doesn't the semaphore so it will
3:53
run normally now the control enters the
3:57
low priority task it will call the
4:00
function send UART and we'll wait for
4:02
five seconds to complete but by then
4:05
high task will wake up and it will also
4:08
call the send UART function it will try
4:12
to acquire the semaphore which is held
4:14
by the low task higher tasks have no
4:18
other option but to wait for the
4:19
semaphore to become available now both
4:23
high and low tasks are waiting medium
4:26
task will wake up and run as it doesn't
4:30
need any semaphore it will continue to
4:32
run every two seconds now think of a
4:36
situation where those five seconds are
4:38
over and low task wants to execute but
4:41
it can't because the medium task is
4:44
still running the high task which was
4:47
already waiting for the low task to
4:49
release the semaphore now also have to
4:51
wait for the medium task to finish its
4:53
execution so high task have to wait for
4:57
the medium task and this scenario is
5:00
called priority inversion once the
5:03
medium task finishes low task can
5:06
execute further and release the
5:21
hi tasks will acquire the semaphore
5:23
print the string and exit again the MPT
5:28
will run and finally the low task will
5:32
exit this whole sequence will continue
5:35
in the similar way forever I hope you
5:38
understood the priority inversion
5:39
properly I will take a picture of this
5:46
to compare with the mutex output now
5:50
let's see how to avoid this priority
5:52
inversion using mutex I am going to
6:00
change the semaphore with mutex and rest
6:03
of the code will remain same
6:15
let's build and debug this now
6:32
let the program run for a while
6:57
okay let's see I will open the picture
7:01
from the semaphore to compare it with
7:21
the initial part is same in both of them
7:24
control enters the high task
7:26
acquire the mutex print the string
7:29
release the mutex and exit the task
7:32
medium task will run it doesn't need
7:35
mutex low task will run and it will
7:39
acquire the mutex just like as it
7:43
acquired the semaphore in the other case
7:52
now there will be a five seconds delay
7:55
hi task will wake and try to acquire the
7:58
mutex now is the important part as the
8:02
high task is waiting for the mutex and
8:05
the low task have the mutex the priority
8:08
of the low task will change to that of
8:09
the high task this scenario is called
8:13
the priority inheritance because the low
8:16
task inherited the priority of the
8:18
highest task waiting for the mutex now
8:22
the medium task cannot preempt this low
8:24
task because the low task has the
8:27
highest priority now this was not the
8:33
case with semaphore and medium task
8:36
preempted the low task in that now once
8:41
the low task releases the mutex high
8:44
task will acquire it print the string
8:46
releases it and exit itself the medium
8:50
task can finally run now and at last low
8:54
task will also exit basically both the
8:58
output are same except the medium task
9:01
card preempted the low task in the mutex
9:03
and that's because the low task inherits
9:07
the priority of the highest waiting task
9:09
which is higher than the medium task the
9:13
same sequence will run forever so this
9:17
is what priority inversion means and
9:19
this is how it is corrected by using
9:21
priority inheritance this is for the
9:25
video guys I hope you understood the
9:28
concept clearly you can download the
9:32
code from the link in the description
9:34
leave comments in case of any doubt keep
9:38
watching and have a nice day