WEBVTT 1 00:00:02.970 --> 00:00:11.040 William Cheng: Welcome to lecture eight. This is an extra lecture, you know, because if you look at the summer 2009 2 00:00:12.300 --> 00:00:25.440 William Cheng: Schedule, you know, during this week, we have three lectures. So the reason is that what I want to do is that is that I want to give you everything that you need to do that, you need to know to implement Colonel one by 3 00:00:26.310 --> 00:00:33.120 William Cheng: You know by Thursday lecture next week. Okay, so this way. Soon after you started your current one. 4 00:00:33.600 --> 00:00:50.010 William Cheng: You will have enough to finish kind of one. Okay, so if I don't add an extra lecture. Now, then he will be delayed by another week. So in that case. It's nice. It's not going to work out very well. Yeah. Alright, so, so, so this, you know, this is for the live lecture at 1pm on Friday, June 12 yeah 5 00:00:52.740 --> 00:00:59.670 William Cheng: All right, so last time we were talking about the IO architecture we talked about, you know, 6 00:01:00.420 --> 00:01:06.660 William Cheng: I guess one of the important concept is to use memory map IO to access to to to to talk to devices. 7 00:01:07.170 --> 00:01:18.360 William Cheng: So the basic idea here is that we're going to use memory locations to to talk to devices. So talking to devices. It's the same thing as reading and writing memory locations, guys, so that's that's what we call memory map I oh 8 00:01:19.020 --> 00:01:29.220 William Cheng: There are two different kinds of IOT devices. A one is called the P IoT devices. They're the slow devices and then they're the DMA devices. Those are the fast devices. Yeah, so the so 9 00:01:30.630 --> 00:01:35.970 William Cheng: Robin is that, you know, we're going to talk over the bus. So, typically the way we sort of think about it is that 10 00:01:36.570 --> 00:01:42.060 William Cheng: You know, for the CIO devices they are for memory addresses that you will use to talk to these devices. 11 00:01:42.840 --> 00:01:47.190 William Cheng: So what happened is I, you know, this is done by the hardware people. So the hard people needs to map these 12 00:01:47.880 --> 00:01:58.680 William Cheng: You know, these are the these memory addresses to devices. So the harlot, people call these things registers. Okay, so, so even though they are registers again. They are simply memory addresses. 13 00:01:59.100 --> 00:02:03.180 William Cheng: Okay. So when will you read from this memory is over here. One of them is called a control register. 14 00:02:03.540 --> 00:02:11.400 William Cheng: So if you right to the control register, something happened to the iOS devices. If you read from the status register so got it from a different memory location. 15 00:02:11.760 --> 00:02:14.400 William Cheng: So in this case, you're going to get a status of that particular device. 16 00:02:14.970 --> 00:02:23.760 William Cheng: There's a reregister you can reframe again a different address on the bus and then there's another right registered on the bus. You can invite to a different address. Are you going to end up putting a pattern into that. 17 00:02:24.330 --> 00:02:30.450 William Cheng: Device. Yeah, so, so, so, so, so, so I guess typically inside of control register. 18 00:02:31.080 --> 00:02:37.980 William Cheng: There are some of the bits will be here for example there interrupt enabling to disable so what you want to do that. You want to tell the the 19 00:02:38.160 --> 00:02:45.600 William Cheng: You want to tell the iOS devices to say that when this operation finishes. Should you you should you interrupt the CPU or not. 20 00:02:45.870 --> 00:02:51.090 William Cheng: Okay, so if you if you said the interruption able bit over here you go to zero, that means that when the devices. 21 00:02:51.540 --> 00:02:55.350 William Cheng: That the device has finished with their app operation. They will not interrupt the CPU. 22 00:02:55.800 --> 00:03:02.820 William Cheng: Okay, so in that case how do you know whether the device is ready or not. Well, in that case, you have to read the status register over here to find out whether the devices ready or not. 23 00:03:02.970 --> 00:03:11.970 William Cheng: Okay, so when you read the status register over here, you can see that there are two bids. One of them indicate that this pretty good device is ready for ready for reading and the other bit over here is ready for writing 24 00:03:12.210 --> 00:03:18.480 William Cheng: Right, so it again by reading these two bits you know whether that devices ready for you to reframe it or whether that device for you to write to it. Yeah. 25 00:03:19.350 --> 00:03:26.820 William Cheng: There are also two more beers over here in the control register. So whenever you tell the devices do something at some point, you need to give it a go signal. 26 00:03:27.150 --> 00:03:31.830 William Cheng: Right. So the idea here is that if you said, go read equal to zero it will start to read operation. 27 00:03:32.190 --> 00:03:40.350 William Cheng: If you, if you said go right over here equals one, that means that you tell it device to start writing right but again you use the other bits over here to to to to to to into 28 00:03:40.680 --> 00:03:43.530 William Cheng: Instructor device, whether you're interested in getting an interruption up 29 00:03:44.310 --> 00:03:50.220 William Cheng: Now, and the reregister right registered. So typically, what you do is that, you know, for example, if this is the keyboard device. 30 00:03:50.430 --> 00:03:55.350 William Cheng: Then this case when you try to read from the keyboard. You want to see which keywords press. What do you have to do. 31 00:03:55.530 --> 00:04:02.820 William Cheng: Well, first you need to make sure that this device is ready for reading. So you will read the status register to make sure that it's ready for reading and once it's ready for reading 32 00:04:03.030 --> 00:04:05.730 William Cheng: That means that when you read on the re register, you're going to get valid data. 33 00:04:06.150 --> 00:04:15.360 William Cheng: There so so if you whatsoever you read the standard register if it turns out that this device is not ready for reading and you try to read the data on the reregister why in this case is going to end up getting garbage. 34 00:04:15.930 --> 00:04:22.530 William Cheng: OK. So again, this is just how the hardware design over the bus. So when you read and write these memory location, you know, these kinds of things happens 35 00:04:23.250 --> 00:04:29.100 William Cheng: Now for the DMA devices. The idea is still the same. Basically the same kind of operation, except that the DMA 36 00:04:29.340 --> 00:04:38.940 William Cheng: You know devices you typically what you want to do is that you want to transfer data from certain device address to memory address or the other way around. When you try to write data from memory to do basis. 37 00:04:39.150 --> 00:04:46.380 William Cheng: You want to copy a bunch of data for memory into the device, guys, so therefore you need to tell the device, you know, what is the starting address and what is the 38 00:04:46.950 --> 00:04:59.490 William Cheng: What is it starting address from memory. And also, what is the starting address for for the device. So this way that device when they take over the bus using the direct memory access what he will do is that it will transfer all this data. Yeah. So in this case, there are 39 00:05:00.540 --> 00:05:07.710 William Cheng: There's going to be for memory locations on the bus for setting up the memory address and for memory location on the bus for setting up the device address. Yeah. 40 00:05:08.100 --> 00:05:16.470 William Cheng: The rest of it. There's going to be a control registered. It's going to be a status register. So again, you do the same thing that the control register typically has a, you know, so instead of 41 00:05:16.860 --> 00:05:23.460 William Cheng: So computers in the piano devices they use the the register register to specify so specified command for that particular device. 42 00:05:23.940 --> 00:05:28.350 William Cheng: So in this case for the DMA devices you write the operation code over here. 43 00:05:28.590 --> 00:05:37.530 William Cheng: Okay, so when you write to the control register if you said go back over here equals 218 Rob enable that either one or zero to say whether you're interested in the interim winner. 44 00:05:37.710 --> 00:05:42.420 William Cheng: And then also need to set all the six pairs to tell the device which operation. You want it to be performed. 45 00:05:43.020 --> 00:05:50.790 William Cheng: OK. And then again, if the government is one over here, then the device will start doing data transfer off on one memory address to the device address or the other way around. 46 00:05:51.000 --> 00:05:59.640 William Cheng: If the interim enable bit of sent over here. That means that when the operation is finished, you will interrupt the CPU. Well, otherwise if the interim and they will be over here, equal to zero. Well, then 47 00:05:59.940 --> 00:06:07.500 William Cheng: Again, the CPU has to come and read the status register. I've already been over here, equal to one, that means that the data is ready and you can actually read data, you know, from 48 00:06:08.430 --> 00:06:15.900 William Cheng: The registers. Yeah, so, so, so, so, so in this case there are there are 10 memory locations over here for you to interact with the device. 49 00:06:16.350 --> 00:06:25.950 William Cheng: So yeah, you need to sort of read the device manually to notify you to figure out exactly, you know what, what, what are the possible operation codes out there and the meaning of every bit you know 50 00:06:26.460 --> 00:06:35.880 William Cheng: Using all these 10 different addresses. Yeah. So yeah, the opposite, you know, sort of the behavior of the iOS device. They're all hardware specific 51 00:06:36.300 --> 00:06:39.690 William Cheng: So so so in this case, you know it, since we're talking about operating system. 52 00:06:40.230 --> 00:06:45.810 William Cheng: Operating system in this class. We want the operating system to be as independent of the device operations as possible. 53 00:06:46.230 --> 00:06:52.950 William Cheng: Okay, because if you ask the program or, you know, to say, okay, I want to talk to this disc over here. How do I make that this to say format itself. 54 00:06:53.850 --> 00:06:59.370 William Cheng: Okay, well, how do I tell her that this will be here to transfer data from, you know, from from different from from this disc address 55 00:06:59.580 --> 00:07:06.270 William Cheng: A trust me to memory. I mean, this case, I have to memorize the operation code and know you know, Colonel program or wants to memorize all these kinds of stuff. 56 00:07:07.110 --> 00:07:11.280 William Cheng: Okay. So in this case, who's going to be able to actually know how to do this right 57 00:07:12.090 --> 00:07:18.900 William Cheng: So be here. I sort of said the point over here is that the kernel developer doesn't really want to know exactly how you talk to these IoT devices. 58 00:07:19.740 --> 00:07:31.080 William Cheng: Okay, because the colonel programmer need to worry about Colonel stuff right not not not not not not to memorize, you know, all these IoT devices and also if you want to talk to them, you need to get the device manual. Try to look to see exactly which a Tupperware 59 00:07:31.800 --> 00:07:39.780 William Cheng: You know typically software program. It doesn't doesn't like to do that. Okay. So typically, this kind of information is provided by the device manufacturer 60 00:07:40.260 --> 00:07:51.000 William Cheng: Okay, so we're gonna have a device manufacturers, so they're the ones that come up with a device they're the one that knows how to talk to the devices. Also, in this case, we want the operating system to be independent of how all these things are designed 61 00:07:51.480 --> 00:08:00.840 William Cheng: Okay, so therefore it's desirable if the colonel is device independent. Okay, so this way. The Colonel doesn't really have to know how do you actually transfer data, you know, from one place to the other. 62 00:08:01.350 --> 00:08:05.250 William Cheng: Because that because, again, because that kind of information. It depends on the device manufacturer 63 00:08:05.850 --> 00:08:13.050 William Cheng: So for example, you know, for the disk controller. There are different kinds of this manufacturer, the different kinds of the quite a few company. 64 00:08:13.440 --> 00:08:20.760 William Cheng: That manufactured. This controller. For example, one of the company might be Western Digital, the other one might be CK, they all use different operation code. 65 00:08:21.150 --> 00:08:31.140 William Cheng: Because, you know, there's no standards and say you have to use this operation Cole, you have to use that operation code. So therefore, they will they will all have their own ideas, some of that is going to provide more features than the other. So this way. They can compete. 66 00:08:31.890 --> 00:08:43.860 William Cheng: Okay, so therefore, again, it's reasonable for the iOS developer, not to know all these detail and we want the however many manufacturer or the device manufacturer to be responsible for us. We're talking to their own devices. 67 00:08:44.610 --> 00:08:48.900 William Cheng: That are. So the question is, how can this be done right. So the solution over here. 68 00:08:49.560 --> 00:08:54.180 William Cheng: Is that we're going to ask the device manufacturer to write software and give the software to the operating system. 69 00:08:54.720 --> 00:09:04.710 William Cheng: Okay, so what are the device manufacturer over here. What it will do is that since they manufacture the device they know how to talk to it, they were right all the code to talk to all these devices and package them into 70 00:09:06.780 --> 00:09:09.270 William Cheng: Into a piece of code that's known as the device driver. 71 00:09:09.900 --> 00:09:19.920 William Cheng: Okay, so for example if this control over here is the Western Digital at this controller, while the Western Digital people they will write a device driver to talk to their own device and they will plug it into your kernel. 72 00:09:20.760 --> 00:09:26.610 William Cheng: Guys. Similarly, you know, we hear that this is the network interface card there tons of network interface card out there, right, there's, you know, 73 00:09:26.820 --> 00:09:28.950 William Cheng: I guess one of the most famous one is Broadcom 74 00:09:29.190 --> 00:09:41.310 William Cheng: Broadcom they are you know very well known for manufacturing their own network interface car. Why not case if you want to talk to them a huge of their driver and they will package that you know they will package all their code into this driver and you're putting into your kernel. 75 00:09:42.450 --> 00:09:46.830 William Cheng: Okay, so this way. Well, usually you know what your current I'll try to use it the right driver. What does your car. Don't have to do 76 00:09:46.980 --> 00:09:55.860 William Cheng: Well, in this case, your kernel, which just have to make function call. Right. So this is going to be, you know, maybe one function for reading from the network interface card. Want to function well writing into the network interface card. 77 00:09:56.250 --> 00:10:05.940 William Cheng: Maybe one function for handle the interrupt service routine, right, because again, those kind of operations going to be device specific. So all of these co will be done by the notebook by by the device manufacturer 78 00:10:06.420 --> 00:10:16.260 William Cheng: Okay, so it's their job to package all this code together and then provide, you know, apply the operating system with all these device driver. Yeah. And this device driver will be plug and play if you switch to a different device. 79 00:10:17.310 --> 00:10:24.420 William Cheng: If you switch to a different device. All you need to do is download their device driver and plugging into the kernel. And now, everything's going to work exactly the same way as before. 80 00:10:25.440 --> 00:10:29.940 William Cheng: Okay, so. So again, if you do it like this, then the operating system can be device independent 81 00:10:30.180 --> 00:10:38.550 William Cheng: Right, because all you have to do is to call these functions over here. So, so in a way where you want to do that when you make these function call, you're going to make the function call in a device independent way. 82 00:10:39.270 --> 00:10:41.700 William Cheng: Okay, so how do I make function calling the device independent wait 83 00:10:42.390 --> 00:10:50.670 William Cheng: Yeah. Guess over here. The picture over here, there are more driver right for the keyboard. There's a keyboard driver for the mouse mouse driver every piece of hardware device. There's a device driver. 84 00:10:51.030 --> 00:11:03.090 William Cheng: Okay, so if we open up a device driver and try to take a look at the detail over here. So this will show you what's inside the device driver. Okay. The device driver in a way has their own the dimension of also has your own, you know, memory segment. 85 00:11:03.510 --> 00:11:08.610 William Cheng: Because when you think about the divider have their own tech segment have their own data segments. So here's the tech side where 86 00:11:08.790 --> 00:11:21.240 William Cheng: That's where you put all the device function over here, they're written by the device manufacturer. Right. So in this example, the, you know, so. So in this example here. So what we're gonna do is I'm going to take all the functions and we're going to put it inside of a rail function pointers. 87 00:11:22.260 --> 00:11:30.060 William Cheng: Okay. So over here, the dysfunction that we function. The right function and the inner function over here. So what we need to do is that we need to put them together into a real function pointer. 88 00:11:30.270 --> 00:11:37.650 William Cheng: The first pointer over here will point to the reef function and the second function of your point to the right function and the third pointer over here will point to the inner function. 89 00:11:38.130 --> 00:11:47.790 William Cheng: So why do we actually use an array, right. So this way when the operating system need to read from any device, all it has to do is to find the function pointer and call the first function inside of 90 00:11:48.150 --> 00:11:55.200 William Cheng: Inside the real function pointers. Okay. Because we know that for this type of device, the first function will always be reading from that device. 91 00:11:55.860 --> 00:12:04.590 William Cheng: Okay, so it doesn't really matter where the devices manufacturer by you know Broadcom or Seagate or wasn't just, oh, if we call the first function over here will always be reading from that device. 92 00:12:04.920 --> 00:12:08.460 William Cheng: Yeah, but it turns out you know that this control and and then we interface car very, very different. 93 00:12:08.910 --> 00:12:17.850 William Cheng: So for every kind of device. We're going to sort of define exactly what the functional interface look like okay and that will be the standard functional interface for that type of device. 94 00:12:18.660 --> 00:12:30.270 William Cheng: Okay, so, so, so in this case the Western Digital so this is worse than usual and see gave that up both this manufacturers their interface to talk to their this the driver, they will look exactly the same. 95 00:12:31.410 --> 00:12:41.100 William Cheng: Okay, so, so in this case the operating system. All they have to do is to find out, you know, you know, which re index to us and make the corresponding function call and then you're going to be end up calling the function 96 00:12:41.310 --> 00:12:47.160 William Cheng: They're implemented by the device manufacturer, so therefore they will know how to deal with their devices in a device independent, which is why 97 00:12:47.640 --> 00:13:00.300 William Cheng: They were, you know, you will execute their co so they will deal with with with with the devices in a device dependent way what the operating system was simply make a function call and deal with the devices in a device independent way. 98 00:13:01.530 --> 00:13:12.420 William Cheng: All right. Okay. So, so the idea over here is very, very simple. You asked all the device manufacturer throughout all these functions, package them together into a ray of function pointer and now you give this a real function pointer to the operating system. 99 00:13:13.530 --> 00:13:18.240 William Cheng: Okay, so this is a real function pointer. So the real function pointer is also known as the interface. 100 00:13:18.840 --> 00:13:29.220 William Cheng: For those of you who know about c++ C plus plus has a class, known as interface. Okay. So as it turns out, what is the interface interface in c++ is simply an array of function pointers. 101 00:13:29.610 --> 00:13:36.870 William Cheng: Okay, so they're all exactly the same thing. Yeah. Yeah. So for example, you know, as far as the operating system is concerned when you need to talk to a disc. 102 00:13:37.470 --> 00:13:45.420 William Cheng: Okay. So in this case, what would you do right, you can think about. I have, I have the abstract data type. It's a disk object, right. So, so we'll look like this right here is this object over here. 103 00:13:45.660 --> 00:13:49.950 William Cheng: So in c++ again if you don't know c++. Okay, we're going to talk about see pretty soon. Yeah. 104 00:13:50.490 --> 00:13:53.610 William Cheng: So if you have this class over here. So in this class. What kind of 105 00:13:53.910 --> 00:13:59.940 William Cheng: You know function, you need to provide why you need to provide a function to refund the dis, you need to pull back function to refund rate right to the days. 106 00:14:00.240 --> 00:14:06.270 William Cheng: You need to provide a function to execute as a interrupt service routine. So this case your this class will have all these functions over here. 107 00:14:06.810 --> 00:14:14.910 William Cheng: Okay, so in c++ they use, they will use a special notation over here, they know all these functions as a virtual function. 108 00:14:15.630 --> 00:14:26.730 William Cheng: A virtual function. That means that you know this particular function. The function behavior can be overwritten. More importantly, over here on the right hand side is equal to zero, that means that that this class has no implementation for these functions. 109 00:14:27.780 --> 00:14:32.070 William Cheng: Okay, so therefore, in this case, all these functions are empty function. So there are no as a placeholder. 110 00:14:32.400 --> 00:14:41.220 William Cheng: placeholder for function. So, later on, you know, when you when you sell class for this class, then you can actually implement all these functions inside a subclass. 111 00:14:41.850 --> 00:14:48.720 William Cheng: Okay, so for example, if I'm wishing digital man. I'm a Western Digital engineer. I want to, you know, sort of rather device driver for my 112 00:14:49.080 --> 00:14:59.610 William Cheng: My, my western digital model 123123 device driver. Right. So, in that case what I would do is I will inherit from this class, I make a subclass. 113 00:15:00.060 --> 00:15:07.950 William Cheng: Call a Western Digital 1230 yeah then and then I'm going to implement my own read and write an inner function. So the function, I would call them were some jewelry. 114 00:15:08.190 --> 00:15:17.130 William Cheng: Sorry, Western Digital 123 wasn't just 123 right. Where's the dishes are 123 interrupt. I'm going to package them all together and then use a real function pointer to ponder. All these functions. 115 00:15:17.430 --> 00:15:20.490 William Cheng: There. And then what I would do is that I will create a class called this 116 00:15:20.670 --> 00:15:33.090 William Cheng: I will give this this to the sort of this class to the opportunities and and with the optimism called the first function over here, which is the reef function as it turns out it will the other wasn't do so 123 function will get executed. 117 00:15:34.140 --> 00:15:41.520 William Cheng: Okay. Similarly, if I'm another competitor when it wasn't just oh I'm CK so so I know maybe have a see gay you know 67 that will be my 118 00:15:42.090 --> 00:15:47.430 William Cheng: You know device driver model. So again, what I would do is that I will, you know, device manufacturer or write their own code. 119 00:15:47.880 --> 00:15:53.340 William Cheng: The function to read from this pretty good this controller is going to be, it's going to cause CK 67 read right 120 00:15:53.760 --> 00:15:55.410 William Cheng: The one that provide the right function is going to be 121 00:15:55.680 --> 00:16:07.530 William Cheng: The CTA 67 right and then the inner service will do will be CK 67 interrupt again a patch them all together into a function pointer and this a real function partner will look exactly the same as the Western Digital a real entrepreneur. 122 00:16:07.980 --> 00:16:17.790 William Cheng: Yeah, so if it turns out that I have, I have a CK 67 device driver instead of getting the Western Digital 123 a real function pointer. I'm going to get the other real bunch of pointer. 123 00:16:18.870 --> 00:16:23.820 William Cheng: So in this. So in this case, when my operating system. You know what it has appointed to get to the this 124 00:16:24.030 --> 00:16:33.210 William Cheng: If it calls the we function magically this other function will get call and now we're going to execute a device manufacturers CO and then we'll talk to the device specific you know operations there. 125 00:16:34.560 --> 00:16:42.690 William Cheng: Okay, so, so therefore, in this way, the operating system can be device. The device device independent while the device driver you know will execute code that's device dependent 126 00:16:43.230 --> 00:16:52.440 William Cheng: Then alright so you see a cinema has a fancy word for it. This is called polymorphous. Okay, so what happened is that, you know, for the rest of your software, you will talk to 127 00:16:53.610 --> 00:16:58.620 William Cheng: You you'll create a class called at this class, but there's no implementation inside of this class. 128 00:16:59.130 --> 00:17:13.620 William Cheng: That the all the operation for this is implementing the subclass of the desk right like wasn't just a 123 SDK 67 over here and then uses polymorphous the mechanism you will call the function inside the abstract class, while the concrete class. What will execute a function 129 00:17:14.880 --> 00:17:25.380 William Cheng: Okay, so again, the obvious is that we will have a pointer to this object. And when you call one of the functions over here. As it turns out, is the concrete class that has the implementation what these budget those function will get actually called 130 00:17:26.160 --> 00:17:29.790 William Cheng: Okay so c++ called this fancy mechanism polymorphous them. 131 00:17:30.450 --> 00:17:38.400 William Cheng: If you try to implement the c++ polymorphous them and see all you need is an array of function pointers. Great. Because in the end, the interface is just a repetition pointer. 132 00:17:38.700 --> 00:17:46.920 William Cheng: Yeah, they sort of just make it look, you know, sort of a mysterious how this kind of stuff happen automatically. But in reality, it's just an array of entrepreneurs. 133 00:17:47.640 --> 00:17:51.510 William Cheng: There so so here's an example. What am I look like. Okay, so here's this class. 134 00:17:51.930 --> 00:17:57.750 William Cheng: He is a real function pointer and notice that this operation is the one of these operations by reading from the does right into the dis 135 00:17:57.960 --> 00:18:07.560 William Cheng: Handling interrupt service routine, you know, for the desk does. These are the disk operations. Right. So the Western Digital people, are they the government device driver for Western Regional model 123 136 00:18:07.770 --> 00:18:20.460 William Cheng: And then they implement wasn't digital 123 wasn't division 123 right wasn't 0123 interrupt. So in this case, what they will do is that they will package all these function together, put it into a real function project I look just like this. 137 00:18:21.540 --> 00:18:33.090 William Cheng: Okay, if you look very, very carefully, you'll see that this data type. And this data type, they're completely compatible, right, because whenever you see a real over here where you can do that. You can add another X streets over here then. 138 00:18:33.330 --> 00:18:38.100 William Cheng: You know, the data type over here is going to be a boy, star, star which is exactly compatible with a disc operation. 139 00:18:38.940 --> 00:18:44.790 William Cheng: Okay. Similarly, if you are Western Digital all your engineer will do exactly the same thing. You're right. All these function. So if you're a Seagate 140 00:18:45.240 --> 00:18:59.550 William Cheng: For model 76 over here. You write a function called secret 76 we seek SMB six right so you get 76 interrupt and you package them all together into this data structure because see gay 76 disk operations over here. Again, it's an array of voice start 141 00:18:59.970 --> 00:19:06.000 William Cheng: Okay, so definitely is compatible with a disc operation over here. Okay, so what happened is that you know your operating system. 142 00:19:06.360 --> 00:19:15.030 William Cheng: You know what, what, I guess, obviously some started are sitting on the desk right and then when you load the operating system you're going to copy all these functions over here into the kernel. 143 00:19:15.330 --> 00:19:26.520 William Cheng: OK, so the colonel will have all these device driver. Right. So all they have to do is to figure out what is the actual device that you have. And then what it will do is it will assign this to point to this URL function pointer and now you can talk to this device. 144 00:19:27.540 --> 00:19:39.120 William Cheng: Okay, so all this code over your already existing set of Colonel, all you have to do is to make the final connection to connect that this operation over here to be either Western Digital 123 this cooperation or CK 76 this operation. 145 00:19:39.600 --> 00:19:50.100 William Cheng: OK. So the code is very straightforward. So obviously I created this class over here, right, this goes over here and that's it is right arrow, this operation recipes will be here point to a data structure look like this. 146 00:19:50.340 --> 00:19:56.880 William Cheng: Right arrow. This operation is going to be this voice, star, star pointer, I simply assign them to either this array of disarray. 147 00:19:57.900 --> 00:20:05.790 William Cheng: OK. So again, this read this read that data tab right exactly compatible because they're both of the type of voice stars are so I can simply use them use an assignment operation over here. 148 00:20:06.300 --> 00:20:20.880 William Cheng: OK, so again I will decide to have that this operation either point to the first data structure on the second data structure depends on whether I discover I have a Western Digital 123 device driver so Western Digital 123 device or a CK 76 device. 149 00:20:21.330 --> 00:20:31.470 William Cheng: Okay, so that operations not certain. So that part of the operating system functionality is no as discovery. So what you need to do is I need to actually go to the bus and that sort of sort of discover you know whether 150 00:20:32.160 --> 00:20:41.610 William Cheng: What kind of device. Do I have. Why does he find out what kind of device. The device that you have, then you assign you actually this this instruction so that it will point to the right array of entrepreneurs. 151 00:20:42.480 --> 00:20:52.050 William Cheng: Okay, so once you sort of, sort of, you know, set up a real function pointer, the sort of this software terminology for for this particular operation is that this is known as binding. 152 00:20:52.980 --> 00:20:59.670 William Cheng: That's what happened is that in the, in the, in the beginning, you'll have a variable over here that's not bound to anything, which means that it's only initialize 153 00:20:59.850 --> 00:21:10.980 William Cheng: Then when you initialize it this way to to have one URL function point over here to be bound to another function which is also due to another data structure that's also a real function pointer and now they are bound together. 154 00:21:11.310 --> 00:21:21.210 William Cheng: So it's kind of like you're using a super glue, glue stuff together. So by using the assignment operator over here, these, you know, the disc operation and the Western Digital one to sit this operation now become one. 155 00:21:22.230 --> 00:21:26.310 William Cheng: Okay, so therefore, from this point on, if I if I call the function using the desktop version. 156 00:21:26.850 --> 00:21:33.960 William Cheng: So it's an area function partners, it actually is an array index over here. If I use a range like zero I'm calling the Western Digital 123 read 157 00:21:34.380 --> 00:21:38.730 William Cheng: Okay if I use a re index number one, I will end up calling Western Digital 123 right 158 00:21:39.000 --> 00:21:47.730 William Cheng: Okay. And I know that array index number zero is always every function array that's number one is always the right function. So this way you know my operating system can call these functions in a device independent way. 159 00:21:48.240 --> 00:21:58.410 William Cheng: Yeah. So in this example here to read from any desk right doesn't really matter. You know what device driver you're using the operating system can call you know the disc operation over here independently. 160 00:21:58.620 --> 00:22:05.850 William Cheng: Using call like this now, so I'm glad you said the tea. The tea is a desk over here, right arrow, this operation over here is going to be a rare function pointer. 161 00:22:06.120 --> 00:22:12.810 William Cheng: Or a index zero is going to be. It's going to be the reef function. Okay. But in this case, or a zero indexing over here it's going to give me a voice. 162 00:22:13.740 --> 00:22:24.570 William Cheng: Okay, I cannot use the voice directly. I need to type cast to the right time. So in this case, the right type over here is going to be a read handler type over here. So I take this argument over here I type Casa to a read. 163 00:22:24.870 --> 00:22:33.660 William Cheng: Handle time over here and then I will call this function in directly. So, so when you try to call the function in directly, you put an asterisk in front of you put a parenthesis and Rhonda. 164 00:22:33.870 --> 00:22:40.440 William Cheng: And now I'm calling this function indirectly, and then the right hand side over here. I'm going to provide all the function argument for the re handler. 165 00:22:41.520 --> 00:22:49.170 William Cheng: Okay, so this way, the operating system or execute like actually your code like this, and this way, it will refund the disc in a device independent matter. 166 00:22:50.730 --> 00:22:57.780 William Cheng: Okay, so, so, so you're kind of assignment, you're not going to see code like this until we incarnate to okay in court or two, we're going to 167 00:22:58.140 --> 00:23:09.060 William Cheng: The Colonel to is no as a virtual file system. So we are dealing with a virtual versus them. Now you need to deal with c++ pilot PC plus plus polymorphous then implementing and see. And this is how you do it. 168 00:23:10.470 --> 00:23:18.540 William Cheng: Okay, so if you don't, if you don't understand this. Right now it's perfectly fine because when you're doing your corner to assign man, you gotta run to call like this and then in that case you will really understand what's going on. 169 00:23:19.110 --> 00:23:29.070 William Cheng: With that, right. So again, this is kind of events you know C code that you are, you know, you're making function call like this guy's again if you don't fully understand it. And right now it's perfectly okay okay 170 00:23:30.900 --> 00:23:34.500 William Cheng: So one more thing about these function call over here, you know, we 171 00:23:35.100 --> 00:23:43.410 William Cheng: Were talking about before the real system called the right system call right when their system call they are synchronous system call, which means that these functions don't return until they're done. 172 00:23:44.070 --> 00:23:48.450 William Cheng: Okay. But if you think about the these operation as an operation on a device. 173 00:23:48.870 --> 00:23:57.780 William Cheng: Okay then. It's kind of weird that if you, you know, if you call the function. What does it mean that this function doesn't return onto the roof function doesn't return until the devices that 174 00:23:58.350 --> 00:24:09.390 William Cheng: Okay, it doesn't really work that way. Right. So when you tell the device to go with something you started, you started I operation and then you'll Colonel said, Go to sleep. And then he said to interrupt service routine you're currently got woken up 175 00:24:10.380 --> 00:24:16.500 William Cheng: Okay, so therefore the function over here, shouldn't really be called the reef function. It's more correct to call this function start read 176 00:24:17.010 --> 00:24:22.380 William Cheng: Okay, so what happened is that when you call this particular function, it will start reading, you know. So you start reading data on the desk. 177 00:24:22.740 --> 00:24:36.420 William Cheng: What does that mean to read it up on a desk right you're asking that this to transfer data from the dis into memory. Okay, so what we'll do is we'll return to handle to you. And then he can, so, so, so, so, so, so later on you can start to try to find out you know the status of this read operation. 178 00:24:37.440 --> 00:24:40.440 William Cheng: OK. So again, okay, you call the start re function over here. He goes, how the device. 179 00:24:40.620 --> 00:24:51.810 William Cheng: Device to start doing this in parallel, right, because if this is a piece of hardware, you can run simultaneously with the CPU and it's going to transfer data into memory using DMA operation right so what it will do is that it will return to you. 180 00:24:52.170 --> 00:24:57.660 William Cheng: To to handle and later. All you want to find out anything about this particular operation. You got to pass the handle back 181 00:24:58.140 --> 00:25:04.980 William Cheng: Okay, so in the end you know if your kernel has nothing to do but to wait for this operation to finish. Well, in that case, we need another function of your call. Wait. 182 00:25:05.160 --> 00:25:12.690 William Cheng: You can say way and then you give the handle value over here and this one will tell you. So when you call the word function over here. This function will be a synchronous function. 183 00:25:14.010 --> 00:25:18.330 William Cheng: Okay, so the reading and the writing function over here, typically for the device. There's a synchronous 184 00:25:18.810 --> 00:25:20.130 William Cheng: asynchronous functions over here. 185 00:25:20.490 --> 00:25:24.990 William Cheng: Before the word function over here, this is the way you say you say I have nothing else to do but I want to wait for this device to finish. 186 00:25:25.140 --> 00:25:33.330 William Cheng: So in this case, you can even call way and you need to say, which one do you want to wait for you want to wait for the read operation. You want to wait for the right operation. So you have to handle back 187 00:25:33.540 --> 00:25:38.250 William Cheng: And now this one has a blocking call so your kernel thread is going to fall asleep in Iowa cute. 188 00:25:38.910 --> 00:25:50.520 William Cheng: Okay, so when the iOS as when when when when when the data was when the I operations finish. It's going to insert interrupt service routine, you will get woken up when your thread get woken up you're going to return from the right function. 189 00:25:51.750 --> 00:25:57.750 William Cheng: Okay, so this is the function that's blocking and this is the way you Colonel can call to actually go to sleep, waiting for iOS device to finish. 190 00:25:58.380 --> 00:26:04.110 William Cheng: Yeah, alright. So, so this is a more realistic implementation, you know, for the for the for the the 191 00:26:04.590 --> 00:26:07.620 William Cheng: The real function pointer, you know, for this device again. Similarly, 192 00:26:07.830 --> 00:26:16.890 William Cheng: For a lot of the IoT devices over here, all these operations just starting them off because again hardware, you know, the all these devices, their hardware. They can run in parallel with the CPU. 193 00:26:17.100 --> 00:26:25.140 William Cheng: So therefore, it doesn't really make sense to make a blocking call right away. You just want to start a device off and later on we have nothing to do that, you wait for the device to finish. Yeah. 194 00:26:27.330 --> 00:26:39.660 William Cheng: Alright, so this is the last slide for IO architecture, we will talk about the bus architecture. So so tip. So in this class when we, when we talk about IO, we always think about the bus architecture. Okay. 195 00:26:40.140 --> 00:26:46.320 William Cheng: So this picture this side over here, you're not responsible for it. I just want to briefly mentioned that there are other type of architecture. 196 00:26:46.560 --> 00:26:54.300 William Cheng: That what are the IO architecture over here is known as the channel arcade architecture. So in this case, you know, this is I guess this was invented by IBM 197 00:26:54.810 --> 00:27:02.790 William Cheng: So, IBM, you know, sometimes you know your program will spend most of this time doing IO. So in that case, the CPU is actually not doing anything useful. 198 00:27:03.210 --> 00:27:10.740 William Cheng: Okay, so in that case, what you will do is that you will actually build very, very powerful. I will processor that's what these our process is known as the channel passes over here. 199 00:27:11.130 --> 00:27:23.820 William Cheng: So obvious that they were shared memory with the main CPU. So here's the CPU for this IBM mainframe and they have a share memory speech share between the CPU and all these channel processor. So in this case, the memory is going to be multi port it 200 00:27:24.180 --> 00:27:32.610 William Cheng: Right, because there are multiple ways to talk to the the memory, either from the CPU or from any of these channel. So in this case, this memory is going to have controller in front of it. 201 00:27:32.970 --> 00:27:37.380 William Cheng: There. So this way you don't have to CPU to try to you know refund the same memory location at the same time. 202 00:27:38.670 --> 00:27:49.230 William Cheng: Right. So in this guy's you'll have all the CPU is over here and what you can do is that you can actually download program for the channel controller over here to execute and they will be the one that talks to the device controller. 203 00:27:50.280 --> 00:27:56.100 William Cheng: Okay. So this guy is, again, we don't have to order the sort of the, the bus architecture, we have a different architecture North Channel architecture. 204 00:27:56.490 --> 00:28:08.430 William Cheng: Right. So yeah, we're not going to get into get into this architecture, because this is pretty old architecture. And I just want to demonstrate that there are also other kinds of our architecture. Yeah. Alright, so we're finally 205 00:28:09.480 --> 00:28:17.850 William Cheng: Done with the the sort of the, the iOS up. The next thing that we're going to talk about in chapter three over here is memory allocation. 206 00:28:18.870 --> 00:28:25.230 William Cheng: That's what we're going to talk about what is memory allocation, right, you'd be using it Malik and free. So we're going to talk about 207 00:28:25.560 --> 00:28:33.630 William Cheng: Malcolm freeze all about, okay, I want to sort of give you a detail of exactly how to implement them. So this way you have a good understanding of what am I looking free are doing now. 208 00:28:34.020 --> 00:28:42.630 William Cheng: So this is under the heading of best fit and first fit algorithm. Right. It's kind of weird. So this is like it's under the heading, but we're going to see what what they are. 209 00:28:43.920 --> 00:28:57.930 William Cheng: All right, so, so, so what is the memory alligator. Okay, so, computer science people they like to personify things. Okay, so, so this. So the way we sort of think about is that there's something inside your 210 00:28:58.560 --> 00:29:02.520 William Cheng: So we run your program. There's something that's managing the heat memory. 211 00:29:02.940 --> 00:29:11.100 William Cheng: Okay, so therefore, when you want some memory, you're going to talk to the memory allocated as a hey you know allocate some memory for me. And we are done with it, you can return memory to the memory alligator. 212 00:29:11.370 --> 00:29:18.630 William Cheng: Okay, in reality, there's really no such thing as a memory alligator, then the memory alligator is simply a collection of functions. 213 00:29:19.680 --> 00:29:30.570 William Cheng: Guys, also as a computer side. People like to sort of make them into sound like a person. So they will call it memory alligator. And in reality the memory alligator is just Malika free. Okay. So, therefore, when you call Malik. 214 00:29:31.590 --> 00:29:43.470 William Cheng: There's less that you call Malik 400. Okay, so I want 400 buyers to the alligator so Malik is the memory alligator. Guess what it will do is that the memory alligator madness this hip area doesn't hire is called heap. 215 00:29:44.160 --> 00:29:53.490 William Cheng: Okay, so when you call Malik 400 what he will do is that it will use some data structure inside the heap and they will try to find your 400 bytes and this farmed by has to be contiguous bites. 216 00:29:53.760 --> 00:29:56.910 William Cheng: Guys, so therefore they lose, they will find 400 bucks over here. Yeah. 217 00:29:57.210 --> 00:30:08.490 William Cheng: And then they will return to you the base, the base memory for this 100 bucks, right, what is the best memory. The base memory, the memory address is going to be the smallest memory address for this 400 400 bytes of memory. 218 00:30:09.030 --> 00:30:15.210 William Cheng: Okay, so it again. It's continuous memory, right. So, starting from this memory, all the way to the memory of just plus 399 219 00:30:15.420 --> 00:30:28.290 William Cheng: They, you know, they are the addresses within this bucket 400 bytes. Okay, so what are those other return this address to use the address to you and what do you do you store in a variable as over here. You said PT Our PT r equals two Malik or 400 220 00:30:29.010 --> 00:30:35.730 William Cheng: Okay, what does PT PDF. Right. So some people will call PT are a heat variable, but there's really no such thing as a heat variable. 221 00:30:36.840 --> 00:30:45.180 William Cheng: Right. Because what kind of variable can you have, whether you can all have two kinds of variables are going to have global variable or you can have a local variable. There's no such thing as a heat variable. 222 00:30:45.750 --> 00:30:54.030 William Cheng: Okay. So typically what we say that what we said he variable, we mean that it's a global variable local variable that point to a memory address that's inside the heat memory segment. 223 00:30:54.840 --> 00:31:01.530 William Cheng: Guys over here, return address to you. You're going to store the address in a pointer tire. So at this point I can be a voice star as well via voice over here. 224 00:31:01.740 --> 00:31:12.420 William Cheng: Again, the address is just a 32 bit number if you're starting to avoid star which is also a 32 bit number, then this case the picture that we chose that inside this point over here, we point to this 400 bytes of memory. 225 00:31:13.470 --> 00:31:26.010 William Cheng: Okay, so this is what Malik does. Okay, what about free right so so again the lifetime of this 100 but but by some memory over here. Once you allocate 400 bucks from the heat over here you signed a contract with a memory alligator. 226 00:31:26.610 --> 00:31:36.210 William Cheng: Okay, you are promising that I'm you know I asked you to look at 400 bytes. And then once I get this pointer over here, I will only use the 400 points, starting at the pointer. 227 00:31:36.570 --> 00:31:43.410 William Cheng: OK, I will not use any memory before we use not not use any memory after it. Okay, so therefore that's my promise. 228 00:31:44.160 --> 00:31:50.640 William Cheng: Okay, the memory amount alligator also promise you that once they allocate these 400 bucks to you, they will not give the 400 229 00:31:51.000 --> 00:32:04.680 William Cheng: The memory allocated will not give this 100% anybody else who is anybody else while it may be other threats inside your program. Okay, so therefore once you you know it says in this, in this case, you got the 400 bytes and then this 400 bucks will not be reallocated to somebody else. 230 00:32:05.970 --> 00:32:14.100 William Cheng: Okay, and the lifetime of this member over here you know this this 100 buyers will belong to your belong to your thread for as long as you want until you call free 231 00:32:14.580 --> 00:32:25.200 William Cheng: That. So when you call free over here, you have to call free on the base address right over here. That's why I started. So inside the variable over here because later on when I'm done with this block of memory. I'm going to call free pointer. 232 00:32:26.280 --> 00:32:33.630 William Cheng: Okay, so I will return this blog over here back into the memory allocators say I'm done with it. I promise I won't touch this 400 bytes anymore. 233 00:32:34.740 --> 00:32:46.650 William Cheng: Okay. And also, or the memory allocation to say, now you return this 100 bytes to me. I can reallocate the 500 buys immediately to anybody who wants it. So maybe another three I called mallards. So in that case, why do you not just 100 bytes. 234 00:32:47.010 --> 00:32:56.820 William Cheng: Maybe allocate to another sweat or maybe only five bites out of the swag about will be allocated another. So, or maybe there's a memory block overlap. This particular memory blog and that will be allocated to the other. So, yeah. 235 00:32:57.090 --> 00:33:05.040 William Cheng: Anything can happen because once you return a block a memory to the memory alligator that you have no control. Okay. And also you promise that you will never touch it again. 236 00:33:06.090 --> 00:33:15.000 William Cheng: Okay, so, so, so, so another important thing to know is that when Malibu return a pointer to you when you call free, you have to use that exact address if you use, you know, 237 00:33:15.570 --> 00:33:29.730 William Cheng: If you're Miss by one bite. Okay. In that case you mess up the memory alligator. Will you mess up the memory alligator that I the terminology that we use that you also cause memory corruption. Okay, so one of the major memory corruption is that you mess up the memory alligator. 238 00:33:30.780 --> 00:33:38.850 William Cheng: Okay, there are other kinds of memory corruption, right, if you give a buffer, you have a buffer overflow. Okay, but also if you don't do what you're supposed to do. Are you going to mess up the memory alligator. 239 00:33:39.030 --> 00:33:44.400 William Cheng: Once you mess up the memory allocated from this point on, Malik and free no longer make any guarantees. 240 00:33:44.730 --> 00:33:53.340 William Cheng: Okay, they can allocate garbage to you, they can allocate a blog that has already has already been allocated somebody else, all bets are off. Once you you know once you make a single mistake. 241 00:33:54.720 --> 00:34:00.390 William Cheng: Okay, so you know way the memory allocation over here is very, very fragile, you make one tiny little mistake. And then all bets are off. 242 00:34:00.900 --> 00:34:08.370 William Cheng: Okay, so please understand that when you use American free. You got to be extremely careful, you got to know exactly what you're doing. Okay. Otherwise, nothing's going to happen there. 243 00:34:09.720 --> 00:34:17.460 William Cheng: So that the memory alligator is just Malcolm free. It's not a. It's not a process. You know, it's just to function inside your address space there. 244 00:34:18.120 --> 00:34:22.050 William Cheng: And over here I talked about the sort of the contract you made with the memory alligator. 245 00:34:22.680 --> 00:34:26.550 William Cheng: So, so now we're going to sort of take a look at the memory allocation implementation that 246 00:34:26.880 --> 00:34:33.240 William Cheng: So the concerns are, you know, we need to have efficient use of storage, right, because the memory allocation is managing the entire heat 247 00:34:33.510 --> 00:34:40.320 William Cheng: Right you if you want. So again, there's typically a spacetime trade off. If you want the memory Alex to be very, very fast. Well, in that case you need to build a 248 00:34:40.560 --> 00:34:51.930 William Cheng: Complicated data structure inside the heat, you're going to end up wasting a lot of memory inside of heat by for the memory allocated to operate that if you, you know, if you don't want to use too much memory then typically what happens 249 00:34:54.030 --> 00:34:59.700 William Cheng: Is that against space that trade off. You don't want us too much memory your memory allocated might be a little slow. Yeah. 250 00:35:01.110 --> 00:35:10.680 William Cheng: So again, this is the there's no clear cut winner, how to do it there. And also, we want to, you know, be fairly efficient. We don't have to be super efficient. And so, so, so in that case, where do we draw the line. 251 00:35:11.520 --> 00:35:17.460 William Cheng: Okay, so we're going to see some typical implementation American free. They are not super efficient, but typically they're considered good enough. 252 00:35:17.760 --> 00:35:24.420 William Cheng: Yeah. So the algorithm. I'm going to see what is called the first fit a memory alligator and the other one's called the best fit memory alligator. Yeah. 253 00:35:26.040 --> 00:35:31.110 William Cheng: Alright, so the idea here is that if we take a look at the heat right the heat is going to start out to be an entire memories that man. 254 00:35:31.410 --> 00:35:38.340 William Cheng: Okay, so once we start calling Malik and free the heat is going to get divided into sort of a, you know, the two different block. 255 00:35:38.730 --> 00:35:43.140 William Cheng: Some of the block or free will. So some of the block are allocated. Right. So when you come 256 00:35:43.710 --> 00:35:51.600 William Cheng: Across going to get returned to you when they get returned to you they they're owned by your application. So now they no longer there are no longer owned by the memory alligator. 257 00:35:52.500 --> 00:35:58.080 William Cheng: Okay. So in a way, the job of the memory allocated is to keep track of all the, the, you know, all the memory blocks that are free. 258 00:35:58.620 --> 00:36:05.820 William Cheng: Okay, so this way when you today with malice when you try to call Malik to allocate memory, they will allocate you know memory from the free blocks. 259 00:36:06.210 --> 00:36:15.840 William Cheng: Good. So in the beginning your entire he is a giant free blog and then why is it called Malik Malik Malik Malik, and then you call free and stuff like that, then you know the the memory is 260 00:36:16.890 --> 00:36:27.120 William Cheng: The heat memory is going to be divided into blocks. Some of them are owned by your application. So those are the ones that are returned from Alex and some of them that are owned by the memory alligator. Right. So those are the three blocks. 261 00:36:28.110 --> 00:36:34.050 William Cheng: OK. So again, the memory allocators job is mainly to manage the free Bloss right because once the return of block to you. 262 00:36:34.260 --> 00:36:40.260 William Cheng: Their hands up, they're not allowed to touch it. So, therefore, you get to manage it and the memory alligator only manage the three blocks there. 263 00:36:40.620 --> 00:36:49.110 William Cheng: So this example over here, we started out with our entire heap of your against is a very simple tiny little silly example over here we're going to end up with a we only have two three blocks. 264 00:36:49.380 --> 00:36:56.940 William Cheng: One is 1300 bytes doll, and the other one is 1200 by stall and they are separated by blocks that are returned back to you. If we application. 265 00:36:57.240 --> 00:37:02.790 William Cheng: Okay, so therefore, as far as the memory alligator is concerned, the white box over here, they're not allowed to touch it. 266 00:37:03.390 --> 00:37:14.340 William Cheng: Okay, because your application owns it. And so therefore, they're only allowed to to to to to touch the free box now. So, typically the way this is done in the memory alligator is that we're going to keep them in a linked list. 267 00:37:15.390 --> 00:37:20.160 William Cheng: OK, so the lingo is sorted based on the you know the pace address of these three blocks. 268 00:37:20.340 --> 00:37:26.220 William Cheng: The smallest one. It's going to be at the beginning of the link lists and the larger ones going to be at the end of the link list, right. So, therefore, if you have free 269 00:37:26.370 --> 00:37:33.720 William Cheng: If you have to free box over here. I'm going to keep things that a linguist. There's literally says two elements. The first one is the first free blog and a second one, the second one. 270 00:37:34.050 --> 00:37:41.580 William Cheng: Okay. So inside the state of Georgia. I need to keep track of how big the block sizes. So this way when I try to allocate memory. I know where they fit inside of block or not. 271 00:37:42.990 --> 00:37:50.040 William Cheng: Okay, so we're gonna take a look at the two algorithm over here. One is called first fit right the first word means that when you call Malak 272 00:37:51.060 --> 00:38:00.150 William Cheng: 400 right so in this case you want to allocate 400 buyers. So what I will do is I will, I will do a linear search from the top of the list and then go all the way to the end of the list. 273 00:38:00.360 --> 00:38:07.470 William Cheng: The first block that I find that's big enough, I have to allocate memory out of that block. Okay, if I'm using the best fit our 274 00:38:08.040 --> 00:38:19.800 William Cheng: Memory alligator the memory allocation algorithm. So in that case, I have to visit every block. Find the one that's the best fit. Okay, so that's also in this case I'm going to find a blog that's closest to 400 and bigger than, bigger, bigger equal to 100 275 00:38:20.400 --> 00:38:28.170 William Cheng: Okay, so that will be the best fit algorithm or as I'm going to sort of take a look at these to our them and see how they how they compare against each other that 276 00:38:28.710 --> 00:38:39.900 William Cheng: First went around the the first word our them. Now, so, so, so this guy is for the first I'm going to call Malak okay 1000 guy. So if we're running the first remember 277 00:38:40.410 --> 00:38:52.830 William Cheng: The algorithm. What we do a search this list over here. The first one over here. Is it big enough. Well, it's 1300 bytes over here is bigger than 1000 so we are required to allocate 1000 bytes out of the 1300 bytes. 278 00:38:53.430 --> 00:39:03.360 William Cheng: Okay. So in this case, Robin. When we're done right the first 1000 bytes over here is going to get returned to our application. So the remaining free blog over here is going to be 300 bikes that are free. 279 00:39:03.900 --> 00:39:06.630 William Cheng: Now, so we're going to take the original free blog over here divided into 280 00:39:07.560 --> 00:39:16.770 William Cheng: Two parts. The first part of years 1000 miles long returns to the application and then the remaining free blog and not only have 300 by slap there. So, therefore, when we're done. It will look like this. 281 00:39:17.670 --> 00:39:25.680 William Cheng: Okay, so again the white part not belong to the application, they don't belong to the memory alligator and more because the memory alligator promise that they will keep their hands off. 282 00:39:26.040 --> 00:39:35.100 William Cheng: These blocks. Yeah. So now the first blog over here 300 is. I mean, we still need to sort in the same order. This one has a small address. So therefore, the first one is 300 and a second blocks 200 283 00:39:35.700 --> 00:39:39.780 William Cheng: That now the application is going to call Malik, you know, 1100 again. 284 00:39:40.290 --> 00:39:46.680 William Cheng: No, I guess not. Again, this one is different now like 1100 again I'm going to do a linear search. The first one is too small so they don't forget 285 00:39:46.890 --> 00:39:56.310 William Cheng: The second one over here is big enough so therefore, again, we're required to allocate 1100 block or 1100 bytes from the second block over here. So the remaining block size will be it's gonna be 100 286 00:39:56.730 --> 00:40:05.460 William Cheng: Okay, so therefore, when we're done, he will look like this that we still have two blocks lab. One of them is 300 bytes and the other ones 100 bytes. So again, they're linked together you know in class. 287 00:40:05.910 --> 00:40:09.750 William Cheng: Next, when I call Malik 250,000 this guy is available. Do a search 288 00:40:09.930 --> 00:40:18.420 William Cheng: The first blog over here is building out. We have to allocate out of the first block. So we're going to allocate 250 bytes under the first blog. The remaining free block size over here is going to be 50 289 00:40:18.600 --> 00:40:27.600 William Cheng: So therefore, when we're done. It will look like this. We have to free blogs left. The first one is small. It's 50 bucks law and the second one over here is bigger its own, but it's also 100 by song. 290 00:40:28.740 --> 00:40:38.070 William Cheng: Guys, so this is the this is the behavior of the first fit memory alligator. Okay. What if we use the best fit memory alligator because this guy is first we call now at 100 291 00:40:38.520 --> 00:40:45.570 William Cheng: We need to look at all the blogs that are big enough and find the one that's the closest fit. So out of these two brothers over here. Which one is a closer fit. 292 00:40:46.350 --> 00:40:52.080 William Cheng: Well, I mean, the one that's bigger than a bigger or equal to 1000 but it's closest to 1000 that will be the one right 293 00:40:52.200 --> 00:41:02.370 William Cheng: So if you outgrow the first one over here you can enter with 300 bytes let if he out of the second one over here, you're going to end up with 200 bucks left. So therefore, the second blog will be a better fit. So therefore, the best 294 00:41:02.760 --> 00:41:10.560 William Cheng: memory allocated, what will allocate you know 1000 out of the second blog over here. So the remaining friburgo size over here is gonna be able to 200 295 00:41:11.070 --> 00:41:21.600 William Cheng: There so so when you're finished doing that it will look like this. The first blog over here is still a 1300 bytes law and the second one over here only has 200 bucks left the 1000 bytes over here is returned to the application. 296 00:41:22.440 --> 00:41:33.930 William Cheng: Right now, you make exactly the same function call. I'm going to call Malik 1100 over here. So in this case, if there's only one block that's that fits right. So, therefore, that one is the best day. So I'm going to allocate one 297 00:41:34.320 --> 00:41:46.410 William Cheng: 111 hundred bytes out of the the first blog over here. The remaining blocks as over here. It's going to be 200 so therefore, when I'm done. It will look like this. They are there's going to be two blocks that each one of them is 200 by law. Okay, what I call Malik. 298 00:41:47.490 --> 00:41:50.940 William Cheng: What's going to happen what Malik is going to film that is going to return all 299 00:41:51.660 --> 00:42:00.450 William Cheng: The guys that were Malik failed to return all to say you know there's there's no way for me to allocate this memory. So in this case, when he called Malik 250 over here. So in this case, you're going to fail. 300 00:42:01.200 --> 00:42:07.560 William Cheng: Okay, so who is the winner over here first rate of best fit well I mean clearly in this example over here. Your first fit. 301 00:42:08.370 --> 00:42:13.440 William Cheng: As it turns out it works actually works better best fit. Even though it's the best fit the Indian doesn't really fit. Very good. 302 00:42:14.310 --> 00:42:24.570 William Cheng: Okay. But he said, wait a second. This is really not fair. I can come come up with another scenario we know when I allocate, you know, sort of a different sizes and different order. And I can demonstrate that best fit I should work better. 303 00:42:25.080 --> 00:42:32.850 William Cheng: Okay, and you will be right. Okay, they will be scenario that you can construct over here so you can actually show best fit. It's better than first it that 304 00:42:33.450 --> 00:42:44.370 William Cheng: So those which one is better. Okay. As it turns out, this is really, really old problem when people write like the first memory alligator. They have this argument to say which one is better. So this problem has been studied to death. 305 00:42:44.790 --> 00:42:53.820 William Cheng: As it turns out, most of the time. First, it actually works better than best fit. So what do I mean works better. That means that if you make a sequence of malice call. Okay. 306 00:42:54.210 --> 00:43:03.270 William Cheng: So in the end, you know, first we will last longer, right, because you know the first time you fail. What that the then in that case, you won't be able to run your program or your program. You have to self terminated because you don't have no memory. 307 00:43:04.350 --> 00:43:12.570 William Cheng: Okay. So as it turns off first hit usually wins. Why is that that. So if you look at this picture over here. Okay, actually sort of tells you why right here. 308 00:43:13.980 --> 00:43:24.300 William Cheng: Okay, so what is the difference between the first remember how can I best of memory allocated over here. Okay. So after you allocate two blocks of memory. You can see that first fit actually has, you know, memory blocks that are all different size. 309 00:43:24.660 --> 00:43:33.180 William Cheng: That if you add up the number of bytes over here. They're actually exactly the same, right. So if you add up all the memory over here first rate has 400 bikes available total and best fit over here is as 310 00:43:33.480 --> 00:43:44.070 William Cheng: Low as 400 bikes available total okay if you call Malik. Well, one more time for the best case scenario. Is there any way for you to combine these 200 bucks buys into A into a 400 bikes, blah. 311 00:43:44.880 --> 00:43:51.630 William Cheng: Well, there's no way for you to do that because you made a promise that you are not allowed to touch all these these other data that you have returned to the application already 312 00:43:52.200 --> 00:43:56.460 William Cheng: Okay, so therefore you're stuck with the situation over here and then then in this case you have to fail. 313 00:43:56.880 --> 00:44:03.240 William Cheng: Okay, there's no way for you to come by all these blocks, right. So if you look at the difference between the first way that best fit over here. One way to look at is 314 00:44:03.570 --> 00:44:08.760 William Cheng: If you look at the distribution of block sizes for first fat. What does it look like right for first word over here. 315 00:44:09.360 --> 00:44:20.670 William Cheng: Here is the block size right block size size over here. Right. They have a size of 100 so there's a size of 100 over here. They also have a size of 300 over here. Right. Each one of them has only one block. 316 00:44:21.270 --> 00:44:26.460 William Cheng: Okay, so this will be the distribution of block size. And here's a number of blocks that of that size. 317 00:44:27.330 --> 00:44:31.200 William Cheng: Okay, so this will be the picture for first fit. What about for the best. And what would that look like 318 00:44:31.980 --> 00:44:45.450 William Cheng: And so in this case the block size over here. We're only have one of them. That's 200 bite by bite size, but in this case the height over yo equal to two. So the number over here is going to be able to to one and two over here. So they'll have two blogs that have exactly the same size. 319 00:44:46.740 --> 00:44:54.990 William Cheng: Okay, so, so the picture over here basically saying that if you use the first word memory allocated, you're going to end up with more variation in the size of the blocks. 320 00:44:55.860 --> 00:45:05.220 William Cheng: Okay, why if you use the best fit that memory alligator. You don't have very, very you're going to end up with much smaller variation. So in this case, the amount of variation that you have these Dec zero 321 00:45:06.060 --> 00:45:14.130 William Cheng: So what is variation. So remember, one or two over here, you're calculating the standard deviation. The survey deviation is is a measure of how much variation that you have 322 00:45:14.430 --> 00:45:22.560 William Cheng: That the larger the variations are a couple of look like this. Right, this will be something where the biggest variation. So the curve as a smaller way as you all look like this. 323 00:45:23.610 --> 00:45:28.320 William Cheng: Okay, so the bottom line over here is that if you run first remember memory allocated for for for a while. 324 00:45:28.500 --> 00:45:37.530 William Cheng: You're going to the you know the the the free blogs that you have is going to have a wide range of variations. What if you run the best memory allocate over here, they're all about the same size. 325 00:45:37.710 --> 00:45:48.030 William Cheng: Okay, so therefore this case they will have a very, very small standard deviation. So in that case, if he if the next memory allocated than the next Malik that you call have a size of this this much. 326 00:45:48.330 --> 00:46:01.680 William Cheng: Right, if you have a big distribution over here there plenty of bra sizes over here was satisfied the request while for a while with a smaller DB for a small small center deviation when you make a big request over here, chances are none of the blog will satisfy the request. 327 00:46:02.910 --> 00:46:09.030 William Cheng: Okay, so this is the reason why the first remember memory alligator typically is going to perform better than the best memory alligator. 328 00:46:09.450 --> 00:46:16.650 William Cheng: Okay, there's another reason people like the first one number educator, as we saw before, right, the performance of the First Amendment alligator is going to be what 329 00:46:17.520 --> 00:46:29.850 William Cheng: Right. So when you try to look for a blog, you have to stop at the first block on the average, how many block. Do you have to traverse before you find a blog that's big enough. Okay, sorry, their inbox over here, right, this is only on the average, yeah. 330 00:46:30.870 --> 00:46:33.600 William Cheng: You're going to end up. Are you going to visit and over two blocks. 331 00:46:34.200 --> 00:46:40.980 William Cheng: Okay. So typically, you know, halfway through the bucket blow the blacklist over here, you're gonna find something that's big enough. Now what about for the best fit memory alligator. 332 00:46:41.580 --> 00:46:47.220 William Cheng: Best remember our kids in the best case in the worst case in the advocates, they always have to visit or inbox. 333 00:46:47.760 --> 00:46:51.690 William Cheng: Okay, so therefore the performance of the best from memory al Qaeda is also much much worse. 334 00:46:51.930 --> 00:46:58.140 William Cheng: Than the first remember alligator and also the distribution of the block size is going to be wrong, right, because they're all they're all about the same. 335 00:46:58.500 --> 00:47:05.100 William Cheng: So therefore, if you've tried to ask for block after block, chances are there's a there's a lesser probability that you can satisfy the request. 336 00:47:05.910 --> 00:47:16.110 William Cheng: Okay, so, so for both reason, nobody will implement the best memory alligator. So whenever we talk about Malcolm free we're thinking about the first fig memory alligator. Yeah. 337 00:47:17.760 --> 00:47:23.610 William Cheng: Alright so here again so so so that that's sort of the difference right best there is always better than the 338 00:47:24.150 --> 00:47:34.440 William Cheng: First had always beat the best fit you know memory alligator in terms of performance and also in terms of how long it's gonna last before you return a nobody will be returned. No, you have family there. 339 00:47:35.250 --> 00:47:41.010 William Cheng: There's also sort of have a problem with these memory alligator. It's called fragmentation. Those are the picture that we saw. 340 00:47:41.850 --> 00:47:47.640 William Cheng: I guess let's go back over here now. So over here, you know, this kind of situation that we're just know as fragmentation. 341 00:47:48.060 --> 00:47:58.290 William Cheng: Is a lot of it is that the memory is going to get fragmented into these all these little blocks over here. And in the end, we're going to end up with a little small blocks over here. And none of these pockets satisfy the request. That's what Malik for a fail. 342 00:47:59.460 --> 00:48:06.210 William Cheng: guys so, so this situation over here, right, when the memory is fragmented into tiny little pieces and you can really combine that and again the reason you're not 343 00:48:06.540 --> 00:48:10.920 William Cheng: Allowed to combine them. Is that because you made a promise you're not allowed to touch any of the other Memory Box. 344 00:48:11.310 --> 00:48:21.150 William Cheng: Guys. So in the end, going to end up with a lot of these tiny fragments over here. And if you think about it, no matter how you implement the memory allocate. Are you always going to end up with some sort of fragmentation. 345 00:48:21.930 --> 00:48:24.720 William Cheng: Okay. So as it turns out, there are two different types of fragmentation. 346 00:48:24.930 --> 00:48:36.720 William Cheng: What is called external fragmentation and that's the one that we saw over here. So the definition of external fragmentation is that the unusable memory is separated into small blocks and it's interspersed by allocating memory. 347 00:48:37.230 --> 00:48:47.580 William Cheng: There so therefore that's exactly the picture that you have over here, right. The I'm usable memory over here, you cannot use any of these memory because they're too small. And they are intersperse over here among the allocated blog over here. 348 00:48:47.910 --> 00:48:54.000 William Cheng: Okay, so where are you using first aid always, you know, using best fit. So in da. You got to end up with external fragmentation. 349 00:48:54.750 --> 00:49:07.860 William Cheng: That the other kind of fragmentation over here is called internal fragmentation. So in this case, the unusable memory is contained within the allocated region. Okay. So that sounds kind of weird, right. So inside the allocated memory over here, parts of it is actually unusable. 350 00:49:09.420 --> 00:49:22.200 William Cheng: Okay, so one of the ways that you can implement memory allocators is to say that in order for me to avoid external fragmentation. Why do I allocate every time somebody asked for something I always allocate memory in the increments of one kilobytes. 351 00:49:23.550 --> 00:49:33.960 William Cheng: Okay, so if I always, you know, allocate memory. You know, in the multiple or one kilobytes, then it will be impossible for me to have a memory bloggers 200 bikes long 352 00:49:35.310 --> 00:49:42.330 William Cheng: Okay, so therefore my smallest walk over here will always be at least one kilobytes long, okay. So in that case, even when somebody called mallards one 353 00:49:42.810 --> 00:49:47.460 William Cheng: Get somebody called Malik one the only one one bike when this is how many by so I have to allocate. Well, I have to 354 00:49:47.880 --> 00:49:53.520 William Cheng: allocate memory on the on the multiples of one kilobytes. So in this case, I'll allocate 1024 bytes over here, right. 355 00:49:53.700 --> 00:50:03.090 William Cheng: Even when somebody called Malik one. So in this case, I'm going to return a pointer. The point to a point that the first memory location over here. And as far as the application is concerned, only one file over here is usable. 356 00:50:03.600 --> 00:50:17.760 William Cheng: Okay, all the other 1023 bites are unusable. Right. So, this is known as the internal fragmentation. The unusable memory is contained within the allocated block and this block is one kilobytes law, only one bite is usable by the application. All the other ones are wasted memory. 357 00:50:18.930 --> 00:50:22.380 William Cheng: Okay, so again, which way which kind of fragmentation is better. 358 00:50:22.980 --> 00:50:34.470 William Cheng: Why, that's a very difficult question to answer. So again, Indians all the trade off. Okay, you could you could either have internal fragmentation, where you can external private fragmentation. Okay. So, yeah, you have to choose which one that you want. Yeah. 359 00:50:36.780 --> 00:50:39.030 William Cheng: So there's actually a good time to break our next time we're going to sort of 360 00:50:40.170 --> 00:50:46.800 William Cheng: Get now we eliminated best fit a memory alligator. So next time we're going to see the implementation of the first first memory alligator. Yeah. 361 00:50:48.240 --> 00:50:51.690 William Cheng: So we're gonna we're gonna see that in the part two of the of this lecture.