Hello, welcome back.
In this lesson, we shall give an overview of semaphores so a SEMAFO is a signal or a key send between
tasks or between tasks and interrupt.
It does not carry any data.
We can categorize semaphores into three groups.
You would see that they overlap a lot, but you would understand the tiny differences, so.
We have binary semaphores, this SEMAFO can assume two values, one or zero, and this indicates whether
at TASC has the key or it doesn't.
So in a binary SEMAFO, you either have the key or you don't have the key.
We also have counting SEMAFO, this SEMAFO has a counter associated with it and the semaphore and the
counter can be incremented or decrements at the counter indicates the number of keys available to access
a particular resource.
You can think of the number of keys available to.
To enter a room with a limit, a capacity, OK, counting semaphores gives us this ability and also
we have something known as a mutex mutex stands for mutual exclusion.
And this is used to allow multiple task to access a single shared resource, but only one at a time.
We see how all of these examples work in real code.
Now, let's see.
Let's see some of the commonly used APIs to create a binary semaphore, we simply need to use the function
x semafo create binary.
And then to give the key meaning, you finish using the SEMAFO or you finish using, you can think of
this as a key to the washroom or the restroom, you finish using the restroom.
So you want to give the key back to give the key back so that someone else can take it and go to the
restroom.
You use the semaphore give and this takes us argument.
The handle of the semaphore.
And there is a version for this when we are dealing with interrupt service routines and one thing I
should point out in frittatas.
All the functions that can be called in and interrupt service routine has the word hostilities, I guess
are at the end of the name.
We are only allowed to use APIs with the letters, I guess are at the end of their name in interrupt
service routines.
And we see when we stop working and interrupt service routines, we'll be using these functions so it's
not allowed to use X semafo, give an uninterrupted service routine.
If we are not interrupt service routine, we have to use X semaphore.
Give from interrupt.
Right, and this takes to document the SEMAFO, and the second argument is a higher priority task to
be Wolken, this particular one will be to describe the second argument much more in the code when we
are explaining it.
OK, next, we have the API for taking the key or the SEMAFO, and this takes to document the handle
of the SEMAFO and it takes to weight, which is very common in most of our APIs.
And now we look at counting semaphores by default.
Counting semaphores are not enabled to use contin semaphores.
We have to go to our free Arktos, configured each file and set config use in semaphores to one.
We can use counting semaphores to count events or to manage resources.
Over here to create account in SEMAFO, we see Ximo for Crate Co. this takes to argument.
The maximum number of count and the initial number of count, so if let's say.
We are using counting semaphore to create the number of tickets for a nightclub and the capacity of
the nightclub is 50.
We can see X semafo crate counting, the maximum count is 50 if we have given five already, we can
see the initial count is five.
But often Woog said the initial count zero.
Right.
Also, take a look at mutex.
We said we can we can categorise him by force into binary semaphores, counting, semaphores and mutex,
and we said we have to enable counting semaphores in our free Archos config that HFA also which mutex
we have to enable this in our free ARTUS conflict each we have to set config use new Texas to one.
And.
To create a mutex we simply call SEMAFO, create mutex function as simple as that.
Now, before we go to our IDY and write the code, let's talk about some some issues that we might face
when we don't properly use semaphores and mutex.
We have something known as priority inversion, this occurs when a higher priority task is waiting for
a lower priority task.
And because it is waiting for this lower priority task, it has its inherently assumes the priority
of the lower priority task.
So you can think of a higher priority task waiting for a semaphore from a lower priority task.
Ordinarily, we have to have the higher priority task running if it needs to run.
But in this case, because it is waiting for something from the lower priority task, its priority gets
inverted, its priority becomes law.
This is what we call priority version.
Also, there is something known as priority inherit inheritance, and this involves raising the priority
of a resource holder to the priority of the higher priority task, waiting for the resource for a short
duration of time.
So we can if a lower priority task has the key or a resource, we can increase its priority to match
the priority of the higher priority task, waiting for the key for a short duration of time.
This is called priority inheritance.
And one last one, which gets a lot of people in trouble is the deadlock.
This is also known as the deadly Embry's.
This occurs when two tasks cannot proceed because they are both waiting for a resource that is held
by the other one.
It's as if the ball has to be bounced, like in basketball, the ball will be thrown in the air and
someone would grab it and then they start controlling the ball.
But you can imagine throwing the ball in the air and the ball is stuck and no one grabs the ball.
You can think of that as a deadlock, of course, would would show a practical example with with code
when we start writing our code, right.
So we have priority inversion, priority inheritance and deadlocks.
Also, there is a task known as the gatekeeper task, which which will introduce, you know, practical
lessons, a gatekeeper task is a task that has the sole ownership of a resource.
Only the gatekeeper task is allowed to access that resource directly.
Any other task needing to access that resource can do so only indirectly by using the services of the
gatekeeper task.
And we can use the gatekeeper task approach to reduce the problems we face.
When we don't properly configure our semaphores, we shall see how to apply the gatekeeper task as well.
So this order is for an overview on semaphores.
If you have any questions, kindly leave them in the questions and answer section and I'll show you
the next lesson.
How?