Hello, welcome back.
In this lesson, we going to see how to work with counting semaphores, I'm going to make a copy of
our Lost Project, this number twenty here.
I'm going to make a copy of, say, copy and then paste over here.
And then I'll call this counting semaphores.
When it's done, I'm going to close this old farmhouse.
OK, so this is just a tiny bit of difference open, I may not see fall over here.
And over here, where we created a SEMAFO, remember the projects, we made a copy of this for the SEMAFO
Mutex, that's why we see this here.
So if we want to create account in SEMAFO, the function to use is X semafo create counting and this
function takes to document.
The first argument is the total count.
So we can say 10 and then the next argument.
Is the initial count so you can think of this as using a bathroom or washroom analogy and the semaphores
being the keys, so if we say we are creating account semaphore, we are saying accounting SEMAFO with
a total count of 10, we are saying we have 10 keys.
To, let's say, 10 separate bathrooms and initially zero of the keys is taken, so zero, so 10 is
the total count, but in our case, our resources, a single one.
In this experiment, we're using one zero port to print the data.
So our number of count, total number of count is one and then initially zero.
That is how we're going to initialize our account and SEMAFO and we leave everything the same.
Let's build and see what we have or click here.
Right, click.
And built project over here.
It's built successfully.
Right click.
And then I'll say run us ASTM 30 to a select number, twenty one over here.
I say, OK, over here.
Let's see, it's downloading, he says 97, it's done.
OK, so this report, I'm going to open it.
Cletis, I see real time, it's not responding.
Going to close and open real tim.
OK.
OK, so the reason.
It's not moving as this count in SEMAFO is just like the binary SEMAFO, before we use the SEMAFO,
we first have to give it.
We have to give the SEMAFO before the first function takes it.
We realized that in our mutex example, we didn't have to give the SEMAFO.
We could simply take it.
But this time we have a single count and we have to give this.
So before we start our scheduler, I will say eczema for give over here.
It's pure successfully.
I'm going to click here to run.
It's downloaded successfully, I'm going to open our support.
See, selectees.
OK, as you can see, it's running when I press the push button, it becomes your right.
OK.
So you may wonder what is the difference between the binary semaphore and in this one, of course,
the binary SEMAFO, like we said, there's one or zero two values.
One key task is the haystack you or it does not, whereas this one, we can create as many clues as
we want.
And one other thing over here, we said this is the total number of count and this is initial count.
If we want to give the key, if we want to give the key without using our zemi forgive here, we can
simply set initial count to one so that we give it to the first the first task.
So we don't need this.
If we comment ximo give and then we said initial count to one, then our task would automatically receive
it.
We don't have to explicitly give from here.
Let's see if what I'm saying is the way it works.
I'll click over here to build.
It's Putin.
OK.
Now click here to run.
I see.
OK, system initialization.
AC.
Verification.
OK, as you can see, it's raining.
The same way.
OK, when I press the push button, we can see we received our data and we're not using this this we've
commented out, but we set initial count one meaning we've given it.
OK, so that's all there is for counting semaphores.
I'll see you in the next lesson.
Have a nice day.