Docsity

Ice Cream Shop Simulation: Manager-Clerk Interaction & Semaphore Sync, Lecture notes of Programming Paradigms

Toggle sidebar
bg1
ProgrammingParadigms-Lecture18
Guest Instructor:All right, everyone, Jerry’s out of town today so I’ll be giving the
lecture. And we have two handouts today, two very small handouts actually, the section
handout and the solution for tomorrow.
So I believe last Friday Jerry started the ice cream store simulation problem. This is a
really huge problem. It’s probably bigger than any single synchronization threading
problem that you saw so far in the lectures, and it’s the merger of two or three final exam
problems, so it’s pretty involved. So try to go through it as clear as possible but feel to
ask me any questions if you want.
All right. So let me just start from where we left off last week. So we were talking about
the ice cream store and we have four places basically in the ice cream store. We have the
cashier which is they are basically to handle the billing issues and we have the customers.
We will say we have ten customers and each customer is gonna ask for a random number
of cones – of ice cream cones from one to four.
So the least we could have it ten cones and the maximum is 40 cones. And every single
customer because he doesn’t want to wait for every single cone to be done sequentially,
then he’s gonna fight of a clerk or dispatch a clerk for every single ice cream cone that he
wants. And so we will have clerks that are making the ice cream cones. We have 10 to 40
of those. But the thing is every single clerk because really giving the ice cream to the
customer it has to ask for the manager’s inspection to get an approval for the cone that he
made.
So the clerks have to get the manager’s approval, and the manger that is only a single
manager, only a single cashier, and if the manager disapproves of the cone then the clerk
basically has to redo the cone and ask again for the manager inspection. The manager, he
doesn’t like to have two clerks in his office at one time. So basically we can’t have more
than one clerk contacting the manager at some point.
At the same time – so this actually starts – you can start to see the communication that is
going between those guys. We definitely can see that we have clerk threads, right?
Because every customer – every single customer would potentially ask for one, two, or
three, four clerks. At the same time, we have ten customers that are running in parallel.
It’s not only a single customer at a time. So we have threads here, we have a thread for
the cashier, a thread for the manager, a total of probably 52 possible threads, okay.
You can see the communication going between the cashier and the customer. You don’t
expect any communication between the cashier and the clerks or the manager, right? You
can see communication between the customers and the clerks depending on how many
clerks he fires off, and you can see definitely a communication between the clerk and the
manager, right.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Ice Cream Shop Simulation: Manager-Clerk Interaction & Semaphore Sync and more Lecture notes Programming Paradigms in PDF only on Docsity!

ProgrammingParadigms-Lecture

Guest Instructor :All right, everyone, Jerry’s out of town today so I’ll be giving the lecture. And we have two handouts today, two very small handouts actually, the section handout and the solution for tomorrow.

So I believe last Friday Jerry started the ice cream store simulation problem. This is a really huge problem. It’s probably bigger than any single synchronization threading problem that you saw so far in the lectures, and it’s the merger of two or three final exam problems, so it’s pretty involved. So try to go through it as clear as possible but feel to ask me any questions if you want.

All right. So let me just start from where we left off last week. So we were talking about the ice cream store and we have four places basically in the ice cream store. We have the cashier which is they are basically to handle the billing issues and we have the customers. We will say we have ten customers and each customer is gonna ask for a random number of cones – of ice cream cones from one to four.

So the least we could have it ten cones and the maximum is 40 cones. And every single customer because he doesn’t want to wait for every single cone to be done sequentially, then he’s gonna fight of a clerk or dispatch a clerk for every single ice cream cone that he wants. And so we will have clerks that are making the ice cream cones. We have 10 to 40 of those. But the thing is every single clerk because really giving the ice cream to the customer it has to ask for the manager’s inspection to get an approval for the cone that he made.

So the clerks have to get the manager’s approval, and the manger that is only a single manager, only a single cashier, and if the manager disapproves of the cone then the clerk basically has to redo the cone and ask again for the manager inspection. The manager, he doesn’t like to have two clerks in his office at one time. So basically we can’t have more than one clerk contacting the manager at some point.

At the same time – so this actually starts – you can start to see the communication that is going between those guys. We definitely can see that we have clerk threads, right? Because every customer – every single customer would potentially ask for one, two, or three, four clerks. At the same time, we have ten customers that are running in parallel. It’s not only a single customer at a time. So we have threads here, we have a thread for the cashier, a thread for the manager, a total of probably 52 possible threads, okay.

You can see the communication going between the cashier and the customer. You don’t expect any communication between the cashier and the clerks or the manager, right? You can see communication between the customers and the clerks depending on how many clerks he fires off, and you can see definitely a communication between the clerk and the manager, right.

So you want to start thinking about what are the constructs that you will need in order to maintain synchronizations issues between all of those players in the game, okay. So let’s start thinking, just try to brainstorm about it and then we can look at the code. The thing is the customer comes in and he wants one to four ice cream cones and he will fire clerks. After he gets all his cones he will go to the cashier, right? But he cannot go to the cashier until all the clerks give him back his ice cream cones.

So you can start to see, like, this kind of 1 to N communication that we talked about, right. You can see that this guy is gonna request N clerks and he has to wait for the N clerks to get the job done before he can go to the cashier, right.

You can also see the communication between the clerk and the manager. So no two clerks can go to the manager at a time which means that there should be this kind of lock around the manager’s office, right. And so only one clerk can access the manager’s office. At the same time the manager will be waiting all the time because he has to wait for all the ice cream clerks to be done for all the customers before he can go home.

So the manager will be in kind of a loop just waiting to be requested for an inspection. And once he finishes the inspections he gets back the result to the clerk and he keeps waiting again until all the potentially 40 cones are done and inspected. Make sense?

So the clerk will basically have to request from the manager who’s waiting. So there’s the manager waiting for the clerk to request. There is also the clerk waiting for the manager to finish his inspection, right.

So we have this kind of binary rendezvous again thing going on in here. You see that? Now there is another constraint on the customers going to the cashier at the end. So the customers are waiting for their cones. And after they get their cones they just go to the cashier in a line and this line has to be maintained on a FIFO order, so a first in, first out, right. And we have to also think about that. How are we gonna insure that the customer that arrives first at the cashier gets really served first and leaves first, right?

You will see, again, the kind of binary rendezvous thing between each customer and the cashier because the cashier has to be waiting all the time for a customer to request the service or basically a customer to show up in line. And after a customer shows up in line he will be waiting for a cashier to finish handling his billing and let him go basically, right. So any question on how the setup is for the problem?

All right. So let’s talk about the main function. Let’s see what happens in the main. Okay, let me write them in here. All right. So we have the main function. I’m not gonna write this argument but it basically has to maintain the total number of cones, right? The main function is basically responsible for spawning all the threads that we need.

We’ll see that the customers are responsible for spawning the clerk’s threads but other than that everything gets spawned in the main function. So the main function is gonna basically – you have the total number of cones which is required to spawn the manager

just described a lot of things going on, right. A lot of basically synchronization things that we have to take care of. We need semaphores in here, right. There is like a [inaudible] I’ve just explained. We need to know after the manager inspects a cone whether it succeeded or not, right.

So that should be something communicated. It is some sort of communication. So because there is a lot of variables, there are so many variables and there are many threads or accessing all of them, we again do – we access them globally. So we’ll have a global variables, global semaphores. There’ll be a lot of them so in order to make it understandable we’re just gonna group them in structures.

So we’ll group all the variables that have to do something with a manager and a clerk in one structure and call it inspection and we’ll later on do the same thing for the customers and the cashier. We have another structure with all the variables for the synchronization and we’ll call it the line, all right.

So let’s have our first global – so we have the struct inspection and, let’s see, the manager’s gonna inspect everything that the clerk shows to him, right? So we have to need basically a Boolean to show us whether the cone passed or not, right. And I’m gonna be very informal in here. I’m gonna give you the initializations but all the initializations are basically done in setup semaphores, okay.

So we’re not gonna go through this function. But this pass is gonna be false. Now what else do we need? The manager has to keep waiting until it’s requested, right, for inspection? And also the clerk has to wait until it knows that the manager finished its inspection, right?

So we need some kind of semaphore for rendezvous the semaphore is gonna be – let’s call it requested. And so this is gonna be signaled by the clerk to the manager that it requests an inspection, right. Make sense?

All right. So this semaphore is gonna be initialized to zero, okay. So basically the manager what it’s gonna do is basically gonna wait on the semaphore which means it’s gonna block, okay. Until a clerk comes and sends the semaphore so it basically wakes up the manager. Make sense?

All right. So we have the semaphore requested. We have the semaphore finished and we also understand that once the manager – once the clerk requests inspection it will wait on this finished semaphore which means it’s gonna block until the manager finishes the inspection and then signal the semaphore and then it wakes up, okay. So what else do we need in here? What else do you think we’re missing? Okay, let’s start working and see what we miss, okay. Let’s start working on the function and see what we need.

So we’ll have the function manager. So that’s void manager, the manager takes an N which is the total number of cones. So let’s call it total cones needed. And here is what the manager does. I’ll have two variables, one of them is required, the other one is not.

The first one is total, let’s call it num approved. So this is num approved which is initialized to zero and another one which is num inspected also initialized to zero.

So what we need to do is the following. While the number approved is less than the total number needed then you basically can go home, you have to keep waiting and keep inspecting, right? So while this is the case what you want to do is the following. You want to wait until a clerk requests your service, right? Okay, so you want to wait on inspection, let me finish this here. So you want to wait on inspection, not requested, right? Once you get a request – yes, go ahead.

Student: Which are not needed as [inaudible]? [Inaudible] not needed?

Guest Instructor :Those are not needed?

Student: Yes.

Guest Instructor :So did you see when that spawned the manager thread? We passed the total cones and the total cones were the total number that a customer’s wanted. So that’s the total number that basically the manager needs to inspect. That’s total number of cones, yeah, that all the customers will buy, you see.

Student: Oh, okay. So that’s the total number of cones.

Guest Instructor :Yeah, I’m just calling – I mean, I’m just changing the name of the parameter but that’s fine, okay. Any other questions? All right. So you want to basically wait on the request for the inspection, okay. And then once you get a request for an inspection you basically want to inspect the cone, right? Okay.

So what you do is you have your – once this is done you basically are inspecting a cone so num inspected will go up and you have to check if you’re gonna approve this cone or not. So we simulate. This is modeling, right? So we have a random function that says sometimes, yeah, we do approve, sometimes not, okay.

So we will say inspection not passed which is the Boolean in here is equal to some random chance. It returns one of the two things, zero or one. And again, I’m not writing this function but you understand what it’s doing, okay. So again remember I’m assigning this to dot passed because this is the Boolean that I’m using to communicate between the manager and the clerk, okay.

Now if you really passed – if inspect got passed then what you want to do is basically increase the number of approved. Make sense? So num approved plus plus. Now we finished your inspection, okay. The only thing he has to do but what you know is that the clerk is potentially waiting on your finished. So you want to signal finished. Any questions? Yeah, go ahead.

Student: Yeah. Are we using global variables mean or –

First of all is that lock is still one because nobody did the wait before in which case you’ll be able to proceed. The other scenario is that this is zero in which case you just block on it and wait until it is signaled, okay.

All right, so you wait on this lock. After you wait on this lock you basically need to request the service of your manager, right. Okay? So what you want to do is semaphore signal, inspection requested. Am I going too fast? Is it clear? Okay, so we signaled the request and after the – yeah?

Student: Some [inaudible] to one?

Guest Instructor :Excuse me?

Student: Would it say requested to one?

Guest Instructor :It would, yeah. So request is originally zero, right? The manager, which I just wrote in here, it doesn’t wait on requested which means it blocks. So the way our semaphores in the library that you are provided the semaphore doesn’t go to a negative value, okay.

So it always maintains a zero or a positive value. So if it’s zero and you try to decrement it, it doesn’t not allow you and it blocks. So you keep waiting there blocked until somebody makes it positive. So you can decrement it to zero, okay. And this is how the blocking works, right.

So what happens here is that the manager would be blocked if he starts before the clerk because what will happen is the manager will try to decrement the zero semaphore but it will block and then it will wait until some clerk comes and does signal the inspection request which will make it positive so the other one will be able to proceed and the command does. Do you see how this works? Okay?

All right. So you did signal request. Now after signal request the manager will get the request, right, and will start working on this. What you need to do at this point is wait until he finishes. Make sense? So you just –

Student: It’s really [inaudible] is like if you have inspection request of him and if you lock up a manager why is it he requested?

Guest Instructor :That’s a good question. Let me finish it and then I’ll answer this question, okay. I’ll just go on because, like, I want you to see the whole picture after we finish it. So we will basically wait until the inspection is done. So after the inspection is finished and once inspection is finished you basically want to know the result of the inspection, right? You want to know if your cone passed or failed. Now how do you know that? Which field tells you?

Student: Inspection dot passed.

Guest Instructor :Inspection dot passed, right? Now you want to read into passed whatever the manager passed in inspection dot passed, okay. So you want to set passed equals to inspection dot passed. Now you know that if passed now is two then you’re not gonna do this anymore and you’re gonna move on, okay. You’re done with this point but remember that you were locking the manager’s office, right. You no longer need to lock the manager’s office. Make sense?

So you want to signal – so you waited on the lock in here. You want to say that it semaphore signal inspection dot lock. There is something missing here. I will finish it when we talk about the customer.

Now back to your question. So the question is the following, why do we need to have lock and requested. Why do we need those two? If lock already grants us access to the manager, right? So think about what would happen if you don’t have them, okay.

So let’s say you don’t have the lock. It’s obvious that you need it, right? Because you will have two clerks to go to the manager at the same time, right? Now, think about if you don’t have requested. Then what will the manager do? Not at all, like, basically the manager will not wait for any inspection, right? It will go on and proceed with passed, give it a random value and go on and keep going in the loop, right?

Now which clerk will be checking its own inspection dot pass you don’t know, right? This lock is crucial and it’s crucial that you maintain this – this is maintaining this kind of critical section in here, right?

This lock is extremely crucial because basically you don’t want to release this lock until you read the value of inspection dot pass. Note that this value is shared among all the clerks and the manager, right? It’s a single value. And because we allow only a single clerk to be with the manager at the time then we can safely assume that the value in here will be up by this clerk that is with the manager.

But you have to insure that no one else can be in that portion, okay. You have to insure. So this lock insures basically that you have this critical section in here. It insures that you can read this passed before you release the lock and before any other clerk can be with the manager.

Because potentially let’s think about it differently. What happens if you switch those two lines? What happens if we first signal the lock, basically release the lock and then check the value of what’s in inspection? What’s in passed? I’m sorry.

Student: [Inaudible] inspection dot passed.

Guest Instructor :Potentially, you’re not sure, potentially. What happens is if we had those two lines kind of flipped what would happen is probably you could have the inspection dot lock signaled, which means that the lock of the officer of the manager is no longer locked and any other clerk could get in.

synchronization of the two agents. Whoever is producing waits for the other to consume and the other way around. Make sense? Any other questions?

All right, so we’re basically done with the manager and part of the clerk. We’ll have to revisit the clerk when we talk about the customer. But before we move to the customer then now we’re gonna be talking about the relation between those two guys. And we’ll also talk about this guy, the relation between those two.

Let’s first start with the easy part. Let’s see where it’s put here. So we have a void customer and the customer takes the random number of cones that he needs to order, right? So he takes an int num cones and let’s see what he does.

The customer gets in the store he is browsing. This is just wasting processing time. Nothing, right? He’s gonna spawn N num cones clerks to do every single cone for him, right? So he will basically need to get num cones clerks, like if this is three clerks to work on his cones and wait until they’re done. Okay, before he can move on to the cashier. Make sense?

So this means that we will need to have some semaphore and why we need to have it because we know that we need to wait for all the clerks, okay? So we need the semaphore. Let’s call it clerks done. And this one we initialized to zero again, okay.

And then for int I is equal to 0, I is less than num cones, int I plus plus, what you want to do is basically fire each clerk for your cone. So you do a thread new right at the end, and then you fire the clerk. That’s a debug name. The function is the clerk, right? And now we need to pass to the clerk the semaphore because you will wait on the semaphore for all the clerks and you want every clerk when he finishes the cone to signal this back to you which is equivalent to handing – to giving you basically the cone. Make sense?

So you want to thread new a clerk passed to it one parameter which is basically the clerk’s done semaphore. Now this takes us back to here, okay. So this clerk takes a semaphore, let’s call it sema to signal. And once it’s done making the cone, inspecting it, approving it and it’s all done then you basically want to semaphore signal the sema to signal. Make sense?

Now that’s for the clerk. Now we’re done with the clerk. This guy just spawned num cones clerks, right? He needs to wait for all of them. So he saw last lecture how we do that. You just have another for loop for int I 0, I less than num cones, int I plus plus. You want a semaphore wait on clerks done.

Now we know that this guy will never get past this point until all the clerks are done with all his cones, right? At what point you just could basically go and free this semaphore because you won’t use it anymore, okay.

So you could just go here and is it free semaphore or semaphore free? Semaphore free. Okay. You can double check that. Semaphore free, clerk’s done. And then you walk to

the cashier. Starting at this point you will see how the synchronization works between a customer and the cashier and I think this is the most tricky and the hardest part of the whole thing. Yes?

Student: [Inaudible] the semaphore wait on the for loop [inaudible]?

Guest Instructor :That’s a very good question. So the question is the following, why don’t we include the semaphore wait call and the for loop in here to just have a single for loop that has basically the thread new and the semaphore wait. Anybody has an idea? Yeah, go ahead.

Student: Because we’ll – the clerks will only be spotting once at a time?

Guest Instructor :Yeah, any other ideas? Anybody [inaudible] that? So what happens if you do that is the following, you get a clerk and you wait on done, right? Now you did actually initialize clerk’s done to zero, right, in semaphore new. Once you wait on this you basically block, okay.

So you will be blocked in this line. You want be able to go any further in the for loop. You understand what I’m saying? Until this guy who you just spawned does a semaphore signal on done so you can get to the next one. So basically you’d be doing it one by one, okay? Yes?

Student: Can you make this [inaudible]?

Guest Instructor :Right. So the question is the following, can we make the semaphore and a shared integer value variable or a [inaudible] to variable. And so this takes us back to the problem of [inaudible] end use, okay.

So the main reason why we using semaphores or locks or those basically concurrency constructs is that we need to insure atomicity. And by atomicity, we mean that we want to decrement the value or increment it and check its value in one operation or in several operations that are guaranteed to be done atomically. So if you have an integer, the thing is you will increment or decrement it and then you will check its value and between the two you could get of the processor, okay. Yes?

Student: Yeah, how does it know what threads it owns? Like, if – say you’ve done the for loop and you went out of bounds by one, so you did like num cones plus 1. How does it know what threads to wait on like –

Guest Instructor :How does it know what threads?

Student: Well, like you say semaphore wait, clerk’s done.

Guest Instructor :Yes.

So we have a semaphore that the cashier is gonna wait on saying, “I’m waiting until I’m requested.” Okay? Until somebody basically shows in line. So we have a semaphore requested and this is going to be initialized to zero.

We also have another semaphore; this is a little tricky, now each customer with the cashier they have some kind of synchronization going on, right? Every customer has to go and say, “Okay, signal me when you are done with my bill. And it has to be me because I am waiting in the queue in this specific position.” Right?

So there should be some – it’s not really that we are the customers and we are waiting for you, cashier, to signal somebody of us. It’s not really somebody, it’s not any one of us and somebody will leave. We’re not caring about all the customers leaving in the end; we are really caring about every single one of them leaving in his turn. Right?

And this should give you an idea about having this kind of struct that would have a semaphore that is a rendezvous semaphore between every single customer and the cashier but this semaphore has to be for every single customer because it has to maintain the order of every single customer. Does that make sense?

So in here we will have a semaphore, let’s call it customers, and those are the customers that basically requested the cashier’s service but are waiting to be serviced, okay? So this is gonna be an array of ten semaphores, one for every single customer in turn, okay. So you come, you pick a number, you know you’re number three, then you are gonna wait on the semaphore of three. And the cashier, once he gets to your turn he’s gonna signal customer of three so that’s you – it’s your turn to leave. Make sense?

Student: Why ten customers?

Guest Instructor :Because we just assume ten customers. You could have a single number, yeah.

Student: So one point that the customer in the front of the line makes their request and that – and then he leaves and then the next person makes their request and he leaves so that the line of clerk only deals with the one – only one customer.

Guest Instructor :This is basically what we’re doing. What you’re trying to say is how can we – so let me ask you the question, okay? How can you insure that the customer – the first one in the line is the one that came in order? Because basically remember that you are not keeping track of the state. There are customers that finish and they get all their cones and they go to the cashier, right?

You don’t remember who came first. You don’t remember the order. You don’t remember which one picked the number, right? If you say the one in the beginning of the line that’s what we’re doing basically, we’re keeping track of who is at the beginning of the line.

We’re gonna say if it’s the one in place number zero and move on to one which is basically the beginning of the line. If you say that they shift, right? But if you don’t do that then you don’t know because you have like threads that finished at random times and you have no clue whatsoever which finished first. Make sense? Yes?

Student: If you had multiple semaphore waits and you do just one signal will all of them wake up or just one wake up?

Guest Instructor :Are you talking about those arrays that I’m having here? When you do a wake up you wake up the specific semaphore. You don’t wake up any one of them.

Student: But generally do you just [inaudible] waiting on a semaphore?

Guest Instructor :At least if you do wake up –

Student: At the signal.

Guest Instructor :– what happens typically – this is an OS issue, but typically what happens is when you have somebody waiting on a semaphore it’s put on a block list, right? And so you have all those threads that will be put on the block list and it depends on the scheduling issue. So it depends on whether you pick a shortest job first or you pick a priority scaling, whatever. But whoever gets to be scheduled once he finds that his semaphore was signaled then he will be able to proceed, okay?

All right. So now we have the customers and we’re still missing one thing which is very similar to the one thing that we missed before. Can anybody see it this time?

Student: Having a lock on the cashier?

Guest Instructor :Having a lock on what?

Student: On the cashier.

Guest Instructor :Having a lock on the cashier. Why do we need the lock?

Student: Because they can only deal with one customer at once.

Guest Instructor :But actually you can see that the cashier is maintaining this thing because the cashier is the one that’s gonna be waiting for requests and he’s gonna be spawning basically – he’s gonna be signaling one of those. Anybody sees any other problem?

Student: Yes, a lock for the number.

Guest Instructor :Exactly. We need a lock for the number. Basically you can think about this. Two guys go, they check what is the number. The number is zero. The other one

So basically wait on line dot customers of your place, okay? And this is your customer. I don’t think I’m missing anything. Does it make sense? Are you guys confused? This is hard. This is not an easy – it’s not that it’s – it’s not as complicated, it’s just that it’s just a lot of problems going on, a lot of synchronization issues going on.

So I would definitely suggest that you guys go and do this again, okay? You have to think about it. You have to think about the interactions between all of the players. You have to think why you need those semaphores and how you’re gonna use them. I’m gonna see you again tomorrow in this section. We’re gonna go over another example for the studying and it’s gonna be related to your assignments – to your project, okay?

Okay, have a good day.

[End of Audio]

Duration: 52 minutes