WEBVTT 1 00:00:01.860 --> 00:00:08.220 William Cheng: Okay, this is the third part of lecture 10 so we're gonna take a look at interrupts 2 00:00:09.269 --> 00:00:16.410 William Cheng: So we're gonna you know no more street threats. Now we need to need need to add in the in the interrupt handling. 3 00:00:17.340 --> 00:00:32.790 William Cheng: A so so this part of the lecture slides. There are three different you know sub parts. The first one is talk about interrupt handling. So that's we're going to focus on today and next time we're going to sort of take a look at the other the other parts of the for work and directed processing. 4 00:00:37.770 --> 00:00:43.050 William Cheng: All right. Alright, so we have talked about how to handle to and also how to handle for 5 00:00:43.530 --> 00:00:54.720 William Cheng: So now we're going to add in, you know, one in three and CC CC walking and we just want to mention that, you know, when they're sticking CPU the blocking lock and blocking on log. They don't seem to work. 6 00:00:55.320 --> 00:01:03.630 William Cheng: But we mentioned that there's a few texts. So if you're if you're muted user space. And if the colonel can be synchronous. Well then, it looks like there's a way to get it to work. 7 00:01:03.900 --> 00:01:13.200 William Cheng: OK, so for can work it. This is the user threat right so now we're going to sort of see the complete solution or how to make the Colonel. Hahaha. How to make the colonel synchronous 8 00:01:15.360 --> 00:01:21.870 William Cheng: Alright, so we're going to focus on one first and then we're going to worry about three. Yeah. So now we're going to turn on interrupt so 9 00:01:23.070 --> 00:01:29.610 William Cheng: So the way we're going to look at this will then take a look at two different cases. What is the most simplest case. The other one is a sort of a more complicated case. 10 00:01:30.270 --> 00:01:42.570 William Cheng: The first day is going to be a non Priyanka, Colonel. Right, so it. Okay, you need to understand exactly what a what a number of the colonel means get in these kind of system. The Colonel thread can never be preempted by another Colonel thread. 11 00:01:43.650 --> 00:01:50.340 William Cheng: There, which means that the colonel threat can be prevented by the interrupt service routine. But you always go back to the same kernel threat. 12 00:01:50.640 --> 00:01:57.840 William Cheng: Okay, so that's a very, very important distinction about going back to the same sort of going back to a different kernels that they're in a non created the Colonel. 13 00:01:58.230 --> 00:02:04.830 William Cheng: Guy. If you're running a colonel threat, then if you get, you know, if you get a holler interrupt you will go back to the same threat. 14 00:02:05.520 --> 00:02:12.780 William Cheng: Okay, so therefore, in a way, you didn't really give out the CPU. Why you give other CPU to handle interrupt service routine. But in a way, you're really not interrupted because you get you 15 00:02:13.170 --> 00:02:15.180 William Cheng: Get to continue to finish what you were doing before. 16 00:02:15.990 --> 00:02:23.100 William Cheng: Right. Alright, so, so, so, so you may switch that you know up the contest temporarily, but you're always return to the same threat contacts. 17 00:02:23.400 --> 00:02:36.450 William Cheng: Okay, so that's what it means by a non preempted, Colonel. OK. So again, now 300 Colonel doesn't mean that you cannot get preempted by anything you can certainly get printed by interrupt, but you always go back to the same thread. So therefore you cannot be preempted by another sweat. 18 00:02:37.770 --> 00:02:39.450 William Cheng: Okay, so some system out there. They have so to have 19 00:02:39.870 --> 00:02:51.810 William Cheng: A real threat priority. So in that case, if you're running inside of CPU and another thing that comes along as highest priority or in that case it will prompt you have the CPU and it will run another threat. So in that case, your swag preempted by a higher priority. So 20 00:02:52.320 --> 00:02:56.010 William Cheng: Okay, so, you know, now we have the Colonel. There's no way for another swear to preempt you 21 00:02:56.580 --> 00:03:04.170 William Cheng: Guys, okay, you can get printed out about even get printed by interrupt that you can never be preempted by another very, very important distinction. Yeah. 22 00:03:04.890 --> 00:03:13.470 William Cheng: Alright, so in this case the threat or the colonel thread, rather than running the privilege mo they emailed the processor only voluntarily. So if they don't call threat switch 23 00:03:13.890 --> 00:03:20.490 William Cheng: You know, they run on the CPU forever, even though they might go into Iraq contacts. But what he returns. He continued to finish what it was doing before. 24 00:03:20.970 --> 00:03:28.470 William Cheng: That. So the reason for this is that this makes the colonel a lot easier to implement because you don't have to implement locking inside the Colonel. 25 00:03:29.280 --> 00:03:40.560 William Cheng: Okay, so. So when would you have to log right so he so he started kind of listening. If you're modifying a link was when you're modifying the lingo is your I'm hooking the pointer and your hook your pointer. So now if you get if you can get preempted by another thing. 26 00:03:40.950 --> 00:03:46.260 William Cheng: If the editor wants to use the same kind of data structure. Well, then in that case. The other thing will get a segmentation file, so therefore that's no good. 27 00:03:46.860 --> 00:03:55.260 William Cheng: That. But if you know for sure that you cannot be preempted by another thread and now you need to, you know, modify a linked list over here. Do you need to lock the new tax. 28 00:03:56.160 --> 00:04:05.670 William Cheng: Day, the answer will be no right. You don't need to know. So again, you might a hokey, one of the pointers over here if you switch to the rock contacts. What are you going to come back and 29 00:04:06.030 --> 00:04:10.710 William Cheng: Come back and finish what you were doing before, right, because you cannot be preempted by another threat. 30 00:04:11.250 --> 00:04:17.490 William Cheng: Okay, if you allow another thread to execute what the industry over try to use the same data structure or in that case, in that case of carnival crash. 31 00:04:18.390 --> 00:04:26.640 William Cheng: Okay, so therefore once we have a number of the Colonel. Well, we try to, you know, we try to modify linguists. We don't have to lock them your tax. 32 00:04:27.150 --> 00:04:38.010 William Cheng: Okay, so this way, your kernel is going to be a lot easier to implement that. All right. So, so you don't have to implement, you know, locking he's had a kernel for every share data structure. Okay. 33 00:04:38.700 --> 00:04:44.550 William Cheng: Over here, it says, you know, although sometimes the new types is still needed to synchronize the colonel threats. So, so 34 00:04:45.300 --> 00:04:56.250 William Cheng: So why is that right, if you look at your kind of want a sideman you kind of want to send that has to function. You have to implement one is called K new tax law and the other one is called Kimmy rights law. I just said that. We don't need Konami tax. 35 00:04:56.790 --> 00:05:06.030 William Cheng: Rate. So why do we have so. So in this case, why are we required to do a Tu, Tu, Tu, Tu, Tu, Tu, Tu lock and unlock me Texas at the Colonel, get the reason for that is the following. 36 00:05:06.750 --> 00:05:12.390 William Cheng: Now, so we need to deploy our witnesses them right so hobbies, like they they're not trying to be super 37 00:05:12.750 --> 00:05:18.870 William Cheng: super efficient so so so let's say that you have to threats gay. So let's have the here's the iOS device over here is our devices. 38 00:05:19.170 --> 00:05:30.060 William Cheng: sitting across the bus over here, right, we have two threads that they're trying to use IoT device or one thread over here is going to help us the I O. And then the other side also want to use the IoT IoT devices over here. What is that a lot 39 00:05:31.980 --> 00:05:39.990 William Cheng: Is that really allowed. Okay, so I'm going to have the iOS device will be yours is, for example, it's at this you target this to transfer data from one place on it. This into memory. 40 00:05:40.230 --> 00:05:45.450 William Cheng: If the other threads over here also come here and tell that this controller do something else that will be really confusing. 41 00:05:46.110 --> 00:05:50.460 William Cheng: Okay, so therefore, in order for you to prevent to Colonel threats from from from 42 00:05:51.450 --> 00:05:59.160 William Cheng: If you want to prevent to Colonel first. I'm working on the same IoT device. So what we can do is that we can put the entire IoT device into a civilization box. 43 00:05:59.370 --> 00:06:03.330 William Cheng: We're going to put it into a civilization box right here. And then we're going to put a new tax in front of it. 44 00:06:04.200 --> 00:06:11.760 William Cheng: That. So this way in order for any Colonel Fletcher tried to use the use the IoT device they have to acquire the new types first 45 00:06:12.360 --> 00:06:21.510 William Cheng: Okay, so this way when the first, you know, for, for in the first Colonel so I try to use our device. It needs a lot of Utah's once you lock them into this can authorize executing inside the inside the 46 00:06:21.900 --> 00:06:29.070 William Cheng: Box and this can actually fall asleep right because if you started to devise over here, you're going to do the CPU, you're going to go sleep in a queue and waiting for the 47 00:06:29.340 --> 00:06:37.380 William Cheng: Hallway interrupt. In the meantime, when the second kernels that comes in over here in it needs a lot of meetups first. So therefore, the colonel to add over here is going to sleep inside the new types queue. 48 00:06:37.710 --> 00:06:45.150 William Cheng: Until the first threat is completely finished. Okay, so the first kernels over here is going to sleep inside the IQ. 49 00:06:45.660 --> 00:06:57.300 William Cheng: Guys so first over here. He's going to sleep. He said, I will kill until the auto completion interact with you on board the colonel threat. So in that case, the Colonel's they will come out of this here is actually box over here. And then the second kernels economic cannot come in. 50 00:06:58.470 --> 00:07:09.060 William Cheng: Okay, so. So again, the reason over here that you need a new text in the kernel, even when you have a number of the kernel is that you want to synchronize, you know, multiple Colonel threads for accessing the same iOS device. 51 00:07:10.560 --> 00:07:20.010 William Cheng: Okay, so, so, so, so, so again, not for modifying Inglis before accessing the same device because you don't want to kernels us to access the same IoT device, you know, at the same time. 52 00:07:21.570 --> 00:07:32.310 William Cheng: Right, so this is done in the early access and also this is done in the winnings operating system. Okay, so once you set driver equals two, one, you know, the inside your kernel assignment. 53 00:07:32.970 --> 00:07:39.810 William Cheng: Okay, so at the end of kind of why you're supposed to drive or equal to one. And now you can't get into it, you can actually get a interrupt interrupt there. 54 00:07:39.990 --> 00:07:47.190 William Cheng: And also drive or equal to one, that means that we are turning on device driver. So, therefore, you try to access any devices while you have to follow this rule. 55 00:07:48.180 --> 00:07:59.130 William Cheng: Okay, so the code for you. You're the CO for access and devices are all waiting for you by the Brown University people so we get a library file that have the older implementation right so what they would do is that you know as soon as you 56 00:07:59.370 --> 00:08:06.300 William Cheng: Said driver equal to one, you can actually set a breakpoint and Ken new tax law and Kimmy attack on log and then you will see that your co actual get executed. 57 00:08:07.140 --> 00:08:16.590 William Cheng: Okay, and that's because you got, you know, when these kind of all Colonel when Colonel through I tried to access our device. He will lock the new types first and then it will start going to access into our device. 58 00:08:17.100 --> 00:08:22.590 William Cheng: So, you know. Well, again, if you think about your device driver code. They're aware of function pointer. Right. The first one is read the second one is right. 59 00:08:22.800 --> 00:08:30.210 William Cheng: At the beginning of every one of those function. It's a new tax law because they need to lock this new tasks. Once they acquired a meat AX, then it can go work on this device. 60 00:08:30.510 --> 00:08:42.660 William Cheng: Okay, so every device is going to have a separate new tax. So in order for you to assess device. First you have to acquire new tax so again this is done for a this is done for the operating system because it makes it really easy to implement. 61 00:08:44.430 --> 00:08:57.210 William Cheng: All the stuff you know so. So in this case, so if you have a non preempted, Colonel. And what if the link Lissa. You're a modifier over here, turns out to be a linguist that also get used by inside interrupt service routine. 62 00:08:58.230 --> 00:09:03.510 William Cheng: Okay. Is your data structure inside the colonel that your third will use and also the interrupt service will tell us 63 00:09:04.110 --> 00:09:11.310 William Cheng: What the answer will be yes. And what would we got data structure. Okay, so that data structure is the wrong queue, because why opinions or interrupt service routine. 64 00:09:11.610 --> 00:09:21.900 William Cheng: Instead of, you know, observers who to unblock a threat, and then you start an exile operation when you're on blockers thread you pick up a threat from IQ and you move into the rank you 65 00:09:23.070 --> 00:09:32.580 William Cheng: Okay, so one of the data structure that's always going to get accessing sorry interrupt service routine is going to be the wrong to you. What about your house. Does your three co also use the wrong to you what we saw to just, you know, 66 00:09:34.320 --> 00:09:41.940 William Cheng: In today's lecture. Right. What were your threat you know when when you feel called switch right inside first which you are using the wrong. Q. 67 00:09:42.390 --> 00:09:47.100 William Cheng: Okay, so there's one data structure that shared by your threat call and share about, you know, our service with you go 68 00:09:47.550 --> 00:09:55.470 William Cheng: Okay, so definitely this case. How do you make a number after Colonel work, what happened is that when you're accessing the run to you. You have to mass. The interrupts 69 00:09:56.160 --> 00:09:58.770 William Cheng: Okay, so one solution over here that we're going to use inner masking 70 00:09:59.310 --> 00:10:04.830 William Cheng: When we try to use variable that's being shared between your flight contacts and enroll contacts. Right. So once you 71 00:10:05.190 --> 00:10:12.150 William Cheng: Master these interrupts. Well, then in that case if you know what's going to happen. They will become pending, and then when you finish updating the the 72 00:10:12.660 --> 00:10:20.100 William Cheng: When you finish using the wrong to you. So, at that time, you can actually a massive the interrupt. Okay, so, so, so it's a very simple solution. 73 00:10:20.640 --> 00:10:29.850 William Cheng: This is almost like a like a chapter to write what you gonna you know when the data structure that they they try to access is also feed the same data structure that's used inside the 74 00:10:30.330 --> 00:10:43.590 William Cheng: Inside of signal handler, right, we saw that that that example sort of the second example. You know, when you have, you know, a long running process. Right. So in that case, what we do is that we we we because sick masks. 75 00:10:45.570 --> 00:10:51.210 William Cheng: Will call one of the sick max function to to to to broaden the signal and then we go access 76 00:10:52.020 --> 00:10:59.670 William Cheng: We don't modify are linked lists. And at the end, we're done. Then in this case we call success mask and then we would sort of signal mass back to what it was before. 77 00:11:00.390 --> 00:11:13.050 William Cheng: OK, so I'll be here for hallway interact with do exactly the same trick when we are when we're ready to modify the wrong. Q. We brought the interrupt. And then when we finish, you know, modify modify drunk, you will unblock the interim. OK. So again, the same idea. 78 00:11:15.210 --> 00:11:18.510 William Cheng: The seven kids over the years. What do you do with it with a preemptive Colonel 79 00:11:18.990 --> 00:11:27.330 William Cheng: Okay, so we have the Cardinals if you're running in the you know kernels, where you can get preempted by another kernels wet. Okay. The solution over here. Sounds like a cop out 80 00:11:27.810 --> 00:11:31.470 William Cheng: OK, so I'll be here. So that's good. What do you do you disable preemption. 81 00:11:31.980 --> 00:11:38.370 William Cheng: Okay, so, so when you disable preemption. If you have a preemptive colonel. Now, you become an employee of the colonel and then we know what to do with number empty Colonel 82 00:11:39.300 --> 00:11:43.350 William Cheng: OK, so the other the sushi is very simple. You say, Well preemption. How do you disable preemption. 83 00:11:44.220 --> 00:11:53.880 William Cheng: Okay, so all you have to do is just set a global variable. He's had a colonel to say now a presumption is disabled. Okay, so this way when you go into the internal contacts you come back to the same threat. 84 00:11:54.990 --> 00:12:00.510 William Cheng: Guys. Okay. Well, you have a preemptive Colonel when you go into the investors who tea and then you can actually come back to a difference there. 85 00:12:00.660 --> 00:12:06.750 William Cheng: So right before you do that, you check the global variable. If the goal rebel to say you are not allowed to do preemption. Well, then you go back to the same threat. 86 00:12:08.160 --> 00:12:14.160 William Cheng: Okay, so, so at this level preemption is super easy. Just use a goal rebel. And then he said, you know, every interrupt service routine. 87 00:12:14.580 --> 00:12:19.920 William Cheng: You check that flag to see if you if you can go to a different way or you have to go back to the same time now. 88 00:12:20.610 --> 00:12:28.800 William Cheng: Alright, so once you disable preemption. Then again you can use interrupt masking. Okay, in case you want to use the same data, is data structure that's used by the interrupt service routine. 89 00:12:29.280 --> 00:12:36.840 William Cheng: And also you can also said, Well, what about for multiple CPU, what, when, when there's multiple CPU, you can use spin lock to lock out the other CPU. 90 00:12:37.170 --> 00:12:45.270 William Cheng: Okay, so now we're actually pretty. So I'm going to show you a complete solution, you know, for all different kinds of, you know, in interference. 91 00:12:46.020 --> 00:12:54.000 William Cheng: With me Texaco. Okay. So, this way, this way we can deal with interrupt. We can also do what preemption. We're going to also deal with multiple CPU case they're 92 00:12:55.680 --> 00:13:04.050 William Cheng: All right. Alright, so, so, so how do you mess it up guy so. So for example, if you're, you know, so, so the 93 00:13:04.620 --> 00:13:13.110 William Cheng: The sort of sometimes the horrors of some sort of will help you out. When you are servicing and, you know, while we are executing interrupt service routine. Right, so go the way we sort of think about is that 94 00:13:13.590 --> 00:13:21.630 William Cheng: You know the CPU is going to implement it interrupt a previous level right so if you said if you know IPL equals to, let's say, a 95 00:13:22.350 --> 00:13:27.840 William Cheng: Certain value. So if I say I say IP or equal to five, then all the, you know, all number from zero to five, they're all block. 96 00:13:28.710 --> 00:13:38.760 William Cheng: Okay, so if you're servicing. You know, I've level a level five, then in that case IPO is going to be set equal to five. So therefore, at that point, you know, all the other interest will get bought the older off on zero to five. 97 00:13:39.060 --> 00:13:43.140 William Cheng: There, blah but ANYTHING FROM six All the Way to highest interest level, they are unblock 98 00:13:44.400 --> 00:13:52.860 William Cheng: OK, so again this is, you know, there's very easy to do. So again, you know, even some CPU, they use, you know, they, they use those that did vector 99 00:13:53.190 --> 00:14:06.330 William Cheng: To control which interrupts be mass. They will also implement the IPL level inside hardware abstraction layer. So, therefore, if you said IPL level into whatever number from zero to that level all the, you know, our blog and all the high level. 100 00:14:08.340 --> 00:14:08.640 William Cheng: Then 101 00:14:10.170 --> 00:14:17.550 William Cheng: Alright, so, so let's take a look at example to see how this how this can be done so. So we're going to take a look at two pieces of code over here. 102 00:14:17.760 --> 00:14:22.860 William Cheng: They're accessing the same variable inside the three contacts and inside the interrupt contacts. 103 00:14:23.250 --> 00:14:30.750 William Cheng: So we're going to sort of execute the same code over here, x equal x plus one over here. This one is a third call and this one is inside the interrupt service routine. 104 00:14:30.960 --> 00:14:42.870 William Cheng: Yeah. So again, instead interrupt service routine the IP is going to be set at some level. So let's say that this is the web servers routine for the disk. And then the disc in service interrupt over here is going to be IP. I go to seven is over here IP. 105 00:14:43.260 --> 00:14:57.960 William Cheng: Will be equal to seven. When you're executing the scope. Okay, so as soon as you know I suppose we start executing that interrupt level from zero to seven. They're all block. Okay. And, you know, from eight all the way to, you know, whatever 31 or whatever 128 they will all be on block that 106 00:14:59.040 --> 00:15:08.610 William Cheng: So in this case. So let's take a look at the case without a second CPU and now we need to handle the case where there's a you know observers routine. Right. So this is the company web servers again, and our threat code over here is executing the pink threat. 107 00:15:09.300 --> 00:15:12.660 William Cheng: Okay, so in this case hardware block out this, you know, how do we actually 108 00:15:13.140 --> 00:15:23.340 William Cheng: Part of this code. Okay, so remember this coca only executing the only execute when we are servicing interrupt level. Number seven. So if we plug in Rob level seven 109 00:15:23.760 --> 00:15:37.320 William Cheng: So if the code over here if we said IPL equal to seven. Then in this case, you know, our level seven is going to be blocked. So, therefore, you know, interrupt us level seven happen, you will become pending until we dropped it into our level below, Sarah. 110 00:15:38.340 --> 00:15:46.650 William Cheng: Okay, so all we have to do over here is that before we asked us to share variable we're going to change the IP level on to, to, to be the one that correspond to disco. Yeah. 111 00:15:48.030 --> 00:16:01.140 William Cheng: So so so that sort of CO look like this nice over here. I'm going to call a function called said IPO said IP over here, this will be the right API level. So in this case, if this code is what IP out seven right so this will be sending you Posada 112 00:16:02.010 --> 00:16:07.650 William Cheng: Okay, so this case set IPO. So you also sort of sort of thing about the internet previous level. 113 00:16:08.160 --> 00:16:16.350 William Cheng: It's actually the contents of your process. So, therefore, if you want to change the interrupt a previous level, you should say the contacts and later on they only can restore it. 114 00:16:16.710 --> 00:16:21.300 William Cheng: Okay, so, so, therefore, the way we're going to bed is that we're going to click local variable, we have to say old IPL 115 00:16:21.510 --> 00:16:28.020 William Cheng: When we consider IP all over here we're going to change the IPL to seven and then we're going to store their current IP out into the local variable via 116 00:16:28.560 --> 00:16:32.040 William Cheng: Okay, what is the current logo. What is the current IP level over here. 117 00:16:32.700 --> 00:16:40.530 William Cheng: Well, if you think about it, right, if all the IPL level, they all correspond to interrupt service routine. Well then, when the IPL up or equal to zero, that means that there's no interrupt. 118 00:16:41.130 --> 00:16:46.140 William Cheng: Okay. So, therefore, if you're in the right context that in that case IPL level here will equal to zero. 119 00:16:47.070 --> 00:16:56.220 William Cheng: Okay. So in this case, when you execute said IPL over here, your career. I mean, since you're in their contacts that means that the IP other where you go to zero. So, therefore, in this case, old IPL will be he'll be equal to zero. 120 00:16:56.910 --> 00:17:07.110 William Cheng: Okay, and then why she said, I feel, go to zero. Can this interrupt service wouldn't get executed what the answer will be no, because this was executed into a level seven and you brought into Rob level seven already 121 00:17:07.770 --> 00:17:17.010 William Cheng: Guys, so therefore this code is block, right. So what you will do is that you can safely do X equals 01 so again. Excellent, excellent won't get combined two three machine instruction, you're going to execute them. 122 00:17:17.310 --> 00:17:21.030 William Cheng: You know, in one atomic operation relative to the variable x. 123 00:17:21.570 --> 00:17:30.810 William Cheng: OK. And then when you're done without you said IPL to be the previous value. So I will I be over here you go to zero. So, this guy's you're going to bring it down to zero. And as soon as you bring it down to less than seven. 124 00:17:31.020 --> 00:17:38.220 William Cheng: If there's a pending interrupt and the interval get deliver and this code will start executing when this course start executing IPL will be set to level seven 125 00:17:39.210 --> 00:17:46.590 William Cheng: So therefore, again, this code over here would be able to execute execute x plus one and your throat coat over here will not be able to execute. Why is that 126 00:17:47.760 --> 00:17:54.990 William Cheng: Right, because your threat is executing an API level equal to zero. So, if you are servicing interrupt local number seven, then IP. I said to seven. 127 00:17:55.140 --> 00:18:01.500 William Cheng: If I'd be able to sell that. Well, then your circle. What I'd be able to execute. So therefore, get this code and this call now become mutually exclusive. 128 00:18:03.240 --> 00:18:07.470 William Cheng: Okay, so, so, so, so in this case. Yeah, this doesn't work. Yeah. 129 00:18:08.310 --> 00:18:16.650 William Cheng: Alright, so, so again, x equal x plus one mean what is Julie x, right. So again, let's be a little more realistic. All right, what kind of data structure is actually share 130 00:18:16.860 --> 00:18:20.100 William Cheng: Between your thread code over here and also be told, Pete, you know, interrupt service routine. 131 00:18:20.370 --> 00:18:30.480 William Cheng: Okay. As it turns out, there are two days or two one is around q i mentioned before, when your thread cold calls. Let's switch your access and run queue instead of interrupt service routine, you're going to unblock us where you can access the wrong cute. 132 00:18:30.960 --> 00:18:35.940 William Cheng: Okay. But there's another data structure that that's shared between your threat. Also, you know, the 133 00:18:36.450 --> 00:18:45.300 William Cheng: email service routine. So remember when you unblock a threat inside your interrupt service routine if this one is is iOS device right then you'll Colonel threat is seeking the IQ. 134 00:18:45.810 --> 00:18:48.780 William Cheng: Okay, how does the current that go inside. Go, go, go. See, these are i o que 135 00:18:48.990 --> 00:18:58.500 William Cheng: What before we call threat switch it has to add itself to the I O que so again the I O que data structure over here is going to be the data structure that shared between your threat CO and interrupt service routine code. 136 00:18:59.520 --> 00:19:10.710 William Cheng: Now, so again, so there are two datasets. We need to worry about what is the wrong to you and the other one is the IoT device that. So let's take a look at an example, see what I look like. So that's a little more realistic than x equal x plus one. 137 00:19:14.160 --> 00:19:23.550 William Cheng: Alright, so let's say that you are you are you know your your user space program make the right system call you come inside the Colonel, you call the kernel version of the right system call and then eventually you know 138 00:19:23.850 --> 00:19:33.450 William Cheng: You, you, you, you need to talk to the desk. Okay, so the last function that you're called over here is called this right. So this right over here. So you're executing this code as a colonel. 139 00:19:33.780 --> 00:19:40.050 William Cheng: The colonel. So I was going to call this right to write to the desk right I mean of course there's other function like displayed right or refund the Kibo all these 140 00:19:40.500 --> 00:19:42.600 William Cheng: So this example we're going to write to the desk. 141 00:19:43.170 --> 00:19:52.470 William Cheng: That and the bottom part over here, this is that this interrupt service routine. Well, obviously this interrupt over here. So we execute the code over here IPL is going to be set equal to seven. 142 00:19:52.950 --> 00:19:56.760 William Cheng: Now, so in this case, you know, again, what will we do inside the disrupt function. 143 00:19:57.120 --> 00:20:06.270 William Cheng: Okay, so, so, so when you try to write to the dance. The first thing you want to do is you want to start I operation and then you add yourself to that this week you and then you can switch to give up the CPU. 144 00:20:06.690 --> 00:20:11.970 William Cheng: That. So the call. We're looking at this, right, that are that are you going to do something. And then finally, you're ready to start our operation. 145 00:20:12.120 --> 00:20:23.190 William Cheng: So you call start, I have to start at this operation. And again, this is a different piece of hardware that can run in parallel with the CPU. So at some point we're going to add yourself to that this week you over here, right, and then ourselves as the currency. 146 00:20:23.670 --> 00:20:29.970 William Cheng: The currency is the one that's calling this function, right. So we're going to add ourselves to that this week you and then we're going to call swish swish to give up the CPU. 147 00:20:30.450 --> 00:20:38.910 William Cheng: Then, so now you know at some point later you know that this is going to transfer data is going to take a while with this finished transferring data, then what do we do with that who actually do the scope 148 00:20:39.270 --> 00:20:47.220 William Cheng: Okay, when I execute this code. So again, we're going to pretend that the that this interactive is that I feel equal to seven. I should. I don't know what will happen is supposed to be. 149 00:20:47.730 --> 00:20:56.850 William Cheng: So it should be this example, we're gonna set it to Sabah, and now we're going to start executing this girl. Yeah. Well, you said it to seven all the threat codes over here. They are all they all blocked out 150 00:20:57.510 --> 00:20:58.560 William Cheng: Now, so, he said. 151 00:20:59.070 --> 00:21:10.830 William Cheng: The discussion around what do you do, right. You can unblock a threat. So this way you're going to go to the it this way. Q over here on blog that swear by the queuing up bye bye bye bye bye bye teacher doing a threat from it this way. Q which is the one that you know 152 00:21:11.190 --> 00:21:17.880 William Cheng: That the colonel says sleeping on over here. And then over here, we're going to check it equal to. No, not if it's not equal to zero, then we're going to add it to the rank you 153 00:21:18.360 --> 00:21:26.550 William Cheng: Guys, again, that's what you're doing on blog blogger kernels. There you go to the thread. So you go to the queue that occur after sleeping, and you wouldn't move it into rocky 154 00:21:26.910 --> 00:21:33.750 William Cheng: Okay so clearly inside the web services anywhere access to data structure. One is that this way. Q. And the other one is around you. 155 00:21:34.260 --> 00:21:42.810 William Cheng: Okay, and also inside the disrupt function over here. We also asked us to Colonel data structure I what is it this way. Q. I'll be here and inside first which you access the rank you 156 00:21:43.860 --> 00:21:50.340 William Cheng: That. So if you write code like this, you know, there's actually a race condition you know so often is that, you know, 157 00:21:51.180 --> 00:21:56.160 William Cheng: What if, as soon as you start the operations over here. We're going to get this interrupt right away for some reason. 158 00:21:56.880 --> 00:21:58.170 William Cheng: Okay. So in this case, you know, 159 00:21:58.620 --> 00:22:07.830 William Cheng: So let's pretend that this control is actually pretty fast, and that data is already in the disk controller buffer. So it doesn't really have to access the hard drive. So in this case you will actually transfer data very quickly. 160 00:22:07.980 --> 00:22:12.300 William Cheng: And before you go through all three switch now that you know our service offerings are executing 161 00:22:12.870 --> 00:22:17.880 William Cheng: Okay so ominous at this course start executing. And again, your kernel throw. Would I be able to execute right because you know 162 00:22:18.210 --> 00:22:28.890 William Cheng: Colonel fair is executed IPL level zero. So you said IPI equal to seven over here and there you go to your DQ itself on the DIS que. So at this point, your career fair has even got on the DIS que yet. 163 00:22:30.000 --> 00:22:35.970 William Cheng: That. So in this case, we're going to teach you this one I'm going to check the equal to know as it turns out people to know. So therefore, we're not gonna, you know, 164 00:22:36.390 --> 00:22:38.610 William Cheng: nothing happen over here instead of disk interrupt. 165 00:22:39.450 --> 00:22:45.300 William Cheng: OK, so now we finished the dishonorable over here. We're going to set it off back to zero. And then the colonel fest are running again. 166 00:22:45.450 --> 00:22:52.860 William Cheng: When the concerts are running. I'll be here. What it will do is it'll add itself to the dispute over here to going to call threat switch to give up the CPU. So who's going to wake up the colonel so that 167 00:22:53.400 --> 00:22:57.690 William Cheng: Nobody's going to wake up the colonel threat. Well, so therefore this race condition over here. 168 00:22:58.110 --> 00:23:07.560 William Cheng: You know, it's going to cause a little bit of problem. Next time, another thread called the disk operation, while the next time we'll get woken up by, by this time, this kernel setup here is going to get stuck inside of this way. Q for a long time. 169 00:23:07.980 --> 00:23:11.730 William Cheng: Right, so this goes to remind you, this is pretty much the same thing as the first 170 00:23:13.110 --> 00:23:23.820 William Cheng: Sort of a signal example that we saw. You know what, what the sort of the signal on example. Right. Well, you try to move. We don't wait for a properly, you're going to end up missing the a lot that 171 00:23:24.510 --> 00:23:29.940 William Cheng: Right. So one of the solution over here. I had the solution over here is to block that you know of service routine, so that this Coke and I execute 172 00:23:30.300 --> 00:23:35.370 William Cheng: That. So what we should do is that when we come into the you know the this route this route operation. 173 00:23:35.610 --> 00:23:43.890 William Cheng: Before we start the operation because now we know that I operation can finish right away so therefore we have to block the interrupt service routine before we started I operation. 174 00:23:44.640 --> 00:23:51.090 William Cheng: Okay, so therefore the colon look like this right over here. Before we start I operation over here we're going to change IP out to seven. 175 00:23:51.600 --> 00:23:54.090 William Cheng: Right. So this way to, you know, observing something will not be able to execute 176 00:23:54.720 --> 00:24:00.390 William Cheng: Okay, so here we're going to save it in the local variable over here. And then when we finish over here. We're going to restore the IPL back to zero. 177 00:24:00.780 --> 00:24:10.050 William Cheng: And so in the middle of this way over here. We're going to start out with IP. I go to zero and then right before we start, I, oh yeah, we're gonna say IP or equal to seven. And we also going to save the contacts over here. 178 00:24:10.260 --> 00:24:17.220 William Cheng: Of the previous IPL level and then when we're done with all the operation when it is finished transferring data when we got woken up, then we're gonna say I got back to zero. 179 00:24:18.720 --> 00:24:19.860 William Cheng: So what's wrong with this code. 180 00:24:20.760 --> 00:24:27.720 William Cheng: What this course is that when you go you know so. So you said I feel equal to seven right here right when you when you call first which. What is your API level. 181 00:24:27.960 --> 00:24:32.910 William Cheng: Your IP. All I was actually equal to seven. So therefore, the does interrupt will never get delivered 182 00:24:33.540 --> 00:24:42.570 William Cheng: Right, because at this you know Rob is a level seven. So if that this enough get generated it will become Penny, who's going to set it all back to zero. Well I pass it back to zero right here, so therefore it's too late. 183 00:24:43.170 --> 00:24:52.290 William Cheng: Now guys are this case again you guys actually we'll get it, we'll, we'll get stopped for a long time. Okay. All right, so 184 00:24:53.520 --> 00:24:57.510 William Cheng: So, so again this is again very similar to to to to to the 185 00:24:57.960 --> 00:25:05.910 William Cheng: Example that we see for the blocking law, right. So, so one solution over here is I'm going to put this say IPL we have before switch right so the CO actually so 186 00:25:06.240 --> 00:25:14.280 William Cheng: So we can actually put the set it all up before that we had before, especially. So again, no matter where we move our code. We always going to end up with a race condition. Remember the 187 00:25:14.700 --> 00:25:25.590 William Cheng: The blocking lock example. Right. Well, you have to CPU running at different speed and we eventually still gotta get into trouble. Okay. So, therefore, no matter where you move this function to to, you know, eventually we're gonna get into trouble. 188 00:25:26.910 --> 00:25:33.960 William Cheng: Alright, so we here, we mentioned before, you know, for the for the blocking loud. Maybe you want to put a spin lot in Southwest where it says that's really a bad idea. 189 00:25:34.200 --> 00:25:39.990 William Cheng: Up and now we can actually use a different approach over here because inside threats which you can actually manipulate the IP else 190 00:25:41.310 --> 00:25:49.740 William Cheng: Okay, so therefore, even though we're going to switch with IPS n equal to seven. Who says that we are not allowed to set it says, I said IP off back to zero insights research. 191 00:25:50.430 --> 00:25:55.320 William Cheng: Okay, so if we do that, then the enroll will get delivered. Then in this case our Colonel to the concept or 192 00:25:55.950 --> 00:26:04.590 William Cheng: Blog, etc. Yeah. So now let's take a look at the solution for this one. So what we're gonna do is, I'm going to look at this right code over here to say that this code is actually correct. 193 00:26:05.160 --> 00:26:11.820 William Cheng: Okay, it's just that are threat switch call that we saw before. It didn't work. So now we need to fix the switch. Right. Why is this code. Correct. 194 00:26:12.390 --> 00:26:17.010 William Cheng: Right, because in order for me to block the the operation from delivery, they interrupt right away. 195 00:26:17.190 --> 00:26:25.500 William Cheng: I should said IPI equal to seven to block on disk and raw and then I started I operation right and then eventually when I'm done, I need to return the other the IPR back to zero. 196 00:26:26.340 --> 00:26:31.620 William Cheng: That. So we're going to declare that this code is actually perfect all we need to do is to fix the first which 197 00:26:32.910 --> 00:26:40.080 William Cheng: Then alright so let's take a look at the final version of threats which, why not not not final version we're getting pretty close. 198 00:26:42.480 --> 00:26:50.940 William Cheng: Alright, so the most important part about this slide is that you can use this. The Colonel one and you should guide people you know come to, you know, 199 00:26:52.710 --> 00:26:58.500 William Cheng: Ask me a question and then eventually I asked them, Is your first which called look like the lecture slides I said no. I said, why not 200 00:26:59.400 --> 00:27:08.190 William Cheng: Okay, this call is given to you instead of, instead of textbook for good reason. Because you're supposed to do it like this and your kernel assignment, otherwise you're switching is not gonna work. 201 00:27:09.060 --> 00:27:15.090 William Cheng: Okay, so, so, so let's let's talk about this call to see why would it work yeah alright so Fletcher is gonna get a little more complicated out 202 00:27:15.330 --> 00:27:20.880 William Cheng: There so again you know what your threat. The way the way it cost us, which is that you add yourself, your, your 203 00:27:21.090 --> 00:27:30.150 William Cheng: add yourself to i o que over here and then you can switch over here to do our CPU my. Similarly, you can actually add yourself to a new tax queue and then you call so I switched over CPU. 204 00:27:30.690 --> 00:27:36.630 William Cheng: So this coast, your work no matter, you go to sleep in a new taxi or condition variable QR IoT or any kind of cute. Now, 205 00:27:37.320 --> 00:27:44.700 William Cheng: Right. So what we're going to do is that you know as soon as we come into stress which the first thing that we're going to do is I'm going to set IPL to the highest possible level. 206 00:27:45.150 --> 00:27:54.720 William Cheng: Okay, in the good old days into only have 32 IPL level. So to go from zero to 31 and 31 will be the highest one is over here, we're gonna do is, I'm going to send it out to 31 207 00:27:55.530 --> 00:28:06.870 William Cheng: Okay, so again, you know, we're going to store the current IP or value. We're going to save the content and restore the contacts. So at this point, the example that we use when we come into the first question over here at this time IPS is equal to seven. 208 00:28:07.800 --> 00:28:14.280 William Cheng: Okay, so we can set it out. We're going to store this functions going to return a current IPO. So this guy's IPO will be equal to seven right here. 209 00:28:15.210 --> 00:28:23.160 William Cheng: That's what we can also draw timing diagram right, I'm done. I'm just gonna look like this, like this over here right horizontal axis is time. So on the left hand side over here is going to be 210 00:28:23.490 --> 00:28:31.140 William Cheng: The vertical axis over here is going to be IPL okay at the beginning of this switch. It says, what's the function. Right. This right over here is equal to zero, I said. 211 00:28:31.560 --> 00:28:38.460 William Cheng: The beginning is going to go to zero, right. So this is over here. Good. And Scott did this. Right. And then this role was set it to seven. So there's equal to seven. 212 00:28:38.880 --> 00:28:46.410 William Cheng: OK, so now you're going to enter threats which, as soon as you enter through this way. So let's say that the highest IPOs equal to 31 so now we're going to set it to 31 213 00:28:48.060 --> 00:28:52.440 William Cheng: Yeah. Why do we say this is 31 what because we need to access the wrong. Q. 214 00:28:53.220 --> 00:28:58.860 William Cheng: Okay, because run queue is being accessed in all interrupt service routine, right, because all you observers routine. 215 00:28:59.070 --> 00:29:06.660 William Cheng: They're going to unblock it through. So therefore, every you know observers routine. They all have to access the wrong key. So if I want to access the wrong. Do I need to block out all interrupt service routine. 216 00:29:07.230 --> 00:29:16.410 William Cheng: Okay, so there was the first thing I would do is I said IPR 222 31 and now I can I can I can I can use a rescue, right, because otherwise it will be unsafe to use the wrong cute. 217 00:29:16.950 --> 00:29:30.090 William Cheng: Okay. So, therefore, what I would do is I would check before we always assume the rump monkey or something to do right now, we can actually check whether the monkey was empty or not, right, if the queue is empty over here. What should we do so, by the way, why should the rank up empty. 218 00:29:31.830 --> 00:29:41.340 William Cheng: WELL, THAT'S WHAT DID YOU THINK ABOUT ALL YOUR KERNEL threat your kernel throughout. You know, they are you know if something that they are there actual ratings run, they will be instead of funky. Right. So. So again, you mentioned McKay, you only have one CPU right 219 00:29:41.490 --> 00:29:45.300 William Cheng: Again, we're not considering a case where there's multiple CPU right now. Okay, there's only one CPU. 220 00:29:45.600 --> 00:29:55.920 William Cheng: Right, so your thread over here is going to execute all the other side, they're all sleeping. Some of them in the wrong key with some of their IQ. Some of them in the new tax you so if the wrong Q is empty. That means that we're all your threats. Well, you're 221 00:29:56.310 --> 00:30:01.440 William Cheng: either going to be on the IQ, or they're going to be the new tax you or they're going to be the condition BBQ. They're all sleeping somewhere. 222 00:30:02.910 --> 00:30:12.750 William Cheng: Okay. So in this case, the reason that you know since over here, we already you know started I operation right so we know that at least one thread over here is going to be waiting for I O 223 00:30:13.860 --> 00:30:25.560 William Cheng: OK. So, therefore, is the monkey is empty then we have nothing to do. So in that case, we need to worry about what MAYBE THAT'S WHERE IS WAITING FOR INTERRUPTING get delivered. Right. Right. No, I feel equal to sever. How do we deliver that, you know, we're going to change IP up back to zero. 224 00:30:26.700 --> 00:30:34.950 William Cheng: Then, so yeah, there's no threat that's ready to run inside the CPU. So therefore, they're all sitting, you know, waiting for something, right. One of them is going to be waiting for an IOU interrupt. 225 00:30:35.310 --> 00:30:40.980 William Cheng: You guys. So therefore, we're gonna say, I tell you what a 00 over here. Now, if at this point there's no pending. 226 00:30:41.310 --> 00:30:47.490 William Cheng: Then that you know all get deliverable. You know, I'll get the liberal going to unblock a colonel so that so next time when you check the monkey the monkey when I be empty and more 227 00:30:48.540 --> 00:30:58.380 William Cheng: Alright, so we need to get out of the infinite loop over here, the way we get are influenced by setting IPL to zero. So, therefore, if there's a pending interrupt interrupt get deliver and now we have something to run inside around cute. 228 00:30:59.070 --> 00:31:09.120 William Cheng: Guy. If there's no pending lawsuit. We do I. So if you're going to sit IPL back to IPO high level set it back to 3131 right here. And why do we need to set it back to 30 why 229 00:31:09.570 --> 00:31:10.920 William Cheng: Because we need to access the wrong queue. 230 00:31:11.340 --> 00:31:18.510 William Cheng: Right, whenever you try to use a run to you better make sure the IPs that has 31. So next I'm going to call I SEE THE MONKEY. THE MONKEY. So let's say that the interrupted and get deliver 231 00:31:18.750 --> 00:31:27.720 William Cheng: OK, we have no pending and Rob. So Rob is still equal to 30 so if you go to city wide receivers to empty. Right. So you said, I go to zero that and then, you know, nothing happens you go 232 00:31:28.020 --> 00:31:34.950 William Cheng: To sell you why. So we're going to go up and down like crazy for a while. Okay. Eventually, is the interim gonna get deliver 233 00:31:36.120 --> 00:31:42.030 William Cheng: What the answer will have the BS right because we started this operation. So eventually, you know, you know, the disk and Rob is going to get the liver. 234 00:31:42.090 --> 00:31:49.560 William Cheng: So at some point when we said IPI equal to zero, that this interview is going to get delivered, and then we're going to go execute this code that we saw before, organized. So actually, in this call. 235 00:31:49.680 --> 00:31:56.370 William Cheng: So in this case, there will be a threat inside the inside I O que so we're gonna do we're gonna take us where we're going to enter a rank you 236 00:31:57.270 --> 00:32:04.890 William Cheng: Guys, so therefore that code will get executed. So, so in that case when we said IP back to hire over here, then in that case the queue. And I'll be empty. 237 00:32:05.790 --> 00:32:13.950 William Cheng: And so cute will not be empty now can actually teach yourself on that for the monkey. So therefore, we're gonna we're going to use the old code that we had before, that the beginning of 238 00:32:14.430 --> 00:32:24.150 William Cheng: This lecture, we have a we have a new switch code, right. So, therefore, the old current over here is going to be the current that. So what is the current that the current says the sweat actually construct switch 239 00:32:26.040 --> 00:32:32.190 William Cheng: Right, because we haven't really switched to any threat. Yeah, right, because the run through has always been empty over here. So, therefore, you know, the thing that comes in over here. 240 00:32:32.400 --> 00:32:42.330 William Cheng: That will be the author and the current slide over here is going to be the one that the queue for the Rutland you from the run queue. And now we're going to call swap contacts and we're going to save the current that's contact into, you know, 241 00:32:42.720 --> 00:32:50.400 William Cheng: Into the second to block, you know, for, for, you know, for, for, for, for the previously running through and then we're gonna go to the news, where hey the data structure, the 242 00:32:50.880 --> 00:32:59.430 William Cheng: Take, take a data contract instead of good researcher and then put it back into the CPU. So when I saw contacts we turn over here that we're going to execute the next instruction. 243 00:33:01.440 --> 00:33:11.760 William Cheng: Okay, so, so again, right at this time IP is still high, right, because we actually the monkey over here. And then we act as a monkey over here. Okay, so this guys were saved excellent monkey because it is equal to 31 244 00:33:12.120 --> 00:33:18.930 William Cheng: That. And then finally, we're done over here when we said IPL back to the fact that all IP out what is the value of the IPL is equal to seven. 245 00:33:20.400 --> 00:33:21.360 William Cheng: what the answer is. 246 00:33:21.930 --> 00:33:26.160 William Cheng: You know, it might not be seven right because you know when we first come over here and look at the wrong key over here. 247 00:33:26.340 --> 00:33:33.390 William Cheng: What does the monkey at that point is not empty, right. So. So in this case, you know, we're going to start, we're going to switch the CPU to to the other thread. 248 00:33:33.570 --> 00:33:42.000 William Cheng: The other so when you finish dropping the contest. Then, in our case, you know, the old IP out over here is going to be the old IP out in the stack frame of the thread that you're waking up. 249 00:33:43.800 --> 00:33:54.930 William Cheng: Okay, so if the other threat went to sleep because he called he called piece of new taxonomy is unavailable. And then when he called swish swish. In that case, what is all IP well all IP. I will be equal to zero. 250 00:33:55.620 --> 00:34:03.990 William Cheng: Right, because that's what is not a threat contact. So in this case, you know, when you said rip up I go IPO, you might go to zero. So it is possible. We're gonna go and then return to execute and after that. 251 00:34:05.040 --> 00:34:11.280 William Cheng: Okay. It's also possible that you know that that will do we keep waiting in the end you know that the, the only thing that we're running over here. 252 00:34:11.670 --> 00:34:22.920 William Cheng: That that's waiting for you to Rob is going to be our this threat. So eventually, when I felt wakes up over here. Then we're going to restore it to it will go. We're going to start running on the same thread that comes in. Why, in that case. Yeah. Or I feel we are equal to be going to seven. 253 00:34:24.330 --> 00:34:34.200 William Cheng: Then, so it also possible that you know the further you wake up over here. They're all IP is actually a different value, maybe you go to three, right, maybe it's a keyboard, you know, Rob, or something like that that that that 254 00:34:34.500 --> 00:34:42.240 William Cheng: That the just happened to be to be the father, you're waking up. So in that case, you know what it will do is that instead of going back to zero. Yeah, actually go back to sweet. 255 00:34:43.950 --> 00:34:48.090 William Cheng: Okay. So, so what does this make sense. Right. You know, so. So Robin is that, you know, 256 00:34:48.480 --> 00:34:55.230 William Cheng: You know, every time when you wake up a sweat, you're going to go back to the old IPL for that threat, but eventually your Iowa get delivered right there you 257 00:34:55.560 --> 00:35:00.210 William Cheng: Go out with deliver that you know absorption is going to start executing and your thread will get woken up 258 00:35:00.390 --> 00:35:10.440 William Cheng: At some point, another thread is gonna cost pressures to give up the CPU. And then when you're 30 venture wake up, then in that case inside your static IP. I'll be here good equal to seven. And then you're going to go back to set it. 259 00:35:12.180 --> 00:35:15.960 William Cheng: Up so so so when you go down to seven over here, you're going to be tough on a switch. And then one other 260 00:35:16.290 --> 00:35:27.180 William Cheng: When you return on switch, switch over here, right, the code will look like this. You return about sessions over here. And then we're going to send it out to the old IP up. This is the old IP inside this function and this all IP all we have equal to zero. 261 00:35:28.200 --> 00:35:35.100 William Cheng: Okay, so therefore, as soon as we talk about so I switched over here and this one is going to send it back to zero. So the picture over here is going to go for seven or go back to zero. 262 00:35:37.260 --> 00:35:44.130 William Cheng: Okay. So yeah, if you asked you a thread for happened with your thread right this is a threat that caught this right. Okay. So you said well you know 263 00:35:44.700 --> 00:35:55.560 William Cheng: Finally, when when this world wakes up and you ask the stress and what happened right this guy says, well, I caught this right my I said IPL to seven and then I call switch and then I fell asleep. I have no idea what happened. 264 00:35:56.760 --> 00:36:01.110 William Cheng: There. So while the IPL we have go up and down, up and down, up and down this way, doesn't really see that 265 00:36:01.650 --> 00:36:04.530 William Cheng: Okay, and also the IPL go to zero the IPL goes to 266 00:36:04.800 --> 00:36:12.870 William Cheng: The I can go to three and then all this kind of stuff. But eventually, when this drug I woken up nobody would do is that it goes to sleep sleeping i o que inside the web server so 267 00:36:13.080 --> 00:36:21.990 William Cheng: He got moving to the wrong QA eventually start running again. What is that running over here says what I want. So running again IP is equal to seven. So therefore, yes. Over here, so sorry. 268 00:36:22.230 --> 00:36:31.620 William Cheng: I will. I got woken up IP is equal to 31 right here. What's equal to 31 right here I change it back to a seven. And then when I return for this ride right before that I said it back to zero. 269 00:36:32.820 --> 00:36:45.210 William Cheng: OK. So, again, as far as the third that calling this lab is concerned is says the IP other seven said it's a 31 fell asleep when he wakes up IPL was able to equal to 31 you change it back to seven, and they change it back to zero. 270 00:36:46.440 --> 00:36:52.290 William Cheng: Okay. So, from this perspective, all you see is the beginning part and the ending part over here in the middle. It has no idea whether 271 00:36:52.680 --> 00:37:00.180 William Cheng: You guys okay if you think about the keyboard that if one of the people said you wake up over here said I peel back to three and they're gonna say I peel back to zero if you asked that. So, 272 00:37:00.660 --> 00:37:09.090 William Cheng: That's that is a long time ago. I fell asleep. Right. So there's a long time ago I was, I was a colonel threat. I said that you observed Level two to three. And then I said it to 273 00:37:09.450 --> 00:37:19.980 William Cheng: Then I call this word I said the IP or 231 and then I fell asleep. Okay, there are some point, you know, when I wake up either IP. I was 31 I said it back to three, I said it back to zero. 274 00:37:21.150 --> 00:37:29.490 William Cheng: Okay, so for every one of these threads. The same thing happened right except that if you look at this picture over here. They're the time when they go to sleep. And I'm due to the way couple we hear 275 00:37:29.700 --> 00:37:35.730 William Cheng: A lot of stuff happening in the middle. They just don't know that's happened. Okay. And also, as if all these thread, if you don't read the clock. 276 00:37:36.090 --> 00:37:44.940 William Cheng: As far as they're concerned is that they call first version and then immediately they got woken up they said IPL back to the previous level and then because if you're back to zero. 277 00:37:45.900 --> 00:37:52.320 William Cheng: Okay, if you start, you draw the picture, you know, for the keyboards that right. Let me draw the picture over here for the keyboard, so that it will look like this. 278 00:37:53.340 --> 00:38:08.010 William Cheng: Yeah, for the this thread over here with it, look at this right go like this, you know. OK. So again, a vertical over here is IPL right they all look very, very weird shape and in the middle of your they all go crazy over here, but none of those. So we'll see see see those things happening. 279 00:38:09.780 --> 00:38:19.800 William Cheng: OK. So again, this is, this is what a real threat switch look like okay that you're manipulating the IPL value over here. And then, you know, all funny stuff happening in the middle where you switch to other contacts. 280 00:38:20.340 --> 00:38:25.470 William Cheng: Yeah, essentially, you know up servers within contacts, all that kind of stuff happening happening between yeah 281 00:38:27.030 --> 00:38:31.770 William Cheng: Alright, so, so again this is going to be the real code that you can actually use your kernel one. Okay. 282 00:38:33.240 --> 00:38:40.050 William Cheng: All right. And again, this IP all be is very tricky. You know when you come in over here. You said IP. If I tell you what to say. 283 00:38:40.530 --> 00:38:48.480 William Cheng: When you go out or when a different thread go out. What, then, in this case IPL won't be seven. Okay. But eventually, when you are the one that actually leave a switch and that gives you 284 00:38:49.230 --> 00:39:00.210 William Cheng: All IP over here. Well, will be so bad because the stock pointer, you know, actually point to the top of the stack for for this stack rank and in this case, when you pick up all you know IPR inside your own stack rank that you'll be able to seven. 285 00:39:01.470 --> 00:39:07.260 William Cheng: Alright. So again, it's very, very important to understand how this code works. Okay, so this way when you try to write your kernel why he was simply the Colonel. 286 00:39:08.430 --> 00:39:17.190 William Cheng: Inside the wind is turned off, you know the name of the function is going to be different that data source is going to be different. So you need to understand this code and then translate this co to be winning scope. 287 00:39:18.240 --> 00:39:24.030 William Cheng: Okay but but in spirit. They need to have exactly, you know, the same kind of logic that's going on here. Yeah. 288 00:39:27.180 --> 00:39:37.170 William Cheng: So the problem with this code over here is Busy waiting right because if the interval doesn't get the liberal over here it is gonna go up and down like crazy over here and then you keep checking the wrong to you to see if it's empty, then it's going to be continued to be empty. 289 00:39:37.410 --> 00:39:49.260 William Cheng: So you are using the CPU cycles. Okay. Any that if you use battery, then you're going to be using a battery. So, therefore, what, what does the right thing to do the right thing to do is that when you need to wait for the interrupt you should give up the CPU. 290 00:39:50.460 --> 00:39:55.140 William Cheng: Okay. But in this case, you have, you have no sweat to give out a CPU do so in this case you should turn off the CPU. 291 00:39:55.740 --> 00:40:05.730 William Cheng: That. So therefore, when we said IPI equal to zero over here and as you know when when we said, I tell you what a zero over here in India. I was pending, you know, I'll get deliver. But what about will interrupt. 292 00:40:06.210 --> 00:40:09.870 William Cheng: Interrupt didn't get pending right so in a, in that case, you should help the CPU. 293 00:40:11.040 --> 00:40:17.580 William Cheng: So therefore, the solution over here is that if you want to be friendly to the CPU don't want to burn out and be friendly to the battery. 294 00:40:17.820 --> 00:40:25.710 William Cheng: We should add to hold the CPU right here. So why she said it. I will see or hear you hold the CPU. If you hold the CPU the CP is going to go to sleep. What is the CPU waiting for 295 00:40:26.790 --> 00:40:37.200 William Cheng: The CPU is waiting for an interrupt okay because the interactive. It's a hardware signal that's come from outside the, you know, this is coming from the bus and the bus is going to send the signal to the CPU to wake it up. 296 00:40:38.460 --> 00:40:44.130 William Cheng: Okay, so therefore we so it's perfectly okay well you know to hold a CPU, as long as you are sure that we hold the CPU. 297 00:40:44.250 --> 00:40:53.400 William Cheng: Eventually you're going to get a hardware interrupt and that will wake up your that will wake up your CPU and as soon as the CPU wake up, what will happen it will return from the hall machines, Russia, and then he likes to the next instruction. 298 00:40:54.150 --> 00:40:56.550 William Cheng: Okay, so in this case if you know observers routines. So 299 00:40:57.150 --> 00:41:00.510 William Cheng: As soon as you be tough on the hall instructions over here, what he will do is that 300 00:41:00.780 --> 00:41:07.650 William Cheng: The CPU is running again. Right. So at this point, IP or equal to zero. So, therefore, you're going to go execute you know observers routine. So, you know, absolutely. 301 00:41:07.980 --> 00:41:13.650 William Cheng: unblock a colonel where you start the next operation and then you're going to return, right, the right after the whole machine instruction. 302 00:41:14.490 --> 00:41:22.770 William Cheng: So in this case you will set IPI equal to hide because you want to check the monkey was empty. And then you come over here and now you see the wrong. He was not empty, so therefore you can access the article over here. 303 00:41:24.570 --> 00:41:27.240 William Cheng: Also. So, by the way, if you write your code like this. 304 00:41:27.870 --> 00:41:36.030 William Cheng: Because it turns out you can actually read your call like this for your current one for your colonel colonel to, as it turns out, if you write, write this call your kernel three is really not going to work. 305 00:41:36.660 --> 00:41:42.960 William Cheng: Okay, for criminal. See, you know, this code is it true that there's a race condition. Okay, so there's so sorry. 306 00:41:44.820 --> 00:41:49.530 William Cheng: You know if this code over here if you don't use the whole machine instructions this co action word for Colonel wanting to 307 00:41:49.920 --> 00:42:02.130 William Cheng: Guys have a current one until this is all you have to do. You don't have to hold a CPU there for Colonel three over here, you're required to have the CPU, because if you don't have the CPU. Once you get into your user space program your user space around would never get to interrupt. 308 00:42:03.150 --> 00:42:12.180 William Cheng: Okay, so you're not. CASEY Who are you going to be you're gonna be in trouble. So therefore, in Colonel three you're required to how the CPU right between studying IPL to zero and sending it out to the highest level. 309 00:42:12.600 --> 00:42:18.690 William Cheng: That. But if you read your code like this, as it turns out, there's a hidden race condition, but I'm not going to tell you what the race condition is 310 00:42:19.470 --> 00:42:25.830 William Cheng: Okay, so this is kind of like exercise you can see the grading guidelines and Colonel through to say that if you don't face this race condition. Nothing you going to lose five points. 311 00:42:26.400 --> 00:42:36.030 William Cheng: Okay, so there's a risk condition over here. It's really easy to face. Once you understand where the recent addition as right so go, Why would you end up with a race condition because it doesn't wait for the interrupt correctly. 312 00:42:36.810 --> 00:42:43.110 William Cheng: Okay. The hallway interactive is an asynchronous event and we mentioned before, there's only one correct way to wait for asynchronous event. 313 00:42:44.010 --> 00:42:49.830 William Cheng: Okay, so first you have to block it right and then you have to do something to generate an external event. And then finally, 314 00:42:50.280 --> 00:42:54.690 William Cheng: You have to use an atomic operation to to unblock it and wait for one atomic operation. 315 00:42:55.380 --> 00:42:59.670 William Cheng: Okay, so that's, you know, that's if you're waiting for an event that generated by you. 316 00:42:59.970 --> 00:43:05.580 William Cheng: And if it turns out that if you are waiting for an event that generated by somebody else why, in that case, would you need to do that he needs a blog event. 317 00:43:05.790 --> 00:43:12.450 William Cheng: And then you need to check the events happened already or not. If the event. Hasn't happened already running that case you need to unblock it and wait for it one atomic operation. 318 00:43:13.350 --> 00:43:20.190 William Cheng: Yeah. So again, follow the principal analyze this code over here to find out, you know, a widest code isn't one. Why is there a race condition that 319 00:43:20.400 --> 00:43:27.270 William Cheng: And you can actually see the race condition. So what you're doing your current oh three when you start running your code and everything's gonna be fine. And all of a sudden, everything's gonna freeze up 320 00:43:28.350 --> 00:43:33.570 William Cheng: Okay. And then what you do is that if you press any key. Like if you touch the Space key and everything is go start going again. 321 00:43:34.290 --> 00:43:45.900 William Cheng: Okay, so if that happened you know that there's a race condition. Okay. And then you need to come to this code over here and try to sort of figure out what do I have to do to actually fix this code that. So, at that time, you know, feel free to discuss this, you know, 322 00:43:47.070 --> 00:43:56.010 William Cheng: In the cost, Google. Google Talk about where the race condition is again don't share code about how to fix it. Okay, but you should talk about why would the rescue race condition habit that 323 00:43:56.790 --> 00:44:09.480 William Cheng: Also got the recommended you have everything is gonna is everything is going to freeze. But when you press the key we generate the next interrupt everything's going to start going again. OK. So again, look at this code over here trying to sort of figure out whether race condition is 324 00:44:10.770 --> 00:44:21.810 William Cheng: Alright, so now let's take a look at the solution for the preemptive colonel. OK. So again, the Korean to cut over here. We also get it done. We're going to also introduce a second CPU. Okay, so we're going to fix everything, all in one shot now. 325 00:44:23.250 --> 00:44:27.930 William Cheng: Alright, so, so again, in order for you to block all the other CPU over here. Right here is 326 00:44:29.370 --> 00:44:37.110 William Cheng: This call can be executed. Another CPU. So in that case, the only effective way to block out the other CPU, we saw before, blocking off doesn't work. Only the spin lock work. 327 00:44:37.440 --> 00:44:43.380 William Cheng: Okay. So, therefore, if we use spill out over here, this call over here. If you're running inside the other CPU. This one is this interrupt service routine. 328 00:44:43.590 --> 00:44:51.600 William Cheng: When they run inside the, the other CPU. So this will be running here and Arthur over here is a pink shirt over here, right. So if you use a spin off over here you can block. Although, you know, 329 00:44:52.320 --> 00:44:55.350 William Cheng: In this case, you're going to block out all the other activity on the bus. 330 00:44:56.070 --> 00:45:00.570 William Cheng: Okay, so again we we started spinning our code before we're going to use to not eat version over here to be nice to the bus. 331 00:45:01.020 --> 00:45:04.710 William Cheng: So in this case over here, we can actually use this to pop up to the park on another CPU. 332 00:45:05.190 --> 00:45:15.450 William Cheng: That. But if you write code like this, exactly like this is not going to work. Right. Because you know this is a you know up servers within it is possible. This interrupt service routine we're executing the same CPU as you 333 00:45:16.470 --> 00:45:29.190 William Cheng: Okay, if it turns out that this code over here also execute inside the same CPU. In that case, you're going to end up the deadlock situation because your thread can can can come over here to spin off and do x equal to x plus y. And what if you get an inroad right now inside your CPU. 334 00:45:30.450 --> 00:45:31.650 William Cheng: Okay, so if you get all you know 335 00:45:32.250 --> 00:45:39.690 William Cheng: Again, if you get to interrupt, but we're here then then you're on CPU is gonna start executing this code over here. And then when you start actually doing this code over your IP is going to 336 00:45:39.900 --> 00:45:44.730 William Cheng: Be set to whatever level here. So this code when I execute anymore because this call is for iPod with zero 337 00:45:45.240 --> 00:45:49.620 William Cheng: Okay, so now you're executing drop service with info and then will you can spend, I will happen. 338 00:45:49.980 --> 00:45:57.600 William Cheng: You will end up with a deadlock, right, because the spin off over here is now owned by the third right here and now you're inside interrupt service routine, you're gonna end up waiting forever. 339 00:45:58.590 --> 00:46:09.810 William Cheng: There. So, therefore, what you have to do is that you have to make sure that this code this this interrupt service routine co can execute in the same CPU as this call. Okay, this call. If you execute in another CPU, then that's perfectly fine. 340 00:46:10.860 --> 00:46:18.510 William Cheng: OK. So again, what do you have to do to do to prevent that from happening. You have to mask, how they interact when you mess up to interrupt you only mass out the inner up on your own CPU. 341 00:46:19.500 --> 00:46:29.010 William Cheng: Why can't U Mass. The interim on the other CPU. So again, as I mentioned before, there's no way for you to tell the other. See if you want to do so different. Well, you're masking. You know, you only masters, they interrupt on your own you. 342 00:46:29.610 --> 00:46:37.350 William Cheng: Got all inside your own CPU that. So, therefore, you know, this call doesn't work. So we need to modify it a little bit over here. Okay. So inside the 343 00:46:37.590 --> 00:46:42.780 William Cheng: Code over here, we need to master, you know, Rob. So first of all we need to do as I mentioned before, is to disable preemption. 344 00:46:43.200 --> 00:46:51.780 William Cheng: Want to disable preemption first over here. And then we're going to mask on interrupts or this code cannot execute your own CPU. But let's go still can execute in another CPU. 345 00:46:52.170 --> 00:46:58.950 William Cheng: Then finally, when we when we're going to ask us, our share variable be here. Now we need to block out the other CPU. So then when you do the spin up 346 00:47:00.090 --> 00:47:05.340 William Cheng: Okay, so in this case the entire system only have one spin lot. Okay. And this will go over here. 347 00:47:05.850 --> 00:47:12.510 William Cheng: So remember the requirement over here is that we use a spin out, you can only use it for a short period of time. Does this one satisfy that requirement. 348 00:47:13.380 --> 00:47:21.570 William Cheng: That when I use the spill out. What do I do, I do x equal to x plus one. Can this be done in microseconds what the answer is yeah, maybe you can be done in a nano seconds. 349 00:47:21.810 --> 00:47:31.710 William Cheng: Okay. And then as soon as I finished that I will do spend a lot. So in this case, I'm using a spin lock for very, very short period of time. Okay, so if you know. So this is the case, you are allowed to use the now. 350 00:47:32.700 --> 00:47:38.100 William Cheng: Okay, so let's take a look at this, to see if it works. So you also get our thread over here is calling this function. 351 00:47:38.670 --> 00:47:42.960 William Cheng: So where the picture right here. If we disable prayer preemption, who do we blocked out 352 00:47:43.710 --> 00:47:48.900 William Cheng: What we block other thread inside our own CPU. So in this case, you know, to, can I have in 353 00:47:49.230 --> 00:47:58.950 William Cheng: There. The next thing that will do, is I'm asked to interrupt. When we mess up my era, one can i happen anymore because this call will not be able to run inside your own CPU. So therefore, in that case, one will be blocked out 354 00:47:59.190 --> 00:48:08.640 William Cheng: And then finally, when we come over here and then we do the spin off when we do spelled out, then the, the second CPU is completely blocked out for accessing the variable right here. 355 00:48:08.850 --> 00:48:13.200 William Cheng: Right, so therefore once we do this. Come on over here. The other one will block out over here. So, therefore, in this case. 356 00:48:13.380 --> 00:48:23.460 William Cheng: We will not, we will get no interference from any threat inside our CPU or other CPU and also we're going to get no interference from the you know of service routines that are CPU only another CPU. 357 00:48:24.000 --> 00:48:39.720 William Cheng: OK, so now accessing x equal x plus one become a comic. So now our current. Oh, so this is a this is a trick to make your kernel atomic. Okay, so these are circled over here, we need to disable preemption master the inner Rob I used to spin off and now we can access to share variable. 358 00:48:40.980 --> 00:48:48.660 William Cheng: OK, so now our current. So, so what about the interrupt service routine, right, he said, interrupt service routine. We only have to use the spin lot when we're when we're going to use the share variable. 359 00:48:49.350 --> 00:49:01.350 William Cheng: Okay. All right. So yes, are we here is that is that it will work, and also the orders he is important. We will not disable preemption first and then we mass on the interrupt. I always got a question to say, okay, you actually switch these two statement. 360 00:49:02.070 --> 00:49:10.230 William Cheng: That the answer is a, you know, the, the, the other over here, I guess, I don't know. On the other side over here. 361 00:49:11.010 --> 00:49:21.030 William Cheng: So so so if you must call the first one. If you actually end up switching to another threat if you switch to another thread the other thread is going to mess mess around with the IPL what things might not work. 362 00:49:21.450 --> 00:49:28.380 William Cheng: Okay. So, therefore, you have to follow this order, you have to disable preemption first and then you must have to interrupt. And then you spin up right 363 00:49:29.460 --> 00:49:41.340 William Cheng: So again, x over here is really not. You know, when I was talking about the variable is naturally talking about the wrong shoes. Right. So again, when you try the asset, the wrong. Q. In that case, the IPL level be here is going to be the highest IPL 364 00:49:41.820 --> 00:49:48.240 William Cheng: If you're accessing the audio cue over here. What, in this case, we need to set the IPL to be the appropriate IPR obvious is messing up 365 00:49:48.420 --> 00:49:56.760 William Cheng: Right when you try to match you know it's not always the highest interest level right for the desk over here with what you just said it to seven that. And finally, you know, 366 00:49:57.390 --> 00:50:02.550 William Cheng: You know, the, the variable over here. If we're talking about a few tasks when the few times coming out of the Colonel. 367 00:50:02.820 --> 00:50:06.000 William Cheng: You know, we'll, we'll get we'll get access to the same variable over here. 368 00:50:06.210 --> 00:50:14.340 William Cheng: When we try to access that variable in order for our Colonel to be synchronous or so in order for our Colonel to be a Tommy again atomic with respect to that few tasks. 369 00:50:14.580 --> 00:50:19.200 William Cheng: If I want to modify that the the few times variable inside the Colonel, we have to also do like this. 370 00:50:20.100 --> 00:50:27.780 William Cheng: Okay, so in this case this will be the third call there will be no you know observers who didn't go because interrupt service and they go, they will not touch on there will actually not use a few texts. 371 00:50:28.380 --> 00:50:36.300 William Cheng: Okay, so therefore, in this case, x will be the lot variable inside a few texts. Okay, so this way or Colonel will be atomic with respect to the few texts. 372 00:50:37.140 --> 00:50:46.530 William Cheng: And so in the end the blocking log. So it doesn't work right. But we know that using few tax and we now can we know how to make the colonel accommodate so now few times will actually work. Okay. 373 00:50:47.550 --> 00:50:50.460 William Cheng: Alright, so now you see the final, final solution for everything. Okay. 374 00:50:51.120 --> 00:51:03.180 William Cheng: Alright, so we are done here. We've done with you know of handling over here and now you have everything that you need to know in order for you to finish Colonel why remember quarter one. We don't have an empty, empty Colonel, guys, so we don't have a dump. 375 00:51:04.140 --> 00:51:07.110 William Cheng: In Colonel while we have enough. We have the Colonel, so you don't have to do things like this. 376 00:51:07.920 --> 00:51:14.490 William Cheng: Okay, so we have a non Priyanka, Colonel. So therefore, all you have to do is to make sure that you mask on the interrupt at the right time that 377 00:51:15.420 --> 00:51:31.680 William Cheng: All right, so. So what I will also do is I'm going to sort of try to record the the Friday discussion section a little earlier. So this way you can have early access to all the information. So you so so you can start you know doing start doing kind of one yet. Alright, see you. See you later.