Lecture 18 | Programming Paradigms (stanford)

This presentation is delivered by the Stanford center for professional development right everyone jet is 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 it’s 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 or threading problem that you saw so far in the lectures.

And it’s the merge of two or three final exam problems so it’s pretty involved so we’ll try to go through it ultimately as clear as possible but feel free to ask me any questions if you want all right so let me just start from where we left off last week so you were talking about the ice cream store and we have four players basically in the ice cream store we have the cashier which is there basically.
Issues and we have the customers we.

Customers and each customer that each customer is going to ask for a random number of cones of ice cream cones from one to four so.

The least we could have is ten cones and the maximum is 40 cones and every single customer because he doesn’t want to wait for every single come to be done sequentially then he’s gonna fire off 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 ten to 40.

Of those but the thing is every single clerk before really giving the ice cream to the customer it has to ask for the managers inspection to get an approval for the corner email so.

The clerk’s have to get the managers approval and the manager that is only a single manager only a single cashier and if the manager.
Disapproves of the coon then the clerk basically has to redo.

The cone and ask again for it 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 click 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 patterns it’s not only a single customer at a time so we have threads here we have fuzzy we have a set for the cashier a thread for the manager a total of probably.

Fifty two possible friends 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 you can see communication between the customers and the clerks depending on how many works he for a 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.

To maintain synchronization issues between all of those players in the game okay so let’s start thinking I just tried 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 now you can start to see like this kind of want to end communication that we talked about right you can see that this guy is gonna request n clerks and he has to wait for the end clothes 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 – clears can go to the manager at a time which means that there should be this kind of lock around the managers office right and so only one cure can access the managers office the same time the manager will be waiting all the time because he has to wait for all the ice cream codes to be.

Done for all the customers before he can go home so the manager would be in kind of a loop just waiting to be requested for an inspection and once he finishes inspections he gets back to the result of.

The clerk and keeps waiting again.

Until all potentially 40 cones are undone and inspect it 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 hear 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 in a FIFO order so a first-in first-out all right and we have to also think about that how.

That the customer that arrives first at the cashier gets.

3d served first and leaves first all right you will.

See again the kind of binary rendezvous thing between the customer each customer and the cashier because the cashier has.

To be waiting all the time for a customer to request a service.

Or basically a customer to show up in line and after the customer shows up in line he would be waiting for the cashier.

To finish handling his bill and give and let him go basically all right so any question on how the setup is for the problem all right so let’s start by the main function let’s see what happens in the main okay let me write them in here that’s happening all right so we have the.

Mean function I’m gonna write this arguments but it basically has to maintain it the total number of Coons right the.

Is basically responsible for spawning all the threads that we need we’ll see that the the the the customers are responsible for spawning the clerk’s threads but other than that everything gets spawn and in the main function so the min function is going to basically get the total number of cones which is required to spawn the manager because the manager needs to know how many cones there is because you can’t leave until he’s done.

With all the cones right so do the normal stuff we ended with.