Hello, okay All right, that almost broke. All right. Welcome back to operating systems. So today choose your own adventure There's not that many people here. So what do we so? The choices are all on the slides have all the topics on them. We can go over all Really quick and I can talk can yap at you or we could look at past midterms and We can see what's important about them or whatever what you want to do blaster everything look at midterms midterms I mean I I wrote them so they're gonna be similar earlier topics processes and page tables. All right, so Here I'll go over the Real quick. So let's go here. So just in case on these slides Examples of midterms I've read so on my site. There's 353. It's essentially the exact same course of midterms the same length 75 minutes We can go through that one first should cover pretty much the same topics There's also CS 111 which was the UCLA version of this is not a first-year course their numbers get their Course numbering is just really stupid. Don't ask me why if it starts with a one It means it's a third or fourth year course. Don't ask me why So that's similar, but theirs was two hours yours is 74 or 75 minutes So that's the style I typically write so we can go through them see what's what So here is a UCLA one. I'll skip it real quick. We can come back to it All right, so here was 353 it's essentially the same course. It's by the same person and it's the same length So this will be fairly indicative of what you will have In fact your cover page will probably look exactly the same because why would I rewrite it? All right So beginning of it had like a bunch of short answers to make sure that you know you've paid attention during lectures and Should be one one or up to three sentences So first one was what prevents a process from modifying their own page tables so The only thing that should manage page tables is the kernel, right? So the kernel is the only thing that should manage page tables and it does it in kernel mode Your process runs in user mode, which does not have access to Physical memory or anything like that your processes only use virtual memory and the kernel does all the page mappings for you, right? questions about that one right All right, then the the follow-up was well Say a process could modify its own page tables. What issues would that have? Well, if you can modify your own page tables that means you can read all of memory So there's nothing independent between processes. I can read, you know, your banking application or whatever You can just read anything nothing is private There's no security no no nothing right pretty bad You could also modify each processes memory so could you imagine you write your program and You try and write correctly and someone just randomly write some new values Your program is not going to work. It's going to be a disaster. Oh Yeah, the next one. Hey, let's see if we can leave this to the audience So how can a process communicate using file descriptors if each process should be independent not quite They're managed by the kernel and file descriptors are like essentially pointers to things, right? So each process has its own independent file descriptors. That's true But what the file descriptors represent can be shared between processes So for instance, that's how you type in something in your shell Then you run your program and when standard out output something you actually see it in the exact same terminal They're all sharing your terminal file descriptors, right? With a pipe while your pipes were sharing file descriptors. You created a pipe got to file descriptors You forked so the new process got its own set of file descriptors that are independent But they point to the same thing so they might point to the same thing But if one process say closed file descriptor for it wouldn't close file descriptor for in the other process So they're independent in that way, but what the file descriptors represent is an independent Good All right So, yeah How can they communicate if they're independent? Well, they're act as pointers if you fork you get copies All right I'll actually leave this to you. So if you have a newly created process Can you create can you communicate with an existing process? That's not directly related using pipes why no so they The only way to share them is through forking, right? So if the only way to share them is through forking Well that already existing process would not have pipes open and because it's unrelated that the unrelated process Wouldn't have those same file descriptors either so there'd be no way to have a pipe between them because they're So it's unrelated so say So say I don't know well, and it's the parent of everything oops so unrelated means like unrelated directly, so And it could have process 2 and then process 3 and Then process 3 could create process 4 So we would say process 2 and process 4 are not directly related right, so if you were process 4 and you want and Process 2 already exists no way to do it Right unless in this case well if you go up to a knit and a knit creative pipe, then they could share it all but then you know You typically don't go up to a knit and it does not do that. So Yeah So pipes only existing between parent and children aren't isn't really true because a parent and a child could create a pipe and Not share it right they could create their own unique pipe So it's that forking that you have to say so So forking is the important part here All right Aside from calling exit or exit group to terminate a process. Is there any other way to terminate it? No So return zero is exit Signal kill it. Yeah, everyone's favorite So process can be terminated by sending a signal signal definitely do it so remember in lab to So you had w if exited and then at some point you realize that that might not be always true And then you check w if terminated or sorry. It's signaled right, so Exit or a signal can kill you So that's like the type of things Questions you'll get that are related to your lab, but aren't like here's your lab code So that made sure you if you did the lab that should be easy because you had the right you did have a case for that, right? All right for scheduling processes. Oh, this is your friend has a poor idea. All right So your friend says that we should switch processes after they execute just a few instructions So you have the lowest response time possible is your friend Smart or not? Yeah, so context switching is going to take some time Probably it's going to take more than a few instructions to implement a context switch So if you just context switch after a few instructions, you'll probably spend a lot of your CPU time just context switching Aka not doing useful work aka. It's just overhead So in this case your friend is silly because most your CPU time is just going to be wasted just Moving registers around and swapping things not actually doing any useful work So you want your context switch to be like less than one percent of your overall execution time something really really small All right, so short answer not too bad, right? We're not we're less scared What? Yeah, you didn't review at all, so this is not bad for being completely cold Yeah, the answer is pretty long and it it was If it's five points and the answer is not that long while we only like subtracted points from everything wrong You said so even if you said not much, right? You could have even got like three points Just don't say untrue things Like if you if you just have verbal diarrhea, will you get pressure? so you So for all these these will be like you should know something about it'll be like a process if you just say I don't know then At least the way I write exams. I don't know shouldn't be valid You should know something. It's like a process or something happens just say something so yeah, my The way things get marked is if you just like Throw up on the page and try and display all your knowledge My friend did that and I hated when he did that because he just tried for part marks and the way It gets graded here everything you say that's wrong. It's essentially just a waste of everyone's time So it's just minus every time you say something wrong So don't write like 10 paragraphs for an answer that is like three sentences so it just makes it longer to mark and Yeah, so here's another one So you ask someone to write you some code that prints 1024 bytes from a file descriptor they come back with two solutions here is our first solution so We essentially have a for loop that executes 1024 times Does that many read system calls and just prints out one byte at a time? And then the second solution is this one is they just create a buffer do a single read System call and then print it all out in a for loop So assuming that read and print F are always successful. What solution is better here? Okay, what's the reason beside less us? Can you not say definitively read is slow? So So is read a function call Our system calls slow Compared to functions Yeah, yeah Yeah, yeah Yeah No, so read well read one byte and then read the next byte and the next byte and the next byte So if you read multiple times you get it sequentially in order. Yeah Yeah Yeah, so in this case. Well, it was the simple it System calls are relatively slower than function calls So if you want things to go fast do as few system calls as you can So the first solution is terrible because it's like a thousand system calls other ones only one so That's why not to Not too complicated. The concept is just system calls slow Yeah, but function calls can be fast. I don't know who's to say it doesn't get inlined if it's a function or something like that That's another course. We don't worry about it All right. Oh Here's a fun one. So this is based off You guys doing labs? So we have a child process It opens as dev null file Which makes anything you write to it go to nowhere. So you never see it again So it opens it you get a file descriptor and then we do a dupe to does do people remember what dupe to does so the second argument copies and then Replaces Yeah, well you have your own So you're allowed one double-sided sheet of paper. Whatever you want Same as the final It'll be the same thing although I just put up a poll this thing for Must be handwritten not mechanically reproduced not true anymore. You can do whatever you want You can try doing what I did for one of my courses, which didn't work out very well I took all the lecture slides made them super tiny and printed everything on a page Yeah, I I did not do good on that exam Huh, no I could read it. I just didn't study before that it's I could I could read it. It just You have to go through like thousands of pages to find an answer. It didn't really work All right, so for this so It says first question here You're trying to debug your program you insert this print statement at line a and You never see the output debugging in your terminal Then you s trace it and you see that you know calls to open Do to are successful and also the right happens. Why do you not see? Debugging I thought printf was supposed to go to your terminal Yeah, so printf well printf just writes the standard out which is file descriptor one and Yeah, that doesn't matter same thing so This writes to file descriptor one and this dupe two replaced file descriptor one to point to that black hole void so We printed we did a right to file descriptor one. It's just we changed file descriptor one to point to something else So that printf actually executed, but we never saw it because while we change what standard out actually points to Some of you probably encounter this in lab two Okay, so next question is Does line be execute yeah, so do people Yeah, so it reloads it loads another program and replaces your process to start executing that instead So if exec LP or exec ve or whatever it's successful Then line be never execute because we're a different process What's the case that line be will execute? Yeah, so if it's except if exec it's successful you start this process is executing LS now No, it gets replaced So it starts executing that program So if so let's say process ID one oh one executing this function P well LS is going to have process ID one oh one because it's exact same it didn't change This process gets replaced and start second. No, so this process only ends whenever it calls exit Program same thing Yeah, so as part of the system call right the kernel is going to open that file We saw the elf file like the whole description of it and it's going to change this process's virtual memory to match that Load everything in the memory and start executing that It's essentially going to do like a make context on it just with more steps Okay, all right. Here's a real fun one then we can do this one. So Wait, can we see that probably not let's see the question so da da da So this is a four-parter. So how many processes get created? What's the state of all of them? after process a hundred terminates Okay, so let's just go back to it You tell me what this does So let's say Process 100 I think yeah So say process a hundred starts executing main Tell me what's gonna happen It forks. Okay. So first it creates x right assigns it to one then it forks So what's fork do? So here on this corner I'll write a family tree so 100 is the current process calls fork it probably oops and Let's just write Let's see what everyone's memory is to so process a hundred It created an x and signed it to one All right. So when we fork We create process 101 In process 101 will it have an x? It should be because it's a exact copy of a hundred at the time of the fork so it will have an x its value is one So the only difference is the return value of fork, right? So in process 100 what's fork going to return? Yeah, yeah in process 100 It will fork will return 101 and then create a variable called PID and assign 101 to that and then What's 101 going to return from fork? Yeah One because it saves us a bit its value of PID With the status of zero so it's Dead done What's its current state? Will it have an x? Yeah, so it'll be an exact copy of process a hundred at the time of the fork So it will have an x equal to one and then a PID equal to 101 What Yep Because it was a child Yeah, but This PID was so we're currently whoops. I just erased it. We're currently here So we're at that so we we're going to reassign it But right now at the time of the fork we go the fork happens first and then we assign it So we're not done yet. We have to read our programs Right That's like the number one thing people think programs just like do random things and don't go in order So fork happens first and then it assigns so Now fork returns and we were reassigning PID So now in process a hundred What is the return value of fork this fork? 102 and then we reassign process 100 and the microphone is cutting out It's been finicky. I will leave it there and hopefully it's fine. It looks like it's fine So it reassigns PID to 102 and then let's say in process 102. What would it get from fork? Zero, so it would reassign PID equal to zero. So 102 is here. So now Which one do you want to execute next? 102 So 102 Executes next it creates a variable called y and assigns three to it so It has what? Whoops. It has y. Damn it y equal to three Yeah All right. Do we want to keep on executing 102? Sure. All right. So is its PID equal to zero It is so it will sleep for three seconds so In this case, let's just assume that a hundred executes because three seconds is like an eternity So process 100 executes. It would create a y Process a hundred is going so 102 is currently asleep for three seconds so 100 went created y and then Yeah, it's PID equal to zero Hopefully not so else So it would change x right So which x does it change? Huh, so does it change this one? No, okay Okay, so we're changing a hundred to be two Does that also change this one to two? I really want to change it too like that. No, no, right. They're independent All right. Good so Let's say a hundred keeps on executing it changes y from three to four and Well four is along so three seconds is a long time. Whoops So it would hit this print. What would it print? So we see x is two y is four and then It returns what's returned from main the same as? exiting so Process a hundred has now exited with zero so now we can go back and Answer this question So what is the state of all of PID's a hundred children after PID terminates? No, it's all of process a hundred children. What state are they in? Yeah, so at the time it exits. Well 101 is an orphan 102 is an orphan and yeah 101 is also a zombie. So it's a zombie orphan So we got a zombie orphan and orphan orphan. So that's what the So That's exactly what said here. So it's a zombie orphan and this one is just an orphan process So that's what we said eight marks pretty good points marks like All right, what do you expect to happen after executing line C? Oh Yeah, so now we can continue executing So it wakes up declares this and calls wait. Oh, that's the problem no, so So wait will wait for your direct child to terminate and the first one right In this case does 102 have any children? That's an error, so wait will just be probably give you an error that says no children. No, you get an error You don't have any children Colonel smart it knows you have don't have a children filed. So you don't block forever So here says you would expect it to fail because the child process calls wait doesn't have any children. Oh We should have checked for errors Something I've told you a lot and hopefully you may have learned So it would have returned negative once that error. No, likely you didn't mean to wait in the child. So best to do that Some students Just changed this around and went like Whoops Not equal to zero and wait, but then you're also Changing around the print statements Which isn't great.  You got a lot of part marks for that, but not total All right, then we said show the output at line D when it's executed for all processes So yeah, we're still going with 102 So 102 would wait do nothing come here print. What does 102 print? It says one why is three so wait wait returns negative one. That's an error. We don't check for errors He's ignored it. So in the question you said that You just have to know it didn't have any children as long as you said wait does nothing because it doesn't have any children That's fine No, no, it just says what it just says what do you expect would happen? Oh Without changing is there anything you can do to improve it? Yeah, you can wait in the right one Just move the weight. That was also fine if you wanted to say that So that Yeah, so it was supposed to just be like check for errors, but I was pretty lenient about it if you Because some of them were like I won't change the placement of weight. I'll change the if condition or I'll change everything around it. So, yeah All right other stuff. So here this will make you feel better Scheduling question. So round robin qualm length of two Everyone is bored to tears when I did in lecture because it was easy. So That will probably be a question for you guys to do. It'll be mostly free marks that will take you like some time Huh? Burst time is like the amount of CPU time it wants to execute So in the lecture we use burst time should be easy. So here's Yeah, so page table question Here I can delete all this So we want to do in the last what 12 minutes Want to do a page table question or go over stuff or what? page table, okay, so too hard to think grand B page tables virtual memory and processes So we did a process. Let's quickly do a page table so This is an odd system generally Be the page size Again, you can bring a dumb calculator But if you know your powers of two you can skip doing a whole bunch of calculation. So quick 256 in powers of two, what is it team bit virtual address page table entry size of 16 bytes powers of two to the power of four So this system supports 32-bit physical 16-bit virtual the weird system Definitely behind that is all of the page table entries fit exactly on a page. So To the eight is our page size and then Two to the four is the page table entry size and that says how many entries you can fit on a single page So again, you know easy math like that. We should be able to do subtraction. It's to the four or 16 so that's the number of page table entries you can fit on a single page So Next question was like if we use multi-level page tables, how many levels of page tables we need So if we remember our equation, so it should be on our cheat sheet if you can't think of it on the top of your head It's the number of virtual bits minus offset bits divided by index Bits so What are the offset bits for so What are the offset bits for how many offset bits do I need? Yeah, so the offset bits are where you are within a page, right? So memories bite addressable So the number of offset bits I need is the number of bits I need to address every single byte on my page Right, which is two to the eight So it's where you are within a page. So it's directly. It's the page size So the page size here is 256 bytes and memories bite addressable so you need to be able to address every single byte Eight bits eight bits So it's just this So our offset bits are eight Then from the question our virtual bits right here is 16 and then the index bits are Which entry in a page table that you are selecting so here I can fit 16 entries in a page table. So I need four bits to Select which entry in a page table, right? So that's the index bits For this system for this system. Yeah, that's why we have the ceiling So this is how many we need minimum So in this case, you know It's eight divided by four which is a nice number. So it's two. So we need two levels of page tables This is the all the bits and are related to the page table entry size of 16 bytes So that's where all the flags and everything are Okay, let's go. Okay. So this is a screw up. So I said is virtual address h1 h1 h1 h1 valid I tried to be funny here because I tried to make it say hi hi hi and didn't realize for some reason that h is not a valid hex character so valid Response was h isn't a hex character.  So it'd be too big. Yeah So the number of index bits is related to how many page table entries you can fit on a single page Which is the first one. So it's this Yeah, so it's this one right here So for four bits All right, so that said oh, okay explain the steps you would need to translate the virtual address One two three four hex into physical address feed zero zero three four so if we take all the components of the address I was So the reason the system so weird is because it nicely fits to hex characters So we needed eight bits for an offset, which is two hex characters So these hex characters right here are the offset and then in each level of page table We need four bits. So that's one hex character. So this is our L zero index and This is our L one index. So anyone what what What would happen if I try to translate one two three four into address feed Assuming that it could do that translation. Everything was valid. Yeah, so Yeah, so they're Yeah, so when we're translating this address well, we would have a root page table Right, what level would the root page table be? L one We only have two levels here. So we only have level L one and L zero So there would be a root L one page table So in order to translate this address We would use that root L one page table and then look at the entry at index what? So in that L one page table when we're translating this address, what index would we look at for the page table entry? One what's in a page table entry? so in a page table entry the important things in it is the physical page number and Then some bits like a valid bit and then well if you for lab three there was like a read write in the custom bit so probably some bits and The physical page number so the physical page number tells you where the L zero page table is right? So we follow that to an L zero page table. What index do we look at in that page table? To and in that page table. What would have to be in that page table entry? Yeah, what what physical page number would have to be there, huh? Yeah, so it says This address This virtual gets this physical address. So L zero should have our direct translation, right? Yes, not four zeros it should just have this right Feed zero zero because that's the physical page number Because the last two is just the offset the offset doesn't change. So the physical page number here is Is 24 bits and Our page table entry size is 16 bytes. So it's way bigger Yeah, so then the set the last thing in the last minute says for the system Could I use a smaller page table entry size if so? Well, that's where it relates. So Like you said the physical page number must be at least 24 bits and if we only had a few flags So soon we just have like a valid bit. We would need at least 25 bits So we could actually just use four bytes for our page table entry So that's 32 bits I also accepted if you want to say like we use 10 bits for flags and then this couldn't get any smaller That's fine. So just in this case We can make the page table entry a little smaller But even if we made the page table entry even smaller Do we see any benefits? No, because we still need two levels of page tables So it might not be full, but we still have to go through two levels anyway, so it's not gonna be any faster Yep Yeah, virtual to physical it's just you replace You put in the physical page number and then use the same offset for this here what just for a poem for you We're all in this together