Hello, welcome back.
In this lesson, we shall see how to work with binary semaphores, I have gone ahead to create a new
project here at number 18, binary semaphores.
And this NMT project is Jesco, our Ewart's in our system clock.
In it, there is no ARTUS code here.
We're going to add that together.
This Jesco our Ewart's.
And our system look like I said, OK, so what are we going to do is we going to start off by having
three tasks and we're going to let them all access the, um, the the support at the same time and we'll
see what happens.
But before we use semaphores, we have to include the semaphores header file in our project.
So I'm going to come to our Simms's, always start over here, highlight's this and then.
Right, click select open declaration.
And down here where I have all of the artists component, I'm going to include one for semaphores,
I'll include I can just copy this and then paste it over here and rename this word to semaphores s m
p h are such that we have semaphores that each like this.
Once that is done, I'm going to come over here to may not see and then I'm going to start off by creating
three tasks.
Let's say these are the controller tasks.
We're going to have one for blue, one for yellow and one for red.
So I'll create those tasks.
Each task would have a stack size of one hundred and two.
All have the same priority.
So this what we have, we have three tasks created.
We have read a little controller task yellowbelly the controller task and then the controller task is
the name were given the task function that we should implement later.
We also going to create a binary semaphore.
Before we do that, we're going to declare a Hando to hold the binary SEMAFO, I'm going to copy this
and.
Paste over here, we have X binary SEMAFO, that's the name of the SEMAFO Hondo, I'm also going to
declare task profiler's that we shall use to profile our tasks.
So we're going to have one for each we first use TYPEDEF to rename it to task profiler's such that whenever
we want to see it, we can simply type this word.
Task profiler, I'm going to declare red profile, yellow ality profile and blue ality profiler, and
we shall drop each profiler in each of the task functions.
So once we've created our tasks and our semaphores, we go ahead and start the scheduler and we do this,
of course, using our V task.
Start such a function like this.
OK, so now we have to implement the task functions, we start off with the first one, we're going
to have the red alert controller task.
And then it's going to have the wah wah loop in here, the infinite loop.
OK, and then we going to copy this and then I'll change the word read here to Blue Sajdah, we create
the task function for blue ality controller.
And then I'm going to paste this over here and then change the word red to yellow.
Such that we have yellow controller.
I'm going to copy the function prototypes and put them on the topia.
This is the one for red.
This is the one for blue.
And then we grabbed the one for yellow.
Like this and also remember, all our tasks have the same priority.
OK.
So what are we going to do is we going to drop the profiler's in each of them?
So I'm going to copy, read a little profiler, and then I'll come over here to read aloud and then
I'll C++, then I'm going to grab yellowbelly the profiler.
Now come here.
And yellow led and then plus plus, you know, grup.
Blue lady profiler will come here in blue.
And then plus, plus.
Put a semicolon here.
OK.
So currently, our tasks does just one thing, it's incremental, this global variable, each of them
have their own global variables that the increment.
But we what are we going to do is we're going to let each task take the SEMAFO we've created.
They take the SEMAFO and then they go ahead to increment their respective global variable, if they've
not taken a SEMAFO, they cannot do that.
So I'm going to surround each increment with our.
Which our semaphore code so red.
Task would take the SEMAFO, the name of our SEMAFO is extraordinary SEMAFO, and then we say Portsmouth's
delay give given the maximum delay here.
So it would first have to take the SEMAFO before its increment once it's done incrementing.
We can go ahead and give back to SEMAFO and then the next task will take the SEMAFO Blue and then it
would after is taken to semaphores you go ahead and run its code, which is simply to increment its
global variable.
Once it's done, it will give back to semaphores so that the next one can take it.
The next the final, which is the next is this one yellow.
It will take the SEMAFO.
Once it is done, it will give back to some awful.
OK, let's run this and see what we have.
I'm going to click over here to build.
It's built successfully.
I'm going to right click and see debuggers ASTM 32, Coatex M iSelect 18 over here.
And then OK.
Then I'll click switch over here.
And what I'm going to do is come to our live expressions window over here are clear, OK?
OK, everything about that, Cledus, and then let's add our profiler's again, red profiler, we are
yellow profiler.
We are blue profiler.
OK, I'm going to click to run Elkwood.
I'm running out code.
But nothing is happening.
I'm going to click to pause to see what the issue may be.
Well, OK, we're running around, could it?
Yes, because we are brought to the kudos for being in a separate program is being executed here.
Oh, stop this.
We do not have an AQ code here.
So I'm going to right, click on binary semaphore projects and then build again.
They would project.
It's built successfully, right, click, debug us, select this.
And then a select number 18, see?
Then switch.
And then I'm going to click to run.
We still stuck our collective paws.
And we are brought to the queue again.
OK, let's find out.
We're going to stop this.
OK, so it's an anomaly which we've committed before or I have committed before.
So when we came here, I mentioned we've created a SEMAFO, but we actually haven't.
I failed to create the SEMAFO.
So that is why we are being sent to that location whenever we post.
So over here, reinitialize we print system in it and we create our SEMAFO X creates binary.
And then we started Hando in our semaphore Hondo before we go ahead to create our tasks.
So I'll click here to build.
It's argued successfully.
I'll click to go to debark.
And then switch.
OK, now a click to run.
OK, let's pause and see.
I said no.
Over here.
Exhumed pended.
OK, I'm going to stop.
No, what we've just experienced is a very common issue in real time operating systems when dealing
with semaphores and other things like mutex, which are a form of SEMAFO, what we are experiencing
now is known as a deadlock or a deadly embrace.
Let me explain.
So you see, we start over here by creating the Red Alyda controller task.
So this is going to be the first task to run since they all have equal priority.
When this task tries to run, it would come over here expecting to receive a SEMAFO because we have
the Zema for take.
But no one is given the SEMAFO.
So this function wants to take a SEMAFO and then run its code and then give back to SEMAFO, but before
it takes the SEMAFO, the SEMAFO has to be given.
So essentially all our tasks are waiting for a SEMAFO.
Each of them can give us a map for once.
They've taken it by.
They are waiting for each of them to give the SEMAFO because if our function started from give the next
function, if our task gave the SEMAFO, then this blue could take it.
Essentially each of them are waiting for a resource that they all have access of providing.
So someone has to give the SEMAFO first to to get the ball rolling without starting by giving the SEMAFO,
we would be kept in this deadlock where each task has the ability to give and take the SEMAFO, but
no one is given.
And because no one is given, no one can take.
So what are we going to do is.
We're going to start off when our program starts.
In a red ality, not in our well-worn loop, but in the Red Élodie task, we're going to start off by
giving the SEMAFO so read the function would first give the SEMAFO and then the wall one loop.
We take the SEMAFO and then we proceed.
Let's see if this will solve our Dedlock problem.
I'll click here to build.
It's built successfully, click here to go to the few.
Click to switch.
OK, I'll click here to run, and now as you can see, we have all of our tasks running and we know
it's running because they are all incrementing profiles and you see that they are run in almost equal
amount of time, not entirely equal because one has to.
Perform its computation and then give the SEMAFO for the other one to perform its computation, but
we see that they are all running right.
OK.
They are running what we are going to do is now we've verified that they are all running with a semaphore.
We're going to say when Red takes the SEMAFO red ones, give it back.
So we're going to comment on this line.
Going to click here to build.
It's built successfully, we click here to go to the debug view.
And then we click here to switch.
And then we click here to run.
Now you see that red round was red, just executed once, but it's executed once and got stuck.
Why?
Because when we take a look at our code red took the SEMAFO.
And then it's executed.
And now no one is given the semaphore, Red was supposed to give the semaphore to the next task to run
and then also give the SEMAFO, but because Red didn't give the SEMAFO after its execution, nothing
else is executed.
Right.
So this is another observation.
OK, so we're going to bring this back.
Now, what are we going to do is we go into print zero data.
We're going to let each of the task access the support and print a piece of information read is going
to print this red task and then yellow will print this yellow task and then blue print its own piece
of information.
So this is yellow task from yellow and then blue over here would say.
This is blue Tosk, so this what we have, OK?
So we click over here to build.
And then.
We click here to run.
OK, I'm going to open real term.
This is from our old project, I'm going to Claire and then click to open the port.
Let's see what we have, we have this is red TASC, this is red task, this is red.
How about the rest?
Let's see.
So let's see, so we have this we have just the.
The Red Cross printed its information over here.
OK, so let's see if only the red thread or task is the one executed by observing the profiler's.
That's the way you try to diagnose problems in the real time system.
So I'm going to click here to go to the box you and we're going to see if our profiles are not changing.
So I'm going to come over here, click here to run, and we have just read a little profiler running.
OK, let's see what we have.
So Red takes the the SEMAFO and Red gives the semaphore.
Lou takes the SEMAFO, the blue gives it, however, only red is running, let's comment at our printf
and see if that would solve the issue.
I'm going to comment on the premise that we just added.
Then I'm going to click here to build.
Secured successfully.
Then we click here to go to debark few.
Click here to switch.
Then I'm going to click here to run.
OK, so now it's running.
OK, so with the with a printer to use the printer with stability, I'm going to add the task delay
of one to each function.
OK, so I'm simply going to advertise this one here.
OK, so read LDV Task DeLay.
I'm going to do the same for Lou.
Then I'm going to do the same for yellow.
Like this, and then I'm going to click here to run.
People suggesting that I'm going to.
Yeah, that's straight by running Facebook, click here to read.
It's a 20 to percent year like computer running low.
OK, it's finished running.
Let's see real tim.
OK, I'm going to open the port and now you can see we have each and every one of them type in their
respective message, we have over here Red TASC, Blue TASC, Yellow TASC, Red Task, blue tasc, yellow
tasc, red, blue, yellow, red, blue.
It's all that one after the other.
OK, so let's see what happens when we see no more semaphores.
We don't need semaphores in our system.
We're going to comment out the SEMAFO take and SEMAFO give functions in our code, in all of the tasks
read, and now we comment it out of blue and then we comment it out of yellow as well.
Like this.
Once that is done, I'm going to click here to build.
It's built successfully and I'm going to click here to run and then I'm going to open to.
It's still downloading onto the board.
It's done downloading, going to open real time here, OK, real time is opened, but this is what we
get now.
It's jumbled up, right, because all of the threads are trying to access the serial port.
Meanwhile, the support is a single resource one has to access at the time we were using the SEMAFO
to enable, there is just a single access.
You can think of the support or the you ought to be a bathroom.
You can have just one occupant.
The occupant has to take the key, opened the door, use the bathroom and then return to key.
Taking the key is similar to our semaphore take function here and then printing this yellow task is
like using the bathroom.
And once you are done, you come out of the bathroom and you give back.
The key is similar to our SEMAFO give back because now the door is open.
There is no key associated.
Everyone is trying to enter the bathroom and mess it up at the same time.
So we have this type of display on our zero program.
Okay, so obviously the best thing to do in this case is to use the SEMAFO.
We would see rather than have semaphore take and SEMAFO Give would see a semaphore mutex that allows
us to use a single API to.
To do the same thing that we use in these two SEMAFO APIs, take and give to achieve, we use the mutex
to to achieve the same thing, but this time we'll call a single function.
OK, but we've seen the the benefits of semaphores over here in accessing shared resources and we've
seen how semaphores can be used to block a part of code as well.
So we've done a number of experiments with semaphores.
I would just build our projects again and take it back to its SEMAFO nature before we conclude this
particular lesson of with the project go into.
Click here to run.
It's downloading onto the board.
I'll clear this.
And it says waiting for the debugger and then we have 97 percent here to almost complete.
OK, it's complete now I'm going to open our port.
And I'm going to open again, and as you can see, we are back to normal operation.
OK, this happens when you try to restart the port in the middle of a transfer, so we are back to red,
yellow, blue, red, yellow, blue again.
OK, so this order is we've explored binary semaphores.
That's all there is for this lesson.
If you have any questions, leave them in the questions and answers area.
I'll see you later.
Have a nice day.