WEBVTT 1 00:00:02.010 --> 00:00:10.889 William Cheng: This is the third part of lecture 14 so we just finish with the hardware, the virtual memory hardware showing you how 2 00:00:11.849 --> 00:00:19.620 William Cheng: You know the MMU is implemented and also a bunch of other issues with using page table and the mute unmute and stuff like that. 3 00:00:20.100 --> 00:00:26.820 William Cheng: So now we're going to go to the last part of chapter seven, which talks about, you know, what is operating system is going to deal with, you know, 4 00:00:27.240 --> 00:00:36.240 William Cheng: How does open systems going to use all this hardware. Yeah. So the first topic over here is no as general concern. So, so these are sort of traditional 5 00:00:36.570 --> 00:00:44.580 William Cheng: Operating System issue when they need to implement virtual memory. So the question is that, you know, when you need to bring data from the disk into memory. 6 00:00:45.150 --> 00:00:51.840 William Cheng: There are several questions you have to answer. Number one, when are you supposed to do it right, that you know that the you know the data sitting on the desk. 7 00:00:52.530 --> 00:00:58.170 William Cheng: Well, you need to bring into memory you bring it in, you know, at the beginning, later or something like that. So we'll talk about that. 8 00:00:58.890 --> 00:01:04.260 William Cheng: The second one is called a placement policy will you bring a page from the data into memory. Where would you put it 9 00:01:05.130 --> 00:01:12.360 William Cheng: And then as it turns out that, you know, using segmentation while if you segmentation eg is the first fit memory alligator to allocate physical memory. 10 00:01:12.810 --> 00:01:16.290 William Cheng: With data can go into. But now, since you are doing paging 11 00:01:16.770 --> 00:01:27.660 William Cheng: The placement is no longer an issue because you're going to use the buddy system and every page, they're all equivalent to other page. So all you need to do. So as the buddy system for any physical page and then you can transfer data from that this into memory. 12 00:01:28.050 --> 00:01:35.850 William Cheng: Okay, so by using paging placement is no longer issue right but in the good old days, you have to worry about it because you because you're you're using segmentation. 13 00:01:36.660 --> 00:01:42.420 William Cheng: The last one over here is called replacement policy or what if you try to bring the data from the disc into memory and you're, you're out of memory. 14 00:01:43.380 --> 00:01:47.670 William Cheng: There. So if you are out of memory, what you need to do is that you need to you know what it is that 15 00:01:47.940 --> 00:01:54.720 William Cheng: You need to look at your memory and see which one you can write it out to the backing store. And this way you can create space in physical memory. 16 00:01:55.140 --> 00:02:01.650 William Cheng: Okay, we talked about that and segmentation right to say that you know that that when the first memory allocated or fail. 17 00:02:02.340 --> 00:02:12.150 William Cheng: In that case you need to create, you know, empty space in memory. You need to take a memory segment or you need to write to the desk and now we need to do this one page at a time. Okay, so this is called replacing policy. 18 00:02:12.660 --> 00:02:19.890 William Cheng: When you run out of memory, which one do you write to the desk in all right into the relative to the back end store. Okay, so we're also going to look at that. 19 00:02:21.600 --> 00:02:24.120 William Cheng: All right, so first let's talk about the Fed's policy. 20 00:02:25.440 --> 00:02:33.210 William Cheng: So So Robin is a most of the operating system today. Are they all use the same kind of a fetch policy. This is known as the 21 00:02:34.260 --> 00:02:45.690 William Cheng: The, the, on demand policy or is it also known as the demand page or on demand page, right. So the idea here is that when you try to bring it when he tried to bring a page from the distinct memory. You do this as late as possible. 22 00:02:46.470 --> 00:02:51.330 William Cheng: Okay, so this is sort of a, I guess in computer science, there's a principle called lazy evaluation. 23 00:02:52.170 --> 00:02:58.350 William Cheng: So basically you try to sort of delay something to do as late as possible because if you wait long enough, maybe you don't have to do it. 24 00:02:58.980 --> 00:03:05.910 William Cheng: So, for example, one of the solution over here is that when you know when you start running your program you take every page from your file and then load into memory. 25 00:03:06.510 --> 00:03:13.860 William Cheng: Okay. So as I mentioned before, the example I keep using is that Microsoft Word. Sometimes you fire up Microsoft Word you modify one word and then you quit your program. 26 00:03:14.610 --> 00:03:18.150 William Cheng: So in that case, you know, I guess, Microsoft Word has a really, really big executable. 27 00:03:18.660 --> 00:03:27.780 William Cheng: It's a really big I suitable program if you print the entire program in there and you only use a few instructions inside the program. Well, then you just wasted. You have just wasted all your time. 28 00:03:28.740 --> 00:03:39.240 William Cheng: Okay, so therefore over here. If you're using the on demand page. And if you're doing lazy evaluation. So what you want to do is that you want to bring only the pages that you need in order for you to execute that particular function that you want. 29 00:03:39.450 --> 00:03:42.660 William Cheng: And and all the rest of the executable, you don't you don't bring into memory at all. 30 00:03:43.380 --> 00:03:48.270 William Cheng: Okay, so therefore we're going to do this as late as possible to at the last minute. So we're going to see how this, this can be done. 31 00:03:49.050 --> 00:03:57.480 William Cheng: Yeah. So, by the way, lazy evaluation. I don't know if you have you heard that, you know, there are these evaluation is sort of a computer science design principle. 32 00:03:58.020 --> 00:04:03.090 William Cheng: Whenever you try to sort of sort of come up with a sort of an implementation or something like that. 33 00:04:03.540 --> 00:04:10.950 William Cheng: You need to sort of consider lazy evaluation because sometimes lazy evaluation works really well sometimes lazy evaluation work really poorly. 34 00:04:11.460 --> 00:04:20.760 William Cheng: So it's something that you need to consider that. So for example, when we try to bring a page influence this into memory. We can do it at the beginning when you start running the program or we can do it at the last final 35 00:04:22.590 --> 00:04:30.150 William Cheng: In the last final microsecond we can do there like that. So again, there's a design decision to make. For the case of the Fed's policy, as it turns out it works out really well. 36 00:04:30.690 --> 00:04:37.140 William Cheng: Okay, so they don't. Well, it's gonna it's gonna see see an example where lazy evaluation is absolutely the wrong thing absolutely the wrong thing to do. 37 00:04:37.770 --> 00:04:43.980 William Cheng: Okay, so, yeah, you know, since this work, this can work really why sometimes when you try to design your, your, your system. 38 00:04:44.310 --> 00:04:49.860 William Cheng: You need to you need to take that into consideration. Yeah. So let's take a look at an example to see how lazy evaluation work. 39 00:04:50.640 --> 00:04:53.370 William Cheng: They are. Oh, well, we'll go. Look at that pretty soon, and we just mentioned that 40 00:04:53.760 --> 00:05:03.810 William Cheng: Plays on policy is really, you know, it's no longer issue as the buddy system for page and you transfer data from this into memory and it doesn't really matter what that page is why doesn't matter where that page it 41 00:05:04.410 --> 00:05:15.060 William Cheng: What the reason is that because you're using address translation and use the page table to point to a physical page. So no matter what I pages that could be at the beginning of the memory or it can be at the end of the memory. It doesn't really matter if 42 00:05:15.420 --> 00:05:23.970 William Cheng: The performance will be exactly the same. Okay. When you're using the first memory alligator when you're performing a linear search one, then in that case, you know, where you put the data will matter. 43 00:05:24.450 --> 00:05:30.810 William Cheng: Okay, but if you will. But if you put into a lookup table, you just point a pointer to it. In that case, the performance without matter. 44 00:05:31.200 --> 00:05:38.670 William Cheng: Okay. So, therefore, if you're using page and refusing to pay stable than the placement policy becomes a bit. Other people become irrelevant. 45 00:05:39.570 --> 00:05:46.530 William Cheng: Now, what about the replacement policy right if you run out of memory, which one do you write it, you know, which paste you write it out to the back end store. 46 00:05:47.430 --> 00:05:51.300 William Cheng: Okay, so, so, so again this is required if there's not enough resource. 47 00:05:51.720 --> 00:05:57.450 William Cheng: So we'll kind of resource that we talked about over here or the resort is physical memory when you completely run out of physical memory, right. So, 48 00:05:57.690 --> 00:06:07.380 William Cheng: On your laptop, you might have eight gigabytes of physical memory if they're all used up and now you need to bring a page from that this into memory. You need to throw out one of the pages over the years, right. So which one do you pick up 49 00:06:07.620 --> 00:06:15.210 William Cheng: Okay, so we need to execute a replacement policy. So what are the possible replacing policy is a one policy is called Pfeiffer or first in first out 50 00:06:15.750 --> 00:06:18.810 William Cheng: Okay, so the idea here is that you cannot use store all your pace rain. 51 00:06:19.050 --> 00:06:32.100 William Cheng: According to the time that the patient was created as a mechanism to you so you have pastry. I'm sitting and the link is over here, this is the oldest why. Here's the newest one if you're using the fivefold policy first in first out. That means that the oldest one is 52 00:06:33.030 --> 00:06:37.530 William Cheng: The oldest one is going to be the one that you write out to the backend store. Okay. Is that a good policy. 53 00:06:38.850 --> 00:06:46.080 William Cheng: Well, I mean, clearly, that's not a good policy right because you know the you know the the the the the patient that you create it maybe at the beginning. 54 00:06:46.260 --> 00:06:52.620 William Cheng: The stuff that you read from the this. Those are the things that needs to stay in memory, all the time. So, therefore, as soon as you read through this. You have to bring it back. 55 00:06:53.280 --> 00:06:59.610 William Cheng: OK. So the four five for policy might be a really, really bad idea. So they don't all we're going to see what other policies out there. Yeah. 56 00:07:00.390 --> 00:07:09.090 William Cheng: Right, so. So first, let's take a look at the take a look at the fetch policy over here. So, so again you know what we're going to do is that we're going to start our program. 57 00:07:09.780 --> 00:07:18.750 William Cheng: With you know you know i think what will mention that in when we talked about segmentation. We're going to start running in our program as if everything is swapped out 58 00:07:19.440 --> 00:07:28.470 William Cheng: Okay so everything's gone through this and then when we start running a program going to bring them in one at a time and the mechanism we're going to use to bring the page. And one of the time is to use the page fall mechanism. 59 00:07:28.980 --> 00:07:34.710 William Cheng: OK, so again you know under what conditions, get a page for all right. One example that we saw is that we address translation. 60 00:07:35.190 --> 00:07:40.500 William Cheng: Here is the page table over here. There's a PT right here. If the equal to zero. 61 00:07:41.130 --> 00:07:48.540 William Cheng: Equals zero. That means that the pace table entry over here is invalid. So in that case we chop into the ordinances. And so he said opera disassemble can sort of decide what to actually bring in 62 00:07:49.470 --> 00:07:59.940 William Cheng: Okay, so, so, so, so, so, so the example I'm going to see is to use the pace for mechanism and use that to perform lazy evaluation to perform on demand pages. Yeah. 63 00:08:00.300 --> 00:08:09.330 William Cheng: So yeah, what happened is that will you get a pace while you're gonna paste on a virtual address good when you get a virtual that you're going to tap into the kernel inside the colonel right inside the process control, blah. 64 00:08:09.690 --> 00:08:16.920 William Cheng: We sort of point to the virtual memory map, which is the outer space implementation. So this is the implementation of the address space right here. 65 00:08:17.070 --> 00:08:27.750 William Cheng: And what we need to do is to say that, you know, every as we you know we here represent a memory segment. So you take your the virtual address that you paid for on and you walk down this list to look for a look for 66 00:08:28.590 --> 00:08:31.980 William Cheng: Look for as region that contain that virtual dress. 67 00:08:32.550 --> 00:08:42.360 William Cheng: Okay, so remember all the ass region over here the virtual address range, they don't overlap. So, therefore, if you come with the pace part over here, you should be able to find it inside one of the the estrogen over here. 68 00:08:42.930 --> 00:08:53.220 William Cheng: Okay, if it turns out it doesn't belong to any region. For example, it could be before the first one, or after the last one. Don't worry, you can actually fall between the gaps of these aerospace region over here. So in this case, what do you do 69 00:08:53.670 --> 00:08:56.130 William Cheng: Well, so in that case, it turns into a segmentation fault. 70 00:08:56.580 --> 00:09:02.520 William Cheng: Okay, so therefore, in that case, the corner will kill your program because you you make it illegal memory reference. Okay. But if it's within a 71 00:09:02.820 --> 00:09:10.560 William Cheng: Region. Well, then you need to look at the SVG. Look at the access rights over here. Look at where there's private or, you know, sure. And then you need to determine what to do. 72 00:09:11.370 --> 00:09:14.760 William Cheng: Okay. So that's the basic algorithm where you get a piece for that. 73 00:09:15.480 --> 00:09:24.630 William Cheng: Right. So let's say you know so I take a look at the example of how on demand agent work right. So in the beginning where we, you know, when we started running a program. Right. So how do we run a program. Right. 74 00:09:25.200 --> 00:09:27.450 William Cheng: You know your your your login show will fork. 75 00:09:27.780 --> 00:09:38.910 William Cheng: Child process and the top I said it will call exact exact we're going to wipe all address space. So we're going to why why but this entire data structure and then we'll go to our executable file and build a brand new address space data structure. 76 00:09:39.840 --> 00:09:47.880 William Cheng: Okay, so this guys want to build this data structure, but we're not going to read any of the data from a distant memory because we're gonna bring them in, you know, on demand. Yeah. 77 00:09:48.960 --> 00:09:53.760 William Cheng: So in this case, we're going to put our address space representation over here. What do we do what we're going to do with the pace table. 78 00:09:54.210 --> 00:10:03.000 William Cheng: Okay, so on. So there's Peace Officers Association, the page they bought the page table was simply going to zero out the entire page table. Okay. So the picture that I draw here is the standard to 79 00:10:03.480 --> 00:10:08.790 William Cheng: The basic to level paste a ball but again since we've got over the hardware. There are many, many implementations of it. 80 00:10:09.300 --> 00:10:13.770 William Cheng: But again, the abstraction that we brought over here is simply a two level page table that 81 00:10:14.370 --> 00:10:24.900 William Cheng: Alright, so, so what we'll do with the patient was going to zero at all. I will be zero. That means that every page table entry has v equal to zero, right. So this bit of yours. V. Right. So that means every entry will be as invalid. 82 00:10:25.980 --> 00:10:33.000 William Cheng: Okay. So whenever you try to perform address translation is invalid. Are you going to travel to the offices that and the operating system will have to fix this particular page a big 83 00:10:33.420 --> 00:10:42.360 William Cheng: OK. So again, you know, the sort of take a look and see what see what see what's going to happen there. Alright, so when you start running your program. What is the first thing that's going to do 84 00:10:43.170 --> 00:10:50.520 William Cheng: That. So again, we, we, for coffee child processes inside job. So we're going to call exactly when you finish doing exact when you return to the to the user space. 85 00:10:50.760 --> 00:10:55.800 William Cheng: The first thing that you will do is that you will execute the startup function right the star function is a function that called me. 86 00:10:56.130 --> 00:10:59.730 William Cheng: Okay, so, so, so the first thing that you do is is going to be an instruction fetch. 87 00:11:00.000 --> 00:11:09.570 William Cheng: Okay, so yeah, he's going to point to somebody inside of tech segment right here. Okay. And then at that point. We're gonna perform either sensation address translation says, Be equal to zero. So, boom, you're gonna trapped inside a carnal. 88 00:11:10.560 --> 00:11:15.420 William Cheng: Okay. So, therefore, the first thing that's going to happen to your user space for a while, when you start running here is that you're gonna 89 00:11:15.900 --> 00:11:19.260 William Cheng: You're gonna get a page Hall, okay, because we're doing on demand agent there. 90 00:11:19.530 --> 00:11:26.220 William Cheng: So in this case, we're trying to kind of live equal to zero, you know, as the first instruction execute over here again over here is the IP. 91 00:11:26.430 --> 00:11:32.490 William Cheng: We perform address translation and then you know the equal to zero, right. So, therefore, we're going to tap into the operating system. 92 00:11:32.940 --> 00:11:37.920 William Cheng: So in this case we don't know what the physical page number is. So what should we do, we ask the buddy system for page. 93 00:11:38.490 --> 00:11:47.190 William Cheng: As well as the buddy system for four kilobytes page over here, right, if the buddy system says, I'm all out, then you have to do the replacement policy, but let's assume that we're going to come come to that later. 94 00:11:47.310 --> 00:11:56.640 William Cheng: So right now we're gonna say the buddy system. So here's four kilobytes page for your old data there. And then what you would need to do is I'll be here that you need to figure out where to transfer data from the disk into memory. 95 00:11:57.450 --> 00:12:04.410 William Cheng: Okay, so I get a pays for it right this this address over here. I'm going to take that virtual address walk down the list of as region over here. 96 00:12:05.100 --> 00:12:16.290 William Cheng: That says click this up a little bit. Right. I'm going to start with a virtual address over here. I'm going to use this virtual I've asked to do a search on this link list to find out which virtual you know which memory segment. Does it belong to 97 00:12:16.710 --> 00:12:19.800 William Cheng: Okay. And in this case, it's pretty simple. I this is you're trying to execute 98 00:12:20.250 --> 00:12:28.860 William Cheng: You try to execute code. So, therefore, this virtual address is going to belong to the to the tech side, man. Right. This is a tech segment. This is data plus BSS. This is the heat. 99 00:12:29.400 --> 00:12:37.680 William Cheng: This is the map right and this is the stack. So therefore, we're going to find this virtual address inside the texts that man over here. And then what do we do 100 00:12:37.980 --> 00:12:47.700 William Cheng: Right, we're gonna find out which page, it belongs to. And then, you know, since the data is sitting on inside of our system hierarchy. So we're going to ask the fall object to bring the data from the test into memory. 101 00:12:48.300 --> 00:12:58.920 William Cheng: Okay, so therefore we're going to follow this point over here. Go to the file object to say, Dear file object. Could you bring you know one of these pages from the desk into memory into memory for only four kilobytes of that. 102 00:12:59.400 --> 00:13:05.010 William Cheng: Okay, so what it will do is that it was so it gets next picture over here. Oops. Over here. 103 00:13:05.970 --> 00:13:13.110 William Cheng: Now, so, so, so in that case the fall object will use a device driver and the device. I will copy four kilobytes of data from this into memory. 104 00:13:13.830 --> 00:13:19.680 William Cheng: Okay, I'll Colonel says going to go to sleep. And when the transfer is complete instead interrupt service routine is going to wake up our kernels. 105 00:13:20.070 --> 00:13:25.740 William Cheng: And now our current or red nose or four, there's four kilobytes of data has been transferred, you know, into this four kilobytes page. 106 00:13:25.950 --> 00:13:37.200 William Cheng: So what does that have to do right and needs to fix the page table over here. So, v equals two, one and and this is a tech segment, we need to set it to read only. And the physical page down Bernie to point to this particular page. 107 00:13:38.460 --> 00:13:44.310 William Cheng: Okay, so he needs to fix all these kind of style. But everything is done over here. What do we do, is that it will return back into the user space program. 108 00:13:44.490 --> 00:13:51.870 William Cheng: And again, when you return back into the user space, since you come into the kernel with the paste all now, it returns what that means that the user circle. I will try it again. 109 00:13:52.980 --> 00:14:00.780 William Cheng: Okay, so what do you do with your tribalist memory reference again and this time address translation will be successful because now be equal to one, and this one is read only so far. 110 00:14:01.710 --> 00:14:13.830 William Cheng: Apart from physical page. So in this case, what you would need to do is it will actually provider translation and it will fetch that instruction from this four kilobyte page over here into the CPU and then he was not executing it was an accident. 111 00:14:16.080 --> 00:14:17.520 William Cheng: That. So again, what it would do is I 112 00:14:18.600 --> 00:14:26.880 William Cheng: Actually get that instruction and then it affects the next instruction that next instruction. So again, on the average, you will be able to execute 100 instruction before you go to the next page. 113 00:14:27.120 --> 00:14:36.030 William Cheng: Okay, but of course you know this instruction over here might not be at the beginning of the page. So after a few instruction. Maybe you're going to get another page fall and paste off. So every time we get a pace, while 114 00:14:36.360 --> 00:14:42.030 William Cheng: You're typing the operating system and the opposite is going to go through that procedure and then copy that did that data for cannabis data. 115 00:14:42.180 --> 00:14:48.120 William Cheng: Scientists into memory and then face the page table and then it will go back into the user space and or try again and December will work. 116 00:14:48.990 --> 00:14:54.900 William Cheng: Okay, so basically we're going to bring in the tech segment on a disk into memory four kilobytes at a time and this is 117 00:14:55.170 --> 00:15:08.400 William Cheng: And this is called on demand agent. Okay, we only needed when we absolutely have to. And when do we absolutely have to. Well, we need to execute that instruction and in this example you know address translation fail. And then we're going to read the data from distant memory. 118 00:15:09.420 --> 00:15:15.810 William Cheng: Man. Alright, so this is done, you know, for, for the tech segment. So, and then what what is what. What is the problem going to do right 119 00:15:16.260 --> 00:15:20.730 William Cheng: So since you started function which you will need to do is that you need to get ready to call the main function. 120 00:15:20.940 --> 00:15:31.950 William Cheng: Well, you need to call the main function. What you need to do that, you need to push the return address onto the stack. Maybe the, you know, the main function has argument. You also need to push the arguments of the sir so therefore you need to go and access the stack segment. 121 00:15:33.030 --> 00:15:42.060 William Cheng: Okay, so, so the first thing that you need to do is the first thing that happened you know we want your program is that you're gonna get a page on the tech side, man. And then you're going to get a page for 122 00:15:43.170 --> 00:15:45.300 William Cheng: Page one inside your stack segment. Yeah. 123 00:15:46.770 --> 00:15:56.460 William Cheng: Alright, so when you try to access a stuck over here, again, in this case we will go to the bottom of the page table and clearly D is still equal to zero, we're going to drop into the operating system. Right. So again, 124 00:15:56.850 --> 00:15:59.610 William Cheng: According to our picture over here. Okay. 125 00:16:00.240 --> 00:16:05.790 William Cheng: In this case, the virtual address is going to be referred to a stocker address. So again, we're going to do the linear search over here. 126 00:16:06.000 --> 00:16:19.110 William Cheng: We're going to find the address over here is instead of stacks, like, man, we can see that this memory segment is read and write. And it's also private, that means that we need to set up for a copy on write then that so here 127 00:16:20.820 --> 00:16:28.200 William Cheng: Instead of pointing to a file object now will have an anonymous object. So what the anonymous up there will be able to do is that you will actually create a four kilobytes for you. 128 00:16:28.590 --> 00:16:33.930 William Cheng: Okay, so how does it clear for four kilobytes of memory for you it cause the buddy system say hey buddies. Then give me four kilobytes. 129 00:16:34.110 --> 00:16:42.120 William Cheng: And now what do we do that on a system like Linux and we next, what do we do, is that it you know there's four kilobytes pages over here. It's going to fill it with zero 130 00:16:43.830 --> 00:16:53.730 William Cheng: Okay, so not all operating system like that, as I mentioned before us, you know, there's obviously some posts hilarious there too. Typically either used by Wall Street. They're typically consider, you know, very high performance. 131 00:16:54.180 --> 00:16:59.820 William Cheng: So what happened is that, you know, in order for them to actually have to have good speed. They actually refuse to fill this page with zero 132 00:17:00.780 --> 00:17:10.950 William Cheng: Okay. Is that okay, I mean, you know, again, this is sort of a, you know, design decision that's made by the operating system for Linux and Linux. They will fill the page was zero for the Solaris operating system. They will not do that. 133 00:17:11.310 --> 00:17:16.650 William Cheng: OK. So again, you need to read the man page is very, very carefully, you know, the safest thing to do is that, you know, 134 00:17:17.040 --> 00:17:24.990 William Cheng: When you try to write your program. You're always assume that your, your, your, your, your, your stack segment is on initialize right and this way, your code is always work. 135 00:17:25.620 --> 00:17:32.580 William Cheng: Okay. All right, so, so, so again this anonymous object is going to be the one that created these four kilobytes pages that you can use. 136 00:17:33.000 --> 00:17:36.150 William Cheng: So yeah, what it will do is that once you have the four kilobyte pages over here. 137 00:17:36.510 --> 00:17:45.960 William Cheng: So let's go through the picture. So the so so the anonymous object over here will ask the buddy system for four kilobytes page. So this page looks just like the other page. Right. Well, yes, but it's it's a four page. 138 00:17:46.140 --> 00:17:53.310 William Cheng: You just create a four kilobytes page, it could be anywhere in physical memory because it doesn't matter, right, the placement ba ba policy doesn't doesn't really matter. 139 00:17:53.790 --> 00:17:58.560 William Cheng: So what we need to do over here is that, so in this case it will be done very quickly because we don't have to go to the desk. 140 00:17:59.400 --> 00:18:02.100 William Cheng: Okay, so this page Hall is very different from the first page. 141 00:18:02.490 --> 00:18:09.360 William Cheng: The first page is very, very slow because we have to go to the desk. This one we asked anonymous object to create four kilobytes page you will just 142 00:18:09.660 --> 00:18:18.300 William Cheng: Ask the buddy system for it. And then what it will do is that will Linux and we need it was zero in it out and then the rest is the same. We're going to be equal to one. And then in this case the 143 00:18:18.720 --> 00:18:30.450 William Cheng: The production is going to be rewrite. All right. Well, I actually, I mentioned before that. Yeah. So. So when we talk about segmentation, that if you want to implement copy on write, we need to we need to write this as read only. 144 00:18:31.650 --> 00:18:39.300 William Cheng: And then we have the physical page number you know ponder this particular page over here. And then what we do is that when we're done, we need to return back into the user space program. 145 00:18:39.690 --> 00:18:42.450 William Cheng: Okay, but when we returned back in your user space program, what's gonna happen. 146 00:18:43.170 --> 00:18:55.890 William Cheng: Right, so, so the you know the user space is going to repeat that operation and that operation is the push data onto the stack now writing it onto the site is incompatible with it was read only. And now you're gonna actually you're gonna get another page. 147 00:18:56.610 --> 00:19:05.490 William Cheng: Okay, so when you come inside a page called over here, you realize that you know that we're that we're doing copy on. Right. So in this case, what you would do is I need to allocate another four kilobyte 148 00:19:07.920 --> 00:19:13.650 William Cheng: Another four kilobytes of memory from the buddy system and there you need to copy it over here because you're performing copy on write 149 00:19:14.610 --> 00:19:20.160 William Cheng: Okay, so you're going to get a mechanic, a copy of it and then you need to change the point of the point of this one. And now you change the rewrite 150 00:19:20.370 --> 00:19:26.040 William Cheng: And then you go return to the user serviceable on for another time. And now the user story for again. We'll try it one more time. 151 00:19:26.220 --> 00:19:36.480 William Cheng: And this time, where they tried to perform the right right will be compatible with the rewrite and you follow this pointer and then in this case, you're going to push data into the copy and not into the original page that they got created 152 00:19:36.990 --> 00:19:43.020 William Cheng: OK. So again, the reason over here is that we're doing copy on write when you write to it for the first time you got to make a copy and go right into the copy 153 00:19:44.850 --> 00:19:52.530 William Cheng: OK. So again, you know, for the stock segmented behavior over here is very, very different from the the the tech segment. Yeah. 154 00:19:53.250 --> 00:20:02.760 William Cheng: Well, he has a similar things happen for the staff over here, although the stack a little different because we need. Yeah, because he needs a backing store and we have the anonymous object. Who's in charge of the backing store. 155 00:20:03.150 --> 00:20:07.800 William Cheng: So they are all going to see, you know, guess what the end of chapter seven. What does he held back and still can be implemented. 156 00:20:08.220 --> 00:20:15.330 William Cheng: They also need to set it out for copy on write. And that's why we started out with read only. And then we take one more page file to change it to rewrite. Yeah, I mean, 157 00:20:16.170 --> 00:20:26.850 William Cheng: Again, this is sort of a, you know, conceptually, what can happen, but will you try to implement this inside the Colonel, you can actually do a little bit optimization, you can try to combine these to paste all into one but again that's implementation issue. 158 00:20:27.450 --> 00:20:34.350 William Cheng: Okay, conceptually, you should go into the user space and then get a second page fall and at the second page while you do a copy on write, you know, 159 00:20:34.560 --> 00:20:40.200 William Cheng: This guy is going to end up with two pages. So some people again think that's a little wasteful, you can actually optimize it a little bit. Yeah. 160 00:20:40.920 --> 00:20:44.790 William Cheng: But anyways, I just wanted to point out that these two pages are very, very different. 161 00:20:45.120 --> 00:20:52.380 William Cheng: And then what you would do is that he will start, you know, running a program you're going to go to me means don't do something. So if your programs. The hello world program. 162 00:20:52.620 --> 00:20:58.800 William Cheng: The main function is going to print the daily onto the screen hello world or something like that. So in that case, Robin is that 163 00:20:59.280 --> 00:21:05.520 William Cheng: When you make a system call well or even the case where the main doesn't do anything. So what it will do is it will actually make the exorcism call 164 00:21:05.850 --> 00:21:14.700 William Cheng: Well, I should exercise trip a bad example is your main pull out is bound to make some system call when it finished making the system call me to set a global variable called Aaron number right 165 00:21:15.810 --> 00:21:24.990 William Cheng: Okay, whenever you finish calling a system called you need to set the air number the air number is going to be a global variable. So the air number is going to be sitting inside the data center and the PSS segment. 166 00:21:25.440 --> 00:21:36.030 William Cheng: Okay, it's going to sit in UI is that one of them are chances are it's are initialized, so therefore it's going to sit inside of VSS admin over here. So naturally, when we try to access the PSS like we know we're, again, we're gonna get a pitfall. 167 00:21:36.900 --> 00:21:44.070 William Cheng: Okay, so, so the next one over here. We're going to get a pays for your inside of data segment does so in this case at the data side one over here. 168 00:21:44.820 --> 00:21:51.750 William Cheng: You know, so, so, yeah. Initially, you know, equal to zero you trappings out of Colonel you walked on that link list over here. So again, let's go back to the limitless. 169 00:21:54.930 --> 00:21:58.140 William Cheng: Okay, let's go. Oops. 170 00:22:01.050 --> 00:22:09.900 William Cheng: That you want on that list it lingers. And again, using the virtual address you find out the truth is virtual address belong to this one is a data plus BSS segment over here. 171 00:22:10.530 --> 00:22:16.680 William Cheng: Okay, so you will notice that this one is rewriting is private, and you're doing copy on write. But originally, we don't have any data. 172 00:22:17.550 --> 00:22:27.510 William Cheng: Okay, so, so, so, so, so, so, so right now. Robin is that even though you're trying to set the ERA number. But originally, this point over your point to the fall object because we don't really know what this data look like on the desk. 173 00:22:27.780 --> 00:22:32.580 William Cheng: Okay. So, therefore, the first thing that we need to do over here is that, let me go back to this page again. 174 00:22:35.040 --> 00:22:45.180 William Cheng: Okay, this particular page over here. Originally sitting on this. Well, actually, we need to go to this because this particular page contains the data segment and the BSS segment. So we need to copy them from the data. 175 00:22:45.420 --> 00:22:55.950 William Cheng: From the data into memory. Again, we're going to ask the buddy system for four kilobytes page and then we're going to ask that this device driver to copy data or kernels that fall asleep, and then when the, when 176 00:22:56.670 --> 00:23:06.690 William Cheng: data transfers complete it's going to wake up our kernels and now there's a data over here, but this data is the original data that comes from the desk and this did over here, we are not allowed to modify it because we're doing copy on write 177 00:23:07.380 --> 00:23:10.200 William Cheng: OK. So again, what we need to do over here is that we need to set this one to read 178 00:23:10.680 --> 00:23:12.690 William Cheng: Read Only and then use the fiscal 179 00:23:12.990 --> 00:23:22.950 William Cheng: A page number two point of this page. We're going to go back into the user space program in the user's Facebook, I need to modify air number, it says Error number equals to five or something like that, right. So, yeah. 180 00:23:23.160 --> 00:23:35.040 William Cheng: Right, it is incompatible with read only. So we go translation. We're going to try things out a carnal. Yeah. And now he's had a colonel. We walked on that list of, you know, as region will find out that we're doing copy on write because as memory. 181 00:23:35.790 --> 00:23:41.370 William Cheng: Memory is private. So in that case, we need to ask the buddy system for another four kilobytes of page and then copy the data over it. 182 00:23:41.610 --> 00:23:51.360 William Cheng: Changes point at the point to the new one. And they said, this one to rewrite and now again we returned to the user space program for the second time for the same virtual address page for all that and now 183 00:23:51.690 --> 00:23:59.220 William Cheng: You know, sitting, you know, the air. Number two, five is going to be compatible with the rules we rewrite. So we're going to modify the copy to say air number over here, equal to five. 184 00:24:01.500 --> 00:24:10.110 William Cheng: Okay, so you can you can actually see that these three different pace fall. There are different. Right. The tech segment over here read from the desk and that the stacks. I've been over here. You don't go to the desk. 185 00:24:10.350 --> 00:24:14.370 William Cheng: And the data segment over here you go to the desk and you're also doing copy. All right. 186 00:24:15.690 --> 00:24:21.510 William Cheng: Okay, so when you're doing your current or three. These will be the three types of pays for that you need to handle at the beginning. 187 00:24:21.990 --> 00:24:24.840 William Cheng: Okay. So, okay, you need to understand the difference between that 188 00:24:25.260 --> 00:24:33.180 William Cheng: So that when you had a pitfall you needed to the right decision. Right. So yeah, the your decision is based on the as region. So when you walk on that email list as region. 189 00:24:33.660 --> 00:24:40.200 William Cheng: You need to look at that data structure and then that will tell you know which one a page volunteers and you need to perform the right kind of operation that 190 00:24:42.120 --> 00:24:51.750 William Cheng: All right, so, and also, you know, remember what the, you know, sort of the, the physical pages over here can be shared. Right. So, so in reality there's not just one process. There are many, many processes. 191 00:24:51.990 --> 00:24:59.340 William Cheng: That every process has their own pace table and some of the entries over here. They are sure they pointed us and physical page because they share the same page, right. 192 00:25:00.030 --> 00:25:06.270 William Cheng: Okay, so in reality is much more complicated. So what happened is that when you get a pays for it is possible that this page frame. 193 00:25:07.200 --> 00:25:12.030 William Cheng: Yeah, I'm going to sort of use the word paste from a physical page you know interchangeably. Because, you know, 194 00:25:12.840 --> 00:25:16.950 William Cheng: You know, I was showing that this one is the four kilobytes physical page. But there's a corresponding PageRank. 195 00:25:17.730 --> 00:25:25.950 William Cheng: Okay, so in that case, all these processes. They're sharing the same page, right. So when I tried to look for a particular page frame I shouldn't just ask the buddy system for four kilobytes page. 196 00:25:26.130 --> 00:25:31.710 William Cheng: First I need to see. I need to look at the data structure a page for him and try to see if the patient exists already 197 00:25:32.160 --> 00:25:42.960 William Cheng: If it exists already, then I just have to share it. If it doesn't exist already in that case I need to ask the buddy system for four kilobytes page. And why is the four gigabytes. Hey, has been allocated, I need to stick it inside a page frame. And now, again, I 198 00:25:43.830 --> 00:25:53.100 William Cheng: End up with a new patient object. Okay, so in reality this is a lot more complicated because Patriot my shared among multiple processes for guys are incredible three 199 00:25:53.550 --> 00:26:01.710 William Cheng: You that that's one of the things you have to implement and also you can call three over here. There's a nose as that you need to make sure that every time when you return back into the user space. 200 00:26:01.920 --> 00:26:04.290 William Cheng: All the colonel data structure earning the consistent state. 201 00:26:04.680 --> 00:26:13.050 William Cheng: Okay, I think I mentioned that in quarter one already an incredible three again it's even more important because, you know, it's just a researcher, there's a little bug in that next time when you come into the kernel. 202 00:26:13.530 --> 00:26:17.790 William Cheng: When do you know what are you going to come into the car. The next time. Okay. Nobody knows. 203 00:26:18.510 --> 00:26:24.870 William Cheng: Okay, so you are getting paid out over here and maybe you will end up, you know, giving up the CPU and then some other you know process is going to 204 00:26:25.110 --> 00:26:31.920 William Cheng: Start running. So next time when you come into the kernel, you can come into the kernel using a higher interest or you can come into the kernel because you know 205 00:26:32.970 --> 00:26:39.330 William Cheng: Another process. Make a system call or maybe another process. Get a paste fall. So it's impossible to predict what's going to happen next. 206 00:26:39.510 --> 00:26:45.480 William Cheng: So therefore, when you whenever you leave the Colonel, you got to make sure that the kernel is the Indian a perfectly consistent state. 207 00:26:45.930 --> 00:26:54.450 William Cheng: Okay, because otherwise next time when, you know, some way you come into the kernel, you're going to hit an inconsistent Colonel state and you know case, you're going to cause a kernel panic. 208 00:26:55.770 --> 00:27:01.620 William Cheng: Alright, so, so again, you got to make sure that whenever you're going to leave the Colonel, you're going to make sure that the colonel is a really good state. Yeah. 209 00:27:03.570 --> 00:27:11.880 William Cheng: Alright, so again, let's review our peaceful peaceful is a very important process right process nine to say something, you know, process and said so. So 210 00:27:12.720 --> 00:27:15.000 William Cheng: So get a piece was a very important mechanism. 211 00:27:15.720 --> 00:27:25.680 William Cheng: The one that we have review quite a few times already, is one of the equal to zero. So, again, let's go through the steps again. The first time trap occurs in this case is because the equal to zero. And after translation fail. 212 00:27:26.040 --> 00:27:35.580 William Cheng: And then what you need to do is that you know you in case you need to transfer data from a distant memory. You need to find a free physical page. And the way you do this, you ask the buddy system for it. 213 00:27:35.820 --> 00:27:42.450 William Cheng: And once you have done that you're going to write the page out if there's no physical page right so if the buddy system says, I'm all out. Right, so when 214 00:27:42.810 --> 00:27:53.040 William Cheng: We say, Hey, you know, give me a new page and in return null pointer. Well, in that case, you know that there's no physical memory. So in this case, you have to use your replacement policy to take one of the 215 00:27:53.940 --> 00:28:01.650 William Cheng: One of the patient and and read it out to the back end store. And this way you can actually create a new page, right. Okay, or in this case, you're going to create a free page right now. 216 00:28:02.760 --> 00:28:10.530 William Cheng: So once you're free pace right there. You can actually ask the device driver to copy the data from the data into memory. So this one, then you finally you fetch the page. 217 00:28:10.740 --> 00:28:15.810 William Cheng: by copying the data from this into memory and then you figure out what your current, current data structure and the return on track. 218 00:28:16.650 --> 00:28:25.350 William Cheng: Yeah. So yeah, these are the very important steps. So again, over here, ask the question in step two. How do you find a free fiscal page. The answer is use the buddy system. 219 00:28:25.560 --> 00:28:34.590 William Cheng: In Step three, where and how do you find an EU physical page and write it out to the desk. Okay. So remember, at this point, you know, when you have no physical page left 220 00:28:34.860 --> 00:28:39.960 William Cheng: That means that every physical pages allocated. So that means that every physical pages inside of page frame object. 221 00:28:40.260 --> 00:28:46.020 William Cheng: There. So what you can do is that you can actually search the list of a patient object and decide which one you want to swap out 222 00:28:46.770 --> 00:28:53.730 William Cheng: Okay, so. So the question is, how do you find that he is physical page while you use the pastry data structure and try to sort of give you the information about how to do that. 223 00:28:53.970 --> 00:28:57.600 William Cheng: Okay, and then you write it out to the back end. So how do you know where the backend stories. 224 00:28:57.810 --> 00:29:08.940 William Cheng: Right, you use the current use a virtual memory map. Use the page frame object use all these data structure in order for you to figure out where to write the word where the back of the story is, and then you can write a date out to the bathroom stall. Okay. 225 00:29:11.010 --> 00:29:17.640 William Cheng: Obviously, it's kind of a silly example. So in this case we have 10 physical page patient 10 physical page over here. 226 00:29:18.270 --> 00:29:28.680 William Cheng: That are available for user space program. Okay, we're running three different program here is program number why his problem number two is broken. Number three program. Number one, use for pace ram over here. So, so again, 227 00:29:29.100 --> 00:29:38.850 William Cheng: In this example, they don't share any pace right so program number. Why, you can actually look at the first of all we hear all the one that has the equal to one, or those are the ones that are used by this application. 228 00:29:39.180 --> 00:29:48.060 William Cheng: Okay, so if you follow the pointer as part of your phone to the physical page number. So, this one will point here. There's no point here to apply here so application number one over here is using three pace rain. 229 00:29:48.270 --> 00:29:52.920 William Cheng: Application number to over here is using for page frame right if you follow the point to file for 230 00:29:53.130 --> 00:30:01.020 William Cheng: An application. Number three over here using three page. Right, right. So if you add them all together. They're using taste a 10 page for him now. So now I'm completely out of PageRank. 231 00:30:02.010 --> 00:30:13.620 William Cheng: OK. So again, I'm using the word page Raymond physical page interchangeably lbs as p zero P one what because they are PageRank right he said a page for him object. There's a physical page. So again, one to one correspondence 232 00:30:13.920 --> 00:30:16.650 William Cheng: OK, so now let's say that I want to run a new program. 233 00:30:17.460 --> 00:30:26.220 William Cheng: Okay, so if I want to run program. Number four over here. So I'm going to start with on demand paging and now we perform address translation and then you try to access the the tech segment over here. 234 00:30:26.400 --> 00:30:36.270 William Cheng: The equal to zero. You try things out officer. So this is what condition three become true right when you try to ask the buddy system say hey, give me a new page rank the buddy system says, I'm all out. 235 00:30:37.350 --> 00:30:39.120 William Cheng: There so I'll be here at the basis is 236 00:30:39.420 --> 00:30:45.330 William Cheng: Mr. So and so, so what do you need to do is that you need to find one of the 10 patient will be here to write it into the back end store. 237 00:30:45.510 --> 00:30:50.160 William Cheng: Okay. So, by looking at this picture, how do you decide which pays for into right into the back end store. 238 00:30:50.370 --> 00:30:59.430 William Cheng: Okay, so this is going to be related to your replacement policy, right, because now we've run out of resource and the resources, the physical page, we need to you need to pick out one of them. And right at the desk. 239 00:30:59.850 --> 00:31:06.210 William Cheng: Okay, according to this theater surgery, there's two ways to do I want is that you can scan this, you know, by looking at the patient data structure. 240 00:31:06.390 --> 00:31:16.800 William Cheng: And try to sort of figure out which one's the right to the desk. Another way that you can actually look at the address space over here. So pick a process over here and then you follow the pointer over here and find out which one is the one that you want to write it to the back end store. 241 00:31:18.150 --> 00:31:25.110 William Cheng: Okay, so they're not all going to talk about, you know, the kind of algorithm that you will use for now. So let's just assume that somehow you find out that patient. Number five. 242 00:31:25.320 --> 00:31:27.240 William Cheng: Is the one that you got to write to the back end so up 243 00:31:27.480 --> 00:31:32.700 William Cheng: Okay, so therefore we can say that while we pick out this entry is over here and we find out that this is the best candidate. 244 00:31:32.850 --> 00:31:41.340 William Cheng: To read into the back of the store. So this guy is what allowed to do right so so again we cannot continue with allocation of patient movie or we're going to suspend the buddy system to say, hey, 245 00:31:41.550 --> 00:31:46.410 William Cheng: You know, now you're out of space over here, so wait for you know wait for a few milliseconds. And then we can try again. 246 00:31:47.100 --> 00:31:53.160 William Cheng: Okay, so what I need to do is I need to actually write this down to the back end storage, right. So the back is over here on this. I need right out to the desk. 247 00:31:53.850 --> 00:32:03.600 William Cheng: That so so again the terminology over here is that I'm going to swap this page from, you know, from the pace ran into the back end store. Okay, so, so, you know, typically this one going to the swap says 248 00:32:04.110 --> 00:32:10.980 William Cheng: So I'm going to start this physical page out into its back in store over here. It says, you know, right, to this. If only the patient is dirty. 249 00:32:12.150 --> 00:32:15.870 William Cheng: Okay, so remember the pace several inches over here. There's actually a 250 00:32:16.710 --> 00:32:28.170 William Cheng: Modified bit right here. Right. So if the modified field over here equals 211 means that this particular patient is dirty and what does it mean to be dirty. That means that this data over here is not the same as the data in the back end store. 251 00:32:28.680 --> 00:32:40.770 William Cheng: Okay, so you know case, we need to write it out to this because otherwise you know what's on this will be the wrong data that what if the modified below equal to zero. Okay. The bottom of the over equals zero, that means that this patient is identical to see what is 252 00:32:42.330 --> 00:32:51.360 William Cheng: This entire four kilobyte page is identical to what's on the desk right so in that case we don't have the right to this. Again, this is optimization track so we don't have to wait for this. 253 00:32:51.900 --> 00:32:52.680 William Cheng: To go on to the desk. 254 00:32:53.280 --> 00:32:55.050 William Cheng: Okay, so if that you know 255 00:32:55.290 --> 00:33:05.190 William Cheng: If the pace room is dirty. That means that the modified been over here is equal to one, that means that the patient data is not the same as sitting on the desk. Why not case, we actually have to write down to the desk. Our current 256 00:33:05.400 --> 00:33:15.540 William Cheng: Needs to go to sleep. Wait for this operations to be done and then eventually when the operation is done that, then that means that this data has been copied on to this and now this become a free PageRank. 257 00:33:16.560 --> 00:33:24.090 William Cheng: Okay. So, therefore, what do we need to do right so now we need to continue with the on demand page. And we asked the buddy system for new page right and now he says, here it is. Here's a new page rank. 258 00:33:24.420 --> 00:33:33.270 William Cheng: Okay, so what we need to do is I need to continue with what we did before. We're going to continue to copy the data from this into memory. Again, our current author is going to fall asleep or the second time. 259 00:33:33.480 --> 00:33:41.100 William Cheng: And this time we're going to copy the data from that this into memory and then we need to way way way eventually what is going to wake up our Colonel so that again. And now we know that the data. 260 00:33:41.460 --> 00:33:45.720 William Cheng: The data over here has been replaced by the new data that's required by process. Number four. 261 00:33:45.930 --> 00:33:53.670 William Cheng: And then we things I want to Colonel do to start your said equals two, one point to this page over here and then we'll return to the user space program and then it will try again, not just have to work. 262 00:33:56.040 --> 00:34:05.460 William Cheng: OK. So again, you know, this is sort of a demonstration of how you can actually run out of pace frame and then in this case, how do you write it into the back of the store and that you can continue with your pace Hall. Yeah. 263 00:34:07.620 --> 00:34:15.840 William Cheng: Alright, so, so, so, so, so you can also see that there's actually a really bad, right, because, because we need to write out what we are in the worst case, we need to write a data out to the desk. 264 00:34:16.020 --> 00:34:22.350 William Cheng: And that will cause you on the order of 10 milliseconds. Right. And then we bring the data back, you know, we hear you going to wait for another 10 milliseconds. 265 00:34:22.500 --> 00:34:33.870 William Cheng: Right. So this means that when you start running your program at the first page is going to work. We cannot wait 20 millisecond on the next page. Are you gonna wait another 20 minutes ago. So if the physical memory over here is completely used up. 266 00:34:34.650 --> 00:34:38.610 William Cheng: Okay, every time we get a paintball, you're going to cost you 20 milliseconds. So that will be really, really bad. 267 00:34:39.390 --> 00:34:47.880 William Cheng: Okay, so the question is, how do you can actually speed this up. Okay. So step three over here is going to give you a big problem over here is that you're going to end up with a big performance here. 268 00:34:48.480 --> 00:34:59.340 William Cheng: So they are you know so so so what what we need to do that. We don't want to wait for the desk. Because if this is too slow there. So we need to read reduces latency of waiting for the desk there. So there are two different approaches. 269 00:34:59.820 --> 00:35:04.590 William Cheng: One is called pre fetching there's a Prefecture is basically use the file system cache. 270 00:35:04.890 --> 00:35:15.390 William Cheng: Because a lot of it is that we try to go to the days to get data right so let's take another profession first. So before, whenever we needed to get to the day they get the data from this this into memory. We always copy four kilobytes. 271 00:35:15.990 --> 00:35:18.240 William Cheng: Okay, why don't we copy more than four kilobytes. 272 00:35:18.870 --> 00:35:26.430 William Cheng: Okay, because this is very slow device. So when we try to get data from a distant memory. Why do we actually copy edition or sector summit this into memory. 273 00:35:26.760 --> 00:35:37.350 William Cheng: So this is called pre fetching right we're going to be a little optimistic. We're going to say that, well, you know, typically the way we access memory is sequential. So once we start accessing these memory. Maybe you want us to access, we'll 274 00:35:38.130 --> 00:35:49.320 William Cheng: start accessing the next few pages, you're pretty soon. So therefore, why don't we bring them in from a distant memory as early as possible and they get the earliest possible is when you bring in the first page. Why don't you bring through more pages. 275 00:35:50.250 --> 00:35:53.850 William Cheng: Okay, so this one. Will you go to the other pages over here, you're going to get a 276 00:35:56.940 --> 00:36:09.000 William Cheng: So in this case, what we want to sort of keep that and instead of file system cache. So, so, so, so, so, so next time when we should actually these pages, you're going to perform a look up inside of our system cache and they're already there. So, therefore, you don't have to go to the desk. 277 00:36:10.080 --> 00:36:16.230 William Cheng: Okay, so again, this was speed things up. Because, because, you know, because you don't have to go to the dentist that offer, but what is the downside. 278 00:36:17.220 --> 00:36:21.720 William Cheng: The downside over here is that you're actually you're using physical memory four times faster, right. 279 00:36:22.140 --> 00:36:32.970 William Cheng: Okay, every time when you bring in a page and I bring in three more pages over here. So, so, so, so, so, so you know that the bad condition is going to happen a lot earlier once a month, once we start, you know, doing pre fetching very aggressively. 280 00:36:34.050 --> 00:36:36.720 William Cheng: Okay, so even though this might help, but it also can make the problem. 281 00:36:37.740 --> 00:36:38.580 William Cheng: Worse than 282 00:36:39.990 --> 00:36:45.960 William Cheng: Another approach over here is to to to to to to to to to be a little more aggressive 283 00:36:46.710 --> 00:36:57.060 William Cheng: So glad to be here. So, you know, the problem of the the problem that we have with the spirit approach is that step three over here. Why do we do it through stuff through at this time. 284 00:36:57.900 --> 00:37:05.670 William Cheng: Well, when we get a paste. All right, we find a free page RAM and the buddy system says I'm. Oh, and now we actually we're gonna write a page of the backend store. 285 00:37:06.630 --> 00:37:18.720 William Cheng: Okay, so when we're implementing over here is actually lazy evaluation we write this page out onto the desk as late as possible. As it turns out, lazy evaluation over here works really poorly. Okay. It works really well as a 286 00:37:19.920 --> 00:37:23.760 William Cheng: As a French policy, but it works really poorly as a replacement policy. 287 00:37:24.330 --> 00:37:31.350 William Cheng: Okay, so this one is doing at the last minute you're going to search the data start to find out which one to write it up and then you read it out at the very last minute when you complete run out of memory. 288 00:37:31.800 --> 00:37:41.670 William Cheng: Okay, so, so, so in this guy's water. We need to do the opposite the opposite of lazy evaluation. Some people call it the eager evaluation, something we had to do it as early as possible. 289 00:37:42.180 --> 00:37:49.770 William Cheng: Okay, so. So in this case, you know, three an on demand is not a good idea over here. So we should perform, you know, this operation or writing the you know the 290 00:37:50.430 --> 00:38:03.930 William Cheng: writing all these these patients on to the desk as early as possible and as aggressive as possible. Right. So the reason we sort of want to do, guys, I want to make sure that the buddy system. Whenever you ask the page on the buddy system devices and one never say I'm all out. 291 00:38:05.640 --> 00:38:13.170 William Cheng: OK. So again, the goal over here is that whenever you try to talk to the buddy system. It's like, give me a new page the buddy system will always say I played your pages I just give you one. 292 00:38:14.460 --> 00:38:18.420 William Cheng: Okay, so the question is, how do you actually do this right. Okay, so you should perform or, you know, 293 00:38:18.900 --> 00:38:30.630 William Cheng: So you should write the data out into their backing store as a separate concurrent activity. We're going to use the thread inside the kernel and this thread is known as the page. I'll demon right you already see page on demand. Colonel one 294 00:38:30.870 --> 00:38:38.130 William Cheng: The Paleo demon Carla doesn't really do anything. I mean, I've been reading since the toy optimism, you only wake up the page, I'll do that when you're completely run out of memory. 295 00:38:38.460 --> 00:38:44.010 William Cheng: Yeah, so I think, for we Nick's, you know, even if you get all the way to Colonel three, you should never run out of memory. 296 00:38:44.790 --> 00:38:50.160 William Cheng: Now guys ever. In that case, you know, the pod and basically never the beta demo never get woken up there. 297 00:38:50.670 --> 00:39:03.990 William Cheng: So the idea here is that he the page of human needs to be run run simultaneously and conquer and concurrently, together with all the activities that the colonel to continuously and aggressively look for free patron and write them out to the desk. 298 00:39:04.710 --> 00:39:13.740 William Cheng: Okay, so what do we do, is that it says what do we do that, it will go to that list of pace frames over here. And then what do we do so aggressively write them out to the desk and this way, they will become free patrons. 299 00:39:14.520 --> 00:39:24.840 William Cheng: That. So the pictures over like this that so my pace range. I'm going to divide them into two different categories. One is a free pace ram over here. So this one is maintained by the buddy system. Right. But he says, I'm over here. 300 00:39:25.470 --> 00:39:33.090 William Cheng: Buddy System over here is maintained a three page over here. So even though, you know, I saw them as array, but the basic idea of yours is gonna be a tree data structure. 301 00:39:33.360 --> 00:39:35.520 William Cheng: As many buddy buddy system over here and then 302 00:39:35.820 --> 00:39:40.980 William Cheng: The other ones that are allocated whenever you ask the buddy system say give me a free page and then it will return a free page over here. 303 00:39:41.160 --> 00:39:52.830 William Cheng: You put a pace frame data circle around it. And now you can maintain your data or just to keep to keep track of all the patients that so that the infrastructure. We're going to call them in US page rank or so in this case, don't look like a list. 304 00:39:53.250 --> 00:39:55.380 William Cheng: We're going to put on patient in a long, you know, 305 00:39:55.830 --> 00:40:06.060 William Cheng: A list of pastry. I'm over here. So, the job of the page or demon over here is this is going to go to this list. Look for patient I'm over here and execute a replacement policy to determine which one you want to write it to the desk. 306 00:40:06.990 --> 00:40:16.590 William Cheng: That. So again, the discovery is a very, very slow device, right. So, therefore, over here, there's a dis que what you need to do is that you need to pick up the pace. Right. And then right enough to the days over here one page at a time. 307 00:40:17.430 --> 00:40:27.000 William Cheng: Okay, so then what you should do is that you should check whether the dirty or whether this particular page room is dirty or not. If it turns out it's not dirty one in this case, what you can do that. You can simply return them to the buddy system. 308 00:40:28.110 --> 00:40:34.140 William Cheng: Okay, if it turns out the patient is dirty, you need to add it to this IO Q over here to have the patient written, other than 309 00:40:34.590 --> 00:40:42.720 William Cheng: This one of the time when they are done, you can return them back to the buddy system. So this way, the buddy system will always have lots of always have lots of patriots. 310 00:40:44.010 --> 00:40:52.560 William Cheng: Okay all right over here says, you know, the pace ran it are used to keep track of all these physical pages, we can actually use a multiple page. I'll demon it. This is not aggressive enough 311 00:40:53.670 --> 00:41:02.400 William Cheng: Okay, so, so, I mean, so what's important. Over here is that the buddy system should never our page, man. So if this is not fast enough. You need to try your best to actually make sure that you know so again. 312 00:41:02.880 --> 00:41:14.460 William Cheng: You know what, you know what, what, why would this be maybe not enough, right, because writing to this is a very slow operation. Okay, so if you are using memory at a very high speed when this case you need to be a little more aggressive. Yeah. 313 00:41:16.320 --> 00:41:26.430 William Cheng: Alright, so let's talk about the repulsive policy, you know, which play, you know, so, so, so, you know, what will you scan this page for I'm over here. Which one do you write it to the backing store. 314 00:41:27.390 --> 00:41:30.750 William Cheng: Okay, so that's the replacement policy. I'm not sort of make an analogy over here. 315 00:41:31.230 --> 00:41:37.500 William Cheng: To say that, you know, what if you have a rack of DVDs right here's a DVD rack or I'll be here DVD. Here's a bunch of DVD over here. 316 00:41:37.740 --> 00:41:42.270 William Cheng: I mean, I know today you all listen to digital music. There is no DVD and more. So let's say you're visiting your parents. 317 00:41:42.540 --> 00:41:47.670 William Cheng: Or your parents has a DVD rack. Right. So how would they actually are performed replacement policy. 318 00:41:48.030 --> 00:41:55.260 William Cheng: Okay, so for example the DVD Rocco here is completely for you went to visit your parent you bought you bought a new movie. And then what you need to do that, you need to put your 319 00:41:55.740 --> 00:42:02.130 William Cheng: New DVD into the DVD rack and the DVD writers completely four. So, which one would you take out an SMS to the basement. 320 00:42:03.210 --> 00:42:09.720 William Cheng: That would you use the first thing first, I'll replace them policy, right. So, so what, what, what would that look like if you look at first in first out 321 00:42:09.930 --> 00:42:12.660 William Cheng: You will soar this list of DVD according to 322 00:42:12.900 --> 00:42:17.520 William Cheng: The purchase date right so this will be the oldest one right and then this will be the newest one over here. 323 00:42:17.610 --> 00:42:25.530 William Cheng: So basically, you know, every DVD has a receipt attached to it and you sort them based on a timestamp on the receipt. So now what you would do that you will take the oldest one over here and send it to the basement. 324 00:42:25.980 --> 00:42:34.410 William Cheng: Will you do that right. As soon as you do that, you can say, Hey, that's my favorite movie. How could you do her so clearly, you know, final policies are really, really bad idea. Okay. 325 00:42:34.740 --> 00:42:38.730 William Cheng: One of the other policy over here, right, what are the policy over here is called the least frequently use 326 00:42:39.450 --> 00:42:45.060 William Cheng: That. So maybe one thing I can do is I can actually, you know, for every DVD over here. Every time I watch it. I'm going to put an X on it. 327 00:42:45.420 --> 00:42:53.070 William Cheng: Okay, so I can actually count the number of marks and I will sort them based on the number of marks over here. So, in that case on one side over here. I'm going to have 328 00:42:53.310 --> 00:43:09.240 William Cheng: The most often, I'll get the most often watch TV DVD over here. Okay. On the left hand side over here is going to be the least often watch TV over here, right. So maybe this one I watched 100 times this one actually 95 times and that 5060 you know 37 that are the three, two and one. 329 00:43:10.440 --> 00:43:19.650 William Cheng: Okay, so in this case what I would do is I you know if I really like is for which one would you put you put in today's man. I'm going to take the lease often watch DVDs and putting into the basement, would you run this policy. 330 00:43:20.550 --> 00:43:29.970 William Cheng: Well, kind of. We're kind of do that. Right. You know, the one that was that our least favorite we tend to send it to the basement. Okay, so if you want to implement this for pastry and how do you implement that. 331 00:43:30.540 --> 00:43:36.270 William Cheng: You guys are gonna paste right we have many patient will have millions of patients. So it's a really, really long list over here. Okay. 332 00:43:36.870 --> 00:43:40.620 William Cheng: It actually will be costing you a lot of work every time, will you access a particular 333 00:43:40.950 --> 00:43:52.200 William Cheng: Patient you need to shuffle among the state of structure to make sure that the list is sorta so even though this might be a reasonable policy for a DVD rack, but he's not a criticism. This will be the wrong policy because the data structure is too big. 334 00:43:52.860 --> 00:44:01.380 William Cheng: Okay, so it's gonna cost you too much to maintain this the sleeveless data structure. So even though they might be reasonable in real life you know he's our operating system in lab you're going to go 335 00:44:02.250 --> 00:44:10.380 William Cheng: Then the next one over here is called the least recently use policy. Right. So again, this case, I can, I can sort them based on the last time I watch this video. 336 00:44:11.250 --> 00:44:18.210 William Cheng: Okay. So in this case, what would I, how would I implement the policy right. One thing I can do is that every time you know or your parents could do is that every time they watch a DVD. 337 00:44:18.450 --> 00:44:22.290 William Cheng: What they will do when I finished watching it. They always put it at the end of the DVD rack. 338 00:44:23.070 --> 00:44:29.520 William Cheng: Okay, so next time they will watch another video, they'll put it under the rug. And then there was another year again and then there was this one again. I put it in a DVD. 339 00:44:30.030 --> 00:44:40.560 William Cheng: Pretty soon this DVD that will be self sorta according to the time that you watch the video. Okay. So, therefore, on the right hand side over here will be the least recently watched video 340 00:44:41.670 --> 00:44:48.660 William Cheng: On the on the left hand side will be the most recently watched video, right, because the long the left over here will be the one that you just watched. 341 00:44:49.410 --> 00:44:58.680 William Cheng: Okay, so this guy, this won't be a self sorting data structure right if you implement this algorithm correctly, it will be a self sorting data structure, after a while, on the right hand side is going to be the least recently watched 342 00:44:58.890 --> 00:45:04.740 William Cheng: On 11 says me and most recently watched and in this case, when you when you bring the new DVD, which one would you send it to the basement. 343 00:45:04.980 --> 00:45:14.670 William Cheng: You're going to send the one on the right hand side over here because this is the one that you haven't watched it for the longest time, right. So, most likely, you know, most likely, you don't really like that movie. So, therefore, this will be 344 00:45:15.030 --> 00:45:20.880 William Cheng: This will be good to center basement. Okay, do you implement something like that. I mean, that's just sounds reasonable. Yeah. 345 00:45:21.360 --> 00:45:28.650 William Cheng: So actually, if you have taken CSI 70, you will see that under certain conditions are they actually talk about the least recently us you know our them. 346 00:45:29.010 --> 00:45:36.810 William Cheng: They actually have approved to say that understood and kind of, you know, condition. This is actually sort of the optimal DDD off my algorithm to use 347 00:45:37.230 --> 00:45:49.410 William Cheng: OK, so our offices and doesn't really satisfy that kind of condition. But if it's if it turns out it works pretty well. Understanding kind of condition we shouldn't need to investigate to see if this is actually work work work reasonably that would be well inside the operating system. 348 00:45:50.670 --> 00:46:01.620 William Cheng: So as it turns out, a lot of criticism or they will use the least recently use up the replacement part the replacement. I'll wear them up because as it turns out, most of the time they actually works pretty well. Yeah. 349 00:46:03.120 --> 00:46:13.740 William Cheng: Alright, so how do you actually implement that. Right. So the idea here is that you're going to look at the reference bit over here if our is equal to one, that means that it's recently used right if are equal to zero here. That means that it's not recently us 350 00:46:14.640 --> 00:46:20.340 William Cheng: Okay, so they don't always talk about exactly how to implement this one at the beginning of the next lecture. So right now we just sort of 351 00:46:21.180 --> 00:46:28.170 William Cheng: You know, sort of hone in on this particular bit. So this page table entries over here is going to tell us that whether this know we mentioned before, you can look at the modified that 352 00:46:28.620 --> 00:46:33.810 William Cheng: Where you need to write things out. You also need to look at the reference page to see if there isn't a user not there. 353 00:46:34.710 --> 00:46:40.710 William Cheng: So, so, by the way, why would that you know one, you know, one page over here, there are more recently used. And then the other page. 354 00:46:41.520 --> 00:46:51.810 William Cheng: Now, so if you're looking at a typical program over here, right, instead of CO segment over here are why we're part of your why we're part of your program be more recently just the other party or program. 355 00:46:52.860 --> 00:47:02.580 William Cheng: Okay, because the reason we write program is I, if you think about the structure of most program where you want to appoint you want to something like that. Most of the time what your program is doing is it stays inside an infinite loop. 356 00:47:03.300 --> 00:47:08.640 William Cheng: Right, so you pull up your status or inner loop. So if you look at this, if this is your text document, write your code is all over the place. 357 00:47:08.820 --> 00:47:19.590 William Cheng: But a certain time, what you will do is it will stay inside an infinite loop over here. So, therefore, all the other part of your tech segment. They are not recently us and the one that you are using inside the loop over here that will be recently us 358 00:47:20.040 --> 00:47:30.150 William Cheng: Okay, Cindy. What about the staff over here right when you are inside the interview will be here, you know, in that case, you know the the top of the stack over here will come up. Now for a while. What about the the other the 359 00:47:30.330 --> 00:47:38.040 William Cheng: Other thing below the current stack. Right, right. So this code is inside the function right this function over here. Can you know the local variable can still change. 360 00:47:38.220 --> 00:47:43.710 William Cheng: But everything underneath the current stack frame. They're all you know they all become now recently us 361 00:47:44.130 --> 00:47:49.410 William Cheng: Okay, and the top of the second row of us is you're making function call and then you return. So the public from over here will be recently us 362 00:47:49.560 --> 00:47:55.110 William Cheng: So again, you know, your data structure over here, you can actually see that some of the patient over here will be more recently used, and some of them are 363 00:47:55.350 --> 00:48:02.970 William Cheng: Our least recently us. So this way you know you can actually run your page or demon scan all these pays for him and find out which one he needs. Right, right, to the back end store. 364 00:48:03.180 --> 00:48:10.230 William Cheng: And then later on when you try to go to a different function, or will you return on this function well then in that case, you got to bring the data in front, the back end store. And then in that case. 365 00:48:10.380 --> 00:48:17.760 William Cheng: Hopefully we asked the buddy system for another page the buddy system will say I plenty of these pastry that I can give it to you know 366 00:48:19.620 --> 00:48:32.490 William Cheng: So we're synchronize with summer 2019 so next time we're going to continue to look at what kind of, you know, the how to actually implement the least recently used replacement policy inside the operating system. Okay. All right.