No, let us clean this mess by adding a SEMAFO.
So how can we add the semaphore in the first place?
We need three steps, first, to create the handle, then recreate the semaphore itself, then we give
the SIMAR for free so that any of the tasks can access it.
So first of all, we need to handle how do we create what's called the handle?
It's very simple.
We say semaphore.
Endl.
The.
And we just call we name a symbol for called binary.
Demo for OK, this is the handle now in order to create a semaphore.
You need what's called the handle.
This is why we are creating this in the first place.
Next, we create the same for itself.
We say binary.
Underscore them for the same one that we named in the upper portion, we say, why not?
Semaphore is equal to.
X demo for.
Create.
Binary.
OK, now I have created my luck and the tasks will use this in order to raise folks who get the binary
lock of the semaphore in order to execute on the shared hardware.
Finally, I need to give the semaphore to the tasks so that whoever is ready to take it is going to
take it.
So it's like releasing it in the set up.
So semaphore give this is not a right semaphores semaphore, OK, give.
Which one we are trying to give?
Actually, it is the binary.
OK, that's it.
This is all I need to do.
Next, remember what is the most important thing in the desk that is using a shared hardware, first
taking the semaphore, doing its thing with the hardware, then releasing the phone.
So let us take the semaphore.
In the beginning of the task, we say X.
For.
Take which one we're trying to say binary.
Nemov.
Once we do this, we are taking the semaphores, it means that we can now go and to the serial.
If this desk, if this instruction is not granted because, well, the semaphore is being taken by some
other task, we cannot actually go on to print anything here.
So this is what I will guard the semaphore.
There is one more parameter that we need to pass here, which is called Boort Max.
DeLay.
Now, Max DeLay is about how long should we wait before we give up trying to take the semaphores?
Well, I will no longer as long as it takes until I get it.
OK, so I'm going to wait indefinitely until my cell phone is ready and available, and then I'm going
to take it.
OK, what happens after we are done with the hog?
We will release it.
We release the semaphore.
We say X.
Demo for Jeff.
Which one, the binary.
Now, I can just copy these to put them here and copy the release of the give and put it here and that's
it.
This is all I need to do.
Now, let me upload.
OK, it's complaining that this tape does not exist, and that's correct, because we did not include
the semaphore library.
So we need to say include.
H.
No semicolon, sorry, so we just including the semaphore.
Now we can go and upload one more time.
Compiling, uploading.
And.
Go let us go check the thermal, monitor their output, as you can see, no more corrupted data.
It's fluctuating between one and two without any issues at all.
So we have fixed this so easily now in the next lecture, we're going to talk about Priorities Mutex
and again semaphores.
We're going to combine we're going to combine some of the common aspects in between them and just compare
them with applications.