WEBVTT 1 00:00:01.770 --> 00:00:12.480 William Cheng: Welcome to lecture 19 so Colonel three, you have a little over a week to finish. If you have come from previous semester, don't look at them. Don't copy them best to get rid of it. 2 00:00:13.049 --> 00:00:24.540 William Cheng: It's important to recur know three FAQ and understand pretty much every all election material that has been covered so far I have also posted the week 10 discussion video 3 00:00:25.440 --> 00:00:34.830 William Cheng: So please also watch it, it will sort of, you know, show show you how do you know look at assembly code and how to get a hello program to work. Okay. 4 00:00:35.670 --> 00:00:44.760 William Cheng: That the grading guidelines. Oh, grade for submission. Make sure you verify your current submission. Some people didn't do that in Colonel to, again, this is very risky. 5 00:00:46.350 --> 00:01:00.480 William Cheng: If you don't, verify your submission to GDP assignment number three. And if you're stuck or have a hard time using GDP, you know, so you should ask questions, you know, come to office, our post the class Google group. The Colonel through recommended timeline. I'm 6 00:01:01.950 --> 00:01:10.740 William Cheng: You know the number didn't add up correctly before so I made a sort of a changes over here. The last one over here, I think. 7 00:01:11.280 --> 00:01:19.050 William Cheng: I wrote down this to be three days and four days. So if you add them all up. It's more than two weeks plus two days. So I need to reduce one of them by one day. 8 00:01:19.680 --> 00:01:30.570 William Cheng: So in Phase four I've changed that you should finish it in two days. So again, in this case, you're only getting a spin in it to work. And most of the bug is going to be in the end map system call so 9 00:01:31.830 --> 00:01:41.580 William Cheng: Taking two days to implement is probably reasonable now. Alright, so you are so, so, so, since we are, we sort of we finished about one week of Colonel three 10 00:01:42.180 --> 00:01:49.740 William Cheng: Which seven days. So you should be about finished phase one and phase two. So by the end of tonight you know your hello progress should be working. 11 00:01:50.010 --> 00:01:59.790 William Cheng: And then you should pass all the section B tests in the next three days and try to get for can work to work by by implementing do forth and also by implementing shadow objects. 12 00:02:00.210 --> 00:02:06.090 William Cheng: OK. So again, I'll give you three days for that. And then after that you run has been in it and then pass all the tests. Yeah. All right. 13 00:02:07.020 --> 00:02:11.460 William Cheng: If your question, you know, send me email, right, we need to continue to talk about device drivers. 14 00:02:11.940 --> 00:02:19.980 William Cheng: So last time we were here talking about the terminal device driver and we talked about the top part over here is going to be done in the threat contacts by the user. So I come over here. 15 00:02:20.400 --> 00:02:32.550 William Cheng: Make a racism call that then. Then again, all these input and output Q their producer consumer problems. So in that case 111 end of the producing consumer problem they have to wait in case the buffer is full day 16 00:02:32.880 --> 00:02:36.090 William Cheng: Similarly, for the input kill you. Also, you're going to use a threat context to handle it. 17 00:02:36.660 --> 00:02:41.940 William Cheng: So the question is about is that, what about the bottom over here, guys. Should you also use a threat context to handle it. 18 00:02:42.720 --> 00:02:54.000 William Cheng: There. So if you have three contacts to handle that maybe anyone wants to add to handle input Q and the other one that to handle the application. So in this case, if both of them are waiting for the both of them can actually go to sleep. 19 00:02:54.690 --> 00:03:00.420 William Cheng: Okay, but I don't know if you have noticed that inside. Inside our current assignment. How many Colonel thirsty, you have 20 00:03:01.320 --> 00:03:08.910 William Cheng: Okay, so once we turn on a device driver and we also notice that, you know, inside the colonel whenever you set a breakpoint. You still have very, very few Colonel threat. 21 00:03:09.180 --> 00:03:14.250 William Cheng: So it's really not the case that, you know, for every device driver. We're going to end up with two more, two more kernels. 22 00:03:14.670 --> 00:03:19.320 William Cheng: Because we start doing this if you have 20 device driver, you're going to end up with 40 different 23 00:03:19.770 --> 00:03:28.920 William Cheng: 40 different kernel threads. So in that case, you know, all the threads. Most of the time they're doing nothing. But again, you know what, you know, we should look for stuff inside the Colonel, you're gonna 24 00:03:29.850 --> 00:03:36.810 William Cheng: You're going to go through a list of Colonel threads, everything can can become more and more costly. Yeah. And also, every Colonel thread is going to have stack space. 25 00:03:37.440 --> 00:03:41.610 William Cheng: So. So the basic idea here is that inside the Colonel, we should have as few threads as possible. 26 00:03:42.600 --> 00:03:48.060 William Cheng: Okay. So, therefore, you know, to handle the bottom part of these producer consumer problem. We shouldn't use another threat. 27 00:03:48.540 --> 00:03:57.540 William Cheng: Okay, so in that case, how should we be doing it right. So, so the way we should be doing. It is pretty much the way I've been describing is to use the internal contacts to handle the data at the bottom over here. 28 00:03:57.840 --> 00:04:00.570 William Cheng: Yeah. So for example, when you send data to the output Q 29 00:04:00.960 --> 00:04:11.460 William Cheng: When do you remove a data, you know, from the alkalinity right is that the interrupt service routine. What you do is that you take one character out of the output you over here and then you send it to it, it sends a device, you start the next IO operation. 30 00:04:12.120 --> 00:04:18.060 William Cheng: Okay. So in this case, you know, all the output. The output you over here at the bottom there all handled inside the internal contacts. 31 00:04:18.270 --> 00:04:25.200 William Cheng: Similarly, when you press the key on the keyboard instead of internal contacts. Would you do that you go to read the key that was just press on the 32 00:04:25.530 --> 00:04:34.920 William Cheng: Phone or device. And then what you would do is that, again, you are the other end of the producer consumer problem. And this guy's you will produce you will add a character to the input. Q And it took you over here is 33 00:04:35.850 --> 00:04:42.240 William Cheng: The implication is full. Other. What you do is that you sent out a peep to tell the user that you know that the Input. Input useful. 34 00:04:42.930 --> 00:04:51.870 William Cheng: OK. So again, typically what's done is that at the bottom over here in order for us not to run too many external threats. We're going to start to try to sort of sort of handle them instead of interrupt contacts. 35 00:04:52.500 --> 00:05:00.180 William Cheng: Okay, so this way you know that things will be a little more efficient inside Colonel well alright 36 00:05:00.810 --> 00:05:10.200 William Cheng: So, so that's the other part is the context. The bottom cards handle. He's not to interrupt contacts. So what are the, you know, so what are the, the remaining issue over here. Okay, so 37 00:05:10.800 --> 00:05:19.350 William Cheng: There's another remains to be here is that, you know, food for the terminal driver is that the input character may needs to be processed in some way before they reach the application. 38 00:05:19.860 --> 00:05:24.000 William Cheng: Okay, I don't know if you have tried this one on one and two. What if you try to read from the keyboard. 39 00:05:24.810 --> 00:05:36.720 William Cheng: Okay, so if you try to read from the keyboard over here, you're typing on the keyboard. And then, and then there's something you will notice that you make you make a mistake, and then you use the backspace key the backspace key or key that you press on the keyboard. 40 00:05:37.320 --> 00:05:41.880 William Cheng: Okay, when you tell your application, try to read from the input. Do you see the backspace case. 41 00:05:42.540 --> 00:05:51.930 William Cheng: Where the answer is no. So somehow the basket base key they disappear from the input. So therefore, how, how could that be okay. I mean, the reason for that is that because because the Colonel. 42 00:05:52.530 --> 00:06:03.450 William Cheng: The Colonel is handling it so that you don't have to see all these backspace keys. Okay. So over here, it says the characters may be grouped into lines of tags subject as simple editing, such as backspace. 43 00:06:04.170 --> 00:06:11.700 William Cheng: And also if you're using a international keyboard. If you try to produce the character like this. There's the excellent at eat over here. So you have us a sort of 44 00:06:12.270 --> 00:06:15.210 William Cheng: The European keyboard, you will you will notice that the way you create 45 00:06:15.570 --> 00:06:24.450 William Cheng: This character is that you you press the single character and then you press the character and all of a sudden these two characters disappear and you get replaced by one of these characters over here. 46 00:06:25.140 --> 00:06:35.070 William Cheng: We're sort of God. If you are the application, you try to read a line attacks from the input. You will notice that you only see one character. A you don't see you know to character fall by backsplash and then followed by the other character. 47 00:06:35.700 --> 00:06:40.950 William Cheng: Okay, so therefore again. All these things are done, is that a carnal. Yeah. So what. So talk about you know how this is done. 48 00:06:41.250 --> 00:06:54.210 William Cheng: And also some application prefer to process all the characters themselves, including the backspace, including the Shift key right and if you're playing a sort of a terminal base game right even when you press the Shift key. You want to see the Shift key. 49 00:06:55.200 --> 00:07:00.720 William Cheng: Okay, so there are different kinds of application. They want to be able to control to say whether we want the colonel to be editing the input. 50 00:07:00.930 --> 00:07:07.560 William Cheng: Or you actually want to see all the characters. OK. So again, the all the, all these things have to be handled inside the sort of the terminal device driver. 51 00:07:07.800 --> 00:07:23.310 William Cheng: Yeah. And then finally over here character type at the keyboard needs to be echo back to the display. You see that in Colonel one right whenever you know whatever character you type the show up on the screen. The show up on the screen right away. Even if your kernel is completely deadlock. 52 00:07:24.960 --> 00:07:28.140 William Cheng: Echoing every key that you press a column to the screen so 53 00:07:28.590 --> 00:07:36.330 William Cheng: So, so why is it that when the colonel threads completely dead della you will still see this character showing up on the screen right so we're going to sort of see how this is done now. 54 00:07:37.020 --> 00:07:46.020 William Cheng: Alright, our first to solve the problem of 3am over here. So the idea here is that, you know, once you send the character to the application. There's no way for you to get it back. 55 00:07:46.590 --> 00:07:48.750 William Cheng: Okay. So for example, you know, if you sent you know 56 00:07:49.320 --> 00:08:01.410 William Cheng: Somebody type of access to either a single call followed by he if you send the single call to the application. There's no way for you to ask for it back. So, therefore, you know, you shouldn't really send data to the application, unless you assure that the either the 57 00:08:02.700 --> 00:08:12.120 William Cheng: Unless you're sure that you will never have to ask for it back. Okay. So when do you know for sure that that you don't have asked for a bath is when you press the Enter key. 58 00:08:13.020 --> 00:08:20.610 William Cheng: Okay, so therefore inside the cardinal, we need to actually have to buffers right before we said there's only one input buffer over here. And now we need to do to to divide them into two buffers. 59 00:08:20.850 --> 00:08:28.050 William Cheng: One of the buffer consists of lines that has been completed, which means that you have press Enter on and the other one is the one that you're currently working on 60 00:08:28.770 --> 00:08:40.800 William Cheng: Okay, so therefore this is, you know, so in this case the input here is going to buy into two. One of them is called a partial line cute partial IQ is good as it's going to be a bunch of character that you haven't press Enter on 61 00:08:41.340 --> 00:08:48.270 William Cheng: Okay, so in this case if you if you try to type the backspace key, then we can actually delete one of the character over here from the partial long queue. 62 00:08:48.540 --> 00:08:56.970 William Cheng: And once you press enter. We're going to copy that entire partial on cute over here and copy them over to the completed lunch you so the completed on cue. There will be lines attacks. 63 00:08:57.720 --> 00:09:07.680 William Cheng: Whereas I don't really know you have noticed that. Again you one by one and two, when you try to refund the keyboard. If you're typing. All right, and then you're in a responsible, the system call it doesn't return until you press enter. 64 00:09:08.880 --> 00:09:17.730 William Cheng: Okay, so you know where the resistance. Go to tried to do have your own show to call Caroline to get one last time, you're trying to get the complete Elia you are not actually working on the partial lot 65 00:09:18.360 --> 00:09:22.860 William Cheng: Okay, so as long as you use it as impressive presenter. You're resistant call is not going to return. 66 00:09:23.850 --> 00:09:35.190 William Cheng: Okay, and that's because, you know, instead of kernel, which we sort of have these two data structure. One is for the partial on cue that are subject to editing and the complete line Q, they are the one that need to be returned to the application one line at a time. 67 00:09:35.670 --> 00:09:44.100 William Cheng: Yeah. Alright, so, so, so that that's how you saw the first problem over here and to solve the second problem over here. 68 00:09:44.460 --> 00:09:57.180 William Cheng: Is that, you know, something you wanted to have all the characters in that case you need to have a way to tell they caught up in the system and say hey you know use, you know, use a to buffer for the for the input Q or use original version only has one buffer. 69 00:09:57.900 --> 00:10:08.970 William Cheng: Okay, so in that case you will make a system call. So I think you're working on, Colonel to you probably see a header file called FC MTL FC and te L. So, this is the file the file control. 70 00:10:09.420 --> 00:10:13.380 William Cheng: Okay, so in that case you can actually say, you know, for standard in I want to use a 71 00:10:13.620 --> 00:10:23.130 William Cheng: Sort of a single buffer or double buffer. So by making a system called over here. You can tell the colonel to say, you know, switch to a switch, a different way of handling these characters now. 72 00:10:23.700 --> 00:10:30.150 William Cheng: Number three. Over here is deal with equity right when the character is typed it should be displayed on to the screen so that you know that the colonel is alive. 73 00:10:30.720 --> 00:10:34.410 William Cheng: So as it turns out that this is actually a design decision different operating system go with different ways. 74 00:10:34.620 --> 00:10:43.110 William Cheng: For Linux and Unix system, what it will do is that as soon as you press the characters over here. The character is going to go into the partial line. Q What do you know what Linux and we 75 00:10:43.350 --> 00:10:49.710 William Cheng: What Linux and Unix system will do is that it will also copy this character and then put it into the upper buffer. 76 00:10:50.910 --> 00:10:55.380 William Cheng: Okay, so what it put into the elbow buffer. That means that, you know, pretty soon, this character is going to appear on the screen. 77 00:10:56.160 --> 00:11:04.440 William Cheng: Right, so that's why we need some Linux when you were inside Linux and Unix. Is that where you press the key will show up. Show up in the print up pretty soon. 78 00:11:04.890 --> 00:11:11.220 William Cheng: So I don't know if you have noticed that you warm up to you're running the simulation, you know, you're always running for a long time and you don't read from the keyboard. 79 00:11:12.000 --> 00:11:22.830 William Cheng: Right you know you're reading from the you know the info for you. Don't worry, from the keyboard. So in that case, you can actually type ahead, and then you start filling up all the, you know, all these bumper over here, your character actually start showing up on the screen. 80 00:11:23.370 --> 00:11:31.050 William Cheng: Is that okay, all right, because you know your program is really not processing these character, they start showing on the screen is sort of messes up the output. 81 00:11:31.650 --> 00:11:38.610 William Cheng: OK. So again, different operating system, a different philosophy for Unix and Linux to say, yeah, this is okay, you're going to mess up the awkward because they're typing ahead. 82 00:11:39.060 --> 00:11:40.920 William Cheng: For the Windows operating system. They actually don't. 83 00:11:41.580 --> 00:11:52.170 William Cheng: This is not allowed. Okay. Because they don't really want to want you to mess up the the output. So whether would do is that, you know, when the entire line get copy into the complete long queue at that time the entire line will actually 84 00:11:52.680 --> 00:11:57.990 William Cheng: Get, get a copy, you know, into the upper proper and then you will see that line and sometimes 85 00:11:59.220 --> 00:12:02.580 William Cheng: I think different version of whether they actually do different things. Maybe in the older version. 86 00:12:02.790 --> 00:12:12.180 William Cheng: Only when you read a line from the complete line to where you read into the application space at that time they will take all these characters and make a copy of that book you and all of a sudden you see the line appear on the screen. 87 00:12:13.020 --> 00:12:23.100 William Cheng: Okay, so for Unix and Linux user that's really weird because you know the UT keep typing the character. You don't see anything any character on screen until the applications that we didn't implement all sudden, they all show up on the screen. 88 00:12:23.820 --> 00:12:31.980 William Cheng: Guys are again different you know operating system. Make it they make different kind of decision, right. So, India, the user sort of feel that they are dealing with a different kind of operating system. Okay. 89 00:12:34.200 --> 00:12:34.620 William Cheng: All right. 90 00:12:36.420 --> 00:12:42.570 William Cheng: Okay, so, so, so let's talk a little bit about, you know, some device device dependent. So let's see. I want to handle the backspace character. 91 00:12:42.840 --> 00:12:47.880 William Cheng: Okay. And this is done in the device driver. So every device has a different device driver. Right. So, for example, 92 00:12:48.270 --> 00:12:57.450 William Cheng: So let's say that you will. I mean, today we're using different keyboards every keyboards manufactured by different different manufacturer and the different manufacturer little call with a different device driver. 93 00:12:57.720 --> 00:13:05.460 William Cheng: Okay, so the question is, you know, should it be the case that every device manufacturer, they have to write the info handling for handling backspace. 94 00:13:06.840 --> 00:13:09.870 William Cheng: Okay, well you type backspace. Right. You try to delete a character. 95 00:13:10.320 --> 00:13:17.610 William Cheng: No. Do you want every manufacturer to implement exactly the same code, right. That doesn't seem to me that doesn't seem to make sense. And also when you try to handle 96 00:13:17.880 --> 00:13:24.840 William Cheng: The, the XML eat over here. I mean, why do you make every keyboard manufacturer to write the code to handle you know to handle the handle accidentally 97 00:13:25.380 --> 00:13:29.520 William Cheng: Okay, it seems that part of the device driver code is actually device independent 98 00:13:30.120 --> 00:13:36.780 William Cheng: Okay. When you sort of handle backspace when you start to handle international character European a character over here, they should be handled in a device independent way. 99 00:13:37.650 --> 00:13:43.200 William Cheng: That. So typically, what's done instead of sort of operating system design is that they will they will actually divide 100 00:13:43.800 --> 00:13:50.700 William Cheng: The device driver into two parts. One is still called the device driver so that device driver over here will be dependent on the device manufacturer 101 00:13:51.030 --> 00:14:01.890 William Cheng: Okay all device manufacturer independence that they go into a different module inside the colonel and typically this is going to be referred to as a line. This one module and align this will monitor is the one is going to be editing all that tax. 102 00:14:02.400 --> 00:14:07.830 William Cheng: Okay, so therefore inside the Colonel, the picture look like this. Okay, so instead of only having one device driver. 103 00:14:08.040 --> 00:14:15.300 William Cheng: I'm going to divide it into two different module one is the line. This one module that's the one that on the application. That's the one that go on the complete line. Q. 104 00:14:15.480 --> 00:14:19.590 William Cheng: That's the one that own the partial IQ as you can actually draw a line, draw a line like this. 105 00:14:19.830 --> 00:14:30.630 William Cheng: Okay. So all these things belong to the same module, while the terminal device driver over here is to be used to talk to the specific terminology that's definitely that's manufactured that's manufactured dependent 106 00:14:31.470 --> 00:14:39.270 William Cheng: Okay, so every time when you press the key over here. The key is going to be received by the terminal device driver, because in order for you to go across the bus to talk to the terminal. 107 00:14:40.260 --> 00:14:49.740 William Cheng: Server to to to talk about it, talk to the keyboard controller that code needs to be web be done in the device dependent way. But once you copy that character inside. He 108 00:14:50.070 --> 00:14:56.070 William Cheng: copied the character into memory. Well, then it goes into a Landesman majority can be added into the partner line queue and then they're subject to editing. 109 00:14:57.390 --> 00:15:06.090 William Cheng: Okay, so that we're doing one level and direction so that the data come inside the terminal device driver are done by the by the manufacturer and the rest is over here. 110 00:15:06.330 --> 00:15:12.930 William Cheng: You know, so eventually the state. I get it is going to get delivered to the application. So again, by doing one leveling direction we're going to get a lot of flexibility. 111 00:15:13.740 --> 00:15:24.930 William Cheng: So, for example, what kind of flexibility that we're going to get right. We can keep exactly the same line. This is a module and we can you okay into operate among all the terminal device driver that are written by all the terminal manufacturers. 112 00:15:25.800 --> 00:15:32.130 William Cheng: Okay, and also the line. This one module over here. We can also switch to different kind of like this module. For example, if you want to align this 113 00:15:34.440 --> 00:15:43.230 William Cheng: module that only has one input buffer and not to have a buffer, you can simply make a system calls, they don't use this one. He was a different one. So he. So, for example, I'm going to call this one the bare bone like this module. 114 00:15:43.440 --> 00:15:48.330 William Cheng: That will be though that would deliver all the characters to the application. Okay. So in this case, right, if you're in 115 00:15:48.630 --> 00:15:57.570 William Cheng: If you're in the previous picture over here. All you have to do is to switch to a different line. There's no module and all of a sudden, now you're going to get that now you know your applications come get all the characters. 116 00:15:57.840 --> 00:16:06.030 William Cheng: Including the ship, including the backspace keys. OK. So again, using one level and direction it will make this implementation and really flexible. Yeah. 117 00:16:07.680 --> 00:16:21.450 William Cheng: All right, so, so again, typically typically Unix or Linux system. They sort of distinguish between you know the different lenses module and also you can sort of see that we can do plug and play Roby is different kinds of like this module, maybe for, you know, 118 00:16:22.770 --> 00:16:27.570 William Cheng: Maybe you can actually have a different lenses with module for handling Chinese character maybe another one for handling. 119 00:16:28.230 --> 00:16:37.770 William Cheng: You know, some some other languages you know that have, you know, multiple key combination of the January characters wrestle again once you start having those kind of stuff. 120 00:16:37.950 --> 00:16:46.200 William Cheng: Different ways of editing, you can actually just simply use a different light. This with module and they can mix and match and they're all interoperate with any kind of people keep what device driver. 121 00:16:46.650 --> 00:16:55.290 William Cheng: Guys, and this guy is going to end up a lot of combination, they all work with exactly the same architecture that. So again, using one level interaction. It's a really good idea. Sometimes 122 00:16:58.980 --> 00:17:07.860 William Cheng: Right. Are the next question over here is that, you know, where do you actually put all these module. Right. I mean, the previous picture over here right applications is right here. The opportunities right here. 123 00:17:08.310 --> 00:17:14.790 William Cheng: On the line. This one module at the terminal driver. They all seems to be very clear. They should stay. They should stay inside, Colonel. 124 00:17:15.420 --> 00:17:23.760 William Cheng: That. So the question is that, you know, should they actually say instead of current user any place else to put all these modules. Okay, can these modules go into the user space. 125 00:17:24.540 --> 00:17:36.390 William Cheng: That's what the answer is maybe. Right, we need to look at the possibility over here. So every time when you think about a module inside Colonel, you need to sort of think about as well, maybe, you know, if I put it inside a user space, then it will be better it will it will be better. 126 00:17:37.650 --> 00:17:43.200 William Cheng: If you put a module inside the user space or every time. Will you try to change that module, you don't have to change the Colonel. 127 00:17:43.830 --> 00:17:49.320 William Cheng: Okay, because once you change the Colonel, you're gonna affect all the other component inside the car and also making the changes that occur was actually a pretty big deal. 128 00:17:49.680 --> 00:17:53.550 William Cheng: Okay, so therefore you need to consider the possibility of moving inside the carnal. Yeah. 129 00:17:53.850 --> 00:18:02.610 William Cheng: Alright, so the three places to put a module number one is into is that a colonel there. So instead of kernel has the advantage of being fast, right, because in this case, you don't have to actually 130 00:18:02.910 --> 00:18:08.520 William Cheng: You know transition from from Colonel to user space and then transition back. So in that case it will be it will be really slow. 131 00:18:09.000 --> 00:18:13.050 William Cheng: So keeping things in carnival is going to be sort of the simplest thing to do. 132 00:18:13.590 --> 00:18:20.880 William Cheng: But, but the problem is that if you're if you're inside a kernel when you start to make changes, you're going to end up changing. You've got to add about the next version of the Colonel. 133 00:18:21.660 --> 00:18:26.130 William Cheng: Okay, so in that case, they become a sort of a management headache want to manage all different kinds of Colonel versions. 134 00:18:26.820 --> 00:18:32.490 William Cheng: Alright, so another solution over here is to put that kernel module inside a user space program. 135 00:18:32.850 --> 00:18:39.360 William Cheng: Okay, so we're going to create a separate user space program and then you're going to put that you're going to eat. You're going to put that module inside our user space program. 136 00:18:40.020 --> 00:18:47.700 William Cheng: Okay, so in that case, you know, for, for example, you can take the line just one module we hear you move into the user space but you keep it instead of different process. 137 00:18:47.880 --> 00:18:52.740 William Cheng: So in this case, when your application needs a character from the line. This will module. What does that have to do 138 00:18:53.310 --> 00:18:59.190 William Cheng: Well, so in this case he needs to ask another application for the data. So in this case, you were doing inter process communication. 139 00:18:59.790 --> 00:19:09.690 William Cheng: Okay, we haven't really seen any kind of interpersonal communication in that class where we're not going to talk about too much about it. Right. But we're sort of seeing one way or process communicated with each other and that's using a Unix pipe. 140 00:19:10.440 --> 00:19:20.100 William Cheng: OK. So the basic idea what we hear is that if I had actually establish a Unix pipe, you know, between these two application. Well, then in that case, I can actually send requests we could get a response back. And this will can get 141 00:19:20.340 --> 00:19:22.500 William Cheng: The data from this other user straightforward. 142 00:19:23.370 --> 00:19:28.950 William Cheng: Okay, but it was still every time when you press the key is going to go into the terminal and then it's going to go into the user space program. 143 00:19:29.160 --> 00:19:40.500 William Cheng: And then the application needs to get the data. So again, the pipe is is object is that occur, know that the application has to go inside the colonel to get the pipe and then get the data from the application. So all these things also seems really really so 144 00:19:41.370 --> 00:19:48.120 William Cheng: Yeah, so, so this is the possibility. Right, so, so, so, so, so, so, so we need to sort of think about it, what it doing this makes sense. 145 00:19:48.990 --> 00:19:54.960 William Cheng: And also if the performance going to be so poor, and then in the end we can really do it this way. Now the third operation. 146 00:19:55.440 --> 00:20:04.650 William Cheng: Possibility is to put that module inside the inside a library routine, for example, make it part of the C library. So this way. If you try to, you know, talk to the terminal. 147 00:20:04.950 --> 00:20:13.950 William Cheng: You know, then there was that as you compile your program we pull from the linking phase. We're going to copy the code for the line. This little module from the C library into your executable program. 148 00:20:14.550 --> 00:20:17.670 William Cheng: Okay, so in that case it will sort of look like this. So let's take a look at this right 149 00:20:17.910 --> 00:20:22.080 William Cheng: Inside the application over here. We're going to have the line display module inside the application. 150 00:20:22.260 --> 00:20:30.060 William Cheng: If you have another application over here that application inside this address space. It will also have the line. This module. So every application will have a line. This one module. 151 00:20:30.570 --> 00:20:38.580 William Cheng: Okay, so what's going to be the problem with that. Right. So when you get a character over here from the terminal device driver, you need to send it to the lenses for module, which one do you deliver to 152 00:20:40.230 --> 00:20:48.360 William Cheng: Okay, it's not very clear, how do you actually control this right so. So again, the problem is over here is that if you send the data to the wrong line. This one module. There's no way for you to get it back. 153 00:20:48.810 --> 00:20:54.630 William Cheng: Right, the operating system kernel can't say that. Oh, you know, the character. I just sent you. Can I have a bad. Well, there's nowhere for you to do that. So you so 154 00:20:54.780 --> 00:21:02.070 William Cheng: So, so, you know, in the textbook. They say that, you know, putting the lightest will module inside the inside a liability makes it very, very difficult to share 155 00:21:02.520 --> 00:21:06.030 William Cheng: Okay, because when you make a mistake, there's really no way for you to get the data back. Yeah. 156 00:21:06.840 --> 00:21:14.010 William Cheng: All right, so these three different choices. So the drivers. So the drivers can also go into the user space program, people have actually tried that. 157 00:21:14.340 --> 00:21:20.340 William Cheng: And it turns out that every time when they do that, they find out that, in the end, the speed is going to be very, very slow. 158 00:21:20.640 --> 00:21:27.750 William Cheng: So the, so you mentioned they all put it, put it back inside of Colonel, right. So yeah, why do people want to put a device driver into the user space program. 159 00:21:28.650 --> 00:21:37.350 William Cheng: That the one. The reason is that, you know, in 1990s Microsoft Windows has a really bad reputation because the, you know, you probably heard of the blue screen of death. 160 00:21:37.710 --> 00:21:40.230 William Cheng: Okay. If you don't, you get a blue screen Microsoft window, you know, 161 00:21:40.620 --> 00:21:49.860 William Cheng: Died. But if you look at the the error message is very, very carefully, you will notice that most of the time when you get a blue screen of death in Windows. It's because it dies inside the device driver. 162 00:21:50.190 --> 00:21:58.770 William Cheng: Data device driver in the old architecture, they're part of the Colonel. So in that case, when a device driver die your kernel died. Okay, why did you device driver always die. 163 00:22:00.060 --> 00:22:07.560 William Cheng: What because they're implemented by the hardware, you know, the bye bye bye with the horror manufacturers. So, you know, maybe, you know, when you go get a sort of this internship. 164 00:22:07.980 --> 00:22:12.990 William Cheng: Instead of company. And then they are doing device driver, so maybe they will have summer interns actually implement the code over here. 165 00:22:13.260 --> 00:22:22.800 William Cheng: And somehow the meeting through production code in the head, their questions are right. I mean, of course, some of the summer interns are really talented, they don't make mistake like that. But once in a while, you know, these hardware manufacturer 166 00:22:23.280 --> 00:22:27.720 William Cheng: You know, they will write the wrong code and when their code dies they bring down the entire operating system kernel. 167 00:22:28.170 --> 00:22:34.050 William Cheng: Okay, so therefore it will make good sense to move the device driver into the user space and this guy is what happened on a device driver die. 168 00:22:34.530 --> 00:22:43.560 William Cheng: Or get into one device rather die. You just cannot use that device, you will you will you will not be able to use that device, but everything else he's had a colonel, they're still running. 169 00:22:44.880 --> 00:22:49.470 William Cheng: Okay, so therefore there's a major divide major that is to move the device into the user space program. 170 00:22:49.770 --> 00:22:54.570 William Cheng: Because you know when it crashes, you know, everything else is still going to be okay. Yeah. But the problem again when you 171 00:22:54.810 --> 00:23:04.350 William Cheng: Once you move into the user space program. It's more like you're going to have now have a user process that will keep the device driver. And then, again, it's going to be really so because your application has to go inside of kernel and then go up. 172 00:23:05.130 --> 00:23:11.760 William Cheng: Up call together data and whenever you press a key. We also need to make it up call to to put data into this user space process. 173 00:23:12.240 --> 00:23:16.980 William Cheng: Then, so again, typically this is not done, because it's too slow. Right. But, you know, people keep trying. 174 00:23:17.220 --> 00:23:25.230 William Cheng: Opportunities and people keep trying to get this job because in the end, you can get this to work, it will be really great because, you know, you don't have to worry about the right driver dying anymore. 175 00:23:25.830 --> 00:23:30.660 William Cheng: Yeah, I mean, of course, MAC, MAC individual apple, they took a complete different approach. 176 00:23:31.230 --> 00:23:37.350 William Cheng: What they do is that, you know, for the device driver. Everything has to be certified by Apple. So Apple is going to test the heck out of it. 177 00:23:37.710 --> 00:23:46.830 William Cheng: To make sure that they're perfectly compatible. You know, before they release into production code. Well guys are again different company has different you know philosophy and end up with different kind of reputation. 178 00:23:48.000 --> 00:23:54.810 William Cheng: So similarly, like this one module, you can keep it inside of Colonel, you can keep inside the user process. Again, it turns out to be 179 00:23:55.110 --> 00:24:04.920 William Cheng: Too slow and if you keep it. Is that a library routine. So in that case, again, why is it deliver the character. There's no way for you to to to to to get it back so so again it's going to make the, you know, 180 00:24:06.720 --> 00:24:10.410 William Cheng: It's going to make the terminal much more difficult to share that 181 00:24:11.580 --> 00:24:19.050 William Cheng: Alright. So in the end, typically the most valuable for solutions to keep them only inside a carnal. Okay, even though it would be nice to be in user space. 182 00:24:21.300 --> 00:24:27.900 William Cheng: Today, we don't have terminals anymore. Right. So terminal. We saw the VT 100 terminal. We don't have like that, you mean when you try to run the terminal program. 183 00:24:28.440 --> 00:24:36.150 William Cheng: On maternal Hawaiian use a fake terminal. Okay, it's just a software that sort of looks like a terminal 80 characters across 24 you know rolls over here. 184 00:24:36.630 --> 00:24:45.810 William Cheng: But in this case, it's not a terminal. So, and this guy was sort of cold call them a pseudo terminal right so what a pseudo pseudo means that it's it's not the real thing is something that's fake 185 00:24:46.830 --> 00:24:54.870 William Cheng: So, you know, so the device driver that we're going to be using for our terminal program is going to be something called a pseudo terminal right so let's take a look at, you know, 186 00:24:56.250 --> 00:25:03.570 William Cheng: Take, take a look at what that is. And also you know where you want your terminal, you are using a graphical display. Okay, so, so, so in this case. 187 00:25:03.930 --> 00:25:11.700 William Cheng: The graphical display runs a program called the window manager. The window manager is going to manage all the resources on the screen right so we're not doing character graphic. 188 00:25:12.060 --> 00:25:15.990 William Cheng: Character based graphics anymore. We're actually doing bitmap graphics that 189 00:25:16.590 --> 00:25:21.660 William Cheng: So, so in this case the window manager, as it turns out the window manager is actually a user space program. 190 00:25:22.050 --> 00:25:28.740 William Cheng: Because they'll kind of similar to what we said before, whenever you press the character you know your keyboard. The character. Actually, it's going to go into the window manager. 191 00:25:29.370 --> 00:25:34.440 William Cheng: Okay, but we just said, hey, this is not very, very efficient. So basically what happened is that for the big map graphic display. They sort of 192 00:25:34.830 --> 00:25:43.110 William Cheng: Took an approach that somewhere in between. Putting the entire module into the user space, you know, versus, you know, just putting some information inside the user space program. 193 00:25:44.040 --> 00:25:50.790 William Cheng: So the idea here is that you're going to have a window manager that manage all the resources on the screen, you know, for example, where the windows are 194 00:25:51.120 --> 00:26:01.230 William Cheng: There so you're on the display over here every application has has many, many windows right every windows, a rectangular area. So here's one application. Here's one application. And then you can also move the mouse. 195 00:26:01.440 --> 00:26:07.800 William Cheng: Well, you move the mouse over here. When you move the mouse every time, will you move the mouse, even by a little bit, it's going to generate interest. 196 00:26:08.430 --> 00:26:14.880 William Cheng: Okay, so we're going to turn right you know Rob over here and there will generate event. So the event says, you know, you move the mouse dx empty. Why does x 197 00:26:15.150 --> 00:26:25.770 William Cheng: Y. So, so this will organize your is that you move to the right by two pixels, you move to the leper or not by two pixels by two units or whatever. So all those measure what will actually get absorbed by the window manager. 198 00:26:26.220 --> 00:26:33.060 William Cheng: Okay, and what the window manager over here was, what will be will be that it will use that information and also depends on the sensitivity setting on your mouse. 199 00:26:33.240 --> 00:26:40.140 William Cheng: It will actually move your move your pointer, you know, with the point on the screen by a certain amount to the right to the left to the top to the bottom. 200 00:26:40.950 --> 00:26:49.590 William Cheng: That's what this is why using software you can control how sensitive your mouse is so when you move the same distance with your physical mouse that the distance on the screen will move a different amount 201 00:26:50.070 --> 00:26:54.570 William Cheng: Okay, because the window manager is actually in control, putting the pointer onto the screen to move to different places. 202 00:26:54.810 --> 00:27:04.530 William Cheng: There and also every time you press the key over here is going to go into the window manager is over here. What I typed the key a over here. So the window manager is going to be the one that owns the character K 203 00:27:04.770 --> 00:27:12.330 William Cheng: Character a so the window manager needed to deliver this character to an application. Okay, so where does it actually deliver this character to the application. 204 00:27:12.780 --> 00:27:16.530 William Cheng: Okay, so the way it's done is that the window system is that, you know, so, so, so over here again. 205 00:27:16.710 --> 00:27:24.870 William Cheng: Though you know on the screen. There's lots of windows over here. The window manager. It knows exactly where every window is so when you use your mouse and when you move your mouse over here we're going to move the 206 00:27:25.110 --> 00:27:35.940 William Cheng: cursor around and then again the window. Know exactly the window manager know exactly where the cursor is right. So when you press the mouse button right the most typically has, you know, I don't know. It depends on whether you are using a Mac or not. 207 00:27:36.390 --> 00:27:47.550 William Cheng: There, there are a few buttons over here. When you click a button. The also generated interrupt so the window manager, I should know exactly what your pointer is and then what he would do is that it will compete to figure out exactly which which window is off. 208 00:27:48.330 --> 00:27:55.620 William Cheng: That the window. That's all over here when you present when you press your mouse button that window become the input focus where what is the 209 00:27:56.070 --> 00:28:04.980 William Cheng: Focus. The input focus is that from this point on every key that you press on the keyboard is going to get delivered to the application that own that that on that window. 210 00:28:05.670 --> 00:28:14.610 William Cheng: Okay. So over here, if this would this why you know if this is the input focus over here, right, that when you type the achy achy will be delivered to the application that on that window. 211 00:28:15.090 --> 00:28:21.540 William Cheng: Okay, so this way when the application, you know, coco, coco, coco rely know Corey. In that case it will get a character. 212 00:28:22.920 --> 00:28:24.660 William Cheng: Okay, so this is actually done so. 213 00:28:27.090 --> 00:28:35.400 William Cheng: So yeah, all the characters over you're going to the window manager and then you know the window manager needs to communicate with your application. So in this case, how do you can get it with the application, right. 214 00:28:35.640 --> 00:28:39.210 William Cheng: The basic it over here is that we don't really want to change the structure of the application. 215 00:28:39.420 --> 00:28:50.340 William Cheng: We want to sort of make it look like what it was before. So as far as the application is concerned, it should be talking to align this with module, but the line is we're module instead of talking to a terminal. Now, it needs to talk to a pseudo terminal. 216 00:28:50.910 --> 00:28:56.340 William Cheng: OK, so the sooner terminal is going to be the one that sort of faith that these characters over here instead of coming from the real device. 217 00:28:56.550 --> 00:29:04.080 William Cheng: It will come from a pseudo device. And what is the pseudo device right the sewer device on the other end is connected to an application which is the window manager. 218 00:29:04.980 --> 00:29:11.370 William Cheng: Yeah, so the pictures that look like this. So over here I guess this is all the stuff that I mentioned before guys are the future look like this. 219 00:29:12.120 --> 00:29:20.940 William Cheng: Okay, my application over here when you try to read the day that you will talk to the land. This module and the land is a module is going to get the data, data coming from the pseudo terminal slave over here. 220 00:29:21.120 --> 00:29:29.670 William Cheng: As it turns out, tip the typical architecture over here is that instead of using only one terminal, they will divide into two different terminal one is called the master. The one is called the safe. 221 00:29:30.000 --> 00:29:40.710 William Cheng: So again, these are really old terminology over here, the master the pseudo terminal master is the one that get the character from the window manager and the pseudo terminal said, well, we're here is the one that that's connected align this one module. 222 00:29:41.520 --> 00:29:55.440 William Cheng: Okay, so therefore, again, Robin is is that whenever you press the character. So that's it I press the character. A on the keyboard over here, it will go into the input queue for the window manager. The window manager will get this this character over here, figure out who is the owner 223 00:29:57.060 --> 00:30:04.530 William Cheng: Of the window. That's the current input focus. Okay. And then what do we do, is that it was search for the other the device driver over here. 224 00:30:04.740 --> 00:30:12.780 William Cheng: Find out which one it is. And then they will send this character. He will be here, down the pseudo terminal master and the senatorial national via will forward this character to the civil technical side. 225 00:30:13.020 --> 00:30:16.200 William Cheng: And now the pseudo terminal slave will act just like the terminal device driver. 226 00:30:16.710 --> 00:30:25.380 William Cheng: Okay, so this way when your application and make the racism call you caught you talk to the line. This one module. If this character is already in the completed line queue and then it will get delivered to the application. 227 00:30:27.210 --> 00:30:33.780 William Cheng: Okay, so in this case the character over here come into the window manager, going to the user space program and then the user space organs over here is going to 228 00:30:34.140 --> 00:30:39.900 William Cheng: You know, send data to the pseudo terminal master. Okay. How does the window, the window manager send data into the pseudo terminal master. 229 00:30:40.260 --> 00:30:47.190 William Cheng: Was going to use the right system call right because over here, this application. If you want to write data to a device you make the right system calls them coming set of code over here. 230 00:30:47.430 --> 00:30:57.480 William Cheng: And then this is going to go across the cinema almost safe and that is going to get picked up from over here and eventually get delivered to the application, right, but I just mentioned the I just mentioned already, this is going to be slow. 231 00:30:58.530 --> 00:31:06.660 William Cheng: Right, because we need to go into the other the other the user space over here and then come down into colonel. So this path over here on the left hand side is going to be wasted. 232 00:31:06.870 --> 00:31:11.220 William Cheng: On the right hand side. Well, this is still the typical way where you tend to get good data. So, therefore, that parse data say 233 00:31:11.640 --> 00:31:17.160 William Cheng: Okay. So now every character that you press is going to go into the user space and companies that are current all so you're going to be slow down. 234 00:31:17.790 --> 00:31:22.770 William Cheng: Okay, but as it turns out that, you know, since Window Manager is one program. The sheer by all the application. 235 00:31:23.040 --> 00:31:30.450 William Cheng: People who will window manager can actually actually optimize the heck out of this co. So in the end, you know, this particular code and not to be pretty efficient. 236 00:31:31.380 --> 00:31:39.090 William Cheng: Manner. So if you want to do this for every application. And that's going to be sort of a very difficult thing to do, but if you do this for only one application, you can actually optimize the heck out of it. 237 00:31:39.360 --> 00:31:48.930 William Cheng: So in the end, the performance will be acceptable and today when you are using your laptop or your desktop, you're always using the window manager, we can actually see that this particular performance is actually not so bad. 238 00:31:49.620 --> 00:31:58.650 William Cheng: Okay, so of course if you get the data directly over here, you know, then, then it's gonna be a little faster. Right. But in this case, even when we go through the window manager to perform is also very 239 00:31:59.700 --> 00:32:00.600 William Cheng: Also very reasonable. 240 00:32:01.350 --> 00:32:07.200 William Cheng: But what about we instead of this split character onto the screen, right, you want to set up a principal officer screen again. You do exactly the same thing. 241 00:32:07.380 --> 00:32:15.390 William Cheng: My application problem will make the racism call will send data to the line. This one module, the line doesn't know how to do was use the output Q send data to the pseudo terminal said 242 00:32:15.570 --> 00:32:23.850 William Cheng: That was sent to a sort of terminal master over here. The window manager over here need to make the system called together. Then over here. And then what he will do is that it will display down to the screen. 243 00:32:24.480 --> 00:32:31.680 William Cheng: OK. So again, the window manager over here will be calling me and then it will block, because normally you don't have anything then that in this case there's no character. 244 00:32:31.980 --> 00:32:40.770 William Cheng: Inside a pseudo terminal master. So, the system will get blocked and eventually when you send somebody over. You're going to wake up the window manager or one of the settings out the window manager and then the character will get display on the screen. 245 00:32:41.760 --> 00:32:52.830 William Cheng: But I finally over here. And if I want to see all the characters over here, right, again, you know, since I'm using multiple levels of indirection over here. I can also simply switch lines as a module. 246 00:32:53.130 --> 00:33:01.080 William Cheng: To be the bare bones, and this is a module over here and then again I will be able to receive all the characters I saw all the backspace key that you press is going to come out exactly the same way. 247 00:33:02.040 --> 00:33:08.430 William Cheng: Okay, so that comes to the final question. Why do you bother to use a pseudo terminal mastering the pseudo demo say why don't you combine them into a single 248 00:33:09.930 --> 00:33:16.260 William Cheng: Into a single a pseudo terminal, you know, device driver. OK. So again, this is done in the early days inside 249 00:33:16.740 --> 00:33:22.020 William Cheng: Inside a UNIX system. So what happened is that you can actually divide them into two parts over here. 250 00:33:22.320 --> 00:33:30.180 William Cheng: That I can actually have the right hand side over here on my application is running on a different computer. Okay. On the left hand side over here. There's gonna be my laptop. 251 00:33:30.510 --> 00:33:38.460 William Cheng: Okay, I don't know if you have actually Secure Shell, too. So there's a machine called Viterbi one B2B the it 252 00:33:38.970 --> 00:33:49.260 William Cheng: RPI dash SC F1 usc.edu. There's also a B2B, so you have to use it. I do. I think all you know all the grass doesn't have access to all these machines. 253 00:33:49.680 --> 00:33:53.130 William Cheng: So you can secure shout to that, then on the right hand side over here up, it can 254 00:33:53.880 --> 00:33:59.910 William Cheng: It can be running your login show on one of these machines over here on the left hand side over here will be your laptop or your desktop. 255 00:34:00.330 --> 00:34:04.110 William Cheng: That. So again, in this case, the only thing that we cut over here. It's going to be the connection. 256 00:34:04.500 --> 00:34:14.040 William Cheng: Between a pseudo terminal mastering a pseudo terminal state a slave over here. So before, if we're inside the same kernel. This will be a direct connection instead of curl. You can actually just you know right to a buffer the 257 00:34:14.640 --> 00:34:19.890 William Cheng: Directly. Now this connection at the bottom over here will be replaced by network connection. 258 00:34:20.730 --> 00:34:25.500 William Cheng: Okay, so if you replaced by another connection. I mean, almost work exactly the same way when the data come 259 00:34:25.710 --> 00:34:32.220 William Cheng: From the window manager into the pseudo terminal. Now you're going to send data over network connection and reach the pseudo terminal stay asleep on the different machine. 260 00:34:32.490 --> 00:34:42.660 William Cheng: Some older when your application over your try to write it on to the screen you will go into the pseudo terminals day and then it will travel through a network connection and travel across the internet and then it will receive by a window manager and then it will display. 261 00:34:43.200 --> 00:34:51.870 William Cheng: Swing that so again we can see the power of one level in the ratchet. If you do this one level of indirection over here, we actually we're going to be able to decouple 262 00:34:52.350 --> 00:35:06.870 William Cheng: You know, the, the, you know the the the terminal application, which is running on the client versus, you know, the laptop, which is running as a terminal server that actually has a display server that displaying all the characters on screen. 263 00:35:08.250 --> 00:35:13.290 William Cheng: Okay, so this is done in the units, very long time ago. So later on. So Windows actually late coming into the game. 264 00:35:13.830 --> 00:35:23.250 William Cheng: So I think the windows in the in 2000 or something like that. They finally come up. Come up with a specialized server, they called the terminal server just to implement this particular feature. 265 00:35:23.670 --> 00:35:32.250 William Cheng: Okay, but because of the UNIX architecture, they sort of have this feature, a long time ago when they started people start playing with the X Window System X Window System. That's how things are connected 266 00:35:32.910 --> 00:35:42.000 William Cheng: Okay, so therefore you know so. So ever since then I think even in the 80s when sort of people are playing with the X Window System. 267 00:35:42.420 --> 00:35:52.050 William Cheng: UNIX is always have, you know, machine. They're sitting across an hour and you can run one program on one machine. I have the display column to the machine okay because they have all these very flexible, you know, architecture. 268 00:35:55.140 --> 00:36:01.140 William Cheng: So, so that's all I want to say about the terminal our device driver. The next thing I wanted to briefly talked about as a network. 269 00:36:01.440 --> 00:36:11.280 William Cheng: device driver. So again, then there will be my job is very similar to the terminal device driver, because the way that you talk to a networking devices that you know from the users perspective you open you read and write and you close 270 00:36:12.000 --> 00:36:18.780 William Cheng: Okay, so therefore the interface is going to be exactly the same. So what is different about network indication that so so over here, there 271 00:36:19.500 --> 00:36:28.110 William Cheng: Are two things. Number one is that performance is crucial for network communication. I mentioned before, the network, you know Rob is very, very higher level interrupt Mohinder of level. 272 00:36:28.530 --> 00:36:32.010 William Cheng: Because you know when you start to get data from across the network. You need to do a very, very quickly. 273 00:36:32.490 --> 00:36:36.570 William Cheng: Okay, while the terminal is, you know, interrupt level three or some of that is the slowest thing. 274 00:36:36.900 --> 00:36:45.240 William Cheng: Because you were typing on the keyboard to display on the manager. So in this case, the speeches that have to be very fast that so performance is crucial in telecommunication right 275 00:36:45.540 --> 00:36:52.260 William Cheng: If you don't serve as and they were very, very quickly, what data is going to arrive across the internet is going to wipe out the data on the device controller. 276 00:36:52.470 --> 00:37:02.220 William Cheng: So in that case, you know, so. So in that case, what happened is that the data needs to be transmitted from all the way from across the internet. So in that case is going to be very, very expensive, then 277 00:37:03.630 --> 00:37:06.240 William Cheng: The second problem with the network communication is that 278 00:37:06.600 --> 00:37:12.420 William Cheng: When the data get travel across know there's actually a lot of data they could travel over. So for example, when you're done downloading 279 00:37:12.690 --> 00:37:25.650 William Cheng: A one 216 point four. It's 1.5 gigabytes data over here. They're coming across the internet, very, very high speed. Right. So in that case, you know, if you're if you're inside a colonel, you can end up with a lot of data. If you keep copying data you're going to run out of kernel memory. 280 00:37:26.820 --> 00:37:35.880 William Cheng: Okay, so one of the important route over here is that inside the Colonel, you know, the rule is that a corner over here is for networking is that you are not allowed to copy. Copy buffers. 281 00:37:36.510 --> 00:37:41.580 William Cheng: Okay, because if your copy buffers and these buffers, a huge pretty soon you're gonna run out of pressures kernel memory. 282 00:37:42.450 --> 00:37:49.830 William Cheng: Okay. So this guy is, what do you have to do, right, if you're not allowed to copy data. What do you, what do you copy or a copy pointers. Okay, so therefore inside the Colonel. 283 00:37:50.160 --> 00:37:55.680 William Cheng: When you start to deal with the network interface device driver, you're going to end up using the different kinds of data structure. Yeah. 284 00:37:58.170 --> 00:38:06.990 William Cheng: So, and, and so, so he's not a he's not a kernel for networking. There's also a lot of data needs to be process. So this example sort of shows you what kind of data needs to be process. 285 00:38:07.620 --> 00:38:17.160 William Cheng: So, for example, the data come across the internet. So, so, you know, the basic idea of you I guess if you don't have any idea, any networking class, you know that, you know, will you send them a 286 00:38:17.640 --> 00:38:25.770 William Cheng: Message. The last message is going to be talking to segment and the segment is going to be transmitted across the Internet, the segment is going to get further chopping two packets 287 00:38:26.190 --> 00:38:34.470 William Cheng: So they know that's going to deliver these packet in any order they want and then eventually when the receiver receive these packets, they will we assemble them into segment and then they will 288 00:38:34.800 --> 00:38:39.690 William Cheng: Make all the segments connect them all together and then deliver the entire data back to the application. 289 00:38:40.350 --> 00:38:44.610 William Cheng: Okay, so. So inside the kernel is actually very complicated. There are different layers of it. 290 00:38:45.180 --> 00:38:49.830 William Cheng: You probably heard of TCP IP as a TCP IP. They're actually separate layer inside the Colonel. 291 00:38:50.220 --> 00:38:56.520 William Cheng: So the data companies IP over here you can sort of call them packets. OK. So the data is going to come out of pocket over here. So, 292 00:38:56.760 --> 00:39:06.810 William Cheng: As packet so so so you sort of need to put the package together over here and then form as a segment and then you need to deliver the segment to the next layer of the application over here into TCP there. 293 00:39:07.410 --> 00:39:11.760 William Cheng: So, typically in the networking all the data structure. So to have this recursive data structure. 294 00:39:12.180 --> 00:39:18.450 William Cheng: One data structure one layer can be divided into two parts. First, there's the header part and then the other part is over here is like the body. 295 00:39:18.900 --> 00:39:28.650 William Cheng: Okay, we already see data structure like that. Well, we talked about the festival system where you try to read directory entry first will read the header and the header would tell us how many more bites are there inside the body. 296 00:39:29.130 --> 00:39:34.500 William Cheng: Okay, so, you know, looking also same thing when we read the head over here. We know how many more bites are inside the body over here. 297 00:39:34.740 --> 00:39:43.140 William Cheng: Right. And then what we do is that once we wait for all the data inside the body to appear there we can actually deliver the body to the next level inside the protocol stack. 298 00:39:43.830 --> 00:39:47.670 William Cheng: Okay, so therefore, his IP over here. Once you finish the entire 299 00:39:48.000 --> 00:39:56.010 William Cheng: The entire data over here you will deliver to TCP and again the TCP. The way you do that it will look at this entire data up I divided into two half 300 00:39:56.190 --> 00:40:05.730 William Cheng: The first half over here is going to be the header and the header will give you information about how to deliver the body to different application layer or maybe they're even higher layer over here, you know, inside the operating system. 301 00:40:06.600 --> 00:40:16.620 William Cheng: Because we've got all these processes are the kernel is going to take a lot of time and if you spent too much time inside your interrupt service routine handling, you know, you know, all these data structure passing 302 00:40:17.400 --> 00:40:25.620 William Cheng: Data structure parsing which you have done a warm up on a warm up to par. See, it takes time, right. So, so in this case we waste a lot of time he's had a kernel. And again, 303 00:40:25.860 --> 00:40:32.430 William Cheng: The new data is gonna come across it, you know, that they will get all get dropped on the floor and then the center, have to be transmitted. So, therefore, you have to do this as soon as possible. 304 00:40:33.210 --> 00:40:42.960 William Cheng: That so so so we we mentioned before in chapter five. The way to do this is that you use the phone work, you're going to take all this work over here for parsing out of Colonel and going to differ work to be done at a later time. 305 00:40:43.170 --> 00:40:46.890 William Cheng: Right, so this way you can finish your interrupt service routine, you're going to get ready to handle boy interrupt. 306 00:40:47.310 --> 00:40:56.160 William Cheng: Now as a part of this that the trip over here to handle them is to to do this in before work. But still, in the end, you know, we don't want to copy data. So how do you make sure that you know I should copy data. 307 00:40:56.820 --> 00:41:00.630 William Cheng: Because of the ways is that, again, we need to copy pointer and now the data is out there. 308 00:41:02.040 --> 00:41:08.820 William Cheng: Alright, so, so again Unix or Linux or the users a special data structure over here, they call the socket buffer. 309 00:41:09.210 --> 00:41:19.440 William Cheng: So what's a socket. Right. So the basic idea over here is that, you know, networking is going to be so easy to use using network is like, plug it into a wall socket. So if you plug into the saga, then you're going to receive service. 310 00:41:19.800 --> 00:41:27.510 William Cheng: Okay, so it's kind of like the Ethernet jack. Do you plug into the Ethernet everything's going to work, magically, okay. So anything that's related to to soccer. It's related to networking 311 00:41:28.110 --> 00:41:33.810 William Cheng: Okay, so he unites when I talk about socket buffer. It's a networking buffer that sort of dental surgeon, so look like this. 312 00:41:34.350 --> 00:41:45.300 William Cheng: Right, here's my socket buffer is the only contains a bunch of pointers over here. The point is over here is going to contain is going to point to the buffer. Okay, so here's the buffer that it's going to receive all the data. 313 00:41:45.690 --> 00:41:47.790 William Cheng: So again, the data over here again. Did I was 314 00:41:48.210 --> 00:41:53.220 William Cheng: Going to get a choppy, the little parts over here and they will arrive individually and sort of in sort of any kind of different order. 315 00:41:53.370 --> 00:42:00.960 William Cheng: So what you need to do is I use some sort of first figure out where the header. Yes. And Heather will tell you how many parts of the body as and then you can allocate this particular buffer. 316 00:42:01.230 --> 00:42:05.460 William Cheng: Okay, so he's not above. Right. You need to know where the header is so you can continue to look for data. 317 00:42:05.670 --> 00:42:11.940 William Cheng: And then you're going to look for the body over here and then the data come across the internet, they will start filling this. The other these blank spaces over here. 318 00:42:12.030 --> 00:42:18.240 William Cheng: So again you to keep track of where all these things are so use the socket buffer over here to point the different parts of the data buffer. 319 00:42:18.630 --> 00:42:26.400 William Cheng: Okay. So, for example, there will be one point or point to where the beginning of the header is 1.01 point that the the the end of the header also beginning of the body is 320 00:42:26.670 --> 00:42:33.600 William Cheng: And then they will also point to the tail of the body. So the two so this part of the body is is ready to be delivered to the next layer. 321 00:42:34.170 --> 00:42:44.370 William Cheng: That you also need to point at the end of the buffer over here. And then at the end over here. There's a bunch of additional data structure. For example, the data structure will will will remember things like the reference count. 322 00:42:45.000 --> 00:42:50.370 William Cheng: Okay. So in this case, this one has a reference kind of one because I'm using the socket buffer to point to the state or structure. 323 00:42:50.940 --> 00:42:57.990 William Cheng: Okay. So, similarly, I guess. Over here says to Q segment over here. So again, if you send data to the TCP layer the TCP layer is going to 324 00:42:58.170 --> 00:43:07.380 William Cheng: Treat a long message as multiple segments. So here's one segment. Here's another segment over here. They are linked together in a linked list. And this will have a link this of socket buffer. 325 00:43:07.620 --> 00:43:12.840 William Cheng: Okay, and every stock about rules like this. And then again, they all point to different parts inside. Inside the data buffer. 326 00:43:13.260 --> 00:43:19.350 William Cheng: OK. So again, the trick is that networking over here is that this buffer, I should never make a copy of it because this buffer can be really big. 327 00:43:20.340 --> 00:43:29.970 William Cheng: That. So in that case, what do I copy. Right. I will copy the socket buffer data structure. So for example, if I wanted to share this buffer among up, up, up, up, up, up to one part of the Colonel. 328 00:43:30.180 --> 00:43:39.210 William Cheng: Another kernel module. What, what, what would I do right so what I can do is I can simply make a copy of this data structure over here, then I can share this data structure, you know, between 329 00:43:40.050 --> 00:43:46.230 William Cheng: Between two kernel modules. Okay, so we can take this this data set to make a copy of it. And I have this data structure right 330 00:43:46.410 --> 00:43:53.310 William Cheng: I need to adjust the next pointer over here and then they will all point to the same place. And now you're going to end up with two socket buffer is sharing the same 331 00:43:54.030 --> 00:44:01.170 William Cheng: Time to socket buffer sharing the same data buffer. So I need to increment the reference hot over here to be to Raza and this guy is one of them is removed. 332 00:44:01.380 --> 00:44:10.680 William Cheng: I would recommend this one to one. Why, when I when I removed the other point over here. I would recommend reference to zero. And then this entire data structure will be able to free now. 333 00:44:11.730 --> 00:44:16.920 William Cheng: So this is the this is one of the one of the way that you can do this. This is the way they can pass one buffer to another. 334 00:44:17.280 --> 00:44:24.120 William Cheng: One kernel module to another kernel module without copying the buffer. How do you pass data to the, to the next level in the protocol stack. 335 00:44:24.780 --> 00:44:31.260 William Cheng: Okay. Will you go from one you know the IP layer over here to the TCP layer again. The rule is going to be the same. You are not allowed to copy data. 336 00:44:31.830 --> 00:44:40.320 William Cheng: Okay, so in that case what I can do over here is I could do the following. So let's say I'm going to clean up this one over here, there's less of that this is this is TCP 337 00:44:41.550 --> 00:44:53.130 William Cheng: There. This is IP over here. Okay. So IP over here IP header is here IP bodies over here. We also know that inside the body over here, as far as TCP is concerned, the body is divided into TCP header and TCP body. 338 00:44:53.760 --> 00:45:03.840 William Cheng: Okay, so therefore all have to do is have a TCP over here is that we can adjust this point or another hurdle will point right here and the body will fly right here says out of the body will point right here. 339 00:45:04.680 --> 00:45:09.630 William Cheng: Okay, and also the next section over here again the header of your point of the beginning of the current body for the IP. 340 00:45:09.810 --> 00:45:18.300 William Cheng: And then the body over here will point to, you know, the, the end of the TCP header and that will be TCP body. And now we can actually share the same buffer between different protocol layers. 341 00:45:18.630 --> 00:45:22.980 William Cheng: Okay, so that visual look like this right IP over here the headers. Here the bodies here. 342 00:45:23.190 --> 00:45:32.220 William Cheng: As far as TCP is concerned headers here and bodies concert. And then if you pass it into the next level of the protocols that over here again for the next layer. The header will be here at the body will be here. 343 00:45:32.910 --> 00:45:40.650 William Cheng: So you can actually share this entire data search over here it's, you know, among multiple a protocol layer over here without making copy of the data. 344 00:45:41.340 --> 00:45:52.740 William Cheng: Okay, eventually, when you deliver the data into the application space. Why that, you know, India, you have to make a copy because the user space buffer is sitting in a different address space compared to what's inside your carnal. Yeah. 345 00:45:56.640 --> 00:45:59.400 William Cheng: One last thing I want to briefly mention is that you 346 00:45:59.940 --> 00:46:06.300 William Cheng: Will you do networking. If you have done any kind of networking programming, you'll notice that inside. Now, who have to do a lot of timeouts. Okay. The reason is that 347 00:46:06.630 --> 00:46:12.000 William Cheng: If you're the opposite, you know everything about your machine, but you have no idea what's going on out there inside the Internet. 348 00:46:12.450 --> 00:46:17.430 William Cheng: Okay, so when you send you know so when you send a message. So let's say you're you're sending packet one packet of time. 349 00:46:17.640 --> 00:46:23.400 William Cheng: When you sell one package so so so you are here, right. He tried to send data to the destination over here and over here, this is the Internet. 350 00:46:24.090 --> 00:46:29.700 William Cheng: So when you send it across the internet, you don't really know whether the data has even reach your destination. 351 00:46:30.180 --> 00:46:37.560 William Cheng: Okay. So in this case, maybe the Internet will drop your data, maybe to a destination is too busy, it will get dropped by that that's an issue or maybe the decision is really, really slow. 352 00:46:37.980 --> 00:46:45.360 William Cheng: Guys are the only thing that you can do is that you are the pieces that you know everything about your machine, you know nothing about outside world. So what do you do that you will want to have a timer. 353 00:46:45.570 --> 00:46:51.510 William Cheng: And when the timer expire. And if you haven't got a response from the destination. Well, in that case, you have to transfer your data. 354 00:46:52.140 --> 00:47:01.320 William Cheng: Okay, so how do you return to data. Well, in order for you to return smear your data, you have to hold on to the data again. That will be the reason that we saw before. I'll be here is that you can pass it off on 355 00:47:01.500 --> 00:47:05.940 William Cheng: One module to the other module. So we hear this might be the module for retransmission 356 00:47:06.180 --> 00:47:15.900 William Cheng: So in order to, you know, so once you've finished sending data you need to keep a copy of so you're going to pass the reference of these objects into another module. And now this module can actually be in charge of retransmission 357 00:47:16.380 --> 00:47:21.720 William Cheng: That. So eventually, when a time I'll happen over here. So in this case, what should you do right you should we transmit a packet 358 00:47:22.110 --> 00:47:25.710 William Cheng: So in that case, it's kind of like it's kind of like a callback function. 359 00:47:26.250 --> 00:47:32.250 William Cheng: Okay. So, therefore, to support network inside the Unix and Linux kernel. You also need to provide callback function inside, Colonel. 360 00:47:32.580 --> 00:47:42.090 William Cheng: We mentioned before that we have a callback function in the user space program that's called the signal delivery, right, because it could specify signal handler. 361 00:47:42.300 --> 00:47:52.350 William Cheng: But that's a user base Facebook when inside of Colonel, we didn't have any kind of a callback mechanism. Why in Windows. They have a generalized callback mechanism, they can call back inside a kernel or is that a user space over 362 00:47:53.010 --> 00:48:03.390 William Cheng: Okay, so to get this to work inside the Colonel. You also need to implement the cobra mechanism is that a kernel and also you need to have a way to hold on to the data by increment in the reference count on the 363 00:48:04.500 --> 00:48:13.380 William Cheng: On the data. So, so, so, so, so, so, so this way you know you'll call back macros me share the same the same data, you know with with other parts of the carnal. 364 00:48:14.250 --> 00:48:22.710 William Cheng: Yeah. Alright, so we are done with device driver. So this is a good time to break and next time we're going to look at virtual machines. Yeah.