Hello, guys, and welcome back and this lecture we're going to talk about.
A totally new concept, and it's called the binary SEMAFO.
Now, when you get to the multi threading world, it's a bizarre world and things don't really run as
expected all the time because there is a lot of consideration that you need to take before you start
writing multi threading programs.
So the first concept that we need to focus on is the semaphore.
Now, let me give you a scenario, you assume that I have two hardware devices that are trying to access
the same resource at the same time.
Now, this example could be one small ultrasonic sensor and one potential meter.
So this ultrasonic is trying to rewrite its value and this potential meter is also trying to write.
At the same time, there is only one hardware here.
And the moment you say at the same time and at the same instant, things start to get complicated because
let us get this question.
Which value will be displayed when these two are trying to do it at the same time, when you are trying
to write at the same time, will the ultrasonic sensor valuable be displayed here or will the potential
military value displayed here?
Well, if they both write at the same time, neither of this will be correct.
The result will be a corrupted data and you hope the program will malfunction and will freeze and hold.
There is a huge possibility of having a corrupted data and a device that is in a corrupted state.
You could be lucky and you could be getting some weird values here or garbage values here because of
what happened.
Because, again, if you try to write five year and this one is trying to write to which value will
be considered by this LCD, you are trying to write to the same place right at the same time.
What is the solution for this?
I mean, how come multiple devices on the hardware sharing the same resource?
I mean, it could it could be an led that two programs are racing to turn them on or off.
Maybe one of them is trying to turn their lead on and the other one is trying to turn that off.
Which one will win?
Well, the result is a corrupted data or corrupted state, or maybe one of them will win, causing the
other desk to malfunction because it could not provide its value.
Right now, the solution is a SEMAFO.
Well, let us imagine it as an object, OK?
Whoever gets this object will be able to write to this LCD.
OK, so let's say that the transmitter here, the ultrasonic acquires the semaphore.
Now it has access to right on this LCD right here.
Right.
It will right its value.
Now, let's say at the same time, this potential meter is trying to acquire the semaphore.
However, the ultrasonic has already warned the semaphore.
Well, it means that this task, that is the potential meter writing to the LCD task will be in the
blocked state waiting for the semaphore to be available again.
So the symbol for right now is with ultrasonic.
This one is racing to get it, but it could not get it.
So and in autos, it will be in what we call the block state.
Do you remember the state we have talked about when we introduced Arktos and multi threading it is that
state.
So right now, a potential meteor cannot access this.
Now, whenever this one is done doing its job with the city, it's going to release the semaphore or
give it away.
Now, this one will be again in the on state.
It will acquire the semaphore and it will be able to write the LCD.
So it is simply as racing on who gets the semafo.
You'll get it, you're right, you data, you release it and you're led, let the next guy in line to
be transferred from the block state to the acquiring semaphores state and you'll get the second device
right.
Let's say while this one is writing, this one also tries to access the semaphore.
This one is already having the SEMAFO.
It writes its data, it releases it.
Then this one will acquire the semaphore again and it will write the data.
So it's just tracing on the SEMAFO and you can imagine it as who gets the key in in order to open this
shared resource.
So as I said, that's a choice to have the semaphores if it's available.
Otherwise, if it's not available, the state will be blocked and will be waiting until it's a freed.
But the question is here, who is the symbol for and takes it, is it automatic or how do you exactly
do it?
Well, inside every task that needs to access a shared hardware, whenever we reach the portion of code
where we need to access this hardware, we need to write a line or a function in order to acquire the
semaphore.
We do our job and then we write another line in order to release the symbol.
Now, all of this, we are going to see it in the next example where we're going to demonstrate a similar
situation.