 All right, everyone here okay? Maybe give it. Well, you know what? It's four o'clock. You people are here. Let's not hold you people up My name is Carl Nestle I'll be covering this at recitation today and we're going to be talking about today is a whole bunch of things Unfortunately, I do apologize. I have to go over a lot of stuff But I want to make sure that we can at least make sure that you people are set up for the first deadline Which is this upcoming Friday? We're going to be going over a little bit about what test when 61 is hopefully you've already kind of taken a Look see through that, but I just want to make sure that you know what you need to do in order to submit an assignment We're also going to be going over the last part of Assignment number one again Ali talked about the first part about that last week with respect to locks and condition variables I'm going to be finish up the last part of that and then moving on to the bulk of today's class Which is going to be assignment number two. I know it kind of sounds a little bit weird to be talking about that Reason being is you probably already know that yes assignment number one is do this upcoming Fridays in a few days The first checkpoint for assignment number two is do next week Okay, next Friday on that too So I want to at least make sure that your kick started with some information to get started working on that Strong suggestion to everyone who is here and to everyone frankly who is not here on that Don't wait until next week to get started. I don't want to kind of ruin people's weekends on this But one week to get familiarized with the beginnings of assignment number one I'm sorry numbers assignment number two is not a lot of time and you want to at least put in some cycles this Upcoming weekend Saturday or Sunday or some combination thereof So again just want to make sure that everyone is up to speed on this So okay a couple things about test 161 here show of hands. How many people have already toyed around with 161 test Okay How many people have actually tried submitting something. Okay, that's good I would hope that by close of business today. Everyone has submitted something Okay, it doesn't matter if it works or what have you but at least you want to go through the dry run on this It's not conceptually difficult But there's some mechanics that are involved in this and I just want to make sure that you don't have what happens to you is what Basically did not happen last year, which is a lot of people waited until Friday evening to get set up with tense test 161 and even though they had a lot of potential points They lost it because they simply couldn't get the submission system up and running on that Okay, a couple points on this as you can see on the board there basically test 161 It's an automated testing suite on this if you haven't gone so already take a look at the web page Scott put up a whole bunch of how-to videos for getting set up with this So just make sure that you are familiar you're going to be using this a lot as you go through this semester on this One caveat to everyone who is here if you are having any problems with test 161 One very common cause is you need to do a couple of the updates number one You need to merge in the staff changes on that I know there were a bunch of if you will get conflicts that emerged a few days ago So if you haven't done so already do so ASAP again merge in the staff changes that got released The other thing that you're going to want to make sure that you do is that you actually update test 161 again There's post about that. I believe it was yes February 10th on that and make sure that you're up to speed on that too One thing about test 161. It's obviously used to submit stuff for grading But it's also really nice that you can run tests locally on your home computer before you actually submit it So part of it is so that you don't hammer on the grading server But heart partly from your standpoint, you're going to get a lot of really good feedback quickly on your home computer on this One question I should anticipate. Hey, wait a minute. I thought I could already run tests from the kernel menu Yes, you can okay This is kind of the same thing with bells and whistles and it's think of it is doing it with a dry run using these If you will the testing grading engine So I guess there's three ways that you can run tests. You can run them from the kernel menu Okay, again, we'll be going over how to launch a user process in a few minutes You can also run test 161 locally in your home computer You can also submit it to the grading server and with the exception I should say of the reader writer test, which is this upcoming week Everything that you have on the grading server you will have locally for the balance of the semester So all the super hard stuff you'll have copies of that and you'll be able to play around with that locally on this Okay, the bottom part of the screen here what I'm trying to talk about is there are some mechanics about test 161 in terms of getting set up for an account and again It looks like a lot of you have already done that for the balance of the class that have not again It's not conceptually difficult But you want to make sure when you have to create an account for yourself on the test 161 website You're gonna have to download a couple widgets one of which you're gonna have to put in your local computer It's kind of Scott calls it a token. There's directions on that You're also going to have to generate a public key and you're gonna have to upload that to your Repository be a big bucket get lab get whatever it is on that So everyone up to speed on that basically what you need to do if you haven't done so already I need everyone here today to make sure seriously by tonight that you've gone through You're signed up with test 161 and submit whatever you have I don't care if it says you got a to remember you can resubmit as many many times as possible And you just want to make sure that there's no technical problems Everyone at the on this so far So far so good questions comments complaints Okay Moving right along then Let's talk about the last part of assignment one again. We already Lolly talked about locks and condition variables last week What we have left is the two synchronization problems namely whales and stoplights and reader writer locks So I'm gonna go through that. I'm not going to be spending a lot of time on that Hopefully it's you're already familiar with the general framework of what's expected But I want to give you at least some tips about how to approach some of these problems And of course obviously judgment day is coming up this upcoming Friday at 5 p.m. On that Moving right along here reader writer locks. Okay Actually, you know Let me bounce Okay, let me go through I want to back up to reader writer blocks at the end here whale mating, okay Whale mating and stop lights. These are the two synchronization problems that I was talking about if you haven't done So already take a look at the code stubs in your code database here All right To make a long story short what you need to do with whales is the helper code is going to be spawning off 30 threads 30 I'm sorry 10 males 10 females and 10 matchmakers and you kind of get the general idea You need to make sure that essentially you have one of each Before you call like the I'm gonna say like the start and end functions And that's what the test code is going to be checking like by the time you call Let's say the whale leave function you need to have at least one of each Otherwise, you know, it's going to say well, you're missing one of three on that. All right Take a look at the way the helper code actually Well sets things up. Okay. This is kind of the bottom part here And by the way, this is good coding practice in general like look what's expected of you. Look at the specs Okay, this isn't on the spec on the website But it is certainly well commented and documented in if you will the setup code What's going to happen is again, it's going to fork off 30 threads here All the males and all the females will be created first Okay, and that's kind of key to keep in point because none of the matchmakers will be created It's going to test to actually make sure that essentially no mating has taken place. Okay, if it is your code will bomb Okay, then what's going to happen is the setup code will create a random number of matchmakers And it's going to kind of send them off into if you will the river the ocean What have you and that's going to make sure that however many let's say it creates who knows 12 matchmakers It's going to make sure that essentially 12 and exactly 12 mating events have taken place no more no less Okay, so again in order for that to happen Basically, you need to synchronize between the matchmakers and the males and the females on this in other words You can't have kind of the males and females zoom right through Okay, because again what we're looking for in that is you got a kind of block the threads for the males and the females Okay, and again, this is where your Synchronization problems come in and this is where you want to think about how I'm going to stop the process of threads the males and the females From kind of going all the way through Okay, and you also want to kind of coordinate some form of communication in other words Exactly how many matchmakers came in that's however many males and females can now go through the mating process and finally be released Okay, and then obviously at the end it's going to make sure that you'll have finished up with all the rest of the threads So everyone with me on this again The key point is create all males and females create a random number of matchmakers And you got to coordinate that number with the males and females Okay, block them until the correct number of matchmakers yada yada yada on this how many people are done with whales Great, okay, so I'd say probably maybe about 30 40 percent of the class for the rest of you It's not a tough assignment this you remember you do have full testing code for this So take advantage of this okay if you were bashing your head in about the earlier part of the assignment This is kind of a nice place to get started. I should say this too. What types of primitives should I use? You know what yes, okay? There's really no right answer. Okay, I've coded these problems several different ways And certainly some problems are easier. I'm sorry some Approaches are easier than others, but as long as you pass the test and understand the problem That's what we're really looking for on that Okay Moving right along that was whales. Let's take a look at the other if you will Synchronization problem stop lights here. Okay, and again here, too. This has a nice write-up on the website They make a long story short You got a four-way intersection and there's going to be cars coming at you every which way and you got a kind of kind of let Them through right what this is looking for is well Let's kind of take a step back Jeff is talking to you people about like synchronization already He's gone through like the like the bank account example. Yeah, okay great Okay He may have mentioned but in terms of synchronization kind of there's a trade-off between Having fewer locks that's easier to implement, but that's less efficient and having a whole bunch of synchronization that is Harder to implement it's more complex obviously, but it tends to be more efficient on this because what's the easiest way to? Solve this entire problem. I've got a whole bunch of cars that want to pile through okay Well, how can I make life easy on myself? Ideas yes One car at a time right because if I have one car at a time how many synchronization primitives do I need? Right exactly okay all I need to do is one primitive and just meter it through like that It's actually going to be really safe here. Okay, but the problem is what I let one par through and it is killing me on what? I'm sorry Efficiency exactly right because if you think about it, okay When you have a green light traffic coming from the other side also has a green light at least on most intersections So that's actually what the test code is going to be checking for all right It's not necessarily that it's opposed to one lock But it's going to be checking to make sure that you are allowing multiple cars in the intersection But of course also you want to make sure that you don't have a car crash now the last I'm sorry the third Arrow there liveness no deadlock. What are we getting at here? Let's say that we want to let a whole bunch of cars in and also let's say we're going to let them in But we're going to make sure that they don't crash What a potential problem with deadlocks here. What are we trying to get at like? Let's say that I let four cars in one from each direction and let's say that they're all Let's say wanting to turn left at the same time problem with that Okay, because if they're all in their first square Okay Did you people waste time when you were really young with maybe I'm dating myself those kind of puzzles It's kind of like you move the squares around and whatnot. It's kind of like a what two-dimensional Rubik's Cube here Okay in order for those things to work You always had to have one at least one free square Because if you think about let's say you have four by four grid and all four of 16 squares are already taken up You can't move anything right well same thing here, too I mean now that's an extreme example, but there are some other scenarios that can create deadlock So you want to be careful again? There's no exactly right answer on this But bottom line is this as long as you pass the tests you allow multiple cars in the intersection with no No crashes. Okay, you're golden at that point. Okay, don't try to waste time making your algorithms Super efficient. Okay, let me say this about this class in general You're gonna learn a lot as you go through this semester. Okay, don't try to be super cute I'm kind of the biggest hypocrite in pride if you will preaching that but honestly You're gonna have a lot of work ahead of you as this semester progresses here Right now you want to learn as much as you can to get over the basic tests Okay, and I think for the most part you're gonna find you're gonna have plenty of work ahead of you Don't waste time trying to over optimize your code when you still have other work to do with me on this Okay, sweet. Okay. So again, those are the two synchronization problems All right back up a little bit to reader writers on here. Okay now reader writers typically have been the bait nor previous classes here Okay, it's not that they're conceptually difficult again What is a reader writer lock as you can see here? Well, it's a lock, but it kind of has two flavors Okay, you can enter and either read mode or write mode and kind of the theory behind this is you know What a lot of the time if all we're doing is reading It's okay to let a whole bunch of readers in simultaneously. So why should we block them? All right, I mean think about this way Okay, think back to maybe 250 where you had let's say a list and let's say that okay So we have a list that's shared among a whole bunch of threads or processes on this Okay, if all you're doing is reading the list How can you possibly muck things up for another thread? Okay, think back to Jeff's bank account example where he's talking about let's say if you will two tellers changing the bank accounts What's the keyword in that the two tellers are doing what to the bank account? They are Changing it right writing it if all they're doing is reading it We can have a million people read the value of the bank account. It doesn't matter Okay, so we might as well just let them all in that's the concept behind reader writer locks on here So in terms of what's going on here, okay? Well, you can see there's a bunch of If you will other terms by which these things are known here Okay, next slide here talks about where some of the stubs are oh by the way one caveat you may have encountered this already I believe Scott put a post on test 161 if you try to submit the code that you have so far It will blow up if you don't have your own stubs created in the dot-c file Okay, so take a look at kind of the interfaces that we're expecting from you at the very least You're going to want to kind of put stubs in your sync dot-c file before you submit even if you have no code in there So just again I got you there All right, but again this is if you haven't started reader writer locks This is kind of the interface against which you are coding now the last slide here I want to spend a moment or two on this How can we actually get reader writers off the ground and for those of you who are already pounding away? Let's talk a little bit about some of the common gotchas on this here. Okay? Simply put here. Okay, you want to think of reader writer locks. It's going to be significantly more complicated than standard locks It's going to be a state machine. Okay, so you're thinking oh good grief remember back to 115 116 And I didn't know when I was remediating that class I remember we had color holders and let's say red triangles and green squares and whatnot I don't know if you're still doing that or not But the point is you had if you will a state machine and it's kind of the same thing with this Okay, obviously you can have the lock and what one of two states readers and writers Okay, but it goes beyond that a little bit and let's talk about what I actually mean here. Okay? Couple if you will gotchas here number one is you need okay How can you actually if you will divide things up between reading and writing here? We have a bit of attention here between the fact that readers can all pile in at once But writers have to mean be metered one at a time So sometimes I see students saying oh, I know what I'll just kind of use a clock And you know allocate so and so many milliseconds to each thing the thing is you're really really going to short change Writers on this because if you have a hundred readers and a hundred writers on average The writers are going to take probably about a hundred times as long wall clock time for them to go through. Okay? By contrast if you let's say meter it by the number of threads that you go through Okay Well, then writers are going to take a hundred times longer than readers So there kind of has to be some sort of a balance again bottom line just pass the test on this This is the bottom if you will I don't know what you call that thing the need to prevent Starvation not the bullet point But whatever it is you need to prevent starvation on this starvation just talked about starvation already has he gotten to schedulers yet? Okay, he has Okay, anyone. Okay. What do I mean by start? He will okay when we were talking about thread starvation What do I mean by starvation in the context of reader writer locks ideas? Let's say just for grins here that I have a hundred readers I'm currently in reader read mode So I've got a hundred readers that currently hold the read lock because again they can all hold it simultaneously and now a writer appears I got a block that writer right because I'm in read mode Okay, and I'm waiting for the read but in the meantime since I'm already in read mode Let's say a hundred more readers keep piling in more readers keep appearing and I'm still in read mode So I'll just wait till all the readers finish. The problem is readers keep piling in see what's going on What's happening to that poor? Writer it's being starved okay, and your code needs to account for this and Jeff will definitely be throwing you plenty of situations where in essence the readers will never cease So at some point you got it aside How am I going to cut off future readers and even though we're in read mode? You know what no mass Okay, I'm gonna start blocking not just writers, but more readers make sense and Vice versa, too. I might add when you are in write mode Okay, let's say you've got one writer holding the thread ten writers that are waiting to grab the lock here Okay, and a bunch of readers that are blocking here Well, you know what even though there are readers waiting and you've got more writers coming in at some point You're gonna have to deny future writers in order to let readers to get a chance at things So this is kind of what makes this lock a little bit trippy tricky to implement and again talk to the staff If you haven't done so already I think most of you have been to office hours But there's a bunch of ways to skin the cat on this thing apologies to the SPCA But basically what you need to do is make sure that in some way or another both Readers and writers get at the lock in a timely fashion That's what's going on and that's really what we are trying to test with this Questions comments complaints about reader writer locks whale breeding car crashes The sounds of silence Not too bad on time Okay, assignment to overview actually maybe just kind of got reaction. How are people doing on assignment one? Thumbs up thumbs down questions Okay, okay good. How many people have started on assignment to Okay Okay, I know some have and I will say this don't worry if you have it because that's kind of the expectation that you have Not you're still officially an assignment one week, but remember Friday evening begins assignment two Okay, a couple things here is Jeff has decided divided up assignment two into two parts this year There's kind of an initial checkpoint next week and then the bulk of the assignment will be due the day of spring break So kind of keep those deadlines in mind on this All right What is a sis call and how does it kind of fit in with respect to? computer organization overall all right and Nifty little diagram up here. This is kind of roughly speaking how things are divided up in a computer There are roughly speaking three layers. Okay. There is user application There is user library code and there is kernel code here this being an operating system class We're focusing this semester obviously on kernel space and my point being is a kernel contains and whole lot of stuff Jeff's already talked a lot about that in lecture All right, you can see one of the parts of the kernel is this thing called sis calls more on that in succeeding minutes But roughly put the sis call is the interface or the bunch of functions that is facing user code And when I say user code, let me be clear about this. I don't just mean Applications user space technically considers roughly speaking applications, but also library code aka platform What have you okay? You can think of library code as either being let's say libc on a unix machine or let's say Android the if you will but like the Android library calls if you're on a smartphone or what have you on that all of that stuff is Technically user space here and how it interacts with the kernel is these thing of my bobs called sis calls All right, so that's kind of the key of this upcoming assignment number two Okay, other things in the current you can kind of see it boils eventually down to the hardware level That's where device drivers and a whole bunch of other funky stuff live All right Basically again to reiterate System call is a way in which a program can request services from a kernel Basically essentially it's the only way that it can interact from a kernel Jeff's talks you people presumably already about privileged mode And how essentially for a whole bunch of reasons the current kernel reserves to itself The right and the ability to do a whole bunch of things like interact with the hardware Okay, to set the system time. Okay to assign memory all that stuff So if a process want something let's say a process wants to read a disk file It has to go through the kernel. Okay, it's not allowed to touch the disk drive Hardware or the disk drive device driver itself. All right, so that's again where the sis call interface is Okay, and again, you can see basically both hardware and software related services What's perhaps an example of a software related service? I'm glad you asked Okay, you've already coded them up and assignment number one. Okay, sometimes User code has a bunch of threads that it needs to coordinate So let's say a user code has a lock and user space. Okay, and what happens if let's say a thread Wants to acquire a lock and it can't and it has to go to sleep How does a thread block? It needs help from the kernel wallah sis call. Okay, so again Sis calls are used for both hardware and software related services on this Okay Last bullet point there take a look if you haven't done so already the sys 160. I'm sorry OS 161 man pages Jeff has placed them on the course website. They are also available directly in the source code If you noticed it's kind of in the root of your get clone directory It's man slash sis calls yada yada yada. Those are your friends repeat the man pages are your friends Okay, Pete they're for your friends read them They are essentially the documentation or the spec that you need to implement and that the staff will be testing your code against Essentially for assignment to and to a slightly lesser extent in assignment three Okay, so what do I need to put into my sys right look at the man page yada yada Okay, now little bit about what a kernel is and how you can actually get into it What are we talking about interrupt handlers? I thought we were saying that interrupt handlers and all that hardware stuff was you know if that's within the kernel Well, Ken Smith. I don't know if you know him. He's one of the sys admins here I know he made a very poignant statement to me a few years ago. Essentially, you can think of a Operating system is a gigantic interrupt handler everything Or all interaction with the kernel essentially begins with an interrupt sometimes known as a trap or a fault There's a bit of a religious war is to to what extent these terms are if you will interchangeable here, okay? So basically an interrupt or something happens, okay And that's where we begin here and you have to go to well as the CPU enters privilege mode here And it runs. Voila the interrupt handler, which by the way is given to you Okay starts off an assembly language routine that you don't really have to worry about if you want to dig into it It's in trap dot see definitely at some point for assignment three you will have to look at this So it determines if you will what type of interrupt do we have here? Okay, and there's a whole bunch of them, you know, you could have a division by zero That's a software fault here. Okay, it could be something like you know what a hardware You know the disk drive controller saying hey, you know what that batch of data that you asked about it's now ready for you Okay, and if it's a hardware interrupt typically you want to call the device driver, okay? The timer Arguably is a type of hardware interrupt although we often kind of talk about it separately on this But when a timer fires typically what happens is what happens? Let's say that a thread has been running the timer fires. It means what times up pal, okay? And you got to run the scheduler All right other things to sis call. Hey, wait a minute. That's an interrupt. Yeah, okay A sis call is basically a programmed interrupt when you write in your user code, you know I want to let's say open a file. So I have to call the open sis call Underneath the hood what's going on is there is a software interrupt that is inserted into your code here So basically again the interrupt handler has to determine what type of interrupt it is and if it is a sis call We then go to this thing called the sis call handler kind of the next layer down in the matrachka doll So far so good on this we start off when the interrupt what type of an interrupt It is if the sis call we now go to the sis call handlers in sis call dot C Clear as crystal clear as coffee sounds of silence Social hour at the trappist No Okay, oh Side note page faults are arguably well. It's another type of fault. You'll be handling those for assignment number three All right now I said that if you will a sis call is your API into the kernel Well, think of it as a fancy-schwanti function. Okay, so how are we actually going to like where are these I Don't know. I don't know Call them, you know what maybe a picture is worth a thousand words here Okay here, let me just bring this up Alrighty, what I just did is I brought up the file sis call dot C and I'm kind of stepping through the contents of the beginnings of the function Sis call dot C or I'm sorry sis call. Okay in other words if there is a Interrupt and it's determined to be of type sis call. This is the function that gets called here Okay, a couple comments about this Ali put this on the slides earlier These comments here read them. I know most people don't pay attention to comments You will need to understand these comments in sis call dot C Backwards and forwards in order to get anywhere with the sis call assignment along with these comments right here again Please read those comments. Okay, essentially the sis call dispatch is a switch case statement Okay, each sis call has a number and depending upon what sis call that is you can see that it simply calls a sub routine So far so good pretty straightforward Okay, now what I was trying to get at in terms of argument passing. Okay, there There it is here. Okay the Comments go over this in a lot more detail. Oh, where is it come on? There it is. Okay, but okay, let's take a look at something for example like the time sis call That's already given to you. Okay sis underscore time. That's simply a function. It's in another file Okay, but if the sis call type has been determined to be the time sis call Okay, what it does is it calls this time function with what's all that gibberish there? Basically, there's two parameters on that. Okay, and actually the man page talks a little bit about that But for now, let's just say that the first parameter is of type user pointer T And the second one is of also type user point of T That is fancy schmancy a pointer to somewhere in user space here And then the next thing here is this trap frame was TF underscore whatever it is. Okay What's going on with this? Well glad you asked Okay, when You call a sis call a bunch of things happen. There's an interrupt But the other thing how many people took 341 especially with Christian learn. Okay, it's going to make it a little bit easier on this To make a long story short You want to be somewhat familiar with MIPS argument passing convention You don't have to ever write any assembly in this code like in this class. Let me say this but Basically by MIPS convention remember MIPS is the chip that's being emulated in if you will sys 161 here basically the first four arguments of Any function are going to be passed through a register and the registers are a 0 a 1 a 2 a 3 a for argument on that It's just kind of a MIPS convention on that these arguments in the registers When you have a sys call are saved out already for you They're already saved out for you by the interrupt handler and a C structure called the trap frame Okay, so again, you're going to need to spend a little bit of time becoming familiar with this It's somewhat niggling and mechanical, but it's going to help you in the course of the next month or so and Simply put okay TF is a pointer. You can see again up to this There it is see that struct trap frame, okay, and there TF is just a pointer to this structure and There's a bunch of if you will Elements in the structure one of which is TF underscore a 0 TF underscore a 1 and those are simply the fields that have been Pre-populated with the saved registers so again to reiterate when a sys call happens It's an interrupt. Okay, and the interrupt handler saves out all of your registers for you very nicely on this trap frame structure Okay, and you need to extract the appropriate values as you need them because again Take a look at what the time sys call is doing here a 0 is the first parameter a 1 is the second parameter Make sense so far Okay Back to PowerPoint or whatever it's called in Linux. Okay Okay, I Already talked about the first couple of bullet points here the first four If you will parameters are saved and registers a 0 through a 3. Oh by the way, what happens if a sys call only has two parameters Well, the others are just not used you can just kind of ignore them all here Okay, these parameters you can use them and by the way the types of these parameters Could be anything they could be that user point or t they could be an int They could be a char point or what have you okay, so again, how do I know what type of parameter is where would I look? the No one where do I look for information on sys call documentation? Man pages exactly okay, so and the man pages will tell me what type of well whatever it is You know what let's actually take a real quick look see at that right now here if I can I'm gonna have to put down the mic again for just a sec. So bear with me So web page down here just put them on there man pages sys calls sys calls and Let's just say that I want to know what are the parameters for who knows time remember that's the one I was looking at earlier. Oh, well, look what do you know here? Okay, the time sys call is expecting some pointer to some user space internal thing. So that's what a user pointer was okay, let's say I want to know what the Parameters are for let's say the open sys call Okay, because this is probably going to be one of the first sys calls you're implementing take a look at that Okay, open by the way for open you only need to implement the first variety Okay, it has two parameters as you can see the first parameter is a char pointer The second parameter is an it so in other words if I'm writing up the sys call Okay, again, let's go back to the example of what I had earlier here Okay, if I were to say kiss case sys open Okay, what I'm going to have here is I'm going to be calling the function sys open and the cast for a zero is going to be what it's going to be a Char pointer exactly right and the cast for the second one is just going to be an it With me on what's going on here? Okay, and you just go right down the list again your man pages are your friends follow the template and populate in this if you will Switch case statement on this now one got you here and this is super important People always follow into the fall into the drink on this every single year All right, and that if I can find it that is come on Ah Take a look at the down there. I'll be highlighted in red here. Let's talk a little bit about pointer arguments Okay, the pointers themselves like that char pointer to the file name for open Okay, that was saved out as if you will one of the Registers on it's going to be an a zero. That's fine. You can use it But when I say use it you can examine the value of the pointer that pointer points to some memory address In user space well, we hope okay, and that's part of the problem here Okay, and if Jeff has gotten to this yet here But one of the whole purposes of all the security and operating systems is that you don't want Processes user code to blow up other processes or blow up the entire computer. So TLDR basically the kernel never never never trusts anything from user space Okay, what that means is the value that the pointer points to You can't trust All right, so, you know normally, okay I have a pointer to the file name that I want to open well I would just do what call like mem copy or stir copy or what have you can't do that As a matter of fact Jeff is going to test that you try doing that it will blow up your kernel and your tests will fail All right, so what do I need to do here? Well, you're already provided with these handy-dandy functions called copy in and copy out Jinghao has already put together a whole bunch of slides that talk about the deets of that also look at like the .h files And which that contains but basically to make a long story short in order to dereference a pointer You can't do it yourself. You have to use these routines which do the safety checking for you This by the way is not if you're thinking this is kind of like futsy. It's like really weird and operate No, this is actually the way all production systems do it linux does this BSD does that any operating system worth its salt has safety checking routines that need to be done to D-reference pointers with me on this So again, how do I add a syscall? Well, I start by looking at the man page That gives me an idea about what the if you have the interface of the function is and the types of the parameters I added into that switch case I can kind of pull the values off the trap frame and then I can now kind of call my Subroutine and that's get me started on that Okay a couple examples I'm not going to spend a whole lot of time on this but time again This is the one we already talked about if you will a zero and a one Okay, those are the first two parameters. Hey, by the way, I didn't talk about a three and v zero I glossed over that the deets are contained in the syscalls.c file But up to now I've been talking about how you get information into the kernel from user space. Those are arguments Well, we got to get information out the other way, right? Return values, there's two types of return thingamajigs that you need to be aware of and it's a little bit confusing Okay, there's an error flag and there is a return code and they're kind of in some way almost combined But to make a long story short you're going to need to pass back two parameters A lot of this is already stubbed out for you in the syscalls.c file That's actually what this if you will slide alludes to can you see there a three indicates whether or not it's exceeding or not V zero actually indicates the return value same thing is true for all of these Okay, oh, I should say one thing Mental note right now. L seek is a pain in the down under. Okay Simply put it's not conceptually difficult, but L seek involves a 64 bit parameter And it also involves a 64 bit return value So it's going to be the exception to a lot of these rules so again repeat you want to allow enough time for you and your partners to kind of digest L seek and Read the code about how you handle 64 bit parameters And it actually even kind of pushes the last parameter past the last argument and on to the user's stack So it's going to be a bit of a corner case. I just want to kind of plant that seed going forward Talked about copying and copy out Example okay pause for a moment or so we have a few minutes left questions comments Everyone understands this Clearest crystal clear as coffee Oh, yeah, actually there's yeah, you're right. We're we're kind of a little bit ahead here Remember this recitation will be repeated Friday, too So by then they'll essentially be done with assignment one. So but yes not to scare But again, I do say again all of you people will hopefully be looking at this Stuff in ideally four days. Okay, so okay, what is assignment two? Okay, again caveat much much harder and longer. I don't want to scare you. I just want to plant the seed I will tell you in return. It's fun Okay, this class it has a sigmoid on a learning curve if you haven't heard that already You have to put a lot of time in but eventually the light bulbs go off one after the other So you put the time in you'll certainly get a lot out about how things actually work. All right essentially assignment two is Writing a process subsystem on this. Okay. I talked a little bit. Okay Let me skip ahead for just a bit what you are given with operating os 161 out of the box You're given a threading subsystem, and you're also given a working file system. You're given threads You're given files what you need to do for assignment two is roughly speaking you need to implement processes support for processes and Then assignment three you'll be doing memory on this. Okay, so what we're doing roughly speaking How do we implement a process subsystems? Well, you're writing syscalls You're also writing the underlying data structures to support the state for a process on that. Okay Good exam question. By the way, what's the difference between a process and a thread? Okay, I won't go into too much detail later on right now But quick version is a process is roughly speaking a bunch of threads that share the same user context All right, that means the same user address space the same user file table of files that are currently open to the process Etc. Etc. On that all right. Oh I should say this too. You're not going to have to implement multi-threaded processes GF makes allows you to make that simplifying assumption So you should know what they are but for the purposes of your coding assignments You only have to worry about one thread per process on that so again here assignment two We're writing a process subsystem but please take the time to read over the code reading questions digest them and Sit down with your partner. You really do need to plan Assignment two is not just a bunch of one-off standalone functions like assignment one Okay, that's one of the things that kind of makes it difficult on that I mean kind of very quick example of that is okay How do you open a file in? Unix on this by the way people have how many people have played around with let's say the basic C CIS call interface like open close read write you've sat down on timber like or elsewhere How many people? Okay, very few. I'm not surprised. Okay Strong strong suggestion again certainly by this weekend. Okay, you want to have Sat down and written code Okay, either on your own computer and you know your vagrant machine on Ubuntu on timber like wherever it is Okay, and a working machine not OS 161 but on a working Unix machine and Play around and demonstrate to yourself how you can open a disk file create a disk file Write stuff out to a file, you know create a file food dot text and write the string Hello world goodbye world to it and then read it back and print it to the screen Okay Why because you're gonna be writing these CIS calls doesn't it make sense to be able to? Know what they do and you use them before they write them Okay, so please once again tell your friends this to tell your enemies this to okay write Test code so you can understand what it is you're coding against before you actually start writing the CIS calls Okay, I didn't know what these things were and I'm sure glad that I kind of went through the motions on this here So back to what's going on here, okay? What we need to do here is you need to be able to let's say I was talking about preserving state Okay, if I want to let's say read a file, okay? You can actually take a look at the man page for that. Let's just pull that up very briefly And where was this? Okay, this is well, that's open. Let's go to read That is okay. Okay. This is how you read a file here Okay, and to make a long story short here the second parameter here points to the Buffer in user space that I want to read the data in the third parameter is how many bytes do I want to read? Well, wait a minute from what file well glad you asked take a look at that first parameter It's an integer. What how do I how does it know whether or not to read food text or bar dot text? well, what you actually need to worry about here is Preserving well, what is this this FD is what we call a Handle to a file descriptor. It's an integer that the kernel uses internally to refer to the structure of file data Okay, that represents food dot text, but how does it know that it's food dot text? Well, remember from what we were talking about earlier with open, okay? It's namely put if you look at the open sis call Okay, you specify food dot text is one of the parameters and the return value you get is this FD How do I preserve this across sis calls? That's your state That's your if you will your data structures and this is where you're going to need to do some heavy-duty architecting with me on this So this is where it becomes Significantly more difficult than in previous assignments on this. So let's hear back to assignment to here What do I actually need to do for assignment 2.1 here? Okay, roughly speaking you need to have a simple hello world program running Okay, that's what the console test does by the way If you've kind of tooled around with it in either test 161 or from the console But basically you need to be able to support printing to the screen now when I say printing I'm talking about printing from user mode Okay, when you are in the kernel menu and you've been running let's say whales and stop lights you're inside the kernel itself That's really not the way true operating systems work Everything in real operating systems gets done from user code and then pass through the kernel And that's what you actually need to kind of code up and support on this So basically what you need to do is have a quote-unquote functional console and pass the console test here What you need to do is at a minimum have a working open syscall and a working right syscall It doesn't have to be perfect, but it has to be good enough to get past the basic console test on that More deets on that here Okay, what you need to do and again go to office hours for the niggling details on this But what unix expects here is that? Console I oh which is fancy for support for reading the keyboard and writing to the screen is already Pre-opened for the in it process Okay, well pre-opened that means you're gonna have to do the pre-opening Okay, this is part of the assignment So you need to figure out how can I open the console and how can I? Pre-populate the file table with these new file descriptors and that has to be added to the inet process on that Okay, so this is kind of the core of this first assignment here I talk a little bit about how the inet process gets started here simply put when you're at the kernel menu Okay, you know you're typing let's say sp for whale mating or what have you you can also type as we have an example here Let's say P, and then let's say who knows bin slash true. It will run as the inet process true dot C okay, and Okay, basically that kernel menu it forks off that child thread And then there's code in OS 161 to create and run the first in that process Okay, that's already given to you Okay, I'd make a couple suggestions about where you can actually start adding code here Okay, the if you will the data structure for the initial process is created in proc run program and The process itself is launched in simply run program in run program dot C Oh one small thing, okay You will need to block the kernel menu look at the if you will web page for details on that But basically put what you want to do is put the kernel menu to sleep while your inet process is actually running Okay, things you're given I already alluded to this earlier out of the box You're given a threading system and a file system you need to op if you will implement Process support which means writing a bunch of sys calls Okay in specific for checkpoint 2.1. You're gonna start by digging in to support for files Okay, the inet process is already created for you, but it has no support for files That means file tables file descriptors or pre-opened console. That's your job for next Friday's checkpoint on that It's a short deadline, but I'll also tell you frankly it was done for a reason. This is a big big assignment and You know you need to see how big the assignment is ahead of you on that So because I can tell you this is peanuts compared to what's coming after next Friday Questions on this Okay, I'm gonna be hanging around as long as there are questions on this So I think another class may be migrating in here shortly, but thank you very much for your time Check out the post online. There's a bunch of stuff on discourse already about how to get started come to office hours Good luck people