WEBVTT 1 00:00:01.770 --> 00:00:12.900 William Cheng: Hey, welcome to the second part of lecture 26 so we're done with chapter six. And now we're going to go to chapter four, to look at the device driver implementation issues. 2 00:00:13.559 --> 00:00:24.330 William Cheng: We talked about the framework for devices major device number of minor device number we talked about process and threats, right, the states that the process and the threat has to go through 3 00:00:24.810 --> 00:00:35.130 William Cheng: We talked about storage management how to manage how the map are falling to address space we skip this part of talk about the lower level kernel, which talks about the device driver implementation. 4 00:00:36.210 --> 00:00:44.520 William Cheng: And so, so what we're going to do is I'm going to talk about two kinds of devices. So today's lecture I'm going to cover 5 00:00:45.060 --> 00:00:52.290 William Cheng: Part of the terminal device driver. So next lecture, we're going to, you know, talk a little bit about the network communication. Okay. 6 00:00:52.980 --> 00:00:56.940 William Cheng: So these two devices are, you know, a very similar. I don't know if you have any kind of a network programming. 7 00:00:57.420 --> 00:01:03.930 William Cheng: The way you do network programming is that you open the socket and the socket. It does. What's the socket socket is just a file descriptor. 8 00:01:04.440 --> 00:01:10.110 William Cheng: Okay so opening that will communication is the same thing as open a file you open a file you read a writer you close 9 00:01:10.800 --> 00:01:19.170 William Cheng: So at the application level a talking to a terminal. Terminal is reading for the keyboard, you really, really can ride, you know, to the to the display. 10 00:01:19.710 --> 00:01:32.670 William Cheng: So again, the interface is very, very different. So we're going to first look at the terminal device driver implementation and see what are the design issues are and then we'll have a look at the network device driver to see what what what kind of different things we have to worry about. 11 00:01:34.620 --> 00:01:42.390 William Cheng: Alright, so this is a terminal. So, so if you blow this one apple on the left hand side, the white part over here it says digital 12 00:01:42.870 --> 00:01:52.110 William Cheng: So this is the Digital Equipment Corp terminals additional equipment court. They are very famous for inventing the mini computer. They are also they use, you know, 13 00:01:54.360 --> 00:02:01.140 William Cheng: The BMS awkwardnesses them the Unix operating system. So, so we use the terminology that we use will want to use the terminology 14 00:02:01.560 --> 00:02:08.790 William Cheng: So when you're sitting in front of terminal. It looks like you're sitting in front of in front of one of those terminals on the right hand side over here it says 15 00:02:10.860 --> 00:02:19.650 William Cheng: So that's a, that's a terminal model. So VT 100 is a very, very popular terminal. Terminal model that were invented, you know, by Digital Equipment core 16 00:02:20.250 --> 00:02:26.400 William Cheng: So the display over here. It's a character based display. You can do graphics. This is really a really old terminal. 17 00:02:26.850 --> 00:02:34.770 William Cheng: There's a keyboard at the back of this terminology here there is a serial line cable that's physically connected the machine. So the machine over here. 18 00:02:35.010 --> 00:02:45.990 William Cheng: There's a similar one connector and the connector. I think on the IBM PC. It looks like a, you know, one of those 15 pin, you know, connected. It looks like it looks like this, or something like that with a bunch of, you know, a little pin on it. 19 00:02:46.470 --> 00:02:58.530 William Cheng: So that's a serial connector so so when you're sitting in front of the terminal. The idea here is that you're sitting in front of them, you're sitting in front of the machine. Okay, so this terminal is the console for your computer. 20 00:02:59.400 --> 00:03:04.050 William Cheng: Okay, so what does that mean it's a constant, right. So if I actually have the right privilege. I will be able to turn off the machine. 21 00:03:04.320 --> 00:03:13.290 William Cheng: Okay, so this connection is very, very different from the connection that you have on the internet. If you're using a web browser you connect to CNN com well. Can you shut down CNN com 22 00:03:13.830 --> 00:03:22.710 William Cheng: Was it again. It's a different kinds of abstraction. Right. So when you're sitting in front of a terminal, you actually you're, you're, you're the owner of this machine and you have the right privilege and it shut down machine. Yeah. 23 00:03:22.920 --> 00:03:28.170 William Cheng: Alright, so, so every key press that you press on the keyboard will generate interrupt on your machine. 24 00:03:28.560 --> 00:03:39.000 William Cheng: There and the display over here is a very slow. You know, it's a slow device. So when you programs are printed out onto the screen, it needs to print the data one character at a time. Right. 25 00:03:39.450 --> 00:03:46.140 William Cheng: So when you try to print one character. It will take a long time for the device to be ready for the next character. So again, you wait for the interruption. 26 00:03:46.260 --> 00:03:52.290 William Cheng: In the next interrupt service routine. You put the next character onto the screen and then the next era. So every interview, you're going to put one character on screen. 27 00:03:52.770 --> 00:03:57.960 William Cheng: Okay, if you put on backslash and on the screen. What it will go to go to the beginning and they can go to the next line. 28 00:03:58.590 --> 00:04:03.330 William Cheng: So so so the way that you you know you print character print stuff on the screen is one character at a time. 29 00:04:03.570 --> 00:04:12.510 William Cheng: And you get the keystroke one character time every keystroke. You don't get to interrupt and he's not to interrupt service routine. You go read the keyboard to say what key. Did you just press and then the sort of final of this 30 00:04:13.140 --> 00:04:20.730 William Cheng: Then, so when you're open to you can you can type divine command echo dollar term it will tell you what terminal is connected to 31 00:04:21.120 --> 00:04:32.730 William Cheng: I think if you're doing all been to is gonna say its is connected turmeric or external color 256 so it's a 256 color exterminator exterminator is a competitor. 32 00:04:33.180 --> 00:04:39.570 William Cheng: For the big 100 terminal. Terminal. There's a company called X terminology. They also make some really fancy terminology 33 00:04:39.960 --> 00:04:46.020 William Cheng: So when you're running Ubuntu, for some reason, their default terminal title is assuming that you have, you know, this kind of terminal. 34 00:04:46.290 --> 00:04:55.080 William Cheng: Yeah, so whenever you're sitting around terminal. It's always emulating one of these physical terminal. It's as if you're sitting in front of the machine and then, you know, whatever that you type over here you get 35 00:04:55.440 --> 00:05:05.370 William Cheng: All these interviews and stuff like that. So this is called terminal emulation. Okay, bye bye bye printing this one up. You know what can a terminology was emulated but it's got a terminals. Although always very, very long time ago. 36 00:05:05.790 --> 00:05:14.520 William Cheng: guys so today we don't have terminals anymore but but still if you are using the terminal program, we're going to pretend that you're using a console, you know, to your computer. Yeah. 37 00:05:16.020 --> 00:05:27.900 William Cheng: All right, so, so, so the terminals along obsolete, but still very relevant because today we have something called a pseudo terminal though sooner terminal is not a real terminal. So we're gonna sort of pretend that again you're sitting in from these terminals now. 38 00:05:28.560 --> 00:05:30.810 William Cheng: So how do you interact with these terminal devices. 39 00:05:31.440 --> 00:05:37.260 William Cheng: So, so when we try to implement a device driver. So one thing we mentioned before device independence is going to be very important. 40 00:05:37.560 --> 00:05:45.390 William Cheng: Okay, so if you want to be device independent, we need to use the device driver that's independent of the device right so then what what kind of device driver that can, can we use 41 00:05:45.720 --> 00:05:54.270 William Cheng: Well, as it turns out that, you know, since you are physically connected to the serial line port over here. Every Linux system come with a serial line terminal device driver. 42 00:05:54.690 --> 00:06:04.020 William Cheng: Okay, so there's the so so in the companies is that they have a terminal device driver, the terminal device driver is sorry sorry the serial line device is very primitive 43 00:06:04.500 --> 00:06:08.610 William Cheng: You can, you know, it's basically it's useful toy sort of a toy application. 44 00:06:09.030 --> 00:06:19.260 William Cheng: It doesn't really have any buffer. So for example, if you're typing really fast is as you're going to miss character and also if you try to send data out to the screen. Very, very fast, you only going to see some of the some of the characters. 45 00:06:19.830 --> 00:06:28.020 William Cheng: Okay. So, therefore, that that terminal device drivers are the serial on device driver that you can actually found in the Linux system. It's not really suitable for the terminal. 46 00:06:28.740 --> 00:06:36.990 William Cheng: Okay, so, so, so we need to sort of find out why is a term, you know, why is this device and the other Sealand you by job over here. It's really not suitable you know for for for 47 00:06:37.980 --> 00:06:48.180 William Cheng: 4444 terminal. But if you will try to sort of, you know, you try to hack Linux and try to play with some kind of a simple project over here, you can actually use the serial and driver. You don't need to own terminal device. 48 00:06:48.630 --> 00:06:53.100 William Cheng: Okay, you don't need your terminal device driver. Okay, so, so we want to have as much 49 00:06:53.760 --> 00:06:59.760 William Cheng: device independence as possible, but who ended up to be completely device independent. Well, then our device is going to be not not very useful. 50 00:07:00.210 --> 00:07:09.750 William Cheng: Okay, our interaction, we can only try very, very slowly, to make sure that we can always keep up with the typing speed and when we tried to print it out the screen. We need to also good. You also need to slow down. 51 00:07:10.620 --> 00:07:17.400 William Cheng: There as well. So talk about some of the stuff. What's going to make you know the terminal, a little bit different from the zero line. Yeah. 52 00:07:17.850 --> 00:07:27.540 William Cheng: The issues were terminal. So here is your the 2222 problem right away the terminals are slow and the character generation are too fast. Okay, when you try to edit a file. 53 00:07:27.990 --> 00:07:31.500 William Cheng: Okay, you're using your editor, all of a sudden all the character shows up on the screen. 54 00:07:32.250 --> 00:07:39.660 William Cheng: Okay, so. So in that case, if you're using a single line driver. Most of the character you're going to miss it. Right, because the civil engineering driver has no buffer. 55 00:07:39.840 --> 00:07:45.330 William Cheng: So all the data is going to be dropped on the floor. So you're going to see if your character on the screen. Okay, he's not acceptable. 56 00:07:45.780 --> 00:07:52.380 William Cheng: I mean, clearly, if you're using editor, that's totally understandable. Right. Also, if you want to do a cat have a file rice a CAD file by file name. 57 00:07:52.560 --> 00:07:59.100 William Cheng: One. And again, most of the character is going to disappear from the screen you're all going is going to see a few character so so clearly this is totally unacceptable. 58 00:08:00.030 --> 00:08:07.110 William Cheng: That. So you're not case, what do you have to do, right. So what you have to do is I, you can. What you can do that inside a colonel, I can use a producer consumer problem. 59 00:08:07.620 --> 00:08:10.860 William Cheng: Okay, so that, in this case, the consumer will be the display. 60 00:08:11.100 --> 00:08:19.410 William Cheng: Right, so the display. What it will do is that it will take the character from the buffer one character at a time whenever they're ready and the producer when they try to produce too fast, the producer has to wait. 61 00:08:19.980 --> 00:08:29.970 William Cheng: Raza by using a producer consumer problem again it's a circular buffer I wraps around over here. So the producer over here is going to be my cat program right when my capital whenever called right to the device. 62 00:08:30.150 --> 00:08:36.090 William Cheng: Which I did right to the device, it will write as fast as possible. But as soon as you catch up with a consumer. Well, then the presents the way 63 00:08:37.050 --> 00:08:38.910 William Cheng: OK, so now the consumer is going to be 64 00:08:39.390 --> 00:08:44.760 William Cheng: You know, the, the, taking the character one character at a time. So maybe inside your kernel, you have a colonel threat. 65 00:08:44.970 --> 00:08:53.520 William Cheng: That will take the character right to the screen. Wait for the device to become ready and then what we'll do is they will take the next character and right on the screen. So they would go right to the screen. 66 00:08:53.910 --> 00:09:02.430 William Cheng: Right there on the screen. Very, very slowly. So using a producer consumer buffer, we can actually slow down the person who can actually slow down a producer. 67 00:09:03.360 --> 00:09:10.860 William Cheng: Okay, so. So inside a kernel for every, you know, for every one of these, the device. We need a specialized buffer that implement a producer and customer problem. 68 00:09:11.700 --> 00:09:16.050 William Cheng: Yeah. What about the characters arriving from the keyboard right we're typing these, you know, 69 00:09:16.560 --> 00:09:22.200 William Cheng: They did have a keyboard over here, we can actually tie very, very fast. But no matter how fast we tied the computer can always keep up. 70 00:09:22.620 --> 00:09:30.330 William Cheng: Okay, but what of our application, right. How does that on occasion. Read it up on the keyboard. Well, our application with each other. We've on the keyboard and make the system call 71 00:09:31.050 --> 00:09:39.240 William Cheng: Okay, so when it cut when he made the racism called is gonna come inside of Colonel, you get a line of text from the colonel and then send it to the application. What if your application never call read 72 00:09:40.410 --> 00:09:44.730 William Cheng: Because, for example, if you're running Colonel to write the colonel to you're doing simulation, are you reading on the keyboard. 73 00:09:45.150 --> 00:09:51.810 William Cheng: Or you're not reading on the keyboard. So, therefore, if you keep typing ahead. I'll get all these character will be lost, right, if you're using a serial on driver. 74 00:09:52.380 --> 00:10:02.430 William Cheng: Okay, because a serial I'm driver can only store one character right so the solution over here is that we also need to use a producer consumer is that a kernel for the data coming from the keyboard and get delivered to the application. 75 00:10:03.090 --> 00:10:08.700 William Cheng: Okay, so the picture will look like this. And then we have one output Q over here to go to the 76 00:10:09.150 --> 00:10:17.670 William Cheng: Decoder display. I mean, this picture over here. I saw them as two different device driver in the good old days or combined into one device driver and that device drivers know as the terminal device driver. 77 00:10:18.150 --> 00:10:26.820 William Cheng: Now you know the the keyboard has their own keyboard driver, because the keyboards hedging actually separated from the screen right in the good old days, it's just one device. 78 00:10:27.240 --> 00:10:35.970 William Cheng: Now the keyboards a separate device and also the display. You know, you're probably going to have a fancy graphics controller has a GPU on. So again, there's very fancy 79 00:10:36.420 --> 00:10:42.270 William Cheng: So, so they're going to use a separate device driver again. But again, in the good old days there combine two one, so it doesn't really matter whether it's one or two. 80 00:10:42.570 --> 00:10:49.680 William Cheng: Inside the Colonel, we here, we need to have to queue. One is the output queue that go to the display and the other one is the input cue that come from the keyboard. 81 00:10:50.490 --> 00:11:01.560 William Cheng: Okay, so this way when we are trying to write too much on to the display our producer will have to wait over here. And similarly, if our program is not reading data from the, from the implication over here. 82 00:11:01.800 --> 00:11:14.550 William Cheng: And we can keep typing ahead, and pretty soon are typing over here will be completely filled a buffer. Okay, so at some point this buffer will get Phil and if we don't call read that data is going to say is that a buffer. So at this point, if you continue to press key, what's going to happen. 83 00:11:15.840 --> 00:11:23.220 William Cheng: I don't know if you actually had this happen to you over here. You keep typing away as it turns out that nobody's calling read at some point, your, your keyboard is going to start beeping at you. 84 00:11:23.760 --> 00:11:30.420 William Cheng: Okay, so why is the keyboard beeping at you why it's telling you that this buffer overhearing started Colonel is dropping characters. 85 00:11:30.960 --> 00:11:32.940 William Cheng: Okay, so I've heard is that when the, when 86 00:11:33.630 --> 00:11:43.620 William Cheng: When you get to keep going or up when you try to add data into this buffer and you find out of this buffer is full. So instead of, you know, a waiting for it. I'd be on how do you wait for it. Right. You have to drop that character because you know 87 00:11:44.010 --> 00:11:51.660 William Cheng: That the user is typing characters over here. So I've heard is that when you drop that Georgia key you going to generate a beat, so that the user knows that. Oh, maybe I should stop hiding 88 00:11:53.100 --> 00:12:03.780 William Cheng: All right, so. So again, these two Q's behave completely differently, right when you try to print data onto the display. You are not allowed to lose a single character, but when the data coming from the keyboard. 89 00:12:03.990 --> 00:12:08.190 William Cheng: You're allowed to lose character. And the reason you're allowed to because the keyboards going to start beeping as you 90 00:12:08.790 --> 00:12:15.840 William Cheng: Guys know what it means that you say, oh, maybe you know that the you know the the keyboard driver over here has dropped some characters over here. So that'd be awesome. Okay. 91 00:12:16.710 --> 00:12:22.980 William Cheng: Okay, so you know way we are being trained by all the software. Okay, so that we know that, you know, when we get beat that we need to stop typing. 92 00:12:24.270 --> 00:12:34.890 William Cheng: Alright, so, so again this is, you know, I guess, the obvious isn't people. They're pretty clever, they come up with this way so that you can actually lose character on the keyboard, but you will never lose any character sending to the display because that because that will be really bad. 93 00:12:35.910 --> 00:12:43.800 William Cheng: Okay, so again the operating system designer for the terminal driver, they need to make all these kind of design decisions so that you know everything appears to be fine. Okay. 94 00:12:44.940 --> 00:12:52.020 William Cheng: All right. So if you look at these two Q's over here who is actually, you know, interacting, you know, who is a producer who's the consumer. 95 00:12:53.340 --> 00:13:00.750 William Cheng: Okay, so, so let's be a little more specific over here. Right. Okay. So if you look at the top side over here and pops out over here is the Pops, you know, 96 00:13:01.170 --> 00:13:08.790 William Cheng: Probably. Here we have the user space program in the middle power had the current all okay so for the outlook you over here. Who is the person who is a producer. 97 00:13:09.480 --> 00:13:16.440 William Cheng: What the producer happened when you know when you application make the racism call when you make the racism, call your user threat become a colonel. 98 00:13:16.890 --> 00:13:20.880 William Cheng: Colonel. So I start executing over here. So your kernel threats, the one that's the producer. 99 00:13:21.660 --> 00:13:24.090 William Cheng: Okay, what about the consumer over here for the input. Q. 100 00:13:24.420 --> 00:13:36.420 William Cheng: The implication over here only if you call the VA system call right then you start taking data out of this interview. So again, your user through I come into the kernel, making the system omega racism call you come into the kernel and now 101 00:13:36.780 --> 00:13:39.210 William Cheng: Your kernel thread over here is reading from the input you 102 00:13:40.230 --> 00:13:49.110 William Cheng: Go. So the top side over here is very clear. Okay, it's going to you. So the top side over here at the producer consumer or handle in the threat contacts. Okay. 103 00:13:49.830 --> 00:13:51.060 William Cheng: What about the bottom over here. 104 00:13:52.020 --> 00:13:58.440 William Cheng: Okay, so one thing that you can do over here at the bottom over here. You can also handle that using using the threat contacts you have a kernel third running over here. 105 00:13:58.680 --> 00:14:04.800 William Cheng: Okay, every time when your device become ready. So therefore, you're gonna you know you're going to sleep in I. O que and you tell the the devices say 106 00:14:04.980 --> 00:14:09.420 William Cheng: Hey, when the IoT devices ready wake me up. So when I wake up, I'm going to go to the 107 00:14:09.660 --> 00:14:19.320 William Cheng: The, you know, the producer consumer problem over here. I'm going to, you know, access the you know the the buffer. Take one character out and then send it to the device. And then I'm going to go to sleep and wait for the interim 108 00:14:20.070 --> 00:14:30.450 William Cheng: Okay, so I can do that. And similarly over here for the keyboard device driver whenever I got an interim over here. My, my, my throat will wake up and then what it will do is it will actually read a character for me using the, you know, 109 00:14:30.990 --> 00:14:41.730 William Cheng: memory map IO to talk to the device and get character that was just press on the keyboard and then I can I can I will become the producer and add the data over here into the into the buffer right here. 110 00:14:42.660 --> 00:14:47.760 William Cheng: Okay, but we actually do that. Okay, so I don't know if you notice how you say your kernel one 111 00:14:48.210 --> 00:14:51.300 William Cheng: Inside Colonel why you can actually use this use this command, Colonel. 112 00:14:51.660 --> 00:15:04.800 William Cheng: Colonel proc right then we run Colonel proc you will see all the all the processes that are running into your kernel and every process only one threat. Do you have a thread that's reading from the keyboard and also sending data out of it this way. 113 00:15:05.850 --> 00:15:11.400 William Cheng: Right. So, even after you said driver you go to why you should see all these the by job Rico, Ronnie. There's actually no Colonel threat right 114 00:15:11.610 --> 00:15:20.970 William Cheng: Okay, so, so, so, as it turns out that if you to handle the bottom part over here of the producer consumer problem using a threat contacts. It seems to be an overkill. And it may be inefficient. 115 00:15:21.420 --> 00:15:30.090 William Cheng: There. Why is that right. So for every device over here. I'm going to end up with two different kernel threads. So if I have 20 devices. I'm going to end up with 20 Colonel threats. 116 00:15:30.570 --> 00:15:38.490 William Cheng: Okay. Every hurdle 30 require a kernel stack. So in this case, I'm using precious operating system resources and most of the time my cursor is not doing anything. 117 00:15:39.450 --> 00:15:42.930 William Cheng: Okay, so typically the Colonel, there's obviously some are not designed that way. 118 00:15:43.290 --> 00:15:50.190 William Cheng: Because, because when you have more threads inside the Colonel, you have to juggle more threat. And if you're using some kind of a fancy scheduler, you're going to slow everything down. 119 00:15:50.910 --> 00:15:58.380 William Cheng: Okay. So inside the Colonel. We want to have as few threads as possible. Right, so you can also know what kind of current. So do you have, okay, the only 120 00:15:59.490 --> 00:16:10.080 William Cheng: The only threads that are inside a kernel is the idol process you create it and then and then and then go to sleep, wherever. And also, there's the page on demand, and there's no other Colonel threat. 121 00:16:10.980 --> 00:16:17.760 William Cheng: Okay, so what about what about you, yo, yo you in the process. The process is going to go into user space become the user space and in process. 122 00:16:18.390 --> 00:16:29.940 William Cheng: Okay. So inside the Colonel. We typically only have Colonel threat they are used by the user process and all the other Colonel threat. We need to have as few of them as possible, right, because otherwise you're wasting precious resource, such as Colonel stack. 123 00:16:30.570 --> 00:16:33.810 William Cheng: OK Corral side is pretty big. Or you don't really want to waste every source. Yeah. 124 00:16:35.010 --> 00:16:36.180 William Cheng: Alright. So in this case, you know, 125 00:16:36.510 --> 00:16:45.900 William Cheng: So how do you handle you know this producer consumer, you know, the bottom over here. Again, that's the way that we typically talk about it right he's at the bottom over here. Their handle inside the interrupt contacts. 126 00:16:46.200 --> 00:16:52.320 William Cheng: Okay, so for the, you know, for, for, you know, for for for sending data out on the display every time, will you get a 127 00:16:52.650 --> 00:17:02.610 William Cheng: Display interrupt. Right. That means that you can, you're ready to write to the display. He's had to interrupt handler. We're going to take one character out of the the output buffer over here and I'm going to send it to a display. 128 00:17:03.180 --> 00:17:06.600 William Cheng: Okay. And next time we'll get another interval going to do this over and over and over again. 129 00:17:06.900 --> 00:17:18.330 William Cheng: Okay, so the bottom part over here is handled inside enroll contacts. Similarly, when you press the key on the keyboard inside the interrupt service routine, we're going to actually go to the device. Get the character and then added to this particular cute. 130 00:17:19.230 --> 00:17:24.810 William Cheng: Okay, so this way, we're not going to waste, you know, Colonel threats to handle the producer consumer problem. Okay. 131 00:17:26.670 --> 00:17:31.380 William Cheng: All right. Okay. So. So over here, it says they'll decide you're done is that interrupt context. 132 00:17:32.160 --> 00:17:36.480 William Cheng: So why they should do you have inside the terminal there. There are three more issues over here. 133 00:17:36.960 --> 00:17:42.000 William Cheng: You know, the input characters may need to be process in some way before they reach the application. 134 00:17:42.480 --> 00:17:48.840 William Cheng: Okay, I don't know if you actually try that in warm up one and two, when he tried to reef on the keyboard. Right. I mean, typically we the keyboards redirect it 135 00:17:49.110 --> 00:17:54.870 William Cheng: Your standard is redirected from a file, right. So, therefore, and also we're going to use the cat command. So you are redirected to a pipe. 136 00:17:55.500 --> 00:18:01.200 William Cheng: Okay I maybe some of you actually tried to refund the keyboard. Okay, well you refund the keyboard. So let's let us 137 00:18:01.590 --> 00:18:06.870 William Cheng: Do you have some character. A, B, and C and then you type the backspace character. 138 00:18:07.320 --> 00:18:11.940 William Cheng: Okay, when you type the past very carefully, you can see that this character disappear and the cursor is poor right here. 139 00:18:12.240 --> 00:18:16.440 William Cheng: And if you have another backspace character this character is going to disappear at day your cursor going to provide there. 140 00:18:16.650 --> 00:18:23.130 William Cheng: When you read this particular line and then you press enter. I will press enter. Over here you got to read the entire life. Do you see the backspace characters. 141 00:18:24.090 --> 00:18:26.640 William Cheng: Well, you don't see the best day card as a weapon to the backspace character. 142 00:18:27.150 --> 00:18:34.860 William Cheng: Right, you might you type the bass player character, but the past recorded disappear. So who took away, who took away the bass, bass character. I mean, clearly the operating system did it. 143 00:18:35.820 --> 00:18:47.160 William Cheng: Okay. So, therefore, the idea here is that the input buffer over here is edited inside the Colonel. Okay, so this way when the operating system give you that entire line. You don't see any of the bass, bass characters. 144 00:18:48.180 --> 00:18:51.030 William Cheng: Okay. And also, they're like, you know, I don't know if they use the 145 00:18:51.240 --> 00:18:59.700 William Cheng: International keyboard right and the national keyboard, you need to compose some character, you're going to have multiple characters and all of a sudden, some characters and I'm disappear, and they get replaced by another character. 146 00:19:00.000 --> 00:19:01.830 William Cheng: So for example, if you are, you know, 147 00:19:02.250 --> 00:19:07.590 William Cheng: Using some European Union stop right if you want to SM that he look like this over here. What do you have to type 148 00:19:07.770 --> 00:19:15.210 William Cheng: Right, you type he followed by a single quote, and all of a sudden, you will see to backspace is going to be done and then it will get replaced by one character over here. 149 00:19:15.900 --> 00:19:21.120 William Cheng: Okay, who's doing that. Right. I mean, again, clearly you know your application is not doing that. So this is done inside the operating system. 150 00:19:21.450 --> 00:19:26.580 William Cheng: There so so some input character may need to be process before they are delivered to the application. 151 00:19:27.300 --> 00:19:36.480 William Cheng: That there are also some application they actually want to see all the characters. So what I mean all the characters. They want to see that you press the control key. They want to see that you press the Shift key. 152 00:19:36.750 --> 00:19:44.700 William Cheng: So you're not. So again, you know, if you want to type capital A, right. You can you type a followed by the you hold down the shift key you type a so 153 00:19:45.330 --> 00:19:51.390 William Cheng: Actually, I don't really know whether that's generated by the keyboard or off racism is doing that, but it could be the system is doing that. 154 00:19:51.930 --> 00:20:03.540 William Cheng: But if you are playing a terminal based games, right, you can actually, you know, you can sit in front of the VP 100 terminal. You can probably play some shoot 'em up games by present some of the keys over here. So in that case, your application will want all the key presses. 155 00:20:03.990 --> 00:20:12.270 William Cheng: Okay, so. So in this case, will you, what will you type shift, shift, followed by lowercase E A over here. I don't want to see one character. I don't want to see two characters. 156 00:20:13.170 --> 00:20:24.570 William Cheng: Okay. So in this case, your application, need to be have a way to control whether you whether the operating system watching all these characters together or you want the organism to deliver to you all the key that WordPress that 157 00:20:25.980 --> 00:20:34.620 William Cheng: All right, and also the character type of the keyboard or echo back to the display right whenever you press the key on to the keyboard. You see that they show up at the terminal. So who's doing that. 158 00:20:35.760 --> 00:20:42.750 William Cheng: Okay. So as it turns out, as the operating system is doing that. Okay, so if you and I guess a USC. They're these machines call B2B 159 00:20:43.380 --> 00:20:52.050 William Cheng: B2B dash SCF one and sex to those those computer. Those are on the remote machine. So if you're a home, we can actually Secure Shell to those machine and you'd be able to 160 00:20:52.260 --> 00:20:56.820 William Cheng: Run that one machine Razzle I think some people are required to run some software on those machine. 161 00:20:57.180 --> 00:21:06.450 William Cheng: So we are home. You just so crucial to that. So in that case, every time when you press the key you're going to see the, the, the, the character show up onto the screen. So that's called echoing 162 00:21:06.960 --> 00:21:13.320 William Cheng: OK. So again, who is doing the echoing okay. I mean, clearly, your application is not doing the echo in when you write a program, you know, 163 00:21:13.890 --> 00:21:18.450 William Cheng: When you press the key you're going to still see the character and your application is not premium this character onto the screen. 164 00:21:18.870 --> 00:21:28.770 William Cheng: Okay, so again, all the stuff needs to be taken care of by the operating system. Okay. So as it turns out, some of these issues can be done in different ways and different operating system might decide to do things differently. Okay. 165 00:21:31.320 --> 00:21:42.390 William Cheng: All right, to solve the first problem or the three problem will be here right you want to modify the data over here. So what happened is that in the Unix operating system and what they will do is that inside the Colonel, they will have two different buffer. 166 00:21:42.630 --> 00:21:48.270 William Cheng: One of the buffer is the one that you're editing. Okay. And the other buffer. Is that the one that you're not editing. Right. So what kind of 167 00:21:48.600 --> 00:21:55.620 William Cheng: A buffer. You don't edit. So as soon as you press the Enter key. Now you have form a line once you form a line. There's no if we edit the previous life. 168 00:21:56.310 --> 00:22:03.750 William Cheng: Okay, so there was ever going to have to input Q over here. One is called a partial line. Q. And the other one is called a completely long queue. 169 00:22:03.930 --> 00:22:10.560 William Cheng: That completely and choose over here every line over here is going to end up with a backslash. And so when you read one at a time. These are the one that you reread 170 00:22:10.980 --> 00:22:19.620 William Cheng: The one that's a partial line. Q. They're incomplete. So therefore, when you type Backspace, right, we'd have backspace over here. What it will do is it will go to the simple buffer erase the last character. 171 00:22:20.370 --> 00:22:28.830 William Cheng: Okay. And if you keep typing backspace over here, you're going to end up erasing all the character and then eventually when you press enter. We press enter. What happens is going to copy all the characters over here. 172 00:22:29.040 --> 00:22:32.610 William Cheng: Added to the completed line. Q. And it also clear out the partial. Thank you. 173 00:22:33.570 --> 00:22:43.680 William Cheng: Okay, so you've probably noticed that when you try to read from the keyboard over here, right. So, what says the letter that you make a recent call each and every father for for the keyboard. You start typing does return 174 00:22:44.400 --> 00:22:51.900 William Cheng: Read actually doesn't return. Okay. Even if you just try to read one character or we call f gets the function doesn't return on to you. Press Enter. 175 00:22:52.440 --> 00:22:58.380 William Cheng: Okay, so as long as the completed long queue over here, they're empty. Will you make a recent call nothing is return. Okay. 176 00:22:58.800 --> 00:23:07.200 William Cheng: And the reason for that is that because the opportunity is still editing that line. Only when you are committed to that level. You press enter. Then the entire lines is delivered to the application. 177 00:23:08.310 --> 00:23:17.340 William Cheng: Alright, so again, everything has a reason, right when you type of, you know, which is, you know, we tried to refund the keyboard. It doesn't return was because the operating system is actually holding things out for you. Okay. 178 00:23:19.560 --> 00:23:30.270 William Cheng: All right, so to deal with three be over here. What if you want all the characters. Okay. So, therefore, what you can do is that you can actually, you know, tell the operating system to use the partial line Q and A complete blank you 179 00:23:30.510 --> 00:23:35.940 William Cheng: Or just use a single queue. OK. So the way you should do this is that you can actually are. You need to make a system call 180 00:23:36.390 --> 00:23:40.620 William Cheng: You're going to use the system call to select single versus multi line procure and that's it. 181 00:23:41.100 --> 00:23:45.840 William Cheng: Okay, so if you're playing a terminal base game. The first thing you need to do is that you need to make a system Hall. 182 00:23:46.560 --> 00:23:54.420 William Cheng: I think in the system called there's a function called FC NTL FC MTF, so that's the file control. Right. So again, you're manipulating 183 00:23:54.750 --> 00:24:00.720 William Cheng: The standard, I guess, in this case, the standard input. So therefore, you're going to make one of the system called over here and the you can actually specify to say, hey, 184 00:24:00.930 --> 00:24:04.740 William Cheng: Don't give me the partial line Q and A company like you just give me all the characters. 185 00:24:05.190 --> 00:24:09.660 William Cheng: So when you when you we made function call, like that. And then from this point all the opportunities that will give you all the character. 186 00:24:09.870 --> 00:24:19.950 William Cheng: Until the next time you say I'm done, I need to go back to the previous mode. So again, make another system call and now you go back and the offices that will actually get rid of the, the single queue and and put into to Q system. 187 00:24:20.580 --> 00:24:32.880 William Cheng: That right, the third issues over here is with echoing right. How do you deal with echo. Right. So whenever you get a character on the keyboard. He is that to interrupt service routine. I will edit to the partial on cue over here. 188 00:24:33.360 --> 00:24:40.800 William Cheng: Okay. At the same time, what I can do is I can actually take this character and then send it to the center to the output you 189 00:24:42.120 --> 00:24:52.620 William Cheng: Okay. So inside the Unix operating system. That's what it does. Whenever you press the key at the time when the key was edited the input. Q They also echo back by putting in putting the data outside the opposite, you 190 00:24:53.070 --> 00:25:03.510 William Cheng: Okay, so what do you, what would you will see is that, let's say that you're running a warm up to you, producing a lot of data, you know, data trace data on the screen. So therefore your applications, keep putting data inside the application over here. 191 00:25:03.720 --> 00:25:11.160 William Cheng: When you press the key, you will see that you the key that you press is going to appear. You know, you know, also into the into the output string. 192 00:25:12.420 --> 00:25:21.060 William Cheng: Okay, so does that look wrong right because you're typing ahead, you're typing commands. So that when your application, instead of your simulation is finished, you can actually run the next command. 193 00:25:21.600 --> 00:25:28.950 William Cheng: Okay, so therefore the keys that you press over here actually showing the output stream that looks wrong. But, you know, once we get used to it. It's really not that bad. 194 00:25:29.400 --> 00:25:35.280 William Cheng: Okay, so again this is a design decision that's made by the operating system for the Windows operating system windows actually doesn't do that. 195 00:25:35.670 --> 00:25:41.970 William Cheng: But, but then again we were using Windows. Have you ever used the Windows command line command line user interface. 196 00:25:42.300 --> 00:25:47.730 William Cheng: Well, you probably never do that. Right. That's why it's called windows because everything has a window there. So they use a complete different interface. 197 00:25:48.180 --> 00:25:57.660 William Cheng: But if you're using the windows power shell or use the command prompt. Then in that case you will notice that if you're running a program and you if you type ahead no character is going to show up on the screen. 198 00:25:57.990 --> 00:26:02.790 William Cheng: Okay, only when you finished running the current program and then all of a sudden orators don't show up. 199 00:26:03.450 --> 00:26:08.670 William Cheng: Okay, so in that case, what does the window do right so what window do over here is that when you're taking this data. 200 00:26:08.910 --> 00:26:17.010 William Cheng: You know, from the complete line Q over here. What it will do is it will take the entire line and then copy them into the display driver over here. And that's why all that character show up at the same time. 201 00:26:18.030 --> 00:26:19.230 William Cheng: Alright, so, so again window. 202 00:26:19.440 --> 00:26:30.660 William Cheng: There are also different choices. Why were you when you press ENTER over here. It's also possible when you copy the data when you move the data into the company like you. You can also, you know, copy lines of Alibaba. So again, the opportunities and the designer. 203 00:26:31.020 --> 00:26:34.440 William Cheng: decide when to actually perform this eco operation. Okay. 204 00:26:35.970 --> 00:26:46.170 William Cheng: All right. Uh, okay. And also, you know, when you try to design your device driver modular ization is an important consideration. Okay. Because, you know, 205 00:26:46.530 --> 00:26:57.510 William Cheng: If you think about this editing of characters over here when I handle a backspace. Doesn't matter what kind of a keyboard. I'm using okay if I'm using a USB keyboard. If I'm using the international keyboard. 206 00:26:58.620 --> 00:27:07.080 William Cheng: You know, it doesn't really matter. And also, you know, for different device manufacturer. If I use a device, you know, if I use a keyboard from one manufacturer that that manufacture us keyboard. 207 00:27:07.320 --> 00:27:14.370 William Cheng: Or another device driver for another cable manufacturer handling backspace shouldn't be device. It shouldn't be device dependent 208 00:27:15.030 --> 00:27:21.090 William Cheng: Okay. So, therefore, if I put something inside the device driver. That means that you know the code is going to be written by the device manufacturer 209 00:27:21.600 --> 00:27:26.730 William Cheng: Okay, so why does every device manufacturer. They all have to handle backspace right it doesn't seem to make sense that 210 00:27:27.390 --> 00:27:36.240 William Cheng: So typically what happened is I insert the unit system, what it will do is it will take the device driver and divide them into two parts. One is device independent and the other one's called 211 00:27:36.660 --> 00:27:48.540 William Cheng: Device dependent, then the device dependent part is the one that needs to know the interrupt operation codes so that they will write everything assembly language to be able to do that but handling backspace. It doesn't need to be done in the device dependent part 212 00:27:48.870 --> 00:27:55.500 William Cheng: Okay, so in Unix. This is called the line discipline module. So what it will do is that will take the terminal device driver divided two parts. 213 00:27:55.710 --> 00:28:02.610 William Cheng: One of them is device independent power that called the line. This one module. And the other part is device dependent. So that one is called the device driver. 214 00:28:03.750 --> 00:28:10.110 William Cheng: OK, so the modern units over here the device driver is only it's only taking care of the stuff that are truly device dependent 215 00:28:10.590 --> 00:28:21.270 William Cheng: OK. So again, the device dependent. That means is manufacturer dependent okay handling Backspace, you know, or, you know, handling this excellent the over here that doesn't have to be, you know, a 216 00:28:21.750 --> 00:28:30.780 William Cheng: Manufacturer dependent. Okay, so all that things are done inside the lines discipline module. Yeah. So this way. You know, I guess. Yeah, the pictures look like this. 217 00:28:31.380 --> 00:28:39.750 William Cheng: Right, I'm going to divide the device driver into two different module we hear one of the lines of the module. So this one belongs to the Japanese to Sam and then the terminal device driver over here. 218 00:28:39.930 --> 00:28:44.220 William Cheng: depends on which manufacturer that you have. We're going to end up with tons of different other 219 00:28:44.940 --> 00:28:50.310 William Cheng: terminal device driver so we can actually have one line. This one module that can work with any kind of a terminal driver. 220 00:28:50.700 --> 00:29:01.020 William Cheng: Okay. So, okay, it's almost like you're using one level indirection. Right, starting with the application. Instead of talking to the terminal driver directly. You go over one level and direction and that will give you a lot of flexibility. 221 00:29:01.410 --> 00:29:10.650 William Cheng: Yeah. And also, if you want to, you know, if you want to receive all the characters. So in this picture over here, the line. This module is the one that has multiple cubes. Right. One of them is the output Q 222 00:29:10.920 --> 00:29:16.320 William Cheng: As I said, one of them is a partial IQ and there was a complete like you. They also managed the output Q over here. 223 00:29:17.010 --> 00:29:26.070 William Cheng: Okay, so this way. If you want to have all the character. All you need to do is that you need to get rid of this line. This one module and replace with another line. This is a module that only has one Q 224 00:29:26.670 --> 00:29:35.790 William Cheng: Okay. So this guy's over here. I'm going to call in this one the bare bone line discipline. This one will take all the data coming from the terminal driver and then they would deliver them all to the application. 225 00:29:36.810 --> 00:29:44.970 William Cheng: OK. So again, once you have one level interaction and it will give you a lot of flexibility so that any kind of align this one module can match with any kind of terminal device driver. 226 00:29:45.300 --> 00:29:58.950 William Cheng: Okay, you can actually also have a like this module that will handle the international keyboard, the European keyboard that can actually modify you know he followed by a single grow and change that into a prime and they can be done again inside instead of different I just have a module. 227 00:30:00.030 --> 00:30:05.160 William Cheng: OK. So again, this is the power of indirection. So once you have that, you can have a lot of flexibility. Yeah. 228 00:30:07.230 --> 00:30:14.370 William Cheng: All right. The last part over here. I want to talk about today is where to put all the modules. I mean, clearly the modules inside the Colonel. Where else can you put it 229 00:30:15.120 --> 00:30:22.920 William Cheng: Okay. So, so what we need to consider you know will notice on the opportunity to Sam on Thursday, those, you know, to all these module have to stay inside, Colonel. 230 00:30:23.340 --> 00:30:36.210 William Cheng: Okay, if they don't worry is that a corner where should they go what they have to go into the user space program. Okay, so. So one obvious place to put all these modules inside of Colonel, but there are also two other path possibility. One is to put it inside is 231 00:30:37.590 --> 00:30:45.150 William Cheng: Putting putting out a specialized program. Okay, we're gonna have a user process, the user process will take all the data out of the, you know, 232 00:30:45.810 --> 00:30:51.030 William Cheng: Out of the keyboard and then what it will do is I know distributed that the characters to all the other applications. 233 00:30:51.690 --> 00:30:58.830 William Cheng: Okay. So that would be one possibility right but but it seems to be, it can be really, really slow because whenever you type of key and he's going to the user space. 234 00:30:59.010 --> 00:31:05.580 William Cheng: And now when I tried to send data to other application as it goes through the Colonel. So in this case, you know, come down inside the cardinal and then go back into the user space. 235 00:31:06.000 --> 00:31:13.950 William Cheng: So that seems to be pretty so guys. So anyway, so. So maybe you need to worry about performance. The other possibility is that you put the other 236 00:31:14.820 --> 00:31:19.260 William Cheng: GUY, HE CAN PUT YOUR the kernel code over here into a user space library. 237 00:31:20.010 --> 00:31:28.800 William Cheng: Okay, so this way. If your program, try to read from standard in a standard is the keyboard. When you try to link with a C library instead of C library that's going to be aligned discover module. 238 00:31:29.460 --> 00:31:33.870 William Cheng: Okay. So typically we don't really put the terminal device driver into the user space. Why is that 239 00:31:34.920 --> 00:31:44.970 William Cheng: Okay, because the terminal device driver. Pretty much every instruction that you execute over there. They are all privilege instruction, right, where are they privilege, because they go over the system bus. They use memory map I O 240 00:31:45.450 --> 00:31:50.760 William Cheng: Is your application. A lot of us memory map I O or clearly they cannot do that, right, because it doesn't have any kind of, you know, 241 00:31:51.360 --> 00:31:57.360 William Cheng: Because if your application to access the bus directly that will give you a segmentation fall and the Colonel is going to kill your program. 242 00:31:58.050 --> 00:32:03.270 William Cheng: Okay, so therefore typically that the the actual terminal device drivers that are written by the manufacturer 243 00:32:03.450 --> 00:32:12.420 William Cheng: Every instruction is going to be privilege. So, therefore, you tend to keep them inside a kernel, even though there have been people who tried to move the device driver into the user space. 244 00:32:13.500 --> 00:32:20.940 William Cheng: Okay, so they don't ask them to talk about what kind of people do that right so you can actually move all this divided into the user space. So in that case, the performance will be terrible. 245 00:32:21.390 --> 00:32:30.630 William Cheng: Right, because every instructor, you actually hear we hear is going to be a privilege instruction. So you're going to trapped inside the colonel colonel says oh well you companies out of Colonel using a privilege instruction. 246 00:32:30.810 --> 00:32:35.130 William Cheng: But in this case, since it's the device driver. I'm not going to kill your program. I should do it for you. 247 00:32:36.270 --> 00:32:43.080 William Cheng: Okay, so in that case every machine structure, you got to try things that occurred only going to be trying to user space so that tends to be really slow. 248 00:32:43.470 --> 00:32:50.400 William Cheng: Okay, but some people will actually try that. Okay, so again, all these things are possibility. So whenever you try to you know will 249 00:32:50.880 --> 00:32:56.850 William Cheng: Whenever you write a kernel module. The first thing you need to think about is, I should just want to stay inside of current or should I go into the user space. 250 00:32:57.330 --> 00:33:07.230 William Cheng: There. So the typical decision over here is that if you put the device driver in the user space going to be too slow. So the device driver will go into the code will stay inside of Colonel, but what about the line. This one module. 251 00:33:08.130 --> 00:33:19.380 William Cheng: Get the latest one module can go into a separate user process. Okay, so that's a possibility. Right. It can also be part of the library routine. Okay, so this way, anybody who wants to, you know, reading up on the keyboard. 252 00:33:19.830 --> 00:33:27.180 William Cheng: Will you finish compiler Lincoln, you're going to copy co from the C library and then all the land is where module code is going to show up inside your application program. 253 00:33:27.960 --> 00:33:31.980 William Cheng: Okay, but in that case is going to be very, very careful because inside the current over here. When the Colonel. 254 00:33:32.460 --> 00:33:38.070 William Cheng: What we press the kings are the Colonel, we need to send the data from the device driver into the line. This one module. 255 00:33:38.640 --> 00:33:44.700 William Cheng: Okay, so this case, you know, we have many, many application. Each one of them has a separate line. This one module when you press the keys out of kernel. 256 00:33:45.000 --> 00:33:50.790 William Cheng: Which one do you actually send it to the legacy module, though, it's going to be multiple every program is going to have a legacy module. 257 00:33:51.330 --> 00:33:58.410 William Cheng: Can you send the data into you need to have a way to figure out what what did deliver that that character into the right place right 258 00:33:59.190 --> 00:34:11.670 William Cheng: And why do you have to worry about that right because once I deliver this guy. So I said I press a character I deliver the a character into the user space program if it turns out I make a mistake. Is there any way for me to ask the character back 259 00:34:12.540 --> 00:34:19.470 William Cheng: Well, there's no way for you to do that because once you send into the application. Now the character is owned by the application if the application consumed it or be too late. 260 00:34:20.610 --> 00:34:27.180 William Cheng: Okay. So, therefore, you know, this case if there are multiple indices module. They're all in the user space. The Colonel needs to make sure they never make a mistake. 261 00:34:28.110 --> 00:34:34.050 William Cheng: Okay, so in the good old days they actually try to do that. So then, you know, so, so, so, so there's only one program. They're running inside the foreground. 262 00:34:34.260 --> 00:34:41.670 William Cheng: And the foreground. The program running is that for a while, will be the one taking all the characters all the other program. They're running in the background. They don't take character from the keyboard. 263 00:34:42.540 --> 00:34:53.250 William Cheng: Okay, so in that case you can actually move the land this with a module into the user space. So anytime when somebody press the key. You can find out you know which one Poland's readings that are currently in the foreground and he would deliver our characters to that. 264 00:34:53.730 --> 00:34:57.630 William Cheng: Okay. And if it turns out that you actually made a mistake. Well then going to be in big trouble. Yeah. 265 00:34:58.200 --> 00:35:06.390 William Cheng: So it is possible to actually implement the lenses with moderating the user's Facebook. Well, as we also see that, you know, they also, you know, the other possibility are 266 00:35:07.140 --> 00:35:08.130 William Cheng: Can actually be 267 00:35:08.490 --> 00:35:19.650 William Cheng: Implemented so whenever you try to implement a module instead of Colonel you first you have to ask you, can this one going to the user space. If I go into real estate. How much slow down is going to be if it turns out the performance going to be acceptable. 268 00:35:20.010 --> 00:35:21.930 William Cheng: Well then in the end of people start using that 269 00:35:23.250 --> 00:35:23.700 William Cheng: Okay. 270 00:35:24.810 --> 00:35:33.780 William Cheng: Of course you can also keep the lines of the module Ii Ii Ii inside colonel and that will also be fine. Okay, so why wouldn't you want to keep it inside of Colonel 271 00:35:34.560 --> 00:35:41.430 William Cheng: While because once you put inside of Colonel, you will be very difficult to change your code because every time when you change your code is going to end up with a new version of the operating system. 272 00:35:41.910 --> 00:35:49.320 William Cheng: Okay, it's much easier to change things that the user space library because in this case, you know, if you change something in the line just one module, you don't have to modify the kernel. 273 00:35:50.430 --> 00:35:58.140 William Cheng: Okay, so there's a preference, I should do all the stuff in the user space. The main question is how much performance. Are you suffering when you put them into user space. Yeah. 274 00:35:58.650 --> 00:36:09.060 William Cheng: All right, this is actually a good time to break. So next time we're going to continue to finish the the terminal device driver and then we're going to talk about the network and device driver and then 275 00:36:09.840 --> 00:36:14.580 William Cheng: I guess we're going to continue with with the chapter four and talk about virtual machines. Yeah.