WEBVTT 1 00:00:11.130 --> 00:00:58.830 William Cheng: Hi, welcome to the live lecture on June 18. Okay, anyone has any questions. 2 00:01:01.050 --> 00:01:01.920 Hardik Mahipal Surana: Good morning, Professor. 3 00:01:02.250 --> 00:01:02.670 William Cheng: Morning. 4 00:01:03.060 --> 00:01:06.030 Hardik Mahipal Surana: I had a question. I do respect to the previous lecture. 5 00:01:06.300 --> 00:01:06.630 William Cheng: Okay. 6 00:01:07.350 --> 00:01:12.390 Hardik Mahipal Surana: So in love lecture nine I think around slide 49 7 00:01:12.990 --> 00:01:14.160 William Cheng: Right. Let me get there. 8 00:01:19.200 --> 00:01:19.500 William Cheng: Yeah. 9 00:01:20.430 --> 00:01:35.640 Hardik Mahipal Surana: So here I just wanted to understand, whenever we are creating a new process, we say that the process is given the entire four GB in virtual memory and we are creating a page table to map those virtual addresses to PHYSICAL. CORRECT. 10 00:01:36.000 --> 00:01:36.480 William Cheng: Correct. 11 00:01:36.660 --> 00:01:44.070 Hardik Mahipal Surana: So I wanted to understand, whenever we create a new process do we create all the 1 million entries in the page name for each process. 12 00:01:44.220 --> 00:01:52.560 William Cheng: So, so, yeah, that would be a problem, right, because you know if every entry is four bytes long than 1 million times four is for four megabytes. 13 00:01:53.610 --> 00:01:58.800 William Cheng: So if every process is going to use four megabytes, I guess. These days we have gigabytes of memory. So it's not so bad. 14 00:01:59.850 --> 00:02:07.410 William Cheng: But yeah but but it's a total waste. So in chapter seven. We're going to talk about different ways to implement a page stable to save memory. 15 00:02:08.790 --> 00:02:15.540 Hardik Mahipal Surana: Okay. So ideally, we will be creating space only for those pages that are allocated for a particular process. 16 00:02:16.020 --> 00:02:26.130 William Cheng: What for the page table right so so so the page table over here right conceptually. So again, we're going to talk about the abstraction that conceptually conceptually there's 1 million entries over here. 17 00:02:26.580 --> 00:02:38.640 William Cheng: Okay, so, so only a few of them will have a will have a pointer that point to the four kilobytes pages, guys. So the main purpose over here is to save the memory right here that are allocated by the buddy system. 18 00:02:41.370 --> 00:02:49.110 William Cheng: Okay, right, because if you have 1 million entries over here. Then we're going to have one millions of four kilobytes pages. That's for that. That's four gigabyte. 19 00:02:49.860 --> 00:02:57.570 William Cheng: Okay, but if we only have five entries that they have a physical address pointer that pointed them. Well, then in that case, we only allocate 20 kilobytes. 20 00:02:58.920 --> 00:03:11.760 Hardik Mahipal Surana: Here. So having this particular setup where only the pages allocated to a particular process are there the table that would make sense. Because last time I think you would mention that there are most of the entries in the page, they will actually have no access 21 00:03:12.090 --> 00:03:14.130 William Cheng: Right. Most of the entries gun look like this. Yeah. 22 00:03:14.460 --> 00:03:21.480 Hardik Mahipal Surana: Yeah. So in that case, if, if a process does not have access to a particular page where it would make sense to have that entering the page. 23 00:03:22.380 --> 00:03:28.380 William Cheng: The page table, you know, it's sort of a fixed size depends on the hardware architecture. So you're sort of stuck with that. 24 00:03:30.480 --> 00:03:35.220 Hardik Mahipal Surana: OK, so the format of the layout of the page table will be the exactly same for all processes. 25 00:03:35.490 --> 00:03:43.950 William Cheng: Right, so, so, so when we talk about what we know when I mentioned the virtual memory map right I said the virtual memory map, you know, and also you know it's a bunch of 26 00:03:44.220 --> 00:03:53.910 William Cheng: Data structure inside the Colonel, one of the data structure, you know, it's special. It's called page table right so people sort of talk about virtual memory map and page table I sort of as a separate thing. 27 00:03:54.270 --> 00:04:00.840 William Cheng: So what's special about the page table is that the page table. It's a kernel data structure that's understood by the hardware. 28 00:04:02.880 --> 00:04:07.230 William Cheng: Okay, so, so it depends on which CPU you're using, you have to build a different page table. 29 00:04:09.150 --> 00:04:09.420 Okay. 30 00:04:11.820 --> 00:04:16.800 William Cheng: Okay, so some page table will use more space, some people some some page, they will use less space. 31 00:04:17.130 --> 00:04:29.550 William Cheng: But conceptually, the way we sort of think about the page table is that it has one 1 million entry and every entry is four bytes long and then you know inside every entry. It tells you what kind of access, you have and also it points to the physical page. 32 00:04:31.980 --> 00:04:39.870 William Cheng: Okay, so, so, so the picture that we see over here. It's kind of like an abstraction, because it because in reality, it doesn't have to look like this. 33 00:04:41.250 --> 00:04:53.700 Hardik Mahipal Surana: Okay. And since the PhD will does mapping of virtual memory to physical memory. How does the access to the page itself will also be reference it to memory address or is it true or the indices. How does that 34 00:04:54.450 --> 00:05:00.960 William Cheng: Yeah, so the virtual page over here. It's going to give you an array index. Right. So if you so so so over here if you, you know, the pages. 35 00:05:01.470 --> 00:05:11.370 William Cheng: As four kilobytes four kilobytes is equal to, to, to the 12 and if you add spaces four gigabytes and that's to to the 32 so two to 32 divided by four. 36 00:05:11.730 --> 00:05:16.650 William Cheng: By two to 12 is going to be able to to to the 20th, right. So, again, that will give you 1 million entries over here. 37 00:05:16.860 --> 00:05:27.090 William Cheng: So if you take a virtual address and if you only look at the first 20 minutes or so, the first 20 minutes over here is going to be the array index and the last hope is over here is going to be an address within the page. 38 00:05:27.450 --> 00:05:36.240 William Cheng: So, therefore, what you will do is that you will use the first 20 bit as a virtual address and use that to to to to to as an array index to access your page table. 39 00:05:37.920 --> 00:05:45.300 William Cheng: Okay, so that's how you access is. And then what happened is that inside the CP. So what so what the CPU do what we'll do is that 40 00:05:45.870 --> 00:05:54.720 William Cheng: So again, the page table is a kernel data structure that's used by the CPU. So what the CPU will come over here and what it will do is it will read a page table entries over here. 41 00:05:55.050 --> 00:06:02.310 William Cheng: Inside the car will read the pace table entry into the CPU and then inside the CPU. What do we do is enable interpret all these numbers. 42 00:06:03.690 --> 00:06:16.680 William Cheng: Okay, so again this is very hardware specific right different CPU will have different you know things inside the inside the page table. So get chapter seven. We're going to go talk about, you know, different kinds of paste a boy. What kind of paste it was out there. 43 00:06:17.820 --> 00:06:18.750 William Cheng: Good. Okay. 44 00:06:18.960 --> 00:06:32.040 Hardik Mahipal Surana: I have one more question with regards to copy and right so in the last lecture. You had mentioned that if we have like a variable x, which has been initialized to a particular value in the parent page table and 45 00:06:32.910 --> 00:06:49.680 Hardik Mahipal Surana: We when we create a child process they reference to the same pages because the page table is also copy. So in a situation where both the parent and the child make updates to the same page, we would ideally be creating a new copy of that page and update the references in the paper. Correct. 46 00:06:50.040 --> 00:06:50.640 William Cheng: Yes. 47 00:06:51.090 --> 00:06:58.800 Hardik Mahipal Surana: In that case, what happens to the original page that was being referenced by both of those pages because now both the parent page table and 48 00:06:59.040 --> 00:07:07.620 Hardik Mahipal Surana: Child page table would point to two different copies of the page where they do their own private updates. But then what happens to the original page, which was being shared. 49 00:07:08.430 --> 00:07:14.430 William Cheng: Right, so. So remember we talked about the fall object, right, in Chapter one The Fall object as a reference count. 50 00:07:15.030 --> 00:07:21.030 William Cheng: Okay, so we can also do use the same mechanism over here. Right. And even though in reality the dip the implementation might be a little different. 51 00:07:21.270 --> 00:07:27.150 William Cheng: But the basic idea here is that if you have a reference cow, then the reference count count the number of pointers that point to that object. 52 00:07:27.420 --> 00:07:33.780 William Cheng: Right. So over here, there's one point to point here. There's another point over here. So this one the reference count over here is going to be equal to two. 53 00:07:34.770 --> 00:07:46.410 William Cheng: Okay, so when you perform copy on write. We're going to allocate a new page over here I this is a four kilobytes page. We're going to make a copy. And then when we change this pointer that points to it. Well, then, in this case a reference will go down to one. 54 00:07:47.160 --> 00:07:54.780 William Cheng: Okay, and then when the trial process does the same thing. They perform right for the first time, they're going to allocate a new page. Make a copy of those changes pointer to point of this 55 00:07:54.960 --> 00:08:02.820 William Cheng: And now the reference count account will go to zero when reference count go to zero, that means that there's nobody pointing to this object. So, therefore, this object can be freed up 56 00:08:05.190 --> 00:08:06.300 Hardik Mahipal Surana: And renders this 57 00:08:08.850 --> 00:08:09.150 Hardik Mahipal Surana: Page. 58 00:08:09.930 --> 00:08:20.070 William Cheng: Well, when you know what will we perform copy on. Right, right. So when you perform copy. All right, you need to update all these pointer when you update all those pointer. You have to make sure that the colonel data structure is consistent 59 00:08:20.550 --> 00:08:31.980 William Cheng: So therefore, if you remove a pointer to this object. Well, then you need to be detrimental reference count. So when you definitely want to reference car. And if the reference. I'll go to zero. What that means that nobody's using it. Well, then they can free it up right at that time. 60 00:08:32.460 --> 00:08:35.970 Hardik Mahipal Surana: Okay, so as soon as it becomes the road is immediately free by the good 61 00:08:36.510 --> 00:08:47.940 William Cheng: Yeah, so, so, so when when you recommend the reference count inside that. So you should read a function. Did that come in the reference cow. Well, you'd recommend the reference count, you should check if this becomes zero, it becomes zero, then it's safe to do to free it up. 62 00:08:48.900 --> 00:09:00.600 William Cheng: Okay, so, so this reference count as it turns out it doesn't have to be equal to two, when you started with us. Maybe this reference count equals to, you know, you go to three. Okay. Because maybe the grandparent is also using this page. 63 00:09:01.830 --> 00:09:06.150 William Cheng: And then maybe the grand grandparents also using this page shows the reference count over your connection. Be very, very high. 64 00:09:07.860 --> 00:09:16.110 William Cheng: Okay. Okay, so, so, so only when you document the reference count as zero, then you know that nobody is pointing to this object and therefore you can pre it 65 00:09:17.970 --> 00:09:25.140 William Cheng: Okay, okay. So, so far in the you know the lecture, we only talk about using the reference count for the fall object. 66 00:09:25.740 --> 00:09:32.160 William Cheng: Okay. As it turns out, later on in the kernel, you will see that there are a lot of places they use reference counting, so, so, so, you know, 67 00:09:32.640 --> 00:09:41.430 William Cheng: When you're writing your program in the user space program like your warm up. Why you warm up to. You're the one that called Malik. And, you know, the one that called free 68 00:09:41.850 --> 00:09:48.780 William Cheng: Inside the Colonel. The Colonel is a little more complicated. You know, because it's because of situation like this. So typically what they don't do is that maybe 69 00:09:49.020 --> 00:09:56.340 William Cheng: Their cases they do came out, okay, free or something like that. But there are also many cases inside the Colonel, they will use the reference counting mechanism. 70 00:09:57.030 --> 00:10:05.670 William Cheng: Okay, so this one. So again, it's a pretty smart way to do it. I think today if you're using c++ they have something called smart pointers. 71 00:10:06.150 --> 00:10:18.750 William Cheng: Smart pointer is based on the same idea. They use reference counting. So this way when the reference count go down to zero, then you know that nobody's pointing to it. So, therefore, you know, so in c++ the c++ library. Why should free to free up that object. 72 00:10:20.370 --> 00:10:29.370 Hardik Mahipal Surana: Ok ok ok and I had a question it with respect to the colonel on assignment, would this be an I tend to ask it was, can I come to your office hours. 73 00:10:29.790 --> 00:10:36.480 William Cheng: Oh no, you're free to ask it because, you know, we have two hours worth of let's just dive in and we never go that far. So I think it's OK. 74 00:10:36.960 --> 00:10:45.450 Hardik Mahipal Surana: OK, so I actually want to get a more clear understanding of creation of the idol process. You'd mentioned in the week five discussion video that 75 00:10:46.320 --> 00:10:56.790 Hardik Mahipal Surana: We are in the bootstrap context, while we are creating the ideal process. And once we create the ideal process we establish the threat context for the interview process and then we transfer our control to that. 76 00:10:57.900 --> 00:10:58.350 Hardik Mahipal Surana: So, 77 00:10:59.550 --> 00:11:05.790 Hardik Mahipal Surana: Technically you can we still follow the same procedure to create the ideal process as we would do for any other process. 78 00:11:06.930 --> 00:11:12.960 William Cheng: Oh yeah. Yes. Yeah. It's just that you don't we create the idol process. So, so, so I think what you said is correct. 79 00:11:13.740 --> 00:11:21.630 William Cheng: Okay, so in the bootstrap contacts is kind of a funny contacts, but you can still call it the same kind of function to create a process. 80 00:11:22.590 --> 00:11:29.610 William Cheng: Okay, so even though you are not in a threat context yet, right, you can still. I mean, you know, the colonel function is a function call. Right. Everything is just an abstraction. 81 00:11:29.910 --> 00:11:35.940 William Cheng: Process that abstractions better abstraction. So, so even though you are not, you know, through a contact, you can still create a process. 82 00:11:38.160 --> 00:11:45.870 William Cheng: Okay, right. You just need to be aware that you are not in the, you know, you're not in a threat context. So, so what does that mean your nine to three contacts. That means that if you are not in 83 00:11:46.380 --> 00:11:55.800 William Cheng: That. So if you're in a contest then switch will work right because the idea of the switch function is that you start with a threat contacts and you give the CPU to another threat. 84 00:11:56.610 --> 00:12:06.060 William Cheng: Okay, so you can only call the switch bumps you if you're in the right context. So if you're not in a threat contact. So how do you give the CPU to another threat. Well, you have to use some other mechanism. 85 00:12:08.730 --> 00:12:09.060 Hardik Mahipal Surana: Okay. 86 00:12:10.320 --> 00:12:20.010 Hardik Mahipal Surana: Okay. And I think that process for doing the context switch from the bootstrap context to the thread context is already there. I think in some of the code that is given to us. 87 00:12:20.610 --> 00:12:32.580 William Cheng: Yeah, so, so I guess in the discussion section, you know, video, I asked you to also look at the function K Mang right inside came a in the beginning it was in a even weirder contacts. 88 00:12:33.300 --> 00:12:39.120 William Cheng: So there's nothing is initialized because you know we just switched from assembly code to see codes and now they're in came a 89 00:12:39.420 --> 00:12:47.160 William Cheng: At the end of the came in what he did what he did was that he created the boost your contacts and then it switched to the Boucher contacts contacts. 90 00:12:48.030 --> 00:12:50.580 William Cheng: OK, so now is that Boucher contact you have the same problem. 91 00:12:51.120 --> 00:12:58.590 William Cheng: You know, you need to create the, the threat contact and switch to the fair contacts. Right. So in that case, you will do the same thing as in inside this function call came a 92 00:12:59.220 --> 00:13:11.640 William Cheng: Good. Okay. Not exactly the same. So you have to read the code there, you know, to see how you create a context and switch to it and then you have to do something very similar inside the bootstrap in in the Boom shaka 93 00:13:12.930 --> 00:13:13.170 Okay. 94 00:13:14.370 --> 00:13:16.980 And I had another question. 95 00:13:18.420 --> 00:13:27.300 Hardik Mahipal Surana: With respect to creating of multiple offices. So I think in lecture on nine slide number 73 96 00:13:27.870 --> 00:13:32.640 William Cheng: Okay, let me go there lecture. Oh, I'm here 73 97 00:13:33.270 --> 00:13:36.360 Hardik Mahipal Surana: This question is also again with respect to the colonel assignment. 98 00:13:37.740 --> 00:13:39.330 William Cheng: This one, yes. 99 00:13:40.590 --> 00:13:42.330 Hardik Mahipal Surana: So here I think. 100 00:13:43.380 --> 00:13:48.090 Hardik Mahipal Surana: So there is a data structure that helps us maintain a list of processes in the kernel. So 101 00:13:48.120 --> 00:13:49.530 William Cheng: That's the process control block. Yeah. 102 00:13:50.220 --> 00:14:04.950 Hardik Mahipal Surana: Yes, so we store all of the process that we create a linked list maker, like a complete list. So I want to understand where does the idol process. Come here is, it is. It's also supposed to be added to that list, since it is a process in itself. 103 00:14:05.370 --> 00:14:11.400 William Cheng: But I don't process, the funny process is creating a weird context. So it's not drawn into picture. 104 00:14:13.830 --> 00:14:19.980 William Cheng: Okay, so I don't process looks like a real process but you know it's create a funny. Nice. 105 00:14:21.810 --> 00:14:30.060 Hardik Mahipal Surana: Okay, so technically starting once the ideal process creates the inner process that's when starting from the unit process we start adding it to the list. 106 00:14:31.410 --> 00:14:36.300 William Cheng: Well, I mean, so you can also think about as well either process has a child caught in that process. Right. Yes. 107 00:14:36.750 --> 00:14:45.810 William Cheng: So, so, so, so for our Colonel sound so I yeah I guess maybe for Linux i don't i don't know because in Linux. The idol process is created using assembly language. 108 00:14:46.200 --> 00:15:00.900 William Cheng: So I don't you know I haven't seen the Linux source code. I don't really know if it actually look like this. Okay, before our Colonel assignment. Right. Colonel Simon is Phoenix. It's not really Linux. So our kernels Sam and the idol process is the parent of the process. 109 00:15:03.750 --> 00:15:08.100 William Cheng: But but but but still this look like a tree. Right. Yes. So that's okay then. 110 00:15:09.780 --> 00:15:10.110 Okay. 111 00:15:11.430 --> 00:15:16.410 William Cheng: Right. But over here it says Petri on Linux on Linux, you know the the root is that the in a process. 112 00:15:18.510 --> 00:15:19.800 Hardik Mahipal Surana: Okay, thank you so much. 113 00:15:20.490 --> 00:15:24.930 William Cheng: Yeah, so, so, so I guess what I said before was still is still going to be true. 114 00:15:25.470 --> 00:15:33.870 William Cheng: If this is the you have all the processes over here, our user space program. Okay, when all of them died, then the process will have no child processes. 115 00:15:34.350 --> 00:15:39.300 William Cheng: So in this case, they need process. What it will do is that it will call exit and it will come inside a kernel and then it will turn off the machine. 116 00:15:39.930 --> 00:15:47.670 William Cheng: Okay, for our for our Colonel one assignment. Everything happened in the kernel. So there's no user space program. So in this case are in a process. 117 00:15:47.910 --> 00:15:51.210 William Cheng: You can still create a bunch of child processes inside the Colonel. 118 00:15:51.540 --> 00:15:59.640 William Cheng: Okay, so when all the child processes are dead, and then it processes will have nothing to do but self terminate when itself terminate it will wake up the idol process. 119 00:16:00.030 --> 00:16:09.150 William Cheng: You know, because of it because the the patent process is is calling. Wait. Right, so I guess we need skirt all the way call is called do way P ID. 120 00:16:09.690 --> 00:16:16.800 William Cheng: Right. So if the parents calling to AP ID when the child process die. It will wake up the parent and the parent. In this case it will turn off the machine. 121 00:16:19.710 --> 00:16:29.940 William Cheng: OK. So again, read the code in idle proc run. Right. So there's this function called idle proc right this is the ideal process. And here's the first procedure. That's why it's called idle proc run 122 00:16:30.720 --> 00:16:36.690 William Cheng: So instead of code. See how it, you know, create the process and then how they wait for it and all that kind of stuff. 123 00:16:38.850 --> 00:16:39.120 Okay. 124 00:16:40.800 --> 00:16:41.910 William Cheng: All right, welcome. 125 00:16:56.400 --> 00:17:00.720 William Cheng: Anyone else has a problem has a question, please feel free to ask 126 00:17:46.740 --> 00:17:49.290 William Cheng: Okay. Nobody has any more questions. 127 00:18:01.980 --> 00:18:03.120 Chao Xiang: Professor, I have a question. 128 00:18:03.510 --> 00:18:06.810 Chao Xiang: Sure in the lecture 10 129 00:18:07.410 --> 00:18:09.150 Chao Xiang: Page 28 130 00:18:10.080 --> 00:18:11.460 William Cheng: All right, let me go there. 131 00:18:17.610 --> 00:18:18.150 William Cheng: This one. 132 00:18:18.690 --> 00:18:21.360 Chao Xiang: Yeah, and I think 133 00:18:22.470 --> 00:18:31.500 Chao Xiang: You're supposed to spread. So where this thread in the running QC or in the Rhonda boasting right or the running state. 134 00:18:32.670 --> 00:18:44.040 William Cheng: So, so if you're the current. So, in the week's assignment you know we only have one CPU. Right, so therefore they can only be one thread that's running set to CPU. So that will be the one to running state. 135 00:18:44.970 --> 00:18:51.060 William Cheng: Okay, and all the other one. You know, if you're not waiting for anything, then you're in the rubble stay and then you sleep in the wrong cute. 136 00:18:52.170 --> 00:18:57.510 William Cheng: Okay, so somebody says, by the way, um, you know, there was a slide that that I guess in lecture. 137 00:18:58.260 --> 00:19:10.350 William Cheng: That talks about the state transition, you know, for threats and and that picture is for Linux for weeks is a little bit different. So please read the colonel one FAQ to see the difference, you know, 138 00:19:11.730 --> 00:19:12.420 William Cheng: In that picture. 139 00:19:13.620 --> 00:19:14.220 Chao Xiang: So, 140 00:19:14.610 --> 00:19:22.410 William Cheng: It's basically the same. But you know, I guess Linux has you know kasliwal asleep now cancel asleep so so so the pictures looks a little bit different. 141 00:19:27.090 --> 00:19:31.380 William Cheng: Right, so, so, so, again, the idea of years at all. All the threats as long as they're alive. 142 00:19:31.680 --> 00:19:33.210 William Cheng: They all sleeping. Some Q 143 00:19:34.320 --> 00:19:41.040 William Cheng: Except for the one that's running out of CPU that will be, you know, the current threat. So, so this one is not a Q. Everything else is a few 144 00:19:44.970 --> 00:19:51.090 William Cheng: Okay, so your current one. Whenever your thread wants to go to sleep you need to find a cue to sleep on 145 00:19:51.750 --> 00:19:56.010 William Cheng: Okay, so that's part of the assignment. There's is for you to figure out where should a threat sleep. 146 00:19:56.910 --> 00:20:02.760 William Cheng: You know, the reason that you need to determine where where the threats, your sleep is that you should think about who's going to wake it up. 147 00:20:03.510 --> 00:20:09.720 William Cheng: Right if another side is going to wake up this thread the other thread needs to know where this thread sleeping so they can wake it up. So if you sleep in the wrong place. 148 00:20:09.930 --> 00:20:14.880 William Cheng: When the other thing that comes to wake up your, your thread thread is not there was then that that is not gonna work. 149 00:20:19.710 --> 00:20:26.910 William Cheng: OK. So again, just like your warm up to your warm up to all your threats work together inside the colonel all the colonel threats work together. 150 00:20:27.360 --> 00:20:29.760 William Cheng: Right, even though they belong to different processes. 151 00:20:30.540 --> 00:20:41.370 William Cheng: So again, every time you see a thread over here, you know, since we're doing MTP equal to wine right MTP equals two, as I would do MTP go to zero. Whenever you see a thread like this you know that there's a process that goes with it. 152 00:20:42.270 --> 00:20:47.610 William Cheng: Right, because every threat is that one process over here, this is the picture of the threat. This is also the picture of the process. 153 00:21:05.040 --> 00:21:37.980 William Cheng: Any other question about lecture 10 154 00:21:54.930 --> 00:21:55.200 William Cheng: Yeah. 155 00:21:56.430 --> 00:22:01.500 Chao Xiang: I don't quite understand the difference between new tech send a few 156 00:22:03.000 --> 00:22:11.580 William Cheng: A few taxes user space. The new tax over here, you can use inside the current design this code over here is for the streets to add implementation of new tax. 157 00:22:12.300 --> 00:22:20.970 William Cheng: Okay, so remember your current assignment you know you you know you're driving equal to zero then exactly straight threads. So, therefore, you can use this code, right. 158 00:22:21.630 --> 00:22:28.740 William Cheng: The new tax, you know, this is the few times is only for Linux. So, you know, for our kind of assignment. We don't have to, you know, we don't have to do a few texts. 159 00:22:30.540 --> 00:22:35.070 Chao Xiang: Oh, we can you use new texting or feminine directory 160 00:22:35.880 --> 00:22:44.940 William Cheng: Yeah, so our current assignment, right, we have these new tags and these new types are so I guess they're called came new tax law and came into a logger, so therefore they are Colonel new tax and against 161 00:22:45.390 --> 00:22:51.930 William Cheng: Current one and two will completely inside the Colonel. So, therefore, you know, as I mentioned before, you are kosher look like this. 162 00:22:53.280 --> 00:23:04.740 William Cheng: Alright, so, so, so this code worse with strength that you know a face shape third implementation at some point we're gonna say driver equals two, one and we're going to start, you know, enabling interrupt. 163 00:23:05.400 --> 00:23:11.940 William Cheng: So in that case, we need to take this code, and we need to modify it. And remember the deal over here is that if you're going to modify a data structure that you share 164 00:23:12.390 --> 00:23:23.400 William Cheng: That you share with a interrupt service routine, then in that case you have to, you have to mask out they interrupt. Okay, so therefore you need to, you know, change the code over here a little bit so that you know you know 165 00:23:25.140 --> 00:23:40.980 William Cheng: Well, I guess, maybe not. Maybe not directly here. But the idea here is that if you need to modify the run queue. You need to make sure that you have to interrupt you have to interrupt you have to interrupt mask, because otherwise, if you go into the interrupt service routine and 166 00:23:42.420 --> 00:23:47.460 William Cheng: When the wrong data structure is dying in a consistent day while you interrupt service routine is going to crash the Colonel. 167 00:23:52.350 --> 00:23:52.560 Chao Xiang: Yeah. 168 00:23:52.650 --> 00:24:01.320 William Cheng: Okay, so, so, so, again, the reason that we talked about few tags, is that for Linux system and windows they implement the one by one model. 169 00:24:01.590 --> 00:24:13.260 William Cheng: Right, so if you implement the one by one model. One of the requirement is that in the user space program. We use a new tags. If the text is available. You should not come inside of Colonel, because otherwise the performance will be on the festival 170 00:24:13.920 --> 00:24:20.430 William Cheng: Okay, so sort of a syrup. You know, we sort of show you how to do that a user space by introducing you know few texts. 171 00:24:21.780 --> 00:24:26.040 Chao Xiang: So either Colonel text we are doing wrong too long, Waldo as well. 172 00:24:28.710 --> 00:24:33.960 William Cheng: So when we get to Colonel three right so kind of one and twos companies that have Colonel, so there is no user space component 173 00:24:34.320 --> 00:24:48.540 William Cheng: When we go to Colonel three yes we're doing with the one level model, right, because the one level model is easy to implement. Yeah. So, so actually in kernel three, the user space. We don't have multi threading. 174 00:24:50.310 --> 00:25:00.480 William Cheng: Right, because we're implement one, you know, one thread per process. So even when, you know, when are they going to the user space we still do one thread per process. So, therefore, there's no multi threading the user space. 175 00:25:04.200 --> 00:25:10.380 William Cheng: Okay, so therefore there's no need for user space mutates there's no you no need for for few tags and nothing like that and user space. 176 00:25:13.980 --> 00:25:19.050 William Cheng: So user space we're about we're going to run a regular program, you know, similar to your warm up one that doesn't you know 177 00:25:20.640 --> 00:25:23.610 Chao Xiang: Yeah, and also a question. 178 00:25:24.060 --> 00:25:26.850 Chao Xiang: So, what, what do you mean for 179 00:25:27.900 --> 00:25:28.140 Chao Xiang: For 180 00:25:30.900 --> 00:25:32.340 Chao Xiang: Lock singular CPU. 181 00:25:33.750 --> 00:25:41.280 Chao Xiang: You mean like all other activity we have stopped and only the lock the lock seem to know what 182 00:25:42.120 --> 00:25:54.780 William Cheng: This locks thing though. OK. So the point of thing. I'll send a lot. See, now this is a lot signal on the bus. Right. So over here, you know, I said, this will what's on this bus. There's address there's data. There's read and read and there's also a lock signal on the bus. 183 00:25:56.100 --> 00:26:08.850 William Cheng: Okay, so when you know when wonder. This is over here, right, you know, a CPU can drive all these signals. Okay, so what the CPU is driving the signal that the lock signal into the bus. No other CPU will get to use the bus. 184 00:26:10.260 --> 00:26:16.290 William Cheng: Okay, so the picture over here is that this this law goes from zero to one. Because, because, you know, the, you know, so again, 185 00:26:16.770 --> 00:26:27.960 William Cheng: The bus is shared by all the CPU. So only one of the CPU can be driving the bus at a time. So, so the CPU that's driving the bus. If they use the lock sick, though they will block out all the other CPUs from using the bus. 186 00:26:28.200 --> 00:26:36.450 William Cheng: Until this lock signal at the end over here, you know, is lower. So yeah, after Lord Lord here. Then another CPU can use the bus. 187 00:26:41.010 --> 00:26:52.470 Chao Xiang: So either a block. It will also block for communication between the RAM and CPU. So basically, all other activity of other CPU are blocked. 188 00:26:53.160 --> 00:27:06.570 William Cheng: Okay, so, so the ram the REM is not a active device right the ravages memory. So therefore, only the CPU can read it up a memory, the memory. Can I send data to the CPU, the memory only respond to the CPU. 189 00:27:07.980 --> 00:27:14.550 William Cheng: Okay, so therefore on the bus. Right. What are the active components on the bus right the CPU is active, because they can I just started b'saykal 190 00:27:14.910 --> 00:27:27.780 William Cheng: Also the IoT devices they have you know they are some of the more sophisticated IoT devices. The one that can do DMA they have a CPU on those IoT devices. Okay, so therefore they can also drive the bus. They can also assert the lock signal. 191 00:27:29.040 --> 00:27:35.250 William Cheng: Okay, so, so basically any one of these, you know, the, the, the, these active components on the bus. 192 00:27:35.700 --> 00:27:44.640 William Cheng: They can all sort of locks it down. So when they are sort of maxing though they're going along, everybody out right. So, therefore, when the CPU is, you know, when the CPU is executing the instruction like this. 193 00:27:44.850 --> 00:27:51.510 William Cheng: It will lock on the bus. So that means that you know if the disk controller want to transfer data law that this control is going up is going to be out of luck. 194 00:27:51.840 --> 00:27:59.130 William Cheng: Okay, but now we're in a phase car want to transfer data what they're locked out on the other. They are locked out for for transferring data in that data into memory. 195 00:28:00.420 --> 00:28:09.330 Chao Xiang: So, is that possible that other CPU still do the calculation, while the locks, even though it's owned by another CPU. 196 00:28:09.930 --> 00:28:15.210 William Cheng: Yeah. So this only loss of the bus. Right. So all the other CPU all the other our devices they are independent hardware. 197 00:28:15.390 --> 00:28:26.220 William Cheng: They can continue to do stuff internally, they're just not allowed to use the bus because whenever they try to use the bus. The bus is busy so therefore they have to wait until the bus become you know not locked 198 00:28:29.550 --> 00:28:34.890 Chao Xiang: Okay, so you can't sue us the memory for calculation. 199 00:28:35.520 --> 00:28:38.130 William Cheng: No. No. The only way to use memory is to go through the bus. 200 00:28:39.480 --> 00:28:45.060 William Cheng: Right. So if the bus. Oh, sorry one CPU over here has locked the bus. No, nothing else can access memory. 201 00:28:49.140 --> 00:29:00.600 William Cheng: Right, because in order for you to use memory. You have to go. You have to use the bus. But this bus right now over here is locked out by the CPU. So therefore, nobody could get can get in between to use it to use a memory. 202 00:29:04.230 --> 00:29:13.500 William Cheng: Right, because if the if another CPU can actually get in between over here to use memory, what stopping the other CPU to read a lock right then we're going to end up with exactly this problem. 203 00:29:14.940 --> 00:29:17.100 William Cheng: The problem that we saw with this problem. 204 00:29:18.360 --> 00:29:24.210 William Cheng: Right, if both CPU can actually read the locker, the same time. Well, then you're going to end up with, you know, implementing locking correctly. 205 00:29:26.340 --> 00:29:29.370 William Cheng: So, therefore, you have to lock out the bus so that nobody can use memory. 206 00:29:30.870 --> 00:29:31.200 OK. 207 00:29:32.670 --> 00:29:41.160 William Cheng: OK. So the picture I draw over here right memory sitting over here, new tax lock is sitting right here. You can have as many CPU as you want as many IoT devices that you want. 208 00:29:41.370 --> 00:29:53.550 William Cheng: Once you lock on the bus. Nobody else get the bus so nobody gets to us. It gives us memory you have exclusive use of memories. Right. So, therefore, you can. What you can do is that you can refer our memory. You can write a memory. So in one atomic operation. 209 00:29:56.430 --> 00:29:59.580 William Cheng: Right, and that this is this is the picture for the atomic operation. 210 00:30:01.620 --> 00:30:04.530 William Cheng: By you read and you write, and nobody can get in between. 211 00:30:08.880 --> 00:30:15.360 William Cheng: Yes. Okay, so, so, so this is this is the true atomic operation. Everything else is built on top of this operation. 212 00:31:28.200 --> 00:31:28.800 William Cheng: I just want to 213 00:31:29.850 --> 00:31:34.140 William Cheng: Ask if anybody has questions about this slide the threat switch slide. 214 00:31:56.280 --> 00:32:01.020 Chao Xiang: So in the kernel. Well, we can directly use this code. 215 00:32:02.040 --> 00:32:06.720 Chao Xiang: Like this. We do do need to not need to change this right 216 00:32:06.810 --> 00:32:08.970 William Cheng: Into a wall. So, so yeah, so 217 00:32:09.360 --> 00:32:17.250 William Cheng: You know, you need to understand this code or, you know, try to understand the structure of this coat. I don't think you need to change the structure of this code. 218 00:32:18.420 --> 00:32:24.960 William Cheng: Okay, so in winnings, they have different function called have different names. The I'll maybe you have to add one more instruction here and one point introduction there. 219 00:32:25.140 --> 00:32:36.330 William Cheng: But the basic structure of the code should be like this. Okay, so some people did was that, you know, they move some of these code around how they move this call here, move the car, you know, so, so that then then then it doesn't work. 220 00:32:37.140 --> 00:32:44.190 William Cheng: Okay, so would you need to do is you understand why you know this particular structure works. And then you can convert them into Phoenix coke. 221 00:32:47.400 --> 00:32:58.770 William Cheng: Or so, so, so again, so we. So think about it is that when I come inside. Over here IPL the example that I gave him the class right IP or equal to seven I change it into 31 and then at some point. 222 00:32:59.130 --> 00:33:06.990 William Cheng: And then eventually when this thread runs again the IP all we hear is going to be. We're going to set it back to seven. Right. And then when you return it is equal to seven. 223 00:33:11.670 --> 00:33:17.010 William Cheng: Okay, and I'll be here, you know, this is how you, how you swap the contacts. So again, we need to have a different way. 224 00:33:17.700 --> 00:33:32.130 William Cheng: To save all the register, you know, into the first argument and then take the second argument put all the register back inside of CPU. So, you know, might not be exactly the same code, but the structure of the code should be the structure of the coach should be like this. 225 00:33:42.780 --> 00:33:49.740 William Cheng: Right so so again this code over here. It looks ridiculous right set up here zero said IPL 31 zero 3130 so 226 00:33:49.950 --> 00:33:58.260 William Cheng: So some people say, oh, you know, maybe I should do something else. Don't do something else in the this is the structure of the code, right, you have to set IP or equal to zero. 227 00:33:58.470 --> 00:34:05.430 William Cheng: So that if there's any pending interrupt will get delivered, and then you said IP r equals 30 while at the highest level because you want to use the wrong key. 228 00:34:05.970 --> 00:34:14.040 William Cheng: Right, so if you don't do that, then you know either. You know Rob will not get deliver or, you know, we each have access outranks you you don't have IP or equal to 31 and bad things going to happen. 229 00:34:14.760 --> 00:34:26.280 William Cheng: Okay, so again you know all these lines. The lines of code over here every line has a reason why it's there, you might end up writing more lines of code, but I don't think you should write less lines of code that might cause that might cause trouble. 230 00:34:40.770 --> 00:34:46.710 William Cheng: And also, you know, the way they should think about threat switches. Right. You think about it from the perspective of your threat right 231 00:34:47.010 --> 00:34:55.560 William Cheng: Now, so, so when when I say API equal to seven IP. I go to seven at the beginning. Over here you should think about. I'm a kernel thread. I'm calling first switch my API equal to seven. 232 00:34:55.860 --> 00:35:01.560 William Cheng: There, when I execute this code. Well, well. So, so this is a he just sort of issue, you know, 233 00:35:02.070 --> 00:35:06.840 William Cheng: Sort of thing about small contact, it just a function call and then it will return right away. 234 00:35:07.050 --> 00:35:18.840 William Cheng: In reality, you might return, much, much later because yeah you're waiting for this dinner. Rob, and something like that. But the abstraction is that this is just a function call. When you finish this function call you return over here. You said IPL back to seven and you return on this option. 235 00:35:20.670 --> 00:35:28.290 William Cheng: Okay, so all the other code over here is is just in case it was empty, just in case in Rob's not deliver right away, then you have to go. You have to go to sleep and all these kinds of stuff. 236 00:35:29.160 --> 00:35:37.860 William Cheng: Okay. But otherwise, think about this at from the perspective of your threat and not really think about this function as, you know, switching the context to another threat right 237 00:35:38.070 --> 00:35:45.120 William Cheng: You know, so in this case over here. Think about it. Yes, I'm giving up the CPUs, I will go to sleep, and later on I'm going to wake up and that will return from this function. 238 00:35:46.260 --> 00:35:53.040 William Cheng: Okay, even though the, the real thing that happened over here is that you give the CPU to another threat if another threat is available to run in the wrong to you. 239 00:36:27.090 --> 00:36:35.790 William Cheng: Okay, if there are no more question. I'm gonna stop the recording and then you guys can talk to each other. Is that, is that okay 240 00:36:41.130 --> 00:36:42.930 William Cheng: I'm stopping the recording now.