WEBVTT 1 00:00:03.120 --> 00:00:14.429 William Cheng: This is the second part of lecture to so we just finished reviewing computer organization. So we're going to come back and talk more about traps and how we're interrupt. Okay. 2 00:00:15.030 --> 00:00:19.470 William Cheng: So on the right hand side. Again, this is the picture that I draw before we have the operating system. 3 00:00:20.160 --> 00:00:24.930 William Cheng: You know, in the middle we have application on top and then we have hardware at the bottom. 4 00:00:25.560 --> 00:00:34.320 William Cheng: So when you tap into the operating system. Right. The idea here is that you're executing application code and then all of a sudden you fall through a trap, and you're trapping the operating system. OK. 5 00:00:34.860 --> 00:00:40.950 William Cheng: So the trap or the general means for invoking the Colonel, or the operating system, you know, from the user code. 6 00:00:41.610 --> 00:00:46.830 William Cheng: So we sort of think about them as trap because that means that you sort of cause an error. And then you tap into the operating system. 7 00:00:47.280 --> 00:00:53.730 William Cheng: Okay, so, so, so we we usually think of a trap is error. So what kind of errors. Do you have well. For example, if you divide by zero. 8 00:00:54.150 --> 00:01:00.900 William Cheng: Okay PSA x divided by zero. If you execute a co like this. What is the you know what what is supposed to happen to your program. 9 00:01:01.350 --> 00:01:08.670 William Cheng: Okay, if you divide by zero. That's illegal operation. So therefore your program is not allow to execute any more code. 10 00:01:09.480 --> 00:01:16.290 William Cheng: Or so therefore it what has to be done is that at this point the operating system has to intervene and kill your program. 11 00:01:16.860 --> 00:01:23.130 William Cheng: Okay. So, so what are the situation like this, right, for example, if you divide by zero, you should do progress should be should be killed. 12 00:01:23.550 --> 00:01:31.320 William Cheng: If you take the square root of a negative number. If you take a logarithm of a negative number. If you cause a segmentation fault, guys. So in that case, 13 00:01:31.680 --> 00:01:35.250 William Cheng: You know you indicate your program needs to be killed. 14 00:01:35.730 --> 00:01:41.730 William Cheng: They also something called bus error as typically will sort of think about bus error and segmentation fault. They are very similar thing. 15 00:01:41.940 --> 00:01:49.500 William Cheng: So the bus error. The idea here is that you try to go to the bus, even though you're not supposed to go to the bus. So if you go to the bus, then in that case is an error. 16 00:01:49.770 --> 00:02:00.150 William Cheng: So so so therefore the opposite the opposite will kill your program. Yeah. So there are all kinds of errors like this. So when you provide do something you're not supposed to the system will kill your program. 17 00:02:00.840 --> 00:02:09.240 William Cheng: Yeah, so, so usually that's what trap means. But as it turns out it doesn't have to be something like an error. Okay, so for example when you make a system called 18 00:02:09.810 --> 00:02:17.250 William Cheng: You need to access hardware you ask the operating system for help. Well, in that case, you know, you know, in that, in that case, it's totally legitimate. 19 00:02:17.730 --> 00:02:24.570 William Cheng: Okay, but how do you come inside operating system what we just talked about it. The only way to come inside operating system is to use the trap mechanism. 20 00:02:25.230 --> 00:02:37.170 William Cheng: Okay, so if you want to ask the opposite will help you also use a similar kind of trap mechanism. Right, we're going to sort of talk about that a little later. Okay, to see how you actually get into the ordinances that meal in a legitimate legitimate way. 21 00:02:38.430 --> 00:02:43.140 William Cheng: The other one, we're going to talk about quite often in this call is something called page fall. So remember, 22 00:02:43.440 --> 00:02:49.650 William Cheng: Earlier on, I guess, intellectual, one is sort of talked about, you know, the fact that you add your space. Part of it is mapping to memory. 23 00:02:49.860 --> 00:02:55.650 William Cheng: And part of this map to the disk. So if you start using your address space if it's if it turns out that your address space. 24 00:02:56.010 --> 00:02:58.500 William Cheng: You know, part of your address is that you're trying to use 25 00:02:58.740 --> 00:03:09.150 William Cheng: Actually map onto the disk one. In that case, the operating system will need to help you to copy the data from the test to memory and then change the memory map. And this way you can continue to execute your program. 26 00:03:09.660 --> 00:03:17.160 William Cheng: Okay, so in that case, again, it's a good thing. Well, so that's usually not as painful. We're going to learn a lot about baseball in this class, right. So over here again. 27 00:03:17.340 --> 00:03:29.520 William Cheng: Just, just going to sort of introduce the terminology here. Well, you get paid for. It's a good thing you ask the operating system to copy data from the dis into memory and then returned back to you so you can continue with whatever you eat that whatever you're doing before. 28 00:03:30.000 --> 00:03:41.790 William Cheng: That. So again, in this case a peaceful is also going to turn into a trap because trap is the only way you can start with the application program and you can go in and go into the system and the system in this case will be able to help you. 29 00:03:42.600 --> 00:03:47.610 William Cheng: There, whereas over here it says the trap always listen to some sort of a response. 30 00:03:47.820 --> 00:03:56.460 William Cheng: Either the operating system is going to kill your program or the minister has been a help you with the system call with the page fall or whatever the action that optimism is supposed to take 31 00:03:56.850 --> 00:04:07.350 William Cheng: Okay, so, so the pictures that I draw over here is that again application. Go over here you trapped into the system and operations will do something for you either kill your program or do something for you. Yeah. 32 00:04:08.910 --> 00:04:15.240 William Cheng: Alright, so a system called it's a special kind of trap that you know because you know when we try to ask 33 00:04:15.810 --> 00:04:22.890 William Cheng: For help. We need to come inside operating system in a very, very controlled way. Okay. You shouldn't be able to come into the organism, any way you want. 34 00:04:23.280 --> 00:04:31.860 William Cheng: Okay, so, so if you want to ask albinism for help in the Unix and Linux system. And there's only one way is for you to try. Peter abundances them. So in this case, 35 00:04:32.220 --> 00:04:43.410 William Cheng: You know, so, so, so how do you actually get in there. Now as it turns out that for every CPU, there's a there's a machine instruction that you need to execute and that machine structure is called the trap machine structure. 36 00:04:43.980 --> 00:04:49.860 William Cheng: Okay, so when you execute a trap machine instruction, you are basically asking the colonel you asking the operating system kernel say 37 00:04:50.040 --> 00:04:57.480 William Cheng: Hey, I need some help. So I'm going to execute this tribe machine instruction on purpose. So I will get inside abundances them and then the offices and we'll try to figure out 38 00:04:57.630 --> 00:05:07.020 William Cheng: What kind of help to you want and offices and we'll do something for you. And when it's done, it's going to return to back to you is going to return back into the user space program and you're going to continue from a trap. 39 00:05:07.800 --> 00:05:13.560 William Cheng: Okay, so therefore the trap is almost like a function call, right, because when you actually do that trap you go inside the operating system and the 40 00:05:13.740 --> 00:05:21.450 William Cheng: System will return back to you. It's like a function call. Right. And when you finish executed in that trap you go to the next instruction. So that's what we call this a system called 41 00:05:21.690 --> 00:05:29.490 William Cheng: Okay, you call upon the operating system for help to ask operating system to do something for you. Okay. So for example, you know, what kind of, you know, system call 42 00:05:29.880 --> 00:05:40.290 William Cheng: Right now you're doing one on one, right. So when you try to read data from the file while you can read the data from the file directly. So therefore, you're going to make a system called you companies that albinism say hey organism. Could you read the data for me. 43 00:05:40.500 --> 00:05:46.410 William Cheng: And returned the data, you know, from the opportunities to the application and now the application. You can see what data is in the input. 44 00:05:47.460 --> 00:05:53.310 William Cheng: Okay, so, so we're going to talk about all kinds of system call because the system calls are kind of important in the operating system. 45 00:05:53.640 --> 00:05:58.590 William Cheng: So so so yeah we're gonna we're gonna look at quite a few different kind of circles. Yeah. Alright. 46 00:05:59.190 --> 00:06:06.780 William Cheng: So system called we're going to talk about no more detail in chapter three as Rana, we just sort of introduced to the idea is that you know you execute travertine structure in 47 00:06:07.020 --> 00:06:15.120 William Cheng: The traveling instructions going to get you inside the kernel. And then the opposite is going to take over. Do something for you. And then returned back into user space program. Yeah. 48 00:06:16.800 --> 00:06:23.820 William Cheng: All right, so, so, so, so, so, so, so this one is I guess we sort of talked about this, this direction over here. 49 00:06:24.420 --> 00:06:25.860 William Cheng: Keep clicking on the wrong place. 50 00:06:26.250 --> 00:06:28.530 William Cheng: The application company set up a new system using a trap. 51 00:06:28.710 --> 00:06:33.930 William Cheng: And the other way is come from the hardware and then you go into the operating system. Right. So that's the next thing that we're going to talk about 52 00:06:34.050 --> 00:06:40.110 William Cheng: So again to interrupt is a request from external devices say that I have. I was doing some I operation for you. 53 00:06:40.290 --> 00:06:49.590 William Cheng: And now it's done. So they have what it will do is that it will call upon the operating system to say execute interrupt service routine for me. So this way you can finish. Yeah. 54 00:06:50.310 --> 00:06:55.200 William Cheng: You can finish the hardware, the horror, the horror, the horror app operation. 55 00:06:55.770 --> 00:07:00.720 William Cheng: But so, so how do you start a horror operation right so the evidence is over here is going to give the horror command. 56 00:07:00.930 --> 00:07:08.400 William Cheng: And we saw before that there's this bus architecture. So when the operating system execute that particular instruction that start a bust cycle on the bus. 57 00:07:08.610 --> 00:07:14.400 William Cheng: And if the address that you put correspond to iOS device that's how you start an operation on iOS device. 58 00:07:14.910 --> 00:07:23.670 William Cheng: Okay, so basically you start a bus cycle and the cycle the address map to the iOS device. What then you start, you start using iOS device right so so again if you want to access the or the harbor over here. 59 00:07:23.940 --> 00:07:28.470 William Cheng: All you have to do is to execute code inside the operating system. Okay, so I guess we 60 00:07:28.950 --> 00:07:31.740 William Cheng: didn't really talk about this. So if you do something like this. 61 00:07:31.980 --> 00:07:43.650 William Cheng: You start the Iowa you you start the iOS device by doing operation for you when it's done. It's going to, you know, call upon the operating system using a how're interrupt to say executed interrupt service routine for me. 62 00:07:44.490 --> 00:07:47.700 William Cheng: So in this case, what or you're supposed to do inside the interrupt service routine. 63 00:07:48.480 --> 00:07:53.280 William Cheng: Okay, so typically the interrupt service routine can do a lot of stuff, but there are two things that has to do 64 00:07:53.880 --> 00:08:00.000 William Cheng: Okay, so. So again, the hardware finish the IO operation. So now nothing. That means that the hardware is free now. 65 00:08:00.480 --> 00:08:06.270 William Cheng: Okay, so if the horror is free now instead interrupt service routine. What you need to do. So you need to start the next IO operation. 66 00:08:07.110 --> 00:08:12.810 William Cheng: Okay, so, so, so why do you have to do that, right, because some of the, you know, sort of the IoT devices are very, very expensive. 67 00:08:13.140 --> 00:08:23.100 William Cheng: You spend a lot of money on these IoT devices. So you want to keep them as busy as possible, right. So, therefore, as soon as the IoT device finished the previous operation, you will start the next operation. 68 00:08:23.820 --> 00:08:30.120 William Cheng: Okay. So inside you know observers will be over here which is executed by the operating system, you will start the next level operation now. 69 00:08:30.510 --> 00:08:31.890 William Cheng: The other thing that you have to do is that 70 00:08:32.280 --> 00:08:40.230 William Cheng: You know, so why do you get an IO, you know, so I will completely interrupt right because you start the IO operation over here by asking the hardware to do something for you. 71 00:08:40.410 --> 00:08:45.330 William Cheng: So in this case you have something inside your operating system kernel waiting for the operation to finish. 72 00:08:46.170 --> 00:08:54.150 William Cheng: Okay, so who who's going to be something that's waiting for the operation to finish. So the way we sort of think about is that, you know, inside organism, you know, there is 73 00:08:54.630 --> 00:09:02.130 William Cheng: I think last that I mentioned before, there's user threat. There's Colonel threat user thread running the user space when it comes inside the Colonel, they become a kernel threat. 74 00:09:02.310 --> 00:09:06.690 William Cheng: So what the current author would do is that he will also start an IO operation on an iOS device. 75 00:09:07.170 --> 00:09:11.340 William Cheng: Okay, so therefore, in this case, when a colonel threat started operation on iOS device. 76 00:09:11.550 --> 00:09:23.790 William Cheng: That hurdle is going to fall asleep, because apparently has nothing to do. Okay, except for except a way for the IO operation to get finished so therefore inside the IO completion interrupt. What you have to do is that you have to wake up that Colonel threat. 77 00:09:24.390 --> 00:09:34.440 William Cheng: OK, so the current sleeping. Somebody is that the colonel and the either the I always finish you wake up the courage to say, hey, this operation is done so, please continue to do whatever that you were supposed to do. 78 00:09:35.940 --> 00:09:44.910 William Cheng: Okay. So is that a server in the rosters within two major things you have to do. Number one is to start a next I operation because you want to keep the iOS device as busy as possible. 79 00:09:45.120 --> 00:09:52.800 William Cheng: And number two is that there's going to be a colonel thread waiting for this IO operation to finish. So, therefore, you have to wake up. If you have to wake up the colonel threat. 80 00:09:53.370 --> 00:09:59.250 William Cheng: or so later on we can also tell you a little more detail about the difference between user thread and turtles. Yeah. But again, right now we just sort of start to 81 00:09:59.670 --> 00:10:04.260 William Cheng: Think about is that there are two kinds of three. I went running the user space. The other one runs at a kernel. 82 00:10:04.650 --> 00:10:12.270 William Cheng: Is that the colonel that Colonel threat my starter I operation and then it will go to sleep when the IO operations is finished, you need to wake up the cardinal threat. 83 00:10:13.110 --> 00:10:24.060 William Cheng: Right. All right, so, so, so here's the sort of said, you know, or the operations was done. So, you know, Rob, they are. They're also handle independently of any user program. 84 00:10:24.570 --> 00:10:32.070 William Cheng: Okay, when you get a hardware interrupt. What is the, you know, you know, so right before you get heard the horror interrupt. What is your CPU doing 85 00:10:33.720 --> 00:10:40.920 William Cheng: What the CPU is doing, doing whatever it was doing. So I mentioned before, you want the CPU to be running user space code, right, because you know the 86 00:10:41.130 --> 00:10:48.900 William Cheng: You know, the, the operating system is there to help out the user space. Space Program. So typically what you want is that you want the CPU to be keep executing user space program. 87 00:10:49.290 --> 00:10:55.620 William Cheng: Okay, so when you get a horror interrupt you know which user space program is actually running well the answer is we don't know. 88 00:10:56.130 --> 00:11:05.700 William Cheng: Okay, because now for your on your laptop you're running 50 different you know you're running 50 different application over here and then the CPU is juggling all these 50 different application like crazy. 89 00:11:06.150 --> 00:11:13.590 William Cheng: Okay, so when you get a hardware interrupt the hardware in Rob actually happen. Totally asynchronously with respect to what are you doing inside a CPU. 90 00:11:14.850 --> 00:11:22.680 William Cheng: Okay, so please understand that the hardware device and the CPU. They're both hardware. Right. So, you know, again, the different kinds of hardware. There's a CPU. There's memory. There's these IoT devices. 91 00:11:22.980 --> 00:11:27.360 William Cheng: They are different pieces of hardware. So they are capable of running a completely in parallel. 92 00:11:27.900 --> 00:11:37.200 William Cheng: OK, so the CPU is doing its own business. And now when the iOS devices finish it will generate interest and this interrupt is completely asynchronous with respect to the CPU. 93 00:11:37.560 --> 00:11:43.590 William Cheng: Okay, so it can happen at any time and chances are, whatever the CPU is doing right now has nothing to do with what caused the interrupt. 94 00:11:44.460 --> 00:11:55.050 William Cheng: That and also from what I just said. Over here you know you had a colonel thread that starts in Iowa operation when the IO operations finish. Well, at that time, what does that Colonel to i doing well. The Colonel Sanders sleepy. 95 00:11:56.010 --> 00:12:01.200 William Cheng: That. So, so what does it mean for Colonel Sanders sleeping right so if a thread is sleeping. That means that it's not using the CPU. 96 00:12:02.460 --> 00:12:11.880 William Cheng: Okay, if a co author is sleeping over here. I mean, clearly when you get a, you know, when you get hardware interrupt the how're interrupt is going to be. It's going to have nothing to do with what's currently running out of CPU. 97 00:12:12.570 --> 00:12:18.360 William Cheng: Right, because you know what started the operation that pretty good Colonel threat at this time, it should be sleeping at that time. 98 00:12:19.470 --> 00:12:26.250 William Cheng: Okay, so therefore again. So this is one, one thing that's very, very different about interrupt versus a trap. Right. Both of them start executing, you know, 99 00:12:27.120 --> 00:12:36.780 William Cheng: Both of them is going to start executing operating system Co. But if you come inside the colonel using a trap. And then, you know, then, then you know exactly who caused the trap. 100 00:12:37.440 --> 00:12:40.770 William Cheng: Right, whoever the cost of trap is the one that actually executing inside of CPU. 101 00:12:41.490 --> 00:12:44.700 William Cheng: Okay, but what about when when you get to interrupt. When you get to interrupt. 102 00:12:44.970 --> 00:12:58.020 William Cheng: You know the you know the the the code that actually caused the interrupt actually belong to a kernel threat and that couldn't offer is currently sleeping. So therefore, when you get a hardware interrupt whatever you're doing inside of CPU has nothing to do with interrupt. 103 00:12:59.070 --> 00:13:06.240 William Cheng: OK. So again, that's a major distinction between, you know, executing kernel code because of a trap or because of a hurry interrupt. Yeah. 104 00:13:07.620 --> 00:13:12.870 William Cheng: Alright, so the response will interrupt may or may not indirectly affect the current running program. 105 00:13:13.290 --> 00:13:24.090 William Cheng: Okay, so you're you're running you're doing something inside your CPU you're running a program. And now you get a hurry interrupt the highway interrupt my have nothing to do with what you will. What will you doing inside of CPU. 106 00:13:24.420 --> 00:13:30.000 William Cheng: Okay, even though there's a possibility that might be related because a program. Can you can run a multi threaded program. 107 00:13:30.270 --> 00:13:38.160 William Cheng: Well, so in that case, you know, whatever you're doing, you might you might be running a different thread inside the same program. So they might be related, but most likely they're not related. 108 00:13:38.910 --> 00:13:45.960 William Cheng: Okay, so. So typically, you know, the action that you're going to take inside interrupt service routine has no direct effect on the current running program. 109 00:13:47.880 --> 00:13:53.190 William Cheng: Alright, so again this is very, very different, you know, from, you know, from a, from a trap. 110 00:13:54.960 --> 00:14:00.660 William Cheng: Alright, so, so again I used to wear a synchronous, asynchronous. Right. So these are important concept. 111 00:14:00.930 --> 00:14:07.950 William Cheng: A hardware, you know, so again every time we talk about the interrupt. We're talking about a hurry. Interrupt okay a hardware interrupt is a asynchronous event. 112 00:14:08.580 --> 00:14:16.710 William Cheng: Okay. It was generated it gets generated in parallel with what the CPU is doing. So, therefore, you know, you really can't control exactly what it's going to happen. 113 00:14:17.550 --> 00:14:28.800 William Cheng: Okay, so it's asynchronous was respected what's actually, you know, executing inside the CPU. So these are the CPU. What can you be executing right okay in the CPU can only do three different things you can be, you know, 114 00:14:29.370 --> 00:14:39.330 William Cheng: You can be in the interim contacts or you can be in the threat contacts in the threat contacts. You can either be in a user to a contest or inside the colonel three contacts. 115 00:14:39.960 --> 00:14:49.200 William Cheng: Okay. So in this case, what you are all you know when a Hora interrupt when how it happens. It has nothing to do with what's executing side of CPU. 116 00:14:49.710 --> 00:14:58.380 William Cheng: Yeah. On the other hand, a trap occurs when a trap happens it happens synchronously, with respect to the executing entity that's excellent inside of CPU. 117 00:14:58.590 --> 00:15:07.560 William Cheng: Because in order for you to cause a trap. You have to be running out of CPU. So, therefore, if you are a threat. That's actually good inside CPU that you are the one that caused the trap. 118 00:15:08.280 --> 00:15:19.200 William Cheng: Okay, so therefore we actually, we know exactly who caused the trap. Okay. But when you get a Harvard interrupt you can really you don't really know. You know, you know, this pretty good harbor interrupt you know who you know. 119 00:15:20.490 --> 00:15:28.920 William Cheng: This particular highway interrupt who does it actually belong to. Okay, so for example again you have 50 different program. One of them starts and I operation, but which one 120 00:15:30.030 --> 00:15:34.440 William Cheng: Okay, so when you get a highway interrupt you. Actually, you need to be able to go inside the colonel and try to look 121 00:15:35.100 --> 00:15:40.530 William Cheng: To try to look inside the colonel data structure and find out you know which program actually cause of this particular interrupt. 122 00:15:40.830 --> 00:15:53.040 William Cheng: Okay, because one of the threads that a program over here is sleeping. Is that a kernel. And that will be the one you need to wake up there. So, so you have to do all these operation in order for you to find out who was actually responsible to handle that interrupt. Yeah. 123 00:15:54.120 --> 00:16:02.400 William Cheng: So again, these are you know to do things synchronously or asynchronously. It's a very important concept. If you're doing things in completely in parallel using different hardware. 124 00:16:02.610 --> 00:16:15.810 William Cheng: Then in that case the event that you generate is going to be asynchronous. But if you do everything inside the same piece of hardware wasn't in that case it will be synchronized. Okay, so a trap is a synchronous event, while hardware interrupt is asynchronous event. Yeah. 125 00:16:18.360 --> 00:16:24.210 William Cheng: There's also something called software interrupt. Right. So when we talk about the word interrupt we're referring to highway interrupt. 126 00:16:24.330 --> 00:16:30.330 William Cheng: But we can actually add a word software in front of it. And this one will be known as a software interrupt whereas what's in the software interrupt. 127 00:16:30.660 --> 00:16:40.800 William Cheng: As it turns out, for CPUs, like, you know, like Intel, there's an instruction called the software interrupting structure. Okay, the operation co for the software interrupt is called end 128 00:16:41.880 --> 00:16:54.960 William Cheng: Okay, so it is followed by numbers in zero x three, you know, or zero x three over here. Again, this is a hex number is equal to three is the same as a decimal three. This is a machine instruction. It says generate hardware interrupt number three. 129 00:16:56.580 --> 00:17:06.870 William Cheng: Okay. So actually, as it turns out, for Intel and the way they generate a trap machines, the way they implement a trap machine instruction is that you they use the software interrupt machine structure. 130 00:17:07.200 --> 00:17:20.610 William Cheng: Okay, so that instruction is called N zero x to eat. Okay. Wiser X to eat. I have no idea. Right. So, so we actually have this machine instructions in zero x to eat this one is a software interrupt. It's used to implement the track machine structure. 131 00:17:21.900 --> 00:17:29.010 William Cheng: Okay, so if you want to tap into the CPU by making the system call. Well, in that case, the, the machine instruction that you execute will be in zero x to eat. 132 00:17:30.240 --> 00:17:40.800 William Cheng: Alright, so, so, so the the instruction over here is followed by a single bite or number. So by has 256 choices, right. So there are 256 different software interrupts 133 00:17:41.700 --> 00:17:46.650 William Cheng: Okay. One of them is special is known as the the trap machines, you're shooting for intimacy and directs to eat. 134 00:17:47.100 --> 00:17:53.730 William Cheng: There are other software interact. For example, so what what are the software interrupt. So for example, we turn it to your program. 135 00:17:54.300 --> 00:18:00.660 William Cheng: We are, how do you develop your program right you're you're running your program. How does he actually you stop the program from running when it hits a breakpoint. 136 00:18:00.930 --> 00:18:11.370 William Cheng: Well, guess what you would do is that you will you will go to the the the machine code for for, you know, for for that particular program and replace one of the machine code in there with a jail, you know, with the software interrupt. 137 00:18:11.940 --> 00:18:18.300 William Cheng: Okay, so I think on Intel the you know the interrupt zero x is this 100 x three. 138 00:18:18.630 --> 00:18:28.710 William Cheng: When you execute that instruction over here that will be a debugging you there'll be a debugging software interrupt. So in that case, you will pause the, you know, you will pause the program that you're running 139 00:18:28.890 --> 00:18:35.970 William Cheng: And now another program is going to be used the article is called a debugger. The debugger is going to be used to, to, to help you figure out what's wrong with your program. 140 00:18:37.230 --> 00:18:50.940 William Cheng: Okay, so there are other software interrupt okay there you can actually execute. So in this case, again we execute their software interrupt. It's, it's kind of like a trap you go inside the kernel. So you go into the kernel and you execute a software interrupt service routine. 141 00:18:52.620 --> 00:18:58.230 William Cheng: Alright guys, again, this is very different from a horror interrupted is how we interrupt this generate asynchronously. 142 00:18:58.440 --> 00:19:05.160 William Cheng: What a software interrupt the southern Europe has to be executed synchronously, because it's part of your program execution stream. 143 00:19:05.520 --> 00:19:09.690 William Cheng: Right, you're executing you know code, code, code inside your program and one of them is the software interrupt. 144 00:19:10.290 --> 00:19:15.870 William Cheng: Okay, so therefore, again, this case, you try things out, Colonel. But you try this out a colonel in a synchronous session. Okay. 145 00:19:16.620 --> 00:19:24.630 William Cheng: All right. Oh, here's a little comment to say, although the mechanism for handling interrupts are very, very similar to software interrupt and we're going to talk about any chapter three. 146 00:19:25.830 --> 00:19:36.360 William Cheng: Alright, so again, you know, you think about the the tribe machine instruction rights at in zero HTTP. It's a software interrupt. Okay, so, so, so you come inside the colonel using the software interrupt. Well, you get a 147 00:19:36.900 --> 00:19:44.280 William Cheng: Lot of it is that you're gonna start executing interrupt service routine. Okay. Similarly, when you get a highway interrupt, you're going to end up excuse to interrupt service routine. So sounds like they're 148 00:19:44.910 --> 00:19:49.890 William Cheng: In the end, they're going to end up to be exactly the same thing you're going to end up being some sort of an interrupt service routine. 149 00:19:50.250 --> 00:20:02.760 William Cheng: Okay, and then you will be right that the end they all end up to be exactly the same thing, right, but conceptually, when we think about trap software interrupt highway interrupt, we need to know about the differences between all these different mechanisms. Yeah. 150 00:20:04.440 --> 00:20:11.070 William Cheng: All right, so, so, so now we sort of know right coming for the application to the opportunities and that's called a trap. 151 00:20:11.280 --> 00:20:17.190 William Cheng: Coming from operating system to hardware. It's called executing code inside the operating system. You can tell the hardware to do that. 152 00:20:17.430 --> 00:20:26.640 William Cheng: By starting a bus operation right coming from the hardware to the operating system that's calling interrupt so we only have one arrow missing that arrow is going from the operating system to the application. 153 00:20:27.480 --> 00:20:37.530 William Cheng: Okay, so that you know this operation, starting from the operating system to the application that's know as an up call, right, because you know operating system in the middle, you're going to go up into the application space. 154 00:20:38.010 --> 00:20:46.440 William Cheng: So why would you want to make a call back. So we're going to talk about the Unix and Linux operating system. So what happened is that, you know, the UNIX and operating 155 00:20:46.770 --> 00:20:51.000 William Cheng: System over here. So let's say that you for some reason you know that your program is going to divide by zero. 156 00:20:51.480 --> 00:21:01.290 William Cheng: Okay, you don't know when. Okay, but what you want to do that you weren't how to use them to say hey you know when I divide by zero. Don't kill my program, but instead call this function on my behalf. 157 00:21:01.800 --> 00:21:06.600 William Cheng: Okay. So inside the application, you know, poem over here you can I just set up a what's called a signal handler. 158 00:21:07.110 --> 00:21:21.870 William Cheng: OK, so I'm going to set up a signal. Hello, I'm going to call it the f function over here. Okay, so what you will do is that you tell the organism say when I divide by zero. Don't kill my program, but instead performance up call into my user space program and call this function on my behalf. 159 00:21:23.370 --> 00:21:28.440 William Cheng: Alright, so it's going to be. So when I say on my behalf. That means that it's going to be as if I am calling this particular function. 160 00:21:28.860 --> 00:21:34.470 William Cheng: Okay. So, therefore, what I would do is I will call this function and then I will return back into the organism as if nothing has happened. 161 00:21:34.890 --> 00:21:41.250 William Cheng: Okay, so in this case I trapping the audiences and because I divide by zero. And now, instead, the organism is going to make an appt call 162 00:21:41.340 --> 00:21:50.910 William Cheng: Call my function and returned back into the audience's them and now the organisms that are pretend that you didn't divide by zero. I'm going to return back to the application program and then you going to go to the next. The next machine structure. 163 00:21:52.260 --> 00:22:00.270 William Cheng: Okay. So in this case, I can actually set up my program so that my program doesn't get killed. You know when when I don't want my program to kill 164 00:22:00.840 --> 00:22:02.820 William Cheng: When, when I don't want my program to get killed. 165 00:22:03.150 --> 00:22:12.000 William Cheng: You guys, why do I want to do something like that like this, right, because I expect to be divide by zero. So listen, I'm going to write a program. This is a very, very complicated program, we're going to run it for days. 166 00:22:12.870 --> 00:22:16.560 William Cheng: If it turns out I divide by zero. I'm going to lose. You know, I'm gonna lose days of work. 167 00:22:17.070 --> 00:22:26.160 William Cheng: Okay, so what I can do over here is that I know that you know my programs that perfect has been in them. So when they run four days and I divide by zero. I want to make sure that my program will survive. 168 00:22:26.970 --> 00:22:34.860 William Cheng: Okay. So in this case, I can sort of make a deal with the opportunity to say hey you know don't kill my program. I'm going to clean out my program. So therefore I will gracefully terminate my program. 169 00:22:35.280 --> 00:22:43.650 William Cheng: Okay, so this is the purpose of our call okay so that I can say days of work instead of just, just get killed by the operating system. Yeah. 170 00:22:45.000 --> 00:22:53.250 William Cheng: So the terminology that we use in the the Unix and Linux world. And this is the dysfunction that you provide over here. This is called a signal handler. 171 00:22:53.700 --> 00:23:03.720 William Cheng: Okay, so in a way of weapons that inside our penises them they notice that something bad has happened and then what it will do is that it will send a signal to you when you send a signal to you you execute the signal handler. 172 00:23:05.040 --> 00:23:14.880 William Cheng: Okay, so, so, so, what other kind of signal to do you have to do you have to handle right. For example, if you run your program and the user press Control. See, when the user spread control. See, it will kill your program. 173 00:23:15.660 --> 00:23:24.240 William Cheng: Okay, so you can actually tell you the app, the opportunity to say hey you know when the user prep control. See, don't kill my program instead call this function. And then, you know, 174 00:23:25.290 --> 00:23:27.900 William Cheng: And then let me let me go back to what I was doing before. 175 00:23:28.440 --> 00:23:37.650 William Cheng: Okay, so in that case again something so so typically will happen is that is that for the upgrade. Is that something happened inside the kernel. And then if you have set up a signal handler for 176 00:23:37.830 --> 00:23:43.830 William Cheng: What he will do is that I will make an appt call and then call your function and then go buy a new kernel, as if nothing has happened. 177 00:23:44.760 --> 00:23:55.350 William Cheng: OK. So again, when the user press Control, see who's going to know that the user has press Control C, Control C is generated by the hardware. So therefore, when you press Control. See, you're going to get a hardware interrupt. 178 00:23:56.430 --> 00:24:00.690 William Cheng: Okay, so in this case how interrupt you going to go into the opportunities, then you're going to execute operating system call 179 00:24:00.840 --> 00:24:04.350 William Cheng: And the opposite says oh control. See, it has been pressed. I'm going to kill your program. 180 00:24:04.500 --> 00:24:16.620 William Cheng: But wait a second, you have a signal handler setup so therefore I will not, you know, kill your program, but instead I will make an appt call so you can do whatever you need to do. And then I will return back in the system as a control C has not been pressed 181 00:24:17.070 --> 00:24:20.940 William Cheng: Okay, so this way when people press Control. See, they will not be able to kill your program. 182 00:24:22.140 --> 00:24:35.970 William Cheng: OK. So again, I'm going to sort of talk more about you know consciously handling and how to implement up call in chapter five. Okay. So, so far so right now we just want to introduce you to the concept of signal handling and an upcoming yeah 183 00:24:38.250 --> 00:24:52.590 William Cheng: Alright, so that's sort of the high level overview of the operating system structure. The next thing we're going to look at is that we're going to start looking at processes. Guys, we look at prophecy, you know, address space and thread all these kinds of stuff. Alright. 184 00:24:53.610 --> 00:25:03.180 William Cheng: So the first thing we'll talk about is how to run a program. Right. So again, the problem. What is the programs or programs when you compile your code like warm up one when you finish 1111 more plan is a file sitting on the desk. 185 00:25:03.720 --> 00:25:07.320 William Cheng: Okay, so if you think about a desk over here on the days over here. There's one by one. 186 00:25:07.590 --> 00:25:18.210 William Cheng: In order for us to run this program, how can we run this program. Right. So again, the purpose of this class is to tell you is sort of show you how to run program, how to provide using contacts to run a program 187 00:25:18.570 --> 00:25:23.850 William Cheng: Okay, so in order for you to run this program. What do you need. OK. So number one, you need memory. Why do you need memory. 188 00:25:24.090 --> 00:25:34.980 William Cheng: Because you're pro, by the way, we sort of thing about a probe is that the program has an address space. Okay. And the abstraction for the address space is a process. So, therefore, you need a process, you need to have memory. And this way you can write your program. 189 00:25:35.310 --> 00:25:39.600 William Cheng: Right. So again, conceptually what you need to do is that here is the memory that you have right 190 00:25:40.170 --> 00:25:48.480 William Cheng: Here's your rap. So what you need to do is that you need to take your program and folded into memory and then start executing code over here. So, therefore, you are required to have memory. 191 00:25:48.930 --> 00:25:51.420 William Cheng: Okay, you also need to have the address space abstraction. 192 00:25:51.990 --> 00:26:00.780 William Cheng: To address space. Our addressable memory location that you're pulling can use and this weekend running around your program. You know where the global variables are the local variables are you have Hebei of all this kind of stuff. 193 00:26:01.560 --> 00:26:09.630 William Cheng: Yeah, all right. The second thing that you require in order for you to run a program is that you have to have CPUs, right, because if you don't have CPUs. Well, how can write your program. 194 00:26:10.350 --> 00:26:17.520 William Cheng: Okay, so when I say that you unfold your program into memory and then you transfer control to it, you are using a CPU to execute code. 195 00:26:18.030 --> 00:26:22.260 William Cheng: You know, for, for, for, for memory location over here who's part of your program. 196 00:26:22.650 --> 00:26:28.920 William Cheng: Okay, so therefore, in order for you to do that. You also have to add CPUs, you have to have processes. So you have to have process source. 197 00:26:29.160 --> 00:26:35.670 William Cheng: The processor cores are there abstraction or threads. So therefore, in order to run your program you need also threats. 198 00:26:36.120 --> 00:26:42.120 William Cheng: Okay, you'd have a single threaded programming also get a multi threaded program in chapter two, we're going to talk about multi threaded program. 199 00:26:42.420 --> 00:26:45.510 William Cheng: Okay, in chapter one, we basically want to sort of stick to a single server program. 200 00:26:46.080 --> 00:26:54.960 William Cheng: Then the third thing that you need to do is that you need to have an execution context. Again, the job of the operating system is to provide the context for you to execute your program. 201 00:26:55.470 --> 00:27:01.230 William Cheng: Okay. So this guy is what is your execution context, right. So, because she has contracts is that we mentioned before. 202 00:27:01.620 --> 00:27:12.690 William Cheng: You know, so what you're required to do is that, you know, you're going to use the CPU to execute your code. But, you know, the CPU. If you only have one CPU you're juggling many, many threads using one CPU. 203 00:27:13.200 --> 00:27:16.470 William Cheng: Okay, so in that case what we need to do is that sometimes we need to suspend your thread. 204 00:27:16.710 --> 00:27:27.390 William Cheng: I saving the execution contacts. Okay. And then we're going to go execute other thread and eventually when we come back to you. We're going to restore the existing contacts, put them back inside the CPU, so you can continue to execute your program. 205 00:27:28.290 --> 00:27:38.970 William Cheng: That. So in this case, what is the execution contacts. OK. So again, the extreme, kind of, you can sort of think about is that is that you know it's whatever that he will then it will take for you to go back to what you were doing before. 206 00:27:39.660 --> 00:27:48.420 William Cheng: Okay, so what does that take right so that takes that you know the CPU needs to go back to exactly the same status before because what I suspend your program your problem with doing something 207 00:27:48.810 --> 00:27:57.030 William Cheng: Okay, your thread was actually using using the using the CPU to do something for you. So in that case, I need to save the entire state of the CPU into a data structure somewhere. 208 00:27:57.870 --> 00:28:08.070 William Cheng: Okay, so later on when I need to resume your execution. I need to copy all that information back into the CPU. Well, what about your address space while you assume that you address space is still exactly the same way as it was before. 209 00:28:09.180 --> 00:28:21.150 William Cheng: Okay, so therefore you know you're choosing contracts is what inside the CPU what's inside your address space. What if you have open the file. If you have open the file. You also need to make sure that the father still open. So all these things are your execution contacts. 210 00:28:22.560 --> 00:28:31.410 William Cheng: Alright, so, so, so it's everything that you need in order for you to continue execute your program will continue for you to pick up where you left off. And those are your execution contacts. 211 00:28:31.890 --> 00:28:42.780 William Cheng: OK. So again, it's the operating system job to save the execution context I later on when you need it. We need to run your program, we're going to restore the existing contacts so you can continue where you were before, as if nothing has happened. 212 00:28:44.220 --> 00:28:51.720 William Cheng: Alright, so again the accident contacts over here is a very, very complicated con concept over the course of the semester. We're going to sort of show you where you say the 213 00:28:52.170 --> 00:29:02.820 William Cheng: Contacts okay at different places because because they actually kinda. It's very, very complicated, right. It's Inclusive means that the CPU includes stuff inside the files that you have open include stuff inside the 214 00:29:03.630 --> 00:29:08.730 William Cheng: Inside your address space. Okay, all these information they might be stored at different places. 215 00:29:09.450 --> 00:29:18.930 William Cheng: Okay so so throughout the entire semester we'll talk about how to provide the execution attacks. Okay, so, so it's okay if you don't know exactly where they are right now because that's how the classes design. 216 00:29:19.440 --> 00:29:21.360 William Cheng: Because it's a complicated matter. Okay. 217 00:29:22.080 --> 00:29:33.870 William Cheng: All right, so there are multiple meanings of the word contacts right context sort of complicated word right we just use it in the in this way, we say the execution correct somewhere right and then we go execute other threads. 218 00:29:34.080 --> 00:29:37.290 William Cheng: And when we need to go back to the server going to restore the extrusion contacts. 219 00:29:37.530 --> 00:29:43.680 William Cheng: So again, the way we typically think about it is that you say the contact you say the CPU state into some data structure, instead of kernel. 220 00:29:43.860 --> 00:29:57.540 William Cheng: And then when you're done, you're going to restore or what you need to go back to you restore the data structure inside is our operating system and put it back inside of CPU. Okay. And that will be the exclusion contacts inside the CPU. Okay. There are other exclusion contacts, then 221 00:29:58.590 --> 00:30:04.380 William Cheng: The other place that we use contacts over here is that, again, your CPU can be either in the internal contacts you know threat contacts. 222 00:30:04.830 --> 00:30:10.380 William Cheng: So in this case, the word contacts also means something. Yeah. It's one of the, you know, sort of, sort of the state of the CPU. 223 00:30:10.800 --> 00:30:20.040 William Cheng: If your CPU is executing interrupt service routine, then it's in the interim contacts. Otherwise, it's going to be in a threat contacts. Okay. So later on. We're also going to learn that the CPU has other contacts. 224 00:30:20.460 --> 00:30:24.810 William Cheng: That they can't you belong to. Okay. But at this point, we only have two or three contacts. 225 00:30:30.000 --> 00:30:31.920 William Cheng: Right. So what kind of program, we're going to 226 00:30:33.090 --> 00:30:36.690 William Cheng: We're going to run. We're going to look at a very simple example from the textbook. 227 00:30:37.410 --> 00:30:48.360 William Cheng: This exam. This program compute the first 100 prime numbers. Okay, so again, the prime numbers are the ones that are divisible by one in the South. Right. So these are the. So one of the first one is a prime numbers. 228 00:30:48.930 --> 00:30:58.380 William Cheng: Is going to be, you know, 2357 1113 a ball by, you know, you got to do 100 of them. So this program complete 100 primes over here. So we're going to use a global variable. 229 00:30:58.980 --> 00:31:09.030 William Cheng: Is going to constant integer global variable number of prime is equal to 100 ok and then we're going to use a rate data structure over here to store the results. Okay, so we finished running this program. 230 00:31:09.600 --> 00:31:24.750 William Cheng: Here's prime is an array. This go from zero to 99 right so you know PRIME OF RE index zero is going to contain the first prime or a index number one is gonna conduct the second prime, blah blah blah, all the way to arrange that number 99 that will contain the 100 number 231 00:31:25.890 --> 00:31:32.850 William Cheng: So this code over here. I'm not going to go over this coat. Okay, so this code over here is going to iterate through and generate 101 hundred prime numbers. 232 00:31:33.090 --> 00:31:42.420 William Cheng: Now I'm going to use this you know the code over here to explain the color code for all my lecture slides that so whenever I SHOW. SHOW YOU A piece of C code. 233 00:31:43.020 --> 00:31:52.080 William Cheng: That then I'll be using the following color code. Yeah, the reserve. See the reserve see words in blue, right. So, for example, cost and 234 00:31:53.550 --> 00:31:59.550 William Cheng: Go to, or can use go to this class right return all these things are see reserved keywords, they 235 00:32:00.810 --> 00:32:01.590 William Cheng: Will be in blue. 236 00:32:02.790 --> 00:32:09.540 William Cheng: The numerical and string constant, they will be in, read, read. Number 100 you know 201 or these kinds of they will 237 00:32:09.960 --> 00:32:18.570 William Cheng: Be zero. If I have a stream. Right. Hello, or something like that, right, they will be in red, you've ever single character Black Flag a backside zero over here. They will also be in read 238 00:32:20.040 --> 00:32:30.810 William Cheng: The comments are in green. This code has no comments right so otherwise they're gonna be in this green color and everything else are black, rest of black are the symbols. They are the variables, you know, these kind of stuff, they will be black. 239 00:32:32.010 --> 00:32:37.530 William Cheng: Okay, so, so this is going to be my my color code for all my program that are posted on the lecture slides. Yeah. 240 00:32:37.980 --> 00:32:46.140 William Cheng: Alright, so this is just one program. So we can't really talk about one program at a time. So the operating systems job is to execute any program. Right. So how do you represent any program. 241 00:32:47.340 --> 00:32:53.520 William Cheng: So that's a little bit tricky. So this is what a go to a model that represent any program. So you probably heard of. It's called a Turing machine. 242 00:32:54.240 --> 00:33:01.590 William Cheng: That a Turing machine is a model for computation. So therefore, they can be used to represent any program that you will have ever run 243 00:33:02.040 --> 00:33:10.470 William Cheng: Okay, so everybody. Anybody here ever run can be represented using a Turing machine model. Okay. So, tomorrow is very, very simple abstraction over your program does. 244 00:33:11.190 --> 00:33:16.290 William Cheng: So, so the picture show on the right over here. Okay, there is a machine sitting over here, right. That's the machine. 245 00:33:16.740 --> 00:33:22.860 William Cheng: That so so so that machine. What's important to understand about that machine is that that machine is a finite state machine. 246 00:33:23.460 --> 00:33:34.980 William Cheng: Okay. This machine has finite number of states. Okay, so if you think about our Intel CPU or Intel CPU. Does it have finite number of states or does it have infinite number of states. Well, the answer is why it has find a number of state. 247 00:33:35.430 --> 00:33:44.640 William Cheng: Because it doesn't have infinite number of bits in them out to represent infinite number of states. Okay. So, therefore, again, you should think about this, you know, this machine right here, over here. It's going to be a CPU. 248 00:33:44.970 --> 00:33:55.500 William Cheng: Yeah, what about this thing over here. So this is called the touring tape get what's important to understand about a touring tape. It's infinitely long, it goes to infinity. This way also going to be this way. 249 00:33:56.220 --> 00:34:01.320 William Cheng: Okay, so on the touring tape. There's a bunch of symbols there. So since its implemented along it has infinite number of 250 00:34:01.590 --> 00:34:08.820 William Cheng: employee number of symbols. Okay, so one of the symbols over here is going to be under right under the machine, the machine over here is look right onto the tape. 251 00:34:09.120 --> 00:34:23.610 William Cheng: That. So in every time step, what the machine can do is to read the symbol under the under the machine over here really inside the machine you can you can also write a civil out on the table. We're here to change what's under your sister, sister. 252 00:34:25.110 --> 00:34:38.280 William Cheng: To change the symbol, you know, under the machine now so so you can read it, you can write reason right to it. You can tell the 20th to go left one symbol or go, go to the tape or right by one simple 253 00:34:39.450 --> 00:34:46.170 William Cheng: Okay, so these are the only and also one more thing you can do that inside the machine over here you can execute earth radical a logical operation relic al you 254 00:34:47.190 --> 00:34:55.350 William Cheng: Okay, so this particular simple model is capable or representing every single program, they have ever written in your life. 255 00:34:56.880 --> 00:35:06.510 William Cheng: Okay, so when we sort of think about you know how, you know, the kind of program that you need to run while, then you can sort of he sort of represent that you know that that program, you need to run using a Turing machine. 256 00:35:07.200 --> 00:35:14.040 William Cheng: Right, right. Okay, I just want to summarize the two important aspect of Turing machine, the machine over here is finite state. 257 00:35:14.310 --> 00:35:29.640 William Cheng: Okay, and then the table over here, as is infinitely long and they are the symbols in every time step. You can read the symbol or right to the symbol or tell the tell the typical left one step or to the right ones that ok so those are already performing internal operation entire machine. 258 00:35:31.140 --> 00:35:36.810 William Cheng: Alright, so, so, so this will be the program that we need to be running in this class. Okay. 259 00:35:37.560 --> 00:35:50.370 William Cheng: All right, so we can sort of connect this with our address space representation, our address space has how many memory location. What has to to the 32 locations right go from zero from zero all the way to zero, x F, F, F, F, F, F, F. 260 00:35:50.880 --> 00:36:01.710 William Cheng: Okay, so our address space is part of the touring tape. Okay, so we're going to turn tape is infinitely long right so we hear or address space is a tiny fraction of our touring tape. 261 00:36:02.640 --> 00:36:06.750 William Cheng: Right. So we also see that this picture over here, we actually we're going to draw one more memory segment. 262 00:36:07.380 --> 00:36:14.730 William Cheng: Okay, so, so, so, so again the texts are going to still where it was before the data segment over here is going to get broken into two parts there. 263 00:36:14.940 --> 00:36:25.080 William Cheng: One of them is going to be the initial get the data segment is what you store, you know, global variables. There are two kinds of global variables. One of them are initialized global variable and the other ones are 264 00:36:25.500 --> 00:36:35.850 William Cheng: Our initial as global variables. Now, so the initialize global variable, they still stay inside the data segment the initialize global variable. They go into this other segment know as BSS 265 00:36:36.600 --> 00:36:41.310 William Cheng: Yeah so BSS is a ruler in the old term is stands for block started by symbols. 266 00:36:41.970 --> 00:36:48.270 William Cheng: Okay, so these are just a bunch of symbols. They don't have initial value. So therefore, there are call you know block started by symbols. 267 00:36:48.510 --> 00:36:55.740 William Cheng: Yeah, all right. And then we still have a dynamic region and we have the stack space over here and again a dynamic region grow downward and a stack, you know, grow up work. 268 00:36:56.220 --> 00:36:59.310 William Cheng: Okay. Your entire address space as part of your to orientate 269 00:36:59.970 --> 00:37:13.590 William Cheng: Okay, I mean so clearly that's really not enough to represent the touring tape right so your program can actually can execute code beyond the touring tape so so what we're going to do is that we're going to call it the rest of the touring tape extended address space. 270 00:37:15.000 --> 00:37:23.190 William Cheng: Alright, so, so the way we sort of think about is that the entire touring tape part of it is your address space. The rest of the touring tape is known as your extended address space. 271 00:37:24.420 --> 00:37:26.580 William Cheng: Okay, so where is your extended address space. 272 00:37:27.720 --> 00:37:34.260 William Cheng: While I mean clearly it's not it's not inside your address space. So basically anything any part of the, you know, sort of, 273 00:37:34.650 --> 00:37:43.920 William Cheng: So later on we're going to talk about where is the touring tape. Okay, you know, because when you are running your program you'll pull up the access any memory location inside your address space. 274 00:37:45.570 --> 00:37:57.120 William Cheng: Okay, so how do you access any memory location is address space. All you need to do is you need to provide a virtual address the virtual address is going to be between zero and zero x F, F, F, F, F, F, F, F, and then this way you can access your address space. 275 00:37:57.510 --> 00:38:08.220 William Cheng: How do you go beyond the address space. Why not case you need to use something else. Okay, so we're going to talk about something else you know that you can use to reach the rest of your touring tape. Yeah. So right now we're going to 276 00:38:09.330 --> 00:38:15.180 William Cheng: Introduce you to this abstract term is that there's something called the extended address space that's outside your address space. 277 00:38:16.320 --> 00:38:19.410 William Cheng: Alright, so later I'm going to, sort of, you know, fill this gap and sort of tell you 278 00:38:20.580 --> 00:38:21.060 William Cheng: Where they are 279 00:38:22.770 --> 00:38:31.200 William Cheng: All right, so so inside your address is how do you use address space, right. So in order for you to use address space. The way you use them is that you use variables. 280 00:38:32.250 --> 00:38:35.490 William Cheng: OK. So, again, in this class. We're going to use the term objects. 281 00:38:35.760 --> 00:38:46.080 William Cheng: To refer to any kind of variable that can be primitive variable that can be complex variable that can be data show to they can be pointers that can be Pointers. Pointers. Pointers, you know, once you get to a point where you can point it to anything. 282 00:38:46.950 --> 00:38:57.690 William Cheng: OK. So again, you know, the way you use your address space is that you start, you start giving your address space memory location, you're gonna start giving them names and these will be the names of your variables. 283 00:38:58.140 --> 00:39:09.420 William Cheng: Okay, so in this example over here. We're going to take our data segment over here. And then we're going to sort of build a bunch of you know that the data structure, instead of that. So for example, you try to 284 00:39:10.530 --> 00:39:10.920 William Cheng: Sort of 285 00:39:12.870 --> 00:39:17.310 William Cheng: Implement a function. So you write a program to keep track of your family tree. 286 00:39:18.030 --> 00:39:25.920 William Cheng: Okay. So in this case, you know, you have, you know, six kids. You want to keep track go up and they are you know Adam Betty Chad Deb's even Fred, A, B, C, D, E, F. 287 00:39:26.190 --> 00:39:31.470 William Cheng: Right. So what you can do, they can actually create six lower variable each one of them represent one of your children. 288 00:39:32.190 --> 00:39:38.850 William Cheng: I mean clearly does not very, very flexible because, you know, for every family every right different program. So that's really, really a bad way to go there. 289 00:39:39.570 --> 00:39:45.330 William Cheng: So, so in that case. Well, what would you do, right. So, one can that you can do that. You can actually build on the rate 290 00:39:45.960 --> 00:39:54.750 William Cheng: Okay, so for your program to be a little more general purpose you know Reiko sons you a record called daughters and you keep them in a separate data structure over here. So again, what's an array. 291 00:39:55.320 --> 00:39:56.280 William Cheng: Okay so array. 292 00:39:56.700 --> 00:40:08.100 William Cheng: So the way we access arrays that we need the base address have an array. Right. So over here, here are two arrays. Want to call Santa one. It's got daughters over here. So the sun's over here is going to point to the first memory location of the array. 293 00:40:08.310 --> 00:40:12.240 William Cheng: And then we use an array index to access different object insiders right 294 00:40:13.560 --> 00:40:22.260 William Cheng: Alright, so, so in this case the first memory location over here for this data structure is known as the base address and the array index will tell you which object we want to access 295 00:40:23.130 --> 00:40:25.860 William Cheng: Okay. So in this case, you know, you might have sort of a, you know, 296 00:40:26.580 --> 00:40:35.610 William Cheng: Clearly we're gonna have infinite array because you know our address space is finite rise up again we can really reach the our extended address space about using this mechanism. 297 00:40:36.180 --> 00:40:42.960 William Cheng: Okay, but are we can be pretty big guys also we have, you know, multiple reasons our data search over here. So, this way we can 298 00:40:43.410 --> 00:40:49.230 William Cheng: We'll be able to us, our address face that another way to do it is to use the dynamically 299 00:40:49.770 --> 00:40:56.700 William Cheng: Allocate object. Right. So in this case, what a call Malik, and we're going to call free to manage our data structure, instead of the dynamic region. 300 00:40:57.030 --> 00:41:04.320 William Cheng: So when you call Malak, again, we're going to ask the memory allocating memory alligator is the one that's that that's in charge of the dynamic region. 301 00:41:04.740 --> 00:41:15.960 William Cheng: In chapter three. We don't look at more detail of the memory alligator. Okay, I'm going to know exactly what it does. Yeah, so for now we just have a memory allocated that in charge of the dynamic region over here. So when you call Malik. 302 00:41:16.860 --> 00:41:23.550 William Cheng: Let's say we call Malik 400 okay what you're trying to do is that you try to allocate 400 bite from the dynamic region over here. 303 00:41:23.790 --> 00:41:29.940 William Cheng: Okay, and what he will return to you as an address, and you gotta remember the address inside the variable we here so wishes this variable go 304 00:41:30.330 --> 00:41:42.630 William Cheng: What is variable again can go inside the tech side. Sorry. Can I go into the text like when you will go into the data segment can go inside the BSS admin, you can go inside the stack space. It can also be another location inside a dynamic region. 305 00:41:43.320 --> 00:41:49.170 William Cheng: Okay, it doesn't really matter where it is you all you need to know is that you need a memory location over here to hold the return value. 306 00:41:49.440 --> 00:41:58.440 William Cheng: Okay, so this one is a point of variable because this variable, you're going to keep an address inside of it well. So for this one. Now you can actually, you know, keep track of all the objects inside the dynamic region. 307 00:41:59.730 --> 00:42:02.790 William Cheng: Alright, so again the message over here is that it doesn't really matter where you are, you know, 308 00:42:03.030 --> 00:42:10.650 William Cheng: When you try to keep track of variables over here, you can have a variable inside the data side we have inside BSS you can have it inside the heat region. 309 00:42:10.860 --> 00:42:16.380 William Cheng: Or you can also keep keep the inside of stack you aren't a programmer. You get to decide where you want to keep your objects. 310 00:42:17.100 --> 00:42:23.580 William Cheng: Okay, by using this, you know, virtual address you can access any memory location inside your address face. Yeah. Alright. 311 00:42:24.510 --> 00:42:35.910 William Cheng: So the dynamism. Any pointers over here. And then finally, you can actually you have objects and live inside the stacks stacks space again, more specifically, these are the objects that are live inside the stack. Right. 312 00:42:36.330 --> 00:42:41.910 William Cheng: Right. So in this example over here we have a main function. So let's say your main function is going to call up, you know, call a function of print 313 00:42:42.420 --> 00:42:48.390 William Cheng: To sort of build the family tree or the print the family tree. So in this case, the family tree will be a recursive function. 314 00:42:48.570 --> 00:42:55.290 William Cheng: So what you would do is that you will call the family to function with one of the the children as an argument. 315 00:42:55.470 --> 00:43:05.010 William Cheng: And now when you make a recursive call, why in this case that you know the argument over here will become a grandchildren. Right. And then you can make another recursive call is going to be the great great grandchildren, you know, etc. 316 00:43:05.700 --> 00:43:16.290 William Cheng: Okay, so in this case again every one of these variable, they will point to different the will appoint a different object. So that's why you need to start reading so you can hold a different version of the same variable. 317 00:43:16.890 --> 00:43:23.940 William Cheng: Yeah, well, so again, what's important for you to understand is that inside the stack rain lives the function argument and and 318 00:43:24.660 --> 00:43:30.390 William Cheng: The local variables. And when we get to Chapter three, we're going to see exactly what it looks like inside a stack rank. 319 00:43:31.170 --> 00:43:39.150 William Cheng: All right. So right now we just introduced you to the idea of the address space there are addressable memory location and it can be anywhere inside your address space. Okay, so this way you can 320 00:43:39.780 --> 00:43:49.710 William Cheng: You can actually have access to this part of your 20 if you want to go outside your outer space, you need to go to your extended address space, right, we're going to talk about that, probably in the next lecture that 321 00:43:52.290 --> 00:44:01.980 William Cheng: Alright, so our original program complete 100% that's very, very interesting, right, because if you, you know, if you write a program to compute the same thing every time when they all you have to do is run a once 322 00:44:02.430 --> 00:44:12.240 William Cheng: OK. So the purpose for you to write a different program. So the purpose, will you to write a program. Is that what you're conceptually every time you run your program. You need to be able to produce different results. 323 00:44:12.660 --> 00:44:17.310 William Cheng: Okay, so therefore, for example, our price Hogan, we should be able to compute any number of prime numbers. 324 00:44:17.580 --> 00:44:25.530 William Cheng: Okay. So in this case, what you will do is that a number of problems over here at the beginning, you don't know what it is. So we here. We're gonna make this global variable and initialize global variable. 325 00:44:25.920 --> 00:44:35.220 William Cheng: Okay. So since it's on initialize so we know that this variable actually live inside the BSS region. Right. So actually the end problems over here is I just sitting right here. This is enterprise. Okay. 326 00:44:35.760 --> 00:44:45.750 William Cheng: The problem is over here. We're going to be an integer pointer. So again, this one also point to the BSS region, there'll be another your data structure over here in this case it will be to just be a pointer. 327 00:44:46.230 --> 00:44:52.590 William Cheng: Okay. Where does the point to why in the beginning. Over here is on initialize or you might point the garbage. My point or something would only know yeah 328 00:44:53.400 --> 00:44:58.530 William Cheng: Right. Our program over here. The first thing that we need to do is that we need to run a program with command on arguments. 329 00:44:58.980 --> 00:45:04.560 William Cheng: So let's say our program over here is called the primes program. So in this case, it will run our program is primes 300 330 00:45:05.250 --> 00:45:09.150 William Cheng: Okay, to say that we want to compute the first 300 prompts, whereas in this case again. 331 00:45:09.360 --> 00:45:19.830 William Cheng: This one will be our via zero. This one will be rvo one so are the one will contain the string 300 and this one will be a string. So in the annual be terminated backup backslash zero 332 00:45:20.010 --> 00:45:29.190 William Cheng: So what you can do, they can call the function at a lie to compute a computer to to to convert a string into integer and you store the integer or we can into enterprise. 333 00:45:29.820 --> 00:45:39.510 William Cheng: Okay. So if we run our program. This way, when we finish executing this at ROI and then we assign them to the enterprise so empire is this memory location right here, it will contain the integer 300 334 00:45:40.830 --> 00:45:46.680 William Cheng: Okay, so next thing that we need to do is that we need to allocate memory location to hold the result. So if we need to compute 335 00:45:47.040 --> 00:45:52.830 William Cheng: The 300 primes. How many memory locations. Do we need whatever integer is four bytes law rise in this case we need 336 00:45:53.100 --> 00:46:03.870 William Cheng: You know, four bytes times 300 it's going to be 1200 bytes. So we're going to call it the malfunction over here and prices equal to 300 size of end is equal to four. So 300 times four is going to be 1200 337 00:46:04.050 --> 00:46:17.160 William Cheng: So we're going to allocate 112 hundred bytes over here instead of dynamic region and the return value over here is going to be stored inside prime. And again, prime is this memory location inside BSS so when this function returns 338 00:46:17.880 --> 00:46:28.860 William Cheng: The primes that you know the memory location that's known as prime is going to contain the address for, you know, for, for, for the memory that's been allocated. Okay, so, so how do you actually 339 00:46:29.910 --> 00:46:41.220 William Cheng: represent them were presented visually right so what you would do is that you will draw a pointer from the primes a variable we here to the memory location over here that you allocate it, I'll use the Malik. 340 00:46:42.300 --> 00:46:47.190 William Cheng: Okay, so, so I'm going to declutter this a little bit. I'm like, can I just see this so right over here. 341 00:46:47.550 --> 00:46:52.380 William Cheng: BSS over here. So prime over here is a variable inside PSS I'm going to draw it on the left hand. 342 00:46:52.680 --> 00:47:01.620 William Cheng: Side is that when you allocate 1200 bytes of memory over here. We're going to point to it using this pointer. So therefore, when we're done. It will look like this. It will point to the memory location. 343 00:47:02.160 --> 00:47:12.810 William Cheng: OK, so again it just not going to re he will point to the base location, which is the smallest memory location over here for that data structure and then from this point on, there's 1200 bytes that will be used to store the result. 344 00:47:13.320 --> 00:47:22.650 William Cheng: Okay, and then we start you know computing are prime numbers over here prime 0.1 all the way to prime of 299 and then we're going to fill up this data structure to compute 300 props. 345 00:47:24.300 --> 00:47:32.550 William Cheng: OK. So again, this program is a little more interesting because every time we run this program, you can use a different command line arguments. And this way you could compute different number of price. Right. 346 00:47:34.560 --> 00:47:41.550 William Cheng: Okay, so. So in this case, you know, we just, you know, we allocate memory. We never free them. So again, I mentioned that a couple of times in the class Google group. 347 00:47:41.850 --> 00:47:52.170 William Cheng: For warm up one and warm up to. We don't really care about you know you allocate some memory. It's okay. You don't read them. Okay. In the colonel assignment, then the room might change a little bit. Okay. 348 00:47:54.870 --> 00:48:07.350 William Cheng: All right, so that's sort of a quick introduction of these concepts. So now that we're going to sort of see how do you manage processes, we need to be able to create them when you be able to you know away for processes or die or these kind of stuff. 349 00:48:08.070 --> 00:48:11.070 William Cheng: So we'll talk about you know how to actually manage all these processes. 350 00:48:11.310 --> 00:48:19.830 William Cheng: Okay. All right. So, so we sort of briefly talk about the process abstraction, right, whenever you think about a process. There's an address space you address pieces are you're touring tape. 351 00:48:20.010 --> 00:48:24.810 William Cheng: And we also need to be able to reach the extended audio space, which we'll talk about in the next lecture, okay. 352 00:48:25.290 --> 00:48:36.810 William Cheng: All right, so with the obstruction. Over here we come with the other sources sort of the API's API sensible application program interface. So in this case, what do we have to do to manipulate processes. 353 00:48:38.160 --> 00:48:42.060 William Cheng: Okay, so how do you create a process. Right. You know, can a user program create its own 354 00:48:42.510 --> 00:48:52.020 William Cheng: will cook in a user program create its own process or the answer, of course, it's not the they cannot do that right in order for you to create a program, you have to ask operating system for help. 355 00:48:52.380 --> 00:48:59.580 William Cheng: Okay. So, therefore, what you have to do is that you have to make a system call ok so the collection or you have your system call is the 356 00:49:00.240 --> 00:49:02.160 William Cheng: Is the API for your operating system. 357 00:49:03.000 --> 00:49:06.900 William Cheng: Okay, in order for you to ask the optimism to do something for you. You're going to use the API. 358 00:49:07.080 --> 00:49:14.940 William Cheng: To make system call and then this way you restart businesses that are the system or do something for you and they will return back to you and then those operation will be done. 359 00:49:15.510 --> 00:49:25.560 William Cheng: Okay, so to manipulate processes. There are four API's. You have to eat that you have to learn. Okay, the first one we're going to look at the first one first. The first one is called the fork system call 360 00:49:26.610 --> 00:49:33.060 William Cheng: All right. And then there's something called exit. There's something called wait and something called exactly going to look at those. The other three system call in the next lecture. Right. 361 00:49:35.550 --> 00:49:42.840 William Cheng: All right, so how do you create a process. Okay, so, so in unison Linux to create a process is deceptively simple. 362 00:49:43.260 --> 00:49:53.310 William Cheng: Okay, so what you have to do is that you need to make a copy of a process. Okay, you start with the process you make a copy of this process and that will be the only way to create a process. 363 00:49:54.120 --> 00:49:58.740 William Cheng: Okay. Alright, so, so, so, so you start with the process and let's I'm going to call this process, the parent process. 364 00:49:59.250 --> 00:50:11.520 William Cheng: Because you got to sort of create a child process out of air as this one called apparent process. Okay, if you want to create a process. We're going to start with the process. We're going to make a copy of itself and then the copy is known as the child. 365 00:50:12.690 --> 00:50:15.690 William Cheng: Okay, and the system called that we're going to make is called the forks have some call 366 00:50:16.500 --> 00:50:23.490 William Cheng: Okay, so instead of parent process over here with the tech segment the text. I'm in as a top over here inside the tech stack men. There's a function called fork. 367 00:50:23.700 --> 00:50:36.240 William Cheng: Okay. For because this isn't call when you make the fork system call you know companies that have colonel colonel what he would do is that it will make a copy of the parent process to create a child process and the child process is identical to the parent process. 368 00:50:37.980 --> 00:50:46.560 William Cheng: Okay, so this is the goal. What is so what it's done what he would do is that it will return the process identifier, you know, for the process that you have created 369 00:50:47.700 --> 00:50:58.260 William Cheng: Right. Alright, so again the process we're fork is called, it's called a parent process and the the copy is known as the child process, the parent and a child, they're exactly the same. There's only one difference 370 00:50:59.700 --> 00:51:08.190 William Cheng: Okay, so. So in this case, what do I mean by exactly the same, right. So, so if you, you know, if the parent make a fork system call you come inside the opera houses that and we're going to create a child process. 371 00:51:08.460 --> 00:51:12.660 William Cheng: When at the time when the child processes created what is the trial process doing 372 00:51:14.130 --> 00:51:21.180 William Cheng: Okay, since the child process is exactly the copy of the parent, the child processes, also in the middle of the fork system call 373 00:51:22.560 --> 00:51:24.720 William Cheng: Okay. That's what I mean by an exact copy 374 00:51:26.100 --> 00:51:34.320 William Cheng: Okay, so therefore, again, the parent companies that are current over here making a system called when the child processes is created the child processes, also in the middle of the system call. So in this case, 375 00:51:34.530 --> 00:51:38.640 William Cheng: We're going to return back to the user space program in both the parent and the child. 376 00:51:39.720 --> 00:51:45.720 William Cheng: Okay, so this is kind of like we're system call you come into the kernel once, but now you need to return simultaneously. 377 00:51:45.990 --> 00:51:51.750 William Cheng: To the parent child process. Okay, how can you return simultaneous with apparently job was as well, in that case you need multiple CPUs. 378 00:51:52.080 --> 00:52:02.580 William Cheng: If you have multiple CPU. It is possible when you make a system call we make a force isn't going to the Colonel, you're going to simultaneously returned back into the user space program. So both of them will return from Fort 379 00:52:04.020 --> 00:52:09.240 William Cheng: Okay, so these two programs are identical. The only difference is in the return value of the fork system call 380 00:52:09.540 --> 00:52:17.460 William Cheng: Okay, with a fork system called return into the parent, the return value over here is going to be the process identifier for the trial process. Okay. 381 00:52:18.240 --> 00:52:25.590 William Cheng: When you return on the fourth system called if it turns out you're returning inside the child the return value for the fork of what the forces of course is going to be equal to zero. 382 00:52:26.820 --> 00:52:34.800 William Cheng: Okay so well. You can do that. You can use a return value of the system call to determine whether you're executing the code as a parent or you actually do the code as the child. 383 00:52:35.220 --> 00:52:41.430 William Cheng: Okay, so if you reject the return value of Arabic F is equal to zero, that means that you're executing inside the child process. 384 00:52:41.610 --> 00:52:49.380 William Cheng: If the return value over here is not equal to zero, that means that the other is that the return value over here is going to be the process identifier for the trial process. 385 00:52:50.310 --> 00:53:02.010 William Cheng: Yeah. What is it, what is the process identifier, a process identifier is simply a 16 bit number. Okay, that that is a number that represent you know that the rep represent the process. 386 00:53:02.580 --> 00:53:13.560 William Cheng: OK. So again, if the return value over here, go to zero. You're executing as a trial process. If the return value is not equal to zero then the return value is going to be the identifier for the trial process. And you are the parent 387 00:53:14.550 --> 00:53:18.780 William Cheng: Okay, so in this way the parent know who the child is a child will have no idea who the parent is 388 00:53:20.340 --> 00:53:26.670 William Cheng: Okay, it's kind of like the human society right with them, you know, when they wouldn't they wouldn't know when the child was born the child knows he knows nothing. 389 00:53:27.300 --> 00:53:33.330 William Cheng: You may only have the DNA from the parent. So he doesn't really know who the parent is but the parent knows, you know, here's my child. Okay. 390 00:53:33.750 --> 00:53:37.770 William Cheng: Alright, so, so this kind of analogous to, you know, to, to the human side. All right. 391 00:53:38.460 --> 00:53:49.050 William Cheng: This is the only way to create a process. Okay, so. So again, this is the only way to create a user space process. So if you're a user space program. If you want to create another process. You make the forks as the call. 392 00:53:49.380 --> 00:53:59.970 William Cheng: Okay. And they also need to handle the return value. So you know that whether you are the parent or you the child right there. This is a weird system call you come inside the colonel once but you return twice. What if you only have one CPU. 393 00:54:00.660 --> 00:54:06.900 William Cheng: You available only one CPU. The one of them is going to return first and the other ones that are returned. Second, right, because there's no way for you to return simultaneously. 394 00:54:08.070 --> 00:54:16.530 William Cheng: OK. So again, you always need to think about a case where what if only have one CPU want to have multiple CPU. In that case, you know, we have multiple CPU. It is possible things might run in parallel. 395 00:54:16.890 --> 00:54:22.530 William Cheng: Okay, when you only one CPU. Then one of them is going to happen first and then the other one is gonna happen at a later time that 396 00:54:24.480 --> 00:54:33.540 William Cheng: All right. Ah, so, so, so, so when do you actually work off entire process and I will be here says the example is that you know if your, your command shell. Right. So when you fire up your 397 00:54:34.080 --> 00:54:42.450 William Cheng: Whole bunch of Linux terminal program you're going to type commands typically UNIX is that, you know, the command that you type is going to be the name of the program. 398 00:54:43.110 --> 00:54:53.460 William Cheng: Okay, so in that case what we need to do is we're going to have the permission to make a copy of the cell, using the system call and now your child processes also going to be your login show or conventional 399 00:54:53.820 --> 00:55:01.470 William Cheng: Okay. So inside the child process we're going to run a different program so so so if your terminal you type slash slash LS 400 00:55:02.520 --> 00:55:12.360 William Cheng: Okay so happens that your terminal over here is going to fall off a child process in the child process, it will run the program has been such a LM while the parent program continue to be the terminal program. 401 00:55:13.230 --> 00:55:15.030 William Cheng: Okay, so that's how you run a program 402 00:55:15.990 --> 00:55:21.390 William Cheng: Okay. All right, so, so, so many other system was gonna get involved. At this point, we all know about a fork system call 403 00:55:21.600 --> 00:55:30.300 William Cheng: And later on we're going to see sort of see what kind of system called, you have to make in order for you to actually run, you know, to, to, to run a program. Okay, again, that will be in the next lecture. 404 00:55:32.340 --> 00:55:42.270 William Cheng: Alright so pictorial it we see now again every time we think about a process we draw the address space. So here's the parent process. Here is the address space, right. So the fork system called over here it's inside the tech side, man. 405 00:55:42.480 --> 00:55:53.910 William Cheng: And when we're running the program over here again. So we're running the function over here. Yeah, IP is going to point to the fourth function and USP is going to point right here and the top step where I'm over here corresponding the for function. 406 00:55:54.450 --> 00:56:00.540 William Cheng: So we just saw the fork API over here. He has no function argument. As it turns out, he also has no local variable. 407 00:56:00.810 --> 00:56:06.030 William Cheng: So in this case, it's you know, it's a very, very simple stack RAM is no function argument is no local variable. 408 00:56:06.390 --> 00:56:14.460 William Cheng: So in this case, what it will do is that he SP is gonna part right here EDP is going to point to the middle of the stack frame over here and then boom, when you make a system called you go inside the cardinal 409 00:56:15.270 --> 00:56:24.480 William Cheng: Okay, well you get inside the Colonel. The Colonel will do is that it will create a child process for you. And then he will also copy the entire address space because the child needs to be a copy of the parent. Yeah. 410 00:56:25.320 --> 00:56:33.450 William Cheng: All right, so look like this right on the right hand side over here. The Carnival create a child process over here and they look exactly the same as the parent process. 411 00:56:33.930 --> 00:56:39.240 William Cheng: OK, so the child is also in the middle of the fork is inside the kernel. And now you need to return simultaneously. 412 00:56:39.450 --> 00:56:45.090 William Cheng: Into the parent of the child. So when the parent return over here it's going to return the process identifier for the child process. 413 00:56:45.300 --> 00:56:52.260 William Cheng: Here the child process. The process identify equal to pee. So therefore, in this case for the parent when it returns from fork. He will get the value of p 414 00:56:52.890 --> 00:57:04.980 William Cheng: OK for the child processes. When we return is gonna, it's going to return a value of zero. So this way if you check the return value, you know that you have the child process. So from this point on, you can actually execute different code inside the parent inside the child. 415 00:57:06.210 --> 00:57:13.230 William Cheng: Alright, so that's why you want to look at, you look at the return value because if you do exactly the same thing. Then why do you. Why do you bother to create a child process. 416 00:57:13.530 --> 00:57:21.330 William Cheng: Okay, so we create a trial process. The goal is to execute a different piece of code. So they have what you will do is that you will look at the return value of fork and then execute different piece of code. 417 00:57:22.080 --> 00:57:31.680 William Cheng: Right. Okay. So we're going to talk about a little later. So at this point, we're sort of going to introduce you. What kind of data structure inside of Colonel, do you use to represent a process. Okay. 418 00:57:33.120 --> 00:57:41.760 William Cheng: All right, the data structure that we use inside, Colonel. So this is a colonel data structure. Okay. It's called a process control block or a PCB right PCB over here. 419 00:57:42.420 --> 00:57:53.070 William Cheng: So I guess all kinds of opportunities and textbook, they use the word PCB to represent a process. Okay, so process control blah is a very, very big data structure. It contains a lot of stuff about your 420 00:57:54.570 --> 00:57:57.870 William Cheng: About your process. So again, a process is a running program. 421 00:57:58.770 --> 00:58:09.210 William Cheng: Okay, we saw our program, you know, was original the parent process over here. And then for call of a child process, the child processes identical to the parent. So we have two processes running exactly the same program. 422 00:58:09.720 --> 00:58:12.810 William Cheng: Okay, and then we should need need to look at the return code to do something different. Okay. 423 00:58:13.110 --> 00:58:21.840 William Cheng: Alright, so the process control blah what's inside the process now know, blah, what number what they need to have a process identifier. So this way, every process will get a different process identifier. 424 00:58:22.320 --> 00:58:33.870 William Cheng: Okay, so part of the over here is the process identifier that the next thing over here is the return code. I don't know if you remember the problems program at the end. The prize program in return zero. 425 00:58:34.590 --> 00:58:44.580 William Cheng: Okay so return value of your program is going to be in your main function. The function in the end he is he supposed to return something in return something that will become the return cup. 426 00:58:45.660 --> 00:58:55.230 William Cheng: Okay, so we have programs about to die. It's gonna be tough on the negotiating the return value that we copy it over here inside the process control block. So in this case, why do you copy that entire process control bar. 427 00:58:55.650 --> 00:59:02.940 William Cheng: Well maybe another program are interested in what is the return code for your program. Right. So this way, another Pro, I can find out how your program died. 428 00:59:03.930 --> 00:59:13.410 William Cheng: Okay. So in a way, this is how process communicate with each other. The only thing they can do about another process is to find out how a diet right did it die because the reason number one reason number two, number three. 429 00:59:13.920 --> 00:59:24.060 William Cheng: This number. As it turns out, it's only a bit value. Okay, so for program to dive. There's 260 so there's two to the right, equal to 256 256 different ways to die. 430 00:59:25.110 --> 00:59:31.260 William Cheng: Okay, so that will be the only information you could communicate from one project to the other. When your process dies. Right. 431 00:59:32.550 --> 00:59:38.670 William Cheng: And then there's the next few years. Okay, this is a really, really big data structure. We're going to sort of talk about the first three fields over here. 432 00:59:39.120 --> 00:59:43.080 William Cheng: The next one over here is called link. So what is link right so link is the next pointer. 433 00:59:43.440 --> 00:59:56.070 William Cheng: In warm up. Number one over here in my for two lists. Okay, we actually, we saw link my full to list element right so my photo this element. What does he have he has a next pointer as a previous point he has an object pointer over here. 434 00:59:56.760 --> 01:00:05.670 William Cheng: Yeah. So in this case, a link over here is, you know, depends on whether you're a single even plus or double even class. If it's a single event list, then he will point to the next object. 435 01:00:06.480 --> 01:00:13.470 William Cheng: Okay, if it's a double anything less than it has 2.01 point to the next one and the other one put up a pop up to the previous one. 436 01:00:14.190 --> 01:00:21.180 William Cheng: OK. So again, this link over here. It could be a single aliens or who plays over here. It doesn't really matter in the textbook. This simply called this a link 437 01:00:21.690 --> 01:00:28.350 William Cheng: OK. So again, the way you think about it is that it's gonna be a next pointer or next plus a previous pointer. Yeah. So this guy is what is the point to 438 01:00:29.460 --> 01:00:39.960 William Cheng: That. So let's say that you are standing in a line right you use your finger to actually point to the next person on, you know, the, the, the, the next person in line. So who's the next person. 439 01:00:41.280 --> 01:00:50.160 William Cheng: Well the next person depends on what kind of line you're in right so if you're in the bank line. Well, then the next person or the person in front of you is going to be the person that will be sir, first by the bank teller. 440 01:00:50.850 --> 01:01:03.630 William Cheng: Okay, if you're in a supermarket checkout what again you point to the next person. The next person will be the person in front of you and that's going to do the supermarket checkout before you did. Okay, so, so, so, you know, the link over here, it will point to the next person and that 441 01:01:14.370 --> 01:01:24.450 William Cheng: Then if there's a backward link over here appointed previous guy again, they will appointed a previous guy in your list your list could be a double ending circle. The list could be a single ladies can be all kinds of list. 442 01:01:24.990 --> 01:01:29.880 William Cheng: Okay, so this link over here just point to the next. So, so in this case it will be our next process. 443 01:01:30.240 --> 01:01:42.480 William Cheng: Okay, this point it will point to the next process control blog and you have a bunch of process control box. They're linked together to form a linguists okay and really depends on what you're waiting for. And that's why that that's how you get on that list. All right. 444 01:01:44.070 --> 01:01:50.280 William Cheng: Okay, in the textbook. They actually draw this picture. Okay, so one of the most important thing that I want to mention about this picture is that 445 01:01:50.790 --> 01:02:01.230 William Cheng: This is not an implementation for anything real okay I don't really know why the textbook uses example. This is a totally unrealistic, you know, example but the textbook says draw this picture. 446 01:02:01.890 --> 01:02:06.180 William Cheng: Okay. So in this case, what happened is that what they're showing is that, you know, maybe what you can do is that 447 01:02:06.570 --> 01:02:10.170 William Cheng: So apparent process will keep track of all the dead child processes. 448 01:02:10.560 --> 01:02:21.660 William Cheng: Okay, so what does the dead child processes, right, so remember what your child process returned from the main function and why we have one reach up on the main function, your process, nothing to do. So therefore your processes can die when you return from the main function. 449 01:02:22.470 --> 01:02:33.270 William Cheng: Okay, so we can design an operating system over here to use this data structure for a parent process to keep track of all the dead child processes and maybe there's another link layer is to keep up with all the child processes either are still running. 450 01:02:34.770 --> 01:02:39.750 William Cheng: OK. So again, this is just a design choice. It doesn't have to look like this way and also this picture. It doesn't 451 01:02:40.050 --> 01:02:45.090 William Cheng: Correspond to any real opportunity said it just that the way the textbook presenters is to have them looking like this. 452 01:02:45.420 --> 01:02:50.730 William Cheng: That. So in this case, what is that inside my process hundred blog, I have my process idea of the return code over here. 453 01:02:51.000 --> 01:02:58.980 William Cheng: I have the link field over here. I also have a list over here. Again, think about the list like my for two lists. Okay, so this is the terminated children less 454 01:02:59.640 --> 01:03:05.790 William Cheng: Guys over here. Here is a link list of all the child processes of this parent process that are all debt. 455 01:03:06.360 --> 01:03:12.150 William Cheng: Okay, so therefore, again, this will form a linked list. So inside every limitless over here. What is the link point to over here. 456 01:03:12.600 --> 01:03:16.140 William Cheng: What the link the link ponder over here will point to a dead sibling. 457 01:03:16.920 --> 01:03:25.890 William Cheng: Right, because all the processes over here. They're all the child process of the parent process. So, therefore, in this case, all these child processes. Their relationship is sibling relationship. 458 01:03:26.460 --> 01:03:36.330 William Cheng: Okay, so we're here every child processes that are dead. You have their process idea of the return code and the link pointer over here is going to point to the next shot or the previous child. There is also dead. 459 01:03:36.990 --> 01:03:46.260 William Cheng: Okay. And then over here, every one of the chapters that they can also have terminated children. Children list because it'd be because again, these processes they might have grandchildren. 460 01:03:46.860 --> 01:03:49.020 William Cheng: Okay, so they also form a list and you can keep track of that. 461 01:03:49.440 --> 01:03:58.890 William Cheng: Guy. So in this case, you're going to end up with the tree hierarchy where you have the ancestor keep track of descendants, and the descendant keep track of other descendants. So in the end, you can end up with the tree data structure. 462 01:04:00.060 --> 01:04:08.640 William Cheng: Okay, so again this is just an example that will show how the colonel data storage. You can be linked together to form a linguist to form a tree or the form of the kind of data structure. 463 01:04:09.360 --> 01:04:12.510 William Cheng: Okay, there's no operating system actually look just like this. Okay. 464 01:04:13.260 --> 01:04:21.960 William Cheng: All right, so use your imagination. When you sort of think about, you know, how does, you know, to the processes, they're related to each other. Right. So again, the parent knows who the child processes are 465 01:04:22.260 --> 01:04:29.310 William Cheng: Okay, and the child process. You know, they may or may not know who the parent and the parent is if you want the the child processes know what the parent is what do you have to do. 466 01:04:29.700 --> 01:04:34.590 William Cheng: Well maybe instead of process control blog over here you have a pointer point to the parents process control, blah. 467 01:04:35.460 --> 01:04:41.520 William Cheng: Okay. So in this case, every child processes can only have one parent, or in this case it will be a single pointer that can point to the parent who has it again. 468 01:04:41.790 --> 01:04:53.790 William Cheng: You know, let your imagination go wild to see how you can actually arrange all these data structure. Yeah. All right, so I'm done with lecture to. So next I'm going to continue from here. Continue with chapter one and we're going to look at other you know 469 01:04:55.260 --> 01:04:58.740 William Cheng: The system call for manipulating your processes. Okay.