Lecture 16 | Programming Paradigms (stanford)

This presentation is delivered by the Stanford center for professional development everyone welcome I don’t have any hand it’s free today you have plenty of handouts from Monday that we still to spend the next few few lectures on you’re not getting an assignment today so you have this grace period from tonight at 10:00 p.

Responsibility of 1:07 whatsoever remember the exam is this evening at 7 o’clock it’s in Hewlett 200 which is this huge auditorium in the building across they beyond the fountain from gates the I’m going to send an email out after lecture but just in case SCPD students are watching is for the exam tonight I’m planning on.
Posting the the exam as a handout at 7:01 p.
tonight and then remote students just download it self administer callin if they have questions and in fact it in what they’re done they don’t need a proctor I don’t need any of that business I just assume people are well suited to just sit by themselves and take an exam without somebody of authority hanging over their shoulder and.

Then SCPD students actually have the option to take it tomorrow morning as well and I actually prefer that SCPD students take it because if there’s a disaster during the exam tonight people in the room can be dealt with immediately whereas it’s very difficult to propagate that information outward so I actually prefer us UPD students to take it tomorrow and then fax it in sometime.

Sometimes before five o’clock tomorrow so we can grade them okay I’m going to try and get the graded exams back to you and available by Sunday evening I can’t promise that okay I actually have not done with the class this large in a long time so we’re dealing with I know it looks like this cozy little family here but it’s not it’s actually 230 some people and it’s been a while since I’ve had a manager grading effort.

That involved that many people it’s also complicated by the fact that I’m out of town this weekend so my PA is are grading it and it might be difficult for them to get you the exams back by Sunday evening but we’ll do our best to make sure that that happens okay when I left you last time.

I had focused specifically on the first multi-threaded example where we had to introduce this notion of a semaphore in order to control access to what we call a critical region so if you remember last time the threaded function the one it’s.

The recipe that 10 different dogs follow while they’re trying to get their work done it looked.

Like this sale tickets took an INT agent it took an int star called numb tickets and numb tickets P and then it also took this semaphore I call lock just to review since this was kind of a fleeting comment in the last 10 minutes of Monday’s lecture this semaphore it is more less like basically a.

let’s say a synchronized counter variable that’s always greater than or equal.

To zero okay and so if I construct a semaphore around the number one and I levy a semaphore wait call against that semaphore this as a function figures out how to atomically reduce this one to a zero okay and so this is basically equivalent to the – – but it does the – – in such a way.

That it actually fully commits – to the demotion of the number – one that’s the one that’s one lower than it okay semaphore signal on the same exact semaphore would actually bring this back up to a 1.

Ok if this were followed by a semaphore wait call followed by another semaphore wakeful then something more interesting happens where this one right here decrements the one down to a zero this one right here would have a very high time because semaphore is at least in our library this isn’t the case in all systems but.

Our semaphore s are not allowed to go negative so when you do a semaphore weight against a zero variable then this thread actually says I can’t decrement that at least not now I need somebody else in an other thread to actually plus plus this so that I can actually pass through a minus minus without making.

The number negative does that make sense to people okay so programmatically the implementation of semaphore weight is in touch with the thread library and.

So it actually went into text is zero behind the scenes it immediately says okay I can’t make any progress right now it pulls itself off the.

Processor it records itself as something that’s called blocked and it puts it in this queue of threads that are not allowed to make progress until some.

Other threads signals of semaphore they’re waiting on ok that’s it what with everybody ok this is not constrained to go.

Between 1 and 0 it can this can be set to either be 0 or 1 or 5 or 10 the only example we’ve seen so far is where the semaphore that’s coming in is initialized to surround the 1 because we really want it to function not so much as one as we wanted to function as a true and it’s basically a light switch that goes on and.

Off on and on and off and it’s used and we use semaphore weight and semaphore signal against that some afore to protect other protect access to the the num tickets variable that we haven’t addressed to so in a nutshell while it’s the case that true is true I want this right here to be.

Marked as a critical region what that means is that I want to be able to do surgery on that num tickets variable without anyone else bothering me ok and the way you do that is to do a.

Semaphore or spell it out here semaphore wait on the lock you do the check to see whether or not a nun tickets of p is equal to zero and if so you don’t do the surgery okay on.

The end somebody else has done it a hundred times already and.

There’s no more no reason to do it again otherwise you want to go through and do this that’s true surgery on what functions as a global variable at least from the perspective of all the threads running this and then you want to release the lock you might do some printing app printing here okay you might sleep for.

A little bit down here there was an extra semaphore signal Kol against the lock to accommodate the scenario where the person who breaks out of the while loop does so after securing the lock so they actually have to release the lock kind of as they go outside the bathroom window was the.

Analogy I used on Monday okay this is considered to be a I’m sorry this right here is considered to be a critical region it’s supposed to be something where that when they’re inside there they cannot have any other threads during their time slices.
Mucking around with this type of stuff okay as.

An arbitrary thread actually gets here given that this thing was initialized to 1 and because there’s every single semaphore wake call is balanced by a semaphore signal it’s going to toggle up and down between 0 and 1 when a thread gets here there’s one of two scenarios it’s staring at a one and so.
It actually successfully does the – – and is allowed.

To pass in here and do the work or the thread blocks on this now you may say well how would that happen a thread could potentially block on this if there’s a zero if another thread saw a 1 here decremented to a zero made partial progress through this but the time slice ended before it got to the.

Signal call does that make sense to people yes no okay just because a thread owns a lock doesn’t mean that it.

Can’t pulled off the processor.

It might acquire the lock get two-thirds of the way through this final instruction here okay and then be pulled off the processor so that other threads can actually say oh maybe I can make some progress but if they get this far they’ll still sing a zero because the thread that owns.

The lock hasn’t released it yet okay so as other threads hit this semaphore wait call and it surrounds a zero they are pulled off the processor that’s kind of what you want if they can’t do any meaningful work you want the thread manager to say you can’t do anything meaningful work I’m not going to let you even use your full time slice and eventually it’ll get back to.

The only threads that can do work which in particular would be the one that owns the lock right here okay does that make sense okay so there’s that.

Right there it typically try to keep the critical regions as small as possible if you’re going to lock down access to code you don’t want to make it arbitrarily long that’s basically like saying I want to.

Do all the work in the world.

So I’m just going to acquire a lock and I’m going to run this triple four loop okay you only do that if.

You have to because it’s a critical region this printf in particular if it’s just logging information it might not be imperative that you actually print to the console while you hold the lock so you could release the lock and let other people make some progress and then without holding the lock just go ahead and print.

To the screen okay does that make sense okay there are a couple other things somebody asked a very good question at the end of lecture on Monday and I think I want to go over it some people were.

Where numb tickets P – – takes a1 down to a zero I don’t mean the lock I mean the actual number of tickets and they thought that was the one problem we were worrying about.

The answer is that’s not the case and I can actually tell you a little bit more about what happens as threads get swapped off the processor and where all of.

Stored and show you that if the number of tickets is originally 100 there’s as much of a race condition without the semaphore weight and semaphore signal calls in the.