WEBVTT 1 00:00:02.520 --> 00:00:12.660 William Cheng: Welcome to lecture three warm up. One is, do Tuesday next week. If you have co from previous semester, please don't look at them. Don't copy them best to get rid of it. 2 00:00:13.290 --> 00:00:22.140 William Cheng: You should have finish part A of the grading guideline by now. And you should be implementing Part B, and as I called part one and two over here. 3 00:00:22.680 --> 00:00:30.660 William Cheng: If you're struggling still with Part A implementing my for to list. I see you should talk to me, send me email posted class Google group. 4 00:00:31.200 --> 00:00:36.300 William Cheng: Do this, as soon as possible, because otherwise you won't be able to finish you know that the second part. 5 00:00:37.020 --> 00:00:44.160 William Cheng: Another reminder that the grading gamma is the only way we can grade and we can only great on the greatest 32 bit of a bunch of 16.04 system. 6 00:00:44.880 --> 00:00:51.870 William Cheng: So again, it's best not to install additional software packages. I just follow the installation instructions. Make sure you install everything 7 00:00:52.170 --> 00:00:57.240 William Cheng: That the installation instructions tell you and don't install anything else. Right. So at least keep keep a clean 8 00:00:57.720 --> 00:01:10.080 William Cheng: Version of a 32 to 60.04 for testing your code. Yeah. And again, it's very important to run against the grading guidelines so that this way, you know exactly how the greater will. Great. Okay. If you make a submission 9 00:01:10.590 --> 00:01:17.850 William Cheng: Read it understand the ticket in the email keep a copy of it. And also, you make sure you should make sure that you follow the verify your submission procedure. 10 00:01:18.240 --> 00:01:28.110 William Cheng: And you know, I guess part of the step over there is that you have to check your, you know, Readme file and things like that. So make sure that everything is perfect. You don't want to lose any silly points. Okay. 11 00:01:31.980 --> 00:01:39.450 William Cheng: Alright, so please understand that we're going at full speed. You know, right now I can. I can't really slow down because we need to sort of cover so much stuff. 12 00:01:39.930 --> 00:01:48.150 William Cheng: So you need to understand the lecture. And then you also need to take the initiative if there's anything that you don't understand. You should contact me right away. Okay, don't wait too long. 13 00:01:48.540 --> 00:01:52.860 William Cheng: Your programming assignments should be doable. If they are not, again, you should ask me questions. Okay. 14 00:01:53.340 --> 00:01:58.680 William Cheng: You should be using GDP or to debug warm up one because the debugger is a very, very important tool. 15 00:01:58.920 --> 00:02:05.790 William Cheng: And if you don't know how to use GDP. The Colonel assignment will be pretty much impossible for you to debug okay if you think that the bugs are bad now. 16 00:02:06.270 --> 00:02:10.710 William Cheng: Wait until you see the you know the the warm up to assignment and also the colonel assignment. 17 00:02:11.640 --> 00:02:19.440 William Cheng: Alright, if you're done with Part A warm up one you should do GDP assignment number one. I've been posted in the class Google group asking you to take a look at it. 18 00:02:20.100 --> 00:02:27.840 William Cheng: So what's important. Over here is that you need to draw a picture on a piece of paper. Okay, so you need to understand when I draw a picture of a data structure. 19 00:02:28.410 --> 00:02:34.560 William Cheng: You know, what does it actually look like under GDP. Okay. So, therefore, you know, says, always, always use that you should correspond 20 00:02:34.860 --> 00:02:44.340 William Cheng: What you see in GDP with a picture right so this way when you see picture you know exactly what it means. And also we examine memory location you know exactly what it looks like in a picture. Okay. 21 00:02:44.790 --> 00:02:53.040 William Cheng: Alright, so the first thing that you need to do is to change num items into test to be equal to three, because otherwise you end up you're going to draw a really, really big picture. 22 00:02:53.670 --> 00:02:58.590 William Cheng: So, change it into three and now would you need to do that, you need to convince yourself what a 23 00:02:58.950 --> 00:03:07.830 William Cheng: web link circular this look like by looking at memory addresses. Okay, so go through this step over here and at the bottom over here and sort of try, try, they will try to sort of 24 00:03:08.580 --> 00:03:13.530 William Cheng: Print out these data structure, make sure that you label every part of your picture very, very carefully. 25 00:03:13.890 --> 00:03:19.320 William Cheng: So in the warm up my spec is show you what a circular list for a for a list well for items will guy. 26 00:03:19.590 --> 00:03:31.770 William Cheng: Now you change it into a three. So it's going to be a little shorter, but you should be able to label everything very, very carefully and look at memory addresses. Again, think about what those memory addresses are, you know, the small memory addresses global variable. 27 00:03:32.040 --> 00:03:35.790 William Cheng: And then there is the heat bearable, they have a little larger memory addresses. 28 00:03:36.120 --> 00:03:42.150 William Cheng: If your memory addresses that are inside the stack, they tend to have very, very big numbers. Okay. So keep those in mind when you're doing the picture. 29 00:03:42.420 --> 00:03:49.080 William Cheng: And and trying to make sure that you understand what picture is telling you hey, do you have any questions about this, feel free to send me email. Yeah. 30 00:03:50.820 --> 00:04:01.050 William Cheng: All right, we're gonna continue from last, last time we talked about the process control block I sort of show you a picture that doesn't really correspond to anything. It just sort of showing you that one possible way. 31 00:04:01.590 --> 00:04:07.080 William Cheng: Inside the Colonel, they will, you know, sort of make association between all these your process control blocks. Yeah. 32 00:04:07.530 --> 00:04:12.780 William Cheng: So again, this doesn't correspond to any real implementation, the textbook, just use it as an example. 33 00:04:13.140 --> 00:04:22.170 William Cheng: To talk about the stuff that we're going to talk about right now. Okay. Alright. So there were four processes API. Right. The first one is fork we seen fork already and something today's 34 00:04:22.410 --> 00:04:32.730 William Cheng: Live lectures and people also also asked me a question about it. That's great, right, because we need to make sure that we understand exactly what for does. And now we're going to talk about the other three you know system calls net 35 00:04:34.350 --> 00:04:40.830 William Cheng: Alright, the next one is called the exorcism called the exorcism cause very simple is a function that returns void. It doesn't return anything 36 00:04:41.070 --> 00:04:47.460 William Cheng: And then he returned this exit value. Right. So what happened is that when you make the exorcism call that means that your process itself terminating 37 00:04:47.730 --> 00:04:52.710 William Cheng: And this you know this argument over here is going to be the access status of your program. 38 00:04:53.040 --> 00:05:00.930 William Cheng: Okay, the access that is also known as a return call. So when you call exit. Basically, this number of here, so less than 40. For example, you call exit five 39 00:05:01.350 --> 00:05:08.730 William Cheng: Okay, so what the operating system will do is that they will take this number five, and then put it into the process control back over here. So the return code over here. We codify 40 00:05:10.080 --> 00:05:16.080 William Cheng: Okay, so when you're when you're program called the exorcism call it's the same thing as returned from the main function. 41 00:05:16.560 --> 00:05:20.010 William Cheng: Well with with that particular with a particular return value. 42 00:05:20.610 --> 00:05:30.390 William Cheng: Okay, so the idea of yours. I will you make an exorcism call you're going to attract into the operating system. And in this case, when you tap into the operating system. You never returned back into the user space because your program is dead. 43 00:05:32.010 --> 00:05:40.350 William Cheng: Alright, so that's the idea of the system call right now again what happened over here is that you know the number five, what we're here to get copying to return, Mexico, but what about the data structure inside the Colonel. 44 00:05:40.650 --> 00:05:44.850 William Cheng: Guys, again, it depends on the ordinances them what the current data structure look like 45 00:05:45.180 --> 00:05:57.510 William Cheng: If your data is supposed to look like this. Now your process is dead. You need to manipulate your process control blogs, so that you need to move it from one place inside your current data structure into the terminated children list of your parents. 46 00:05:58.740 --> 00:06:07.440 William Cheng: Okay, so again if the data searches that are supposed to be maintained, like this. Then when you process that you need to move your process control block to the right place. Okay. 47 00:06:08.160 --> 00:06:14.070 William Cheng: All right. So anyway, so that's the, that's the oldest pictures. Tell me again. This is not a real implementation. Yeah. 48 00:06:15.960 --> 00:06:29.460 William Cheng: Alright, so, so, so when we, you know, I don't know if you remember the premise program. In the end, the prime score and says return zero. Right. So what does return zero do right so you have a main function when you return from the main function. What is the return to 49 00:06:30.510 --> 00:06:33.900 William Cheng: Okay, so you should see that you know there is a function that called the main function. 50 00:06:34.260 --> 00:06:40.920 William Cheng: Okay, that function we're going to refer to that function as the startup function. That's the startup, you know, that's the function that start your program up 51 00:06:41.730 --> 00:06:44.970 William Cheng: OK. So the code for the startup function simply look like this. 52 00:06:45.900 --> 00:06:51.270 William Cheng: Guy. So you start a function. I mean, you never have to write. There's not a function, because the startup function is the same for everybody. 53 00:06:51.420 --> 00:07:01.350 William Cheng: So, therefore, you don't have to write it. So the code for startup budget look like this. So, what it will do is that it will call the main function and take the return value for me the return value for me and then call exit on it. 54 00:07:02.850 --> 00:07:11.820 William Cheng: Okay, so therefore the only way that your program should die is that it has to call it has to call the exorcism call okay you can call it anywhere in your program. 55 00:07:12.210 --> 00:07:19.110 William Cheng: Okay, or you can return from the main function because when you reach up on the main function, the startup function will take the return value from May and collected on it. 56 00:07:20.130 --> 00:07:29.040 William Cheng: Okay. So in the end, there's only one way for your program to die and your program has to call the exorcism call, and will you make the exorcism call you go inside the kernel and you never come back into user space again. 57 00:07:30.060 --> 00:07:32.970 William Cheng: Okay, so that's the abstraction, where you make the access code. Yeah. 58 00:07:34.710 --> 00:07:42.630 William Cheng: Alright, so another third system called, it's called the way system called the way system called is used for the parent process to wait for a child process to die. 59 00:07:43.020 --> 00:07:49.410 William Cheng: OK, so the parent process called the forces and create a child process. And now you know the parent process and HR process will run in parallel. 60 00:07:49.860 --> 00:07:56.160 William Cheng: OK. So again, we don't distinguish between parallelism and concurrency. If you only one CPU. We're going to juggle between the two processes. 61 00:07:56.580 --> 00:08:00.750 William Cheng: But eventually, let's say that the parent has nothing to do but we want to wait for the trial process to do 62 00:08:01.260 --> 00:08:06.720 William Cheng: You don't want to wait for the child process to die. Well, in that case, what you will need to do that, you need to make the way system called 63 00:08:07.440 --> 00:08:15.390 William Cheng: Okay. The unit racism was a little weird, you know, if you want to wait for the child process today, you can really specify which helps us out. You want to wait for 64 00:08:16.050 --> 00:08:20.280 William Cheng: Okay, so when you make the racism call you say that I want to wait for any child process to die. 65 00:08:20.520 --> 00:08:28.440 William Cheng: Okay, so maybe your, your parent process create 100 child process. And now you need to wait for any of the trial process to that. So in that case, you call the way system call 66 00:08:28.860 --> 00:08:32.340 William Cheng: As it turns always just impose a little weird. It actually return to values. 67 00:08:32.850 --> 00:08:40.110 William Cheng: There. One is the return value of the function, right, because every, every function to return about you, the return value over here is going to tell you which process is dead. 68 00:08:40.650 --> 00:08:49.830 William Cheng: Okay, so again if you have 100 child processes that are running some of them might be dead. Some of them might be alive. You don't know. You don't know which one is that, so when this function returns, he would tell you which one is that 69 00:08:50.760 --> 00:08:57.000 William Cheng: Okay, if it turns out that none of the child processes that there are still running than this system called is called a blocking system call 70 00:08:57.450 --> 00:09:06.060 William Cheng: Okay, so the banking system called meaning that when you call this function, your parent process. In this case it will fall asleep inside the Colonel. Okay, until one of the child processes that 71 00:09:07.200 --> 00:09:14.880 William Cheng: OK. So again, there are two kinds of system call you know two kinds of any call one of them is a blogging system call is that you know your thread. We haven't really talked about a three year 72 00:09:15.060 --> 00:09:18.900 William Cheng: But your thread is making a function call this function will actually make you go to sleep. 73 00:09:19.590 --> 00:09:29.340 William Cheng: Okay, so in that case, it's called a blocking system call right so right now we're talking about a process, a process. If you want to wait for a child process today, you make a blocking system call and then your process will go to sleep. 74 00:09:30.420 --> 00:09:36.750 William Cheng: Okay, if it turns out that you're the YOU'RE THE ONE OF YOUR CHILD processes already dead. Well, in that case. This function will return right away. 75 00:09:37.080 --> 00:09:43.320 William Cheng: Okay, whenever we turn right away. It will tell you which process is dead, providing you with the process identifier for that particular trial process. 76 00:09:44.010 --> 00:09:53.910 William Cheng: That. So again, the menial blocking system callers that this particular system call may block. Okay. And then, you know, whoever that's making the system call will fall asleep, waiting for something to happen. 77 00:09:54.690 --> 00:10:00.930 William Cheng: Okay. So in this case, will you make this way system call. If not, if all the child processes are running well then your parent process will go to sleep. 78 00:10:01.440 --> 00:10:09.780 William Cheng: Okay, at some point. Well, one of the child process. You know, the one that child process dies when, in that case, your parent, the parent process will get woken up 79 00:10:10.230 --> 00:10:17.760 William Cheng: Okay, when the parent got woken up what he will do is it will return from this function and the return value over here will be I will identify 80 00:10:17.970 --> 00:10:27.180 William Cheng: To the HR process and also the arguments over here. Now what contain are the, the, the return code or the Mexico for the HR process. 81 00:10:27.840 --> 00:10:34.830 William Cheng: Okay. So, in the previous example over here. I said I yo your child processes are going to call exit number five over here. So in this case, what you will do that you will call way. 82 00:10:35.100 --> 00:10:43.440 William Cheng: And then the arguments over here is going to be the address of the return code. Okay, so this way you can actually return, you'll be able to receive the 83 00:10:45.810 --> 00:10:48.900 William Cheng: In this case, you will be able to receive the data return Mexico. 84 00:10:49.560 --> 00:10:58.500 William Cheng: Okay. So in this case, you know, when you're done over here. So let's say that you know when you finish doing this, you're going to examine our memory location, there should be a five in there because the child process die with the exit code or five 85 00:10:59.490 --> 00:11:02.790 William Cheng: Or so pretty soon we're going to see the example how to actually use all these code. Okay. 86 00:11:03.510 --> 00:11:09.000 William Cheng: Alright, so the parent process will be a call this function right you have 100 child processes running one of them is going to return. 87 00:11:09.180 --> 00:11:21.120 William Cheng: And then he will tell you what the dead child processes. And what's the return it ethical for that day. Chopra says okay and you also know that that child process is dead. So, therefore, that child process will not execute any more code. Yeah. 88 00:11:23.100 --> 00:11:27.210 William Cheng: All right. So if you look at this picture over here. So let's say that I'm the parent process over here. 89 00:11:27.630 --> 00:11:39.000 William Cheng: Okay, so if I make the waste some column going to come inside of Colonel instead of Colonel, I will see this data structure if this is the data structure that I see, I would know that I have two dead child processes right now. 90 00:11:39.690 --> 00:11:50.280 William Cheng: Okay. So, therefore, what the colonel can do is that the colonel can decide to pick one of the day trial process and return that information to you right away. So this way. Your way system called does not have to block. 91 00:11:50.760 --> 00:11:53.430 William Cheng: Okay, so even though it is a blogging system for that just means that there's 92 00:11:53.940 --> 00:12:00.090 William Cheng: There's a possibility that it will block, but it doesn't have to blog because if the colonel has enough information you want to return right away. 93 00:12:00.510 --> 00:12:03.690 William Cheng: Okay, so it is possible that the colonel decide to pick up this 94 00:12:04.080 --> 00:12:13.110 William Cheng: This data structure over here and then the return value over here will be the process ID of this dead child and it will also pick up the return code over here and they're ready to this memory address 95 00:12:13.530 --> 00:12:24.060 William Cheng: That's that are given as a function argument for the way system call. Okay, so once you return the process ID and the return code over here, this data structure become useless inside the Colonel. 96 00:12:24.750 --> 00:12:32.910 William Cheng: Okay. The Colonel will have no longer any use for this data structure. So after this all these values are return the organism Colonel actually free up this data structure. 97 00:12:34.020 --> 00:12:43.590 William Cheng: Okay, so it's very, very important to understand that the opportunities are before the parent process called way. And before you return the process ID and the return Co. You have to keep this data structure around 98 00:12:44.640 --> 00:12:48.060 William Cheng: Okay, because otherwise you know when the parent called Wait, you don't have that information anymore. 99 00:12:49.380 --> 00:13:04.080 William Cheng: Okay, so therefore it's the job of the operating system to maintain that data structure for dead child processes. This way at a later time. The, you know, the parent process can call waste as a call to retrieve the exit process so sorry to retrieve the exit status for HR process. 100 00:13:05.340 --> 00:13:12.780 William Cheng: Or is it again. You need to make the connection we do the system call and what do you have to do inside of the operating system kernel to make sure that all these information is maintained. 101 00:13:13.590 --> 00:13:20.820 William Cheng: Yeah. Alright. So in this example, we pick up this day trial process. It's also possible that your opportunity to actually pick up the other day trial process. 102 00:13:21.090 --> 00:13:27.240 William Cheng: There is no requirement that you have to find the first one or the last one, or anyone. Okay, so he just added one of the child processes dead. 103 00:13:27.600 --> 00:13:41.220 William Cheng: The opportunities that has a choice to pick up any one of them, return the process ID in Mexico, and then free up that entire data structure. Okay. And before you return to Mexico and return the process ID. You are not allowed to destroy the data structure. Yeah. 104 00:13:43.530 --> 00:13:48.060 William Cheng: All right, so I guess over here. So, to explain I guess. Explain what I just said over here and then again. 105 00:13:48.600 --> 00:13:53.850 William Cheng: It's important to understand the way system called. It's a blocking system called. That means that it is possible 106 00:13:54.090 --> 00:14:02.220 William Cheng: That there's no HR process. So what would that look like there's not that child process right in this case the parent process to terminate children list over here will be an empty list. 107 00:14:03.060 --> 00:14:08.190 William Cheng: Okay, so if there's an empty list over here. Then in this case the parent process and will fall asleep inside the Colonel. 108 00:14:08.400 --> 00:14:18.360 William Cheng: Right. So in this case, what would have to fall asleep. Well, because you want to give out the CPU so that so the answers. So the other process will be able to run inside the CPU and your process has nothing to do but way for child process to die. 109 00:14:18.900 --> 00:14:24.060 William Cheng: Okay. So in this case, your parent process of a fall asleep. And eventually, when would the child process die. 110 00:14:24.600 --> 00:14:31.050 William Cheng: Well, when the child process called the excess system call the child process would die. So in that case, the Chopra says when you know sort of 111 00:14:31.290 --> 00:14:38.550 William Cheng: Make the exorcism call it will come to the parent and then we will notice that the parents terminated children. This is empty. So what it will do is it will add itself to the 112 00:14:39.060 --> 00:14:48.600 William Cheng: To the termination from this if it also finds out that the parent process of sleeping inside Colonel waiting for child processes to die in this case what it will do is it will actually wake up the parent process. 113 00:14:49.530 --> 00:15:03.330 William Cheng: That we mentioned before, there are other places to wake up. Wake up a program for example instead interrupt service routine if if you have a colonel thread waiting for a while waiting for iOS device to finish what it was doing, why not case when 114 00:15:04.470 --> 00:15:14.460 William Cheng: The IO operation is finished, you want to wake up that Colonel threat and now we have a similar situation, you have a parent process waiting for a child process to die. So therefore, when the child process to die. 115 00:15:14.640 --> 00:15:22.830 William Cheng: You need to wake up the corresponding the the sleeping parent process. Okay, what are the parent process is not sleeping well, in that case, you know, you have nobody to wake up. 116 00:15:23.550 --> 00:15:28.380 William Cheng: Okay. So all of these things will happen inside the system called instead of a system called inside the 117 00:15:28.620 --> 00:15:36.810 William Cheng: You know, the, the inside of the system call so so later on when you get to implement the operating system kernel, you have to take care of all these conditions. 118 00:15:37.410 --> 00:15:48.180 William Cheng: Okay, so, so, so I guess the the first programming assignment is no as processes and threats. So, therefore, you have to implement all these options you have to implement all these operations inside the cardinal okay 119 00:15:49.230 --> 00:16:01.260 William Cheng: So, so before you get there might be the concept is a little vague, but once you're done with Colonel one this concept should be very, very, very, very clear because you you have to make it to work inside your first assignment. Yeah. 120 00:16:03.540 --> 00:16:09.960 William Cheng: Alright, so here's a typical way that you know the the parent process will create a child process and wait for it to die. 121 00:16:10.350 --> 00:16:14.610 William Cheng: You can see that it only has a few lines of code. So this code is actually pretty tricky. 122 00:16:15.150 --> 00:16:24.570 William Cheng: Okay, so, so once you to make sure that you understand this code. So you should read this quote a few times and make sure you understand exactly what's going on. Okay. Alright, so this code, but also the parent process. 123 00:16:25.050 --> 00:16:34.290 William Cheng: Okay, so remember what you for code which operates at the Chopra says get a copy of the parents address space. So, therefore, this code is both for the parent process and for the child process. Okay. 124 00:16:34.560 --> 00:16:38.220 William Cheng: Alright, so the way it goes over here is that the parent process is gonna is gonna execute this co 125 00:16:38.460 --> 00:16:50.010 William Cheng: dependent process is going to call it the fourth system call and the return value over here is going to be the process identifier for the child. And we're going to store the process identifier for the child inside this local variable over here called P P ID. 126 00:16:50.760 --> 00:16:59.220 William Cheng: Okay, so please understand, again, that the parent process is going to call forth, but both the parent and the process and the child process, they will both return from fork. 127 00:17:00.060 --> 00:17:11.070 William Cheng: Okay. So in this case, when both of them returned from Fourth one of them will will will will will store the process ID of the child process and that will be the parent process. The other one will see the p it over here equals zero. 128 00:17:11.610 --> 00:17:21.930 William Cheng: OK. So the code over here it says store the return value over here into this local variable and immediately. Compare that against zero if it's equal to zero then this code will execute right here. 129 00:17:22.950 --> 00:17:27.360 William Cheng: Okay, if it's not equal to zero, then we go to the park, then this co execute 130 00:17:28.650 --> 00:17:35.970 William Cheng: Okay, so you can see that in the parent process code in the code of the parent process, we need to write a code, both for both for the child process. 131 00:17:36.240 --> 00:17:45.810 William Cheng: And for the current process. And we're going to use the if statement over here to decide, you know the the child process is going to be the one to execute this code and the parent process will be the one execute this code. 132 00:17:47.220 --> 00:17:53.730 William Cheng: OK. So again, a very simple line of code, you call for each of the return code to return co equal to zero, then you are the child process. 133 00:17:53.880 --> 00:18:01.800 William Cheng: You actually do this. Go. If the return code is not equal to zero. Well, then P ID over here will be the process identifier for the child process and then you execute this code. 134 00:18:03.210 --> 00:18:08.850 William Cheng: Okay. Alright, so now let's take a look at the trial process code right the trial process go, what does it do it is to I put on in comment over here. 135 00:18:09.030 --> 00:18:18.450 William Cheng: Some code is here for the child process to execute and when the child processes done what he would do is that it will call the exorcism call with the value over here and and as a manager. 136 00:18:19.290 --> 00:18:29.130 William Cheng: Okay, so what it will do is it will call exit n. So, so in this case you couldn't make the exorcism call and then the the end over here will become the the return code or the x value for the child process. 137 00:18:30.120 --> 00:18:38.910 William Cheng: All right, and then we make a system called and now the child process will cease to exist, it will not. And when I execute any more code. In the meantime, what the what is apparently going to do 138 00:18:39.510 --> 00:18:45.390 William Cheng: OK. So again, apparently over here is gonna is going to allocate memory over here to store the return value. 139 00:18:45.630 --> 00:18:51.810 William Cheng: So in this case, it will use the local variable. So I'm going to have a local variable we hear of integer type and this one is going to be the return call 140 00:18:52.020 --> 00:19:03.660 William Cheng: And then when I make the way system called the argument over here again you return. Go, go to the previous slide over here, it will show you, you know that the the argument over here is a pointer to an integer. 141 00:19:04.890 --> 00:19:10.740 William Cheng: OK. So again, when you see a pointer to energy. It doesn't really mean that it's a pointer. Sometimes it means that this is the return value. 142 00:19:11.250 --> 00:19:18.000 William Cheng: Okay, so what I would do is I will, I will provide you with a pointer to point to a memory location where you can actually store the return value. 143 00:19:18.690 --> 00:19:26.910 William Cheng: NAS and this example over here. We're going to take the address of return code. So in this case, this argument will be appointed that points that are returned go right here. So this way. 144 00:19:27.300 --> 00:19:32.820 William Cheng: You know when you return from the way system call the return code over here will have the value of the child process Mexico. 145 00:19:34.230 --> 00:19:42.360 William Cheng: OK. So, again, to read the way system called return two things. One is the process ID of the HR process. The other one is the return value and it will be stored inside this local variable. 146 00:19:42.810 --> 00:19:47.850 William Cheng: OK. So the basic idea over here is that when you make the way system call this is the right way to write your code. 147 00:19:49.170 --> 00:20:01.380 William Cheng: Okay, if you want to write it in a different way. Walk me can do it, but that's probably not a good way to go. Okay, so what you would normally do is that you create a local variable and you pass the address of the local variable to receive the return value for this function. 148 00:20:02.400 --> 00:20:05.490 William Cheng: There. So if you look at this code over here. What is the parents don't do right, the parents. 149 00:20:05.850 --> 00:20:17.730 William Cheng: Do is, is going to stay in an infinite loop right there's while loop over here and what it will do is that it will call weight and then it will compare the return value against PhD what his PhD PhD is the local variable over here. 150 00:20:18.720 --> 00:20:30.210 William Cheng: Okay, so what it's doing over here is that this parent process. Maybe it has created 100 child processes. So what it will do is that in these to wait for the right child to die in this example at the right time is the one that you have just created. 151 00:20:31.440 --> 00:20:39.600 William Cheng: Okay, so it is possible when you make the way system. Oh, it's going to return is going to return with some other child processes that you are now waiting for. Well, in this case, if you write your code like this, what would it do 152 00:20:39.870 --> 00:20:42.330 William Cheng: It will ignore it and he will call wait one more time. 153 00:20:43.050 --> 00:20:50.490 William Cheng: Okay, and then way return again it will return to you another child process idea, and it's not the one that you're looking for. And then in this case it will keep looping like this forever. 154 00:20:51.000 --> 00:20:57.750 William Cheng: Until eventually the right child process died this case, the righteous process and this CO is the one that you just create it, right. So, therefore, at 155 00:20:57.990 --> 00:21:08.040 William Cheng: Some point you know this function will return and the process ID will be exactly the same as the one that you have just created one this case they will be equal and then you jump out of the infinite loop and then what is the process do 156 00:21:08.310 --> 00:21:13.830 William Cheng: Right. So in this example over here. We're going to execute this code in the comment over here and then continue to execute code over here. 157 00:21:16.080 --> 00:21:18.660 William Cheng: Alright, so again this code is actually very, very complicated. 158 00:21:18.810 --> 00:21:27.900 William Cheng: Right, because you create a child processes how process has a life of its own. It's going to do it with stuff. And what is done is going to make the exorcism call the parent process over here is going to wait for the child prostitute to die. 159 00:21:28.530 --> 00:21:40.770 William Cheng: So this particular Co. You know, when you have one CPU and have two CPUs. When you have 10 CPUs in my actually behave differently. Okay, so what you need to understand is that this code will always work no matter how many CPUs, you have 160 00:21:42.720 --> 00:21:47.760 William Cheng: Alright, so, so you need to understand, you know, by looking at this code. What is the abstraction that we're trying to tell you 161 00:21:47.940 --> 00:21:52.740 William Cheng: The parent create a child processes that child processes are running in parallel, the parent with a child process to die. 162 00:21:52.920 --> 00:22:01.740 William Cheng: In the meantime, is some other child process. Today we're going to ignore it and eventually when the righteous process to die, then we proceed and continue to execute execute code over here. 163 00:22:03.270 --> 00:22:13.020 William Cheng: Okay, so that's the meaning of the code. So this is called fork and way we folk of HR process and we wait for that particular trial process to die. And this is going to be the code for doing that. Yeah. 164 00:22:14.910 --> 00:22:22.620 William Cheng: Alright, so again this code is very, very complicated, right, you need to take care of the case where the child process. The way you know that we will you make the way system call 165 00:22:22.860 --> 00:22:27.510 William Cheng: What if they're already dead child process weightings out of Colonel why in this case you will return right away. 166 00:22:28.080 --> 00:22:39.570 William Cheng: Okay. And in the case where there's no HR process, you know that the settings at a colonel why, in this case a parent process will fall asleep and then every time when a child process to die, you need to. So a wake up the child. 167 00:22:39.990 --> 00:22:44.880 William Cheng: The parent process return all these values so that the you know the application program over here. 168 00:22:45.060 --> 00:22:50.430 William Cheng: Can check to see if that's the right one. If it's not the right one dependent processes over here is when I made the way system call again. 169 00:22:50.610 --> 00:22:54.090 William Cheng: Coming into the kernel to check whether you know whether this list is empty or not. 170 00:22:54.300 --> 00:23:04.110 William Cheng: If this is list is empty, then it will go to sleep, if the list is not empty, you will return right away so that the application code over here to check to see if it's the right child process that has that has died. 171 00:23:05.370 --> 00:23:14.100 William Cheng: Okay, so again this code is actually very, very involved. If you think about all the possible things that can happen you know when when when when you come inside the carnal. Yeah. 172 00:23:15.810 --> 00:23:24.210 William Cheng: All right, always get a question, you know, some people say, well, what if I, you know, make this call. What about made that call. Some people asked me, What if the child process make another four or something like that. 173 00:23:24.930 --> 00:23:36.180 William Cheng: So what's important to understand over here is that the operating system gives you all the system calls and it will tell you exactly what these systems are supposed to do. It's up to you to make sure that you use a system called the right way. 174 00:23:37.260 --> 00:23:45.840 William Cheng: Okay, the code over here is shut it is showing you what is the right way to make a system call to create a child process and to wait for that child process to die. 175 00:23:46.200 --> 00:23:52.860 William Cheng: Okay, so if that's what you want. You want to create a child process that the child process to run and wait for the child processes to that. Well, this will be the code that you write 176 00:23:53.370 --> 00:23:59.490 William Cheng: Okay, don't write your code, any other way. Okay, if you write your code, any other way. You shouldn't be surprised that if it doesn't really work this way. 177 00:24:00.150 --> 00:24:10.020 William Cheng: Okay, it's up to you to decide how you make the system call if you use a sort of a weird way to make a system called that's really not the recommended way. Well, then you're going to end up with unexpected results. 178 00:24:10.500 --> 00:24:19.050 William Cheng: Okay, so if you, you know, if you really like unexpected result. Well then feel free to write code anywhere you want, but if you want predictable result, you should always write your code, the right way. 179 00:24:19.710 --> 00:24:28.350 William Cheng: Okay, so in this class, you know, typically what we're gonna do is, I'm going to show you what is the right way to write you write all this code, and it's up to you to choose to write your code. The right way all the time. 180 00:24:29.370 --> 00:24:29.640 William Cheng: Okay. 181 00:24:31.380 --> 00:24:31.860 William Cheng: All right. 182 00:24:33.810 --> 00:24:37.260 William Cheng: Okay, so some of the remaining should have. We have a process termination. 183 00:24:37.530 --> 00:24:54.240 William Cheng: The process ID over here. It's a 16 bit Long Integer right we saw before. It's declared as a short integer a short integrates the 16 to be an integer. So to to the 16 that's equal to 64,000 or 65,000 so there are 64,000 or 65,000 a process IDs that are unique. 184 00:24:54.720 --> 00:25:04.560 William Cheng: From that point on the process ID will be reused. Okay. Is it possible that inside your machine you're going to end up creating more than 64,000 processes and I mean certainly it's possible. 185 00:25:05.400 --> 00:25:11.730 William Cheng: Okay, so therefore we're going to be very careful because in the end, you know, we're going to end up with process it being reused while then in that case. 186 00:25:11.880 --> 00:25:16.770 William Cheng: When we try to wait for particular process to die. We can't really tell the difference between one process and the other process. 187 00:25:17.280 --> 00:25:23.490 William Cheng: Okay, so therefore it's up to the operating system implementation to guarantee that the process ID over here is unique. 188 00:25:24.300 --> 00:25:29.640 William Cheng: Okay, so one of the way that this can be unique is that whenever you create a process ID whenever you create a process control block. 189 00:25:30.090 --> 00:25:32.250 William Cheng: Inside the process control blah, there's a process ID. 190 00:25:32.700 --> 00:25:38.310 William Cheng: Okay, so next time we'll you need to create another process control blah and you want to make sure that the process ID is not reuse. 191 00:25:38.460 --> 00:25:45.750 William Cheng: Well, then, what we need to do is that we need to go through all the process control blah, and then make sure that the newly created process ID is not an existing process it. 192 00:25:46.980 --> 00:25:58.500 William Cheng: Okay, so this is one of the reason we are not allowed to free up the process control blog because if you're free it up. Well then, we then as possible. We will use reuse the process ID and then some of the system called over here will not be working the right way. 193 00:25:59.430 --> 00:26:02.250 William Cheng: Okay, so therefore inside the operating system we have to implement this correctly. 194 00:26:03.180 --> 00:26:08.520 William Cheng: There. Alright, so therefore, when your process. Make the exorcism. Call your process die you going 195 00:26:08.730 --> 00:26:16.170 William Cheng: To Colonel you're on. You're not allowed to leave the process control blog because if you delete the process control blah, it's apparent hasn't get your process idea. 196 00:26:16.380 --> 00:26:24.900 William Cheng: Well, then in that case, you know, next time when we reuse the process ID, then the parent process will get it will get the return Mexico for the wrong process. 197 00:26:25.890 --> 00:26:34.620 William Cheng: Okay, so therefore it's very, very important to understand that when you make the exorcism call you go into the colonel your process is dead, but your process control blog is still kept around 198 00:26:35.310 --> 00:26:47.460 William Cheng: Okay, so what is your process control bar can be can be free. It can only be free. When you're about to return from the way system call and the process ID and the return code that you're returning is the one that you that you are a lot of free 199 00:26:48.300 --> 00:26:51.750 William Cheng: Okay, so only at that time you allow to free the process control blah, right. 200 00:26:52.440 --> 00:27:00.240 William Cheng: Right. So in this case, you know where your process maybe exorcism call you go inside the Colonel. It's dead, but it's still hanging around as a data structure. 201 00:27:00.690 --> 00:27:08.280 William Cheng: We call this, you know, the state of your process to be a zombie state. Okay, so I guess, y'all. Watch zombie movie before so you know what a zombie. 202 00:27:08.850 --> 00:27:12.120 William Cheng: Zombie is something that's dead, but you can try to kill it. It just doesn't go away. 203 00:27:12.930 --> 00:27:19.680 William Cheng: Okay. So inside, Colonel. We have the same thing when a process, make the exorcism. Call now the process get turned into a zombie state. 204 00:27:19.860 --> 00:27:22.830 William Cheng: So the process control blocks, you're hanging around inside the kernel. 205 00:27:23.010 --> 00:27:34.920 William Cheng: Until eventually when the parent call wait and you want to return the process control blog. So if you want to return the process identifier and the return code for this process. In that case, you can delete the process control blah and now it's no longer in the zombie state. 206 00:27:36.630 --> 00:27:43.020 William Cheng: Alright, so again, you know, it's very important to keep a zombie state so you can keep track of the process ID and also the return Mexico. 207 00:27:43.590 --> 00:27:49.290 William Cheng: Okay. Only when you're completely sure that you never need that information ever again. And then you can destroy the process control, blah. 208 00:27:49.530 --> 00:28:00.150 William Cheng: Right, and then the only time you can do that is that is what you about to return the process ID and the Mexico back to the parent process because now the parent process know how this child process. 209 00:28:01.500 --> 00:28:01.740 William Cheng: Okay. 210 00:28:04.770 --> 00:28:12.570 William Cheng: Alright, the next thing I'm going to look at is going to look at the fourth you know system call the forces and call is running a program. Okay, so in UNIX 211 00:28:13.320 --> 00:28:22.590 William Cheng: You know Munich's is these are different from other operating system in order for you to run another program. What you have to do is that you, the first thing you need to do is that the parent process need to make the fork system call 212 00:28:22.950 --> 00:28:26.130 William Cheng: Okay, so we're going to start with a parent process over here, the parent process. 213 00:28:26.430 --> 00:28:34.950 William Cheng: Is going to make a fork system call you create a child process and the trial process and apparent process. Now they're exactly the same program, but they have a different process identifier. 214 00:28:35.460 --> 00:28:43.500 William Cheng: Okay, so the next thing that you need to do is that instead of a child prop process over here, you're going to make a system called to run a program inside its address space. 215 00:28:44.250 --> 00:28:49.470 William Cheng: Okay, so how do you do that, right. So what happened is that you going to make a system call the system call actually there are many, many choices. 216 00:28:49.890 --> 00:28:54.360 William Cheng: So Unix and Linux. We call them. We call this kind of system opponent exact system call 217 00:28:54.870 --> 00:29:02.970 William Cheng: Okay, the exact system was where you need to execute a program. So in this case, it's a system called so happens that the child process over here will make a system called come inside. Colonel 218 00:29:03.210 --> 00:29:13.410 William Cheng: When you make one of the exhaust system called inside the colonel what he would do is that it will destroy the address space of the trial process. And then what we'll do is enable go to the desk and then the 219 00:29:13.770 --> 00:29:24.210 William Cheng: So, so in this case, will you make the exact same call the argument over here will be, you will be the name of the program that you want to run for example the name of poker, you want to run MIGHT BE SLASHED bins LS 220 00:29:25.140 --> 00:29:30.480 William Cheng: There. So in that case, the names the program. Right. So you come into the Colonel. The Colonel would destroy your address base. 221 00:29:30.750 --> 00:29:38.160 William Cheng: Go to the desk and find this program and then what you would do is, is that this program will be sitting on the desk right so what it will do is that it will unfold it into memory. 222 00:29:38.340 --> 00:29:43.140 William Cheng: And then create the address space for the child. And now the child process, get a new address space. 223 00:29:44.010 --> 00:29:53.160 William Cheng: Okay, the new address space. What does it represent the new address space going to correspond to the slash slash is program. And now you transfer control to it. Where would you transfer control to a 224 00:29:53.670 --> 00:29:56.970 William Cheng: While you transfer control to the main function. And now this program was are running 225 00:29:57.570 --> 00:30:07.770 William Cheng: Where I mean, to be more precise, we know that the main function is called by the startup, I'm sure. So in this case you will transfer control into the startup. I'm sure the setup and you will call me and now you've been such our x will be running 226 00:30:09.210 --> 00:30:15.390 William Cheng: Okay, so this is the same thing happen when you turn around warm up wine. Right. You will when you run your warm up, point your mama one in your in 227 00:30:15.990 --> 00:30:23.460 William Cheng: Your command show your command show what it will do is it is that it will for call for trial process. So your child processes. Also the commercial 228 00:30:23.640 --> 00:30:29.550 William Cheng: And then the child processes over here will make an exact system call specify warm up one as the first argument. 229 00:30:29.820 --> 00:30:39.270 William Cheng: Okay, to the exact system call and now we can start a colonel would destroy the address space with a child process. So now the child process is no longer your, your, your, your, your commercial 230 00:30:39.690 --> 00:30:52.260 William Cheng: Then will you will do that you will go to the desk. Look at the warm up one program and then build an address space for it. And now you transfer control to the startup function for one and one more points in the call may and now your co star running 231 00:30:54.120 --> 00:30:58.230 William Cheng: Alright, so, so, so, so, so this is how you run another program. 232 00:30:58.950 --> 00:31:11.250 William Cheng: That sort of code look like this. OK. So again, the basic structure is the same as before the parent process is going to be a commercial. We're going to call the fork system call we're going to compare the return value is equal to zero if equal to zero over here, we're gonna 233 00:31:13.440 --> 00:31:23.520 William Cheng: Move on. They, in this case, it will be the child process and then we will run a program is our HR process. Now the special exact system called that we're going to make over here. It's called exact L. 234 00:31:24.030 --> 00:31:34.800 William Cheng: As it turns out, this actually 15 different exact system called there is exact elders exact v. There's exact CX etc. The other there's tons of different choices they all start with the word exact and then did 235 00:31:35.130 --> 00:31:40.140 William Cheng: The argument for this function over here. The first argument is going to be the program that you want to run 236 00:31:40.710 --> 00:31:43.830 William Cheng: Okay. The example that we see over here is going to be the apprentice program. 237 00:31:44.430 --> 00:31:51.180 William Cheng: Okay, so when we try to run the program. The first argument over here is going to be the location for your price program inside of our system. 238 00:31:51.750 --> 00:32:01.800 William Cheng: Okay, in this case, this example is slash homes HBC I'm BC. Right. So Tom says BC, so I should be in such primes and then followed by the the the command line arguments. 239 00:32:02.580 --> 00:32:10.230 William Cheng: Okay, so in this case we're going to read this program and the first minute argument over here will be prime. So, this one will be this one will be our 240 00:32:10.650 --> 00:32:24.480 William Cheng: V zero RT zero be primes are the are the one will be 300 okay will be the string 300 and are we have to our V have to over here will be the null pointer. 241 00:32:25.440 --> 00:32:31.950 William Cheng: Lesson this case when your program structure, right, if you look at your main function okay arc see over here will be equal to, you know, 242 00:32:32.670 --> 00:32:44.190 William Cheng: Will be good to two and then are the zero over here will be primes an RV one over here will be will be the string 300 there. So again, this is a sushi. Sushi chain. So therefore you will be terminated by the backslash zero 243 00:32:45.630 --> 00:32:53.040 William Cheng: Alright, so this is why when you start running your, your, your program. The main function at the first argument is going to be something about you executable program. 244 00:32:53.400 --> 00:32:59.580 William Cheng: So I think on Linux. This actually was going to be the same as this particular program path so you know exactly 245 00:32:59.940 --> 00:33:06.630 William Cheng: You know where your program executed from and the second argument over here will be the commander will be the starting of the command line arguments. 246 00:33:07.440 --> 00:33:16.320 William Cheng: OK, so again you know what you should do is that you should, you know, run your warm up one program on the GDP, where you get domain print all these things out so you know exactly what you're dealing with. Yeah. 247 00:33:16.770 --> 00:33:28.860 William Cheng: Alright. So in this case, what happened is that now what you need to understand over here is that the child process over here will execute will make a system called come inside the kernel and you will destroy the address space. Okay, which address space that is destroyed. 248 00:33:30.270 --> 00:33:36.300 William Cheng: Okay, he will destroy this address space, right, because this address space that you see over here. This code is inside the address space. 249 00:33:36.540 --> 00:33:40.680 William Cheng: This code is inside the address space for both the parent process and the child process. 250 00:33:40.980 --> 00:33:51.870 William Cheng: Okay, so therefore y su returned from fork. What you see over here, this is the code for both the parent and the child. So therefore, the child processes will destroy its own address space for the parent, the parents to have the address space. 251 00:33:53.070 --> 00:34:05.130 William Cheng: Okay, so in this case the chocolates, then we'll do the story is all address space and now it will start running it will go to the desk and then you know ability address space for the primes program and it will start computing the primes for the next 300 price. 252 00:34:06.270 --> 00:34:13.770 William Cheng: Okay. So in this case, what would the parent do right the parent over here. What it will do is that is I changed a little bit. So over here, it says what it would do is that I won't wait for the trial process to die. 253 00:34:14.010 --> 00:34:24.030 William Cheng: So in this case, the argument over here. I'm going to use a null pointer. OK, so the no point of me and I'm not interested in the return code because if I'm interested in return. Co. I'm going to use the address of the 254 00:34:24.960 --> 00:34:33.840 William Cheng: Integer variable, right. So this way I can actually hold the return value, right, if I'm not interested in the return value. I'm giving you a null pointer to say I don't want i know i don't really care about the return value. 255 00:34:34.110 --> 00:34:38.460 William Cheng: Just tell me which processes that that and again I'm going to put inside of infinite loop. 256 00:34:38.820 --> 00:34:47.760 William Cheng: If the if the way function return the process ID and the equal to the child processes that I've just created. Well, then in that case, I would jump out of the infinite loop. 257 00:34:48.000 --> 00:34:51.990 William Cheng: Right. Otherwise, I'm going to keep them forever until the child processes, the child processes that 258 00:34:52.800 --> 00:35:04.800 William Cheng: OK. So again, if this is your financial you are waiting for your child process to die. You're waiting for the program that you're running to terminate. Okay, if your, if your child processes and terminate. I'm going to get stuck inside this infinite loop forever. 259 00:35:06.600 --> 00:35:13.260 William Cheng: Okay. All right. So, by the way over here where it says that if you do men exactly how you're going to see that. Exactly. I always specify like this. 260 00:35:13.680 --> 00:35:17.700 William Cheng: You know, it has function, you know, you'll get it returns an integer value. 261 00:35:18.120 --> 00:35:26.790 William Cheng: The integer value if it returns zero. So a lot of the UNIX system call when it returns zero, it tells you that everything is fine. What did return a non zero value that means something has gone wrong. OK. 262 00:35:27.210 --> 00:35:37.110 William Cheng: So again, you need to read the men pages for this simple, very, very carefully to understand exactly when I tried to return the value. What is what, you know, what is the appendices and trying to tell you that 263 00:35:37.740 --> 00:35:46.080 William Cheng: This function. The first argument over here is going to be the executable path for your program and the second argument over here, it will be org and then the main page actually says, da, da, da. 264 00:35:46.950 --> 00:35:53.700 William Cheng: Okay, so this is a. So, this is called variable number of arguments, you know, for, for this particular for this particular function call. 265 00:35:54.180 --> 00:35:57.390 William Cheng: So in this case, you know, how do we actually know how many argument does it have 266 00:35:57.990 --> 00:36:05.760 William Cheng: Okay. So in this case, every argument over here is going to be a C string. So in the end, the last argument over here will be a null pointer that will tell you where is the end of the argument. 267 00:36:06.690 --> 00:36:19.860 William Cheng: Okay, so that's why it over here. We need a zero at the end over here to tell you that this is the end of the string or you kind of read and will you encounter a no no pointer. So, so, so, so, so, so this way you know that that's the end of the climate command line arguments, then 268 00:36:21.240 --> 00:36:30.450 William Cheng: Alright, so it's a string function over here are but actually it's not strange because if you look at the main page for print f my printer printer function, right, how many arguments does print off, take 269 00:36:30.930 --> 00:36:45.030 William Cheng: Well, we don't know. It depends on how many percent stub or in the first argument over here and that will tell you how many arguments are there. So again, printer function over here is also a bar Ark kind of function, you can take variable number argument, just like exactly, then 270 00:36:46.500 --> 00:36:57.030 William Cheng: Also by the way over here. If you look at the code over here. What is exactly what I'm doing over here. Okay, so when you make the exhaust system call. I'm going to wipe out the address space. So therefore, the access system called over here will be gone. 271 00:36:58.230 --> 00:37:05.970 William Cheng: Okay, so if you look at this code. What is the exact over here actually failed, why did it fail. Well, maybe the first argument over here. It's an invalid. 272 00:37:06.330 --> 00:37:17.190 William Cheng: Invalid path. So therefore, there's no such file or maybe there's permission denied you saw that one up one right if you if you give an invalid path you're going to get, you know, you know, the 273 00:37:17.940 --> 00:37:28.860 William Cheng: You cannot find the file or you do not have the privilege. So in this case, exact out with monastic seat right so exactly doesn't succeed, then the address space for the child is still here. 274 00:37:29.790 --> 00:37:36.870 William Cheng: Okay, so in that case you will return on exactly what the error code. So this code over here says if that's the case, I will return with the export one 275 00:37:38.340 --> 00:37:42.330 William Cheng: Okay, so in this case when the child will not be able to execute the price program. 276 00:37:42.810 --> 00:37:52.560 William Cheng: In this case, the child processes will die and then he will return the ethical over here will be one. So this way if the parent or we're trying to find out how the child died. If you're interested in the return icicle 277 00:37:52.740 --> 00:37:59.160 William Cheng: You will see that the tax code over here. It's going to be go to one. Okay, so in that case it means that the child processes didn't execute correctly. 278 00:38:00.630 --> 00:38:07.440 William Cheng: So you can actually use that, you know, for the parent child to communicate that information to say that the child in executed correctly. So therefore, 279 00:38:07.740 --> 00:38:17.070 William Cheng: You know, the parent can actually print an error message say hey you know for some reason the child didn't really execute correctly. Okay. And in this example, we decided not to do that right again because there's just a silly example. Yeah. 280 00:38:19.380 --> 00:38:27.090 William Cheng: Alright, so again the pictures are to look like this right in the, in the beginning, over here, the parent process is going to call fork and then he will return with the process ID for the child process. 281 00:38:27.540 --> 00:38:32.790 William Cheng: The child prices over here is going to return with a value of zero. And now the child processes will execute this code for exactly 282 00:38:33.210 --> 00:38:43.710 William Cheng: So over here I show to show you that in the child process. I'm going to execute this code with the executives. But again, it's a system called you're going to go inside the kernel and they are you going to replace this address space with the new one. 283 00:38:44.250 --> 00:38:48.000 William Cheng: Okay, so therefore when exactly I'll finish it will look like this. 284 00:38:48.300 --> 00:38:57.420 William Cheng: Okay, this address space going to get replaced with a different programs address space over here, I sort of draw the pictures and say that. And on the left on originally these to address may look exactly the same. 285 00:38:57.900 --> 00:39:02.220 William Cheng: Okay, because it's a copy of the address space. And now you run a different program. So therefore, again, 286 00:39:02.490 --> 00:39:10.320 William Cheng: The address is going to look different. And now we're going to start executing coast self starter routine to shower business is going to call me and then we're gonna start running our program. 287 00:39:11.640 --> 00:39:15.690 William Cheng: Alright, so again in the address space over here. We're going to get a brand new address space. Okay. 288 00:39:18.690 --> 00:39:26.970 William Cheng: All right, this I already talked about. So there's sort of put it all together and run through the code a little bit over here using a different way to sort of visualize this. 289 00:39:27.330 --> 00:39:34.440 William Cheng: So again, I'm going to draw this picture on the top part over here is application or the user space in the middle part over here is going to be. Oh, so the kernel space. 290 00:39:34.680 --> 00:39:41.910 William Cheng: At the bottom part over here is going to be hardware. We're not going to draw the hardware right now. Alright, well I'm going to see what's going on in the application program also inside a colonel. 291 00:39:42.120 --> 00:39:50.670 William Cheng: inside corner, we're going to divide the colonel into a bunch of subsystem. So right now we're looking at processes over here. So everything happened in the process of system. 292 00:39:51.300 --> 00:39:59.220 William Cheng: That one or the other you know subsystem, or there's the graphics subsystem. There's a networking subsystem there the files obsessing over here. So again, the data and a bunch of stuff over here. 293 00:39:59.310 --> 00:40:04.800 William Cheng: So we're going to ignore all of them. For now, we're only going to focus our attention inside the process of system that. All right. 294 00:40:05.220 --> 00:40:13.710 William Cheng: The red arrow over here. I'm going to typically I'm going to use a red arrow to show you where he IP is pointing to, again, so he so he's not a CPU over here CPU over here. 295 00:40:13.920 --> 00:40:20.850 William Cheng: I've registered called the IP right yeah IP over here is the one that points right here. So that means that I'm going to execute a call right here. So again, 296 00:40:21.090 --> 00:40:29.640 William Cheng: On the right hand side. I'm showing you sicko. Grab a yuppie as you point to a machine code because the sequel get compiled into machine code. And I'm going to execute a machine instruction one at a time. 297 00:40:30.030 --> 00:40:34.440 William Cheng: Yeah. But again, this is just an abstraction. So I'm sort of show you the red arrow. This is going to be the code on executing 298 00:40:34.920 --> 00:40:45.030 William Cheng: Yeah, all right. On the left hand side over here, the parent process is the is right now you're logging shall write all your credentials over here to say it's a shell and I'm going to execute you know this fork system call 299 00:40:46.410 --> 00:40:52.680 William Cheng: All right. Okay. So this is a system called boom you are trapped inside of current alright so here I draw tribe, then you instead of Colonel 300 00:40:52.890 --> 00:40:58.950 William Cheng: Will you come inside of Colonel, I'm going to show you that inside the Colonel, there's going to be a process control blog for the parent process. 301 00:40:59.610 --> 00:41:06.270 William Cheng: Okay. So inside the process control blah, I need to say the context of the parent process. So, therefore, instead of parent brother instead of 302 00:41:06.570 --> 00:41:11.880 William Cheng: Process Control blah, there is an area inside the Colonel. I'm going to sort of call them the context of the parent process. 303 00:41:12.750 --> 00:41:23.190 William Cheng: OK. So the idea here is that you start in the user space program and now you're executing. These are the Colonel, I need to remember where I was in the user space program. So therefore, I need to save the contacts inside, Colonel. 304 00:41:23.610 --> 00:41:27.450 William Cheng: Okay, so where would I say this piece of information. I'm going to go to my process control blog. 305 00:41:27.960 --> 00:41:34.170 William Cheng: I look for the contacts area. I'm going to say the user three contacts or the program contacts inside this data structure over here. 306 00:41:35.010 --> 00:41:43.020 William Cheng: There. So therefore, in this case, you know, again, simple my picture you try things out of Colonel, I'm going to save the context of the user space program instead of contacts area. 307 00:41:43.350 --> 00:41:46.350 William Cheng: OK. And now what I will do is I need to create a child process. 308 00:41:47.310 --> 00:41:52.800 William Cheng: Okay, alright. So what I would do is I'll create a chocolate says, and again every time when the child child process, we need to draw the address space. 309 00:41:52.980 --> 00:42:04.920 William Cheng: And now we also know that inside corner there is a process control, blah. So we also need to create a process control block for the child inside the process control blog over here will also be the context for the child. Okay, so therefore it will look like this. 310 00:42:06.540 --> 00:42:15.360 William Cheng: Okay, so now the parent process over here in the user space. He went to sleep inside the colonel rises seen it's sitting right here. So therefore, the child processes doing exactly the same thing. 311 00:42:15.780 --> 00:42:24.360 William Cheng: Right, because the child prices get exact copy of the parent. So we'll get a copy of the address space and also the child process is doing the same thing. It's also sleeping inside the Colonel. 312 00:42:25.800 --> 00:42:35.310 William Cheng: Okay, so therefore, again, the idea here is that we're going to copy the contents of the parent into the Tao of the interest of the child. So now the child and the parent. They are in exactly the same state. 313 00:42:35.880 --> 00:42:47.520 William Cheng: There. So now we are ready to return back into the user space again if we only have one CPU, we can only return one at a time. If we have multiple CPU. It is possible. We can simultaneously return both into the parent into the child. 314 00:42:48.000 --> 00:42:54.480 William Cheng: Yeah. So in this example, we're going to return that one at a time. So what is it also simulate that you know this is going to go really fast. 315 00:42:54.990 --> 00:42:56.850 William Cheng: So first the parents going to return. 316 00:42:57.690 --> 00:43:06.210 William Cheng: Okay, so in this case the parents gonna return, we're going to return the process identifier for the child and the P ID over here is going to be the process identifier for the child. Okay, so, so this 317 00:43:06.420 --> 00:43:11.580 William Cheng: Is sort of show you the child is still sleeping inside the Colonel, while the parent return over here already. 318 00:43:12.450 --> 00:43:22.140 William Cheng: Okay, so how do you, how do you return. Right, we're going to take the context area inside the process control blah, we're going to put it back into the CPU and now we're going to return back into the user space program. 319 00:43:22.350 --> 00:43:26.640 William Cheng: And now our users visible and will continue to execute a will return from the forks and some call 320 00:43:27.000 --> 00:43:37.020 William Cheng: Okay, so if you look at this picture, what happened over here is that we're going to store the return value over here into a local variable with a compare against zero and then we'll do all these kind of stuff. OK, so the parent process is ready to do that. 321 00:43:37.860 --> 00:43:50.220 William Cheng: Okay, if we have another CPU simultaneously. We can also return into back into the jar. So, by the way, I showed this picture over here to draw the you know the inside, apparently contacts area over here, it changed to a question mark. 322 00:43:51.690 --> 00:44:03.780 William Cheng: Okay, so what happens over here is what I'm showing over here is that if you return into the parent. Now, the parents going to start running the code over here. So the state of the parent is no longer in sync with the context of the parent inside 323 00:44:04.380 --> 00:44:05.790 William Cheng: Inside the process control, blah. 324 00:44:06.450 --> 00:44:17.970 William Cheng: Okay, the process control blog over here will store the information where the parent goes to sleep. But once the parents are running while we're going to use the CPU to run the parent process as fast as possible. So we will no longer be updating the 325 00:44:18.240 --> 00:44:25.800 William Cheng: You know, the, the process control, blah. Okay, so therefore the state information instead of process control blah, some of the information over here will be outdated. 326 00:44:26.760 --> 00:44:36.480 William Cheng: Okay, so why do I say some of the information over here will be outdated. I mean, clearly the contacts area we feel will be outdated because now the process is running inside of CPU again. So therefore, all the information is that 327 00:44:37.230 --> 00:44:39.780 William Cheng: All the information about what's inside the CPU or they'll all be wrong. 328 00:44:41.010 --> 00:44:44.760 William Cheng: Okay, but what about the process ID or the passes ideas to be correct, right. 329 00:44:45.090 --> 00:44:49.260 William Cheng: You know, so other information over here like linguists that we saw before. I mean, instead of Colonel 330 00:44:49.500 --> 00:44:54.930 William Cheng: All the other kind of information over here will be correct. But part of the information will be wrong. So therefore, over here, I'm going to replace 331 00:44:55.350 --> 00:44:59.100 William Cheng: You know, part of the process control blog over here with a question mark. Just say, I have no idea what they are. 332 00:44:59.670 --> 00:45:12.150 William Cheng: Okay, because what's running inside the CP over here will be different. Well, you know what the information that you kept track of inside Colonel okay because as soon as you return into the user space program. If you only have one CPU. Now your kernel is no longer running 333 00:45:13.920 --> 00:45:23.250 William Cheng: Okay. So remember I over here. What happened is that you make a system called you come into the kernel. And as soon as the system and the Colonel finish with this system is going to return back into user space. And now the colonel stops running 334 00:45:24.480 --> 00:45:35.220 William Cheng: Okay, so unless you have other CPU that you can actually make this kernel run again but it'd be if you only have one CPU. Now you returning to the user space worldwide and now you're running the user space program. So now you are no longer running inside, Colonel. 335 00:45:36.450 --> 00:45:41.790 William Cheng: Whereas, look at this is very important to understand if you have, if you have only one CPU, you can only do one thing at a time. 336 00:45:42.570 --> 00:45:53.640 William Cheng: Now what if I have multiple CPU right simultaneously. I can also return on the child. Right. So in this case, I'm going to take the context of the child. I'm going to restore the back into the CPU. So, so in this case it will look like this. 337 00:45:54.720 --> 00:46:00.840 William Cheng: Okay, so in this case the return value over here will be different from the parent because that will be the only thing that's different between the parent of the child. Right. 338 00:46:01.080 --> 00:46:06.420 William Cheng: The parents going to get the process identifier for the child. The child over here will get the return value of zero. 339 00:46:06.690 --> 00:46:11.910 William Cheng: Whereas, in this case over here. Again, we're going to take the context area that was store that was stored inside the cardinal over here. 340 00:46:12.270 --> 00:46:15.900 William Cheng: For the charts process control blah, we're going to return it back into the CPU. 341 00:46:16.290 --> 00:46:27.030 William Cheng: There so the child program can start running and now inside the process control block with the chakras as the information is that they will become outdated because now if you have a second CPU, the child prices will also be running parallel 342 00:46:28.530 --> 00:46:34.470 William Cheng: Right. So in this case, again, what would the Chopra says do right it will it will take the return value of the fork and they will copy into the local variable here. 343 00:46:34.680 --> 00:46:42.060 William Cheng: compared against zero and now the child process will see that equal to zero and then you execute code over here. Okay, so we're going to continue from there. 344 00:46:42.810 --> 00:46:52.080 William Cheng: Yeah, so the child classes over here. So the checks to see the return value equal to zero. So the Chopra says over here is that actually the exact system call. So now Chopra says will make the exact system call well 345 00:46:52.260 --> 00:46:57.870 William Cheng: While the parent process over here, maybe call this already, if he has multiple CPU, Renee. Maybe there's only one CPU. So again, 346 00:46:58.410 --> 00:47:02.340 William Cheng: The picture. The real picture is really complicated advocate, because it depends on how many CPUs, you have 347 00:47:02.850 --> 00:47:08.580 William Cheng: Okay, so otherwise over here that the parent over here will continue to run in parallel, while the chart over here will call exact L. 348 00:47:08.760 --> 00:47:13.650 William Cheng: And exactly i was the system call rise up again. Boom, you're inside the Colonel. Again, we're going to come inside of Colonel 349 00:47:13.860 --> 00:47:23.280 William Cheng: We're going to save the user through our content. So sorry. We're going to space. We're going to save the user space context of the trial process inside the contacts every year. So the question mark over here is gone. 350 00:47:23.580 --> 00:47:37.890 William Cheng: Now because now we say the cat is over here, we start executing inside the Colonel. Okay, so since this is the exact L system call what we have to do over here is to get rid of this address space right and then go to the disk over here and then to the new address space for it. 351 00:47:38.910 --> 00:47:47.310 William Cheng: There so therefore the picture I'm concerned is going to look like this. We're going to wipe out the address space over here. And now we're going to load a new program will be here new program, or he'll be the primes program. 352 00:47:48.780 --> 00:47:50.610 William Cheng: Right, so now they will look like this. 353 00:47:51.000 --> 00:47:59.520 William Cheng: Okay. And then what the next thing that we need to do is that we need to return back into the user base. Well, well. So we returned back into the user space program inside the Colonel, the contacts area. 354 00:47:59.760 --> 00:48:07.890 William Cheng: The information is going to be out of sync again and now we're gonna start executing code over here in the startup function and the syrup or she will call me. So therefore, we're gonna start executing code in May. 355 00:48:10.020 --> 00:48:18.090 William Cheng: Alright, so again it's complicated because all these things can happen in parallel. In the meantime, while we're the parent do by the parent over here. What it will do is that it will make the waste some call 356 00:48:18.300 --> 00:48:26.610 William Cheng: When they make the way system called is gonna come into the kernel and boom, over here, you can come into the kernel. And now we're going to save the user space context for the parent process over here. 357 00:48:26.820 --> 00:48:30.480 William Cheng: Instead of instead of contracts area. And now we're going to go execute inside the cardinal 358 00:48:31.320 --> 00:48:36.810 William Cheng: Okay, so what would the parent do right now. In this example, the child processes computing 300 primes. 359 00:48:36.990 --> 00:48:43.440 William Cheng: So therefore, it's going to be doing things in parallel, the parent companies that have Colonel, there's no dead child process. So therefore, the Parent PLUS is going to go to sleep. 360 00:48:44.340 --> 00:48:52.560 William Cheng: Okay, so now the contact center over here you know it STORE INSTEAD OF process control, blah. And now we're going to have the powder that the parent process also sleeping side carnal. 361 00:48:53.370 --> 00:49:00.660 William Cheng: That the child process over here is going to continue to run. It's going to compute what a 300 primes. When it finishes computer 300 prize. What we would do. 362 00:49:01.320 --> 00:49:12.690 William Cheng: Is I don't know if you remember that the prompts program at the end, what you will do is that they will call return it will execute return zero when you return from the main function, you will call the exorcism call. So in this case, it will be x is zero. 363 00:49:13.890 --> 00:49:24.390 William Cheng: Right because may is going to return zero. And then what the startup function will do is that it will take the return value of May and call exit on it. So they will call exit zero. So again, this x is zero is not the same as x and y. 364 00:49:24.600 --> 00:49:27.330 William Cheng: As far as the child processes is concerned, this address space is gone. 365 00:49:28.620 --> 00:49:36.750 William Cheng: Okay, so therefore this x zero, we here will be the code inside the primes program. And since again exit is the system call. So in this case, it will come into the kernel. 366 00:49:36.930 --> 00:49:45.690 William Cheng: And then we're going to say the child process contacts inside the contact area over here. And now when we come inside the cardinal, we noticed that the parent is actually waiting for this child process to die. Why 367 00:49:46.110 --> 00:49:49.650 William Cheng: I should correct myself the parent is waiting for HR process to die. 368 00:49:50.040 --> 00:49:58.080 William Cheng: Okay, in this example, you know, the parent passes only has one child process. So, therefore, in this case where the only child classes today. So in this case, we're going to die inside the Colonel. 369 00:49:58.890 --> 00:50:01.980 William Cheng: When we die inside the corner over here. Should we destroy the process control, blah. 370 00:50:02.520 --> 00:50:07.050 William Cheng: Guys. But again, we don't. We shouldn't really do that the parent of the child prices over here. We're going to go into a zombie state. 371 00:50:07.320 --> 00:50:11.640 William Cheng: And in this particular example, as it turns out, the parents actually waiting for it to die. 372 00:50:11.970 --> 00:50:17.790 William Cheng: Okay, and there's no other child process. So, therefore, this case, what we will need to do is that we need to copy the return exit code over here. 373 00:50:18.060 --> 00:50:30.330 William Cheng: And then we're going to set it up so that when you return over here you will return the Mexico and also returned the process ID. Okay, so why don't you return the exit code for the trial process and the process ID, then we could actually free up this data structure. 374 00:50:31.530 --> 00:50:41.010 William Cheng: Okay, so again, in this particular example, we can free up this data structure because in this case we're going to return right away because the parents. Parents just happened to be waiting for the child process to that. 375 00:50:41.460 --> 00:50:50.490 William Cheng: Okay, what are the parent process actually doing something else you're executing the other CPU wanting that case child processes will turn into a zombie process. And next up with a parent process call 376 00:50:50.970 --> 00:50:58.290 William Cheng: What does and what we need to do is that we need to adjust the colonel data structure to remember that the child process right now. It's a debt process for the parent 377 00:50:59.130 --> 00:51:09.270 William Cheng: Okay, so therefore next time when a parent process called the way system call you will come into the kernel and nobody will see that the child processes there at that point it will return the exit code. He will return the process ID. And at that point. 378 00:51:09.480 --> 00:51:11.370 William Cheng: The parent process can actually delete the data structure. 379 00:51:12.480 --> 00:51:20.220 William Cheng: That. So anyway, so the picture that I draw over here is that, you know, in this example, we can free up this data structure. But in general, we can't really free up the data structure. 380 00:51:20.430 --> 00:51:26.580 William Cheng: But what we know for sure is that we can actually free up the, you know, we can free up the the address space for the child program. 381 00:51:27.660 --> 00:51:31.980 William Cheng: Okay, because as soon as the child program make the exorcism call you come inside the colonel over here. 382 00:51:32.190 --> 00:51:38.730 William Cheng: We don't know whether we should keep the process control blog around or not. In this example, we will have freed up another example. We won't be able to 383 00:51:38.940 --> 00:51:43.260 William Cheng: But we know that we have no need for the address space, right, why don't we need to address space anymore. 384 00:51:43.680 --> 00:51:52.080 William Cheng: Well, because we don't execute any more code right you make the exorcism call you coming out of Colonel your child processes dead when your processes that you don't need the address space, right. 385 00:51:52.440 --> 00:52:04.140 William Cheng: Again you address space stores the code for your program, you know, in the code anymore you address based or your global variable local variable Hebrew, but you don't need any of those. So therefore, when you make your exorcism call you can actually free out the address space right away. 386 00:52:05.340 --> 00:52:15.480 William Cheng: Okay, the process hundred blog is a little more complicated, but you can free IP address space right away. The process control block depends on whether the parent as already retreated has has has retrieved the ethical are now. 387 00:52:16.470 --> 00:52:24.330 William Cheng: There are. So in this example, the parent just happened to be waiting is that occurred. Also, in this case, you can copy all this data structure back or you can actually free up this data structure. 388 00:52:24.510 --> 00:52:32.070 William Cheng: And the parent over here is going to return with the process ID of the dead child process and you can compare that against that you see there the same so you will proceed around your code. 389 00:52:32.730 --> 00:52:40.080 William Cheng: Okay, so therefore, in this case, you will continue like this you can notice that in the parent process over here in the contacts area things going to go out of sync. 390 00:52:40.290 --> 00:52:48.270 William Cheng: And the parent process will continue to execute over here. I use the black arrow over here to say that, you know, we're done with this example and apparent processes will continue to execute code over here. 391 00:52:48.840 --> 00:52:56.310 William Cheng: Okay, if your parent process over here, you're logging show what it would do is that it will give you the next command prompt to say, hey, maybe you want to enter another command. 392 00:52:57.390 --> 00:52:57.690 William Cheng: Yeah. 393 00:52:59.340 --> 00:53:07.800 William Cheng: All right, so, you know, this is, you know, so this is an example. So again, this example is pretty confusing because many, many different things going to happen. 394 00:53:08.070 --> 00:53:14.040 William Cheng: And then, you know, many things inhabit depends on whether the parent is doing something or now with the child take how long to do something. 395 00:53:14.430 --> 00:53:24.450 William Cheng: Whether whether there are other child processes that are running. How many CPUs. Do you have. So again, very, very easy how complicated example all done in a few slides. Okay. All right. 396 00:53:25.980 --> 00:53:27.330 William Cheng: A little more on the system call 397 00:53:27.540 --> 00:53:38.610 William Cheng: They are the system called over here is a so interface between the user and the Colonel, we talked about before, we want to control entry into the operating system kernel. Right. So therefore, the only way you can come into the kernel. 398 00:53:38.850 --> 00:53:43.170 William Cheng: In this case, either you cause an error or you're going to use a system called interface coming out of Colonel 399 00:53:44.490 --> 00:53:49.680 William Cheng: Okay, so, you know, the set of system called define what kind of operating system you have 400 00:53:50.370 --> 00:54:02.460 William Cheng: You guys are what are we talking about unisys them. So what does the UNIX system right a UNIX system has a set of system called interfaces. So by specify what it was by specifying what the system interfaces are you define an operating system. 401 00:54:03.840 --> 00:54:08.160 William Cheng: Okay, so a collection of all the system call. That's how you define an operating system. 402 00:54:08.820 --> 00:54:13.650 William Cheng: Okay, so therefore the system call you know face is very, very important if you want to use a particular operating system. 403 00:54:13.800 --> 00:54:23.940 William Cheng: You need to learn all the system. Paul interface right then you know how to use that operating system right so yeah well you to learn the system interface you did you learn a very, very carefully to understand exactly what every system call is doing. 404 00:54:24.780 --> 00:54:34.380 William Cheng: Now, alright. So, again, a system called over here. So what is the system call right so inside the system over here, typically a system called is a very, very small function. 405 00:54:35.040 --> 00:54:40.200 William Cheng: Okay, so in the middle of a system called over here is going to be the machine instruction again for intel is going to be in 406 00:54:40.620 --> 00:54:50.400 William Cheng: Zero X to eat right so that's trying to change again or four other CPU is some other instruction over here. So a system called is a thin wrapper around a trap machine structure. 407 00:54:51.210 --> 00:54:56.790 William Cheng: Okay, so what we do over here is that is that every system called over here is going to set up for the trap invoke the travel you 408 00:54:57.120 --> 00:55:04.260 William Cheng: Know if it's in zero x to eat you cause of salt. He cause of software interrupt you tap into the ordinances and now the ordinances that will save your contacts. 409 00:55:04.500 --> 00:55:15.270 William Cheng: And then what we'll do is that it will it will sort of figure out what do you want the organism to do when it finished doing it the way you would do is that it will return from the trap and then what you will do is that you will return for the system call 410 00:55:17.550 --> 00:55:24.210 William Cheng: Okay, so that's one of the characteristic of a system called every system calls very, very simple. It's a thin wrapper around a trap machine instruction. 411 00:55:24.390 --> 00:55:32.640 William Cheng: You set up for the trap you invoke for the trap and now you transfer control into the Colonel. The Colonel is going to do its thing when it returns you return from the system call 412 00:55:33.930 --> 00:55:35.880 William Cheng: Okay, so every system calls like that. 413 00:55:37.350 --> 00:55:44.610 William Cheng: Okay. What if the system called the colonel cannot you know perform the job that it wants to do so in that case what it will do is it'll return an error to you. Okay. 414 00:55:44.880 --> 00:55:50.490 William Cheng: The way that this is returned to you in Unix and Linux is them is going to return to you as a global variable. 415 00:55:51.060 --> 00:55:55.710 William Cheng: There. So for example, this example we have a really see what a racism called So later on we're going to talk about it. 416 00:55:55.920 --> 00:56:03.660 William Cheng: In the racism, call it has three argument. The first one is called FD and followed by a buffer and followed by a proper size. If you read a man pages of right 417 00:56:03.900 --> 00:56:09.900 William Cheng: You will say that. So again, the way you read the man pages for the right, you're going to say, man, followed by two followed by right 418 00:56:10.350 --> 00:56:15.240 William Cheng: Because it turns out there are many, many right you know a program inside your Linux system. 419 00:56:15.690 --> 00:56:22.410 William Cheng: So when you try to look at the men page for right. You want to tell you know one to you. Yo yo yo command shell which man page you want to look at 420 00:56:22.890 --> 00:56:27.840 William Cheng: So in this case, the one that's about system called they are usually in Section two of the main pages. 421 00:56:28.110 --> 00:56:38.190 William Cheng: Yeah, so therefore you just say men to right over here to say I'm interested in the man pages for right but please give me the main page in Section two of your managers. 422 00:56:38.970 --> 00:56:46.890 William Cheng: Okay, so there are two different syntax men to write on then minus as to right. So, you know, give me, you know, the main pages for right in section to 423 00:56:47.490 --> 00:56:53.010 William Cheng: That. So if you look at that, you will see what all these arguments are. And most importantly, it will tell you. 424 00:56:53.580 --> 00:57:04.260 William Cheng: You know how the openness to them tell you that there's an error. Okay, so in the racism call when there's an error, it will return you with the value minus one and they will also tell you what are the possible error numbers are 425 00:57:04.770 --> 00:57:15.330 William Cheng: Okay, the error number is actually a global variable. So, therefore, we hear you can actually use the, the goal variable vehicle era number, er, and oh, and this is just an integer, it will tell you what Eric is 426 00:57:15.990 --> 00:57:23.790 William Cheng: That one of the air over here. Maybe so. So I guess when you are doing your one on one. You need to be able to distinguish between whether you will 427 00:57:24.180 --> 00:57:37.650 William Cheng: Try to open a file, maybe the file doesn't exist. Or maybe you don't have permission to access the file. Okay. So again, these two conditions are completely different. Right. One is that the file doesn't exist. The other one is that the fall exists, but you don't have permissions to it. 428 00:57:38.880 --> 00:57:42.540 William Cheng: Okay. So, therefore, what you're supposed to do is that you need to print a correct error message. 429 00:57:42.960 --> 00:57:50.970 William Cheng: Okay. The example over here. So to show you that, you know. So one way is to print the air number out of the air number over here. So, so what if this number is 42 430 00:57:51.570 --> 00:57:54.750 William Cheng: Okay, you say air 42 nobody knows what that means right so you shouldn't do that. 431 00:57:55.080 --> 00:58:07.680 William Cheng: Okay, so one thing that you should do is that you should look at the main page for a function called P error. If you use the PR function. What it will do is it will print it. Error number specific error, it will prevent Aaron number specific error message. 432 00:58:08.820 --> 00:58:13.890 William Cheng: Okay, so in that case what you should do is that with your walk why you should try to play around with the P error you know function. 433 00:58:14.130 --> 00:58:20.910 William Cheng: Give it a bunch of different argument and see what it does. Okay, and then sort of figure out how you how you're supposed to use P error in case 434 00:58:21.390 --> 00:58:29.730 William Cheng: You know when you fail to open a file. Okay. Another thing that you can do is that when you try to open a file if it turns out open fail, you can simply says I cannot open the file. 435 00:58:30.750 --> 00:58:40.590 William Cheng: Okay, you don't distinguish the case where you know you open the file because it doesn't exist, or whether you don't have access to the file in that case you will print a correct Aramis it used to actually open the file. I don't know why. 436 00:58:40.800 --> 00:58:44.340 William Cheng: I'm just telling you that can open the file And that will be a correct error message. 437 00:58:45.750 --> 00:58:56.190 William Cheng: Right, so you're strongly encouraged to play around with P error or if you don't want to play the piano. You can put a generic correct error message to say, I can open the file And here is the file name, I tried to open 438 00:58:56.580 --> 00:59:00.360 William Cheng: So you ask the user to say maybe, maybe you want to look into it to see why you can open that file. 439 00:59:00.690 --> 00:59:07.830 William Cheng: Okay. And again, that will be totally that's acceptable for warm up one and two, you are just not allowed to print in an incorrect error message. 440 00:59:08.100 --> 00:59:15.750 William Cheng: Yeah. What an incorrect error message. For example, a default doesn't exist. If you put in the Air Base matches to say, I don't have access to the file. Well, then that will be incorrect. 441 00:59:16.020 --> 00:59:23.460 William Cheng: Similarly, if you don't have access to the file but you print an error message to say the file doesn't exist. As it turns out, the file exists. Well, then in that case it's wrong error message. 442 00:59:24.540 --> 00:59:32.430 William Cheng: Right. So whenever you put an error message. He has to be correct. And again, look at the main page. Look at the P arrow function and try to print out a good error message. Yeah. 443 00:59:34.230 --> 00:59:34.740 William Cheng: All right. 444 00:59:36.330 --> 00:59:40.680 William Cheng: Okay, one more thing about the system call over here. So I'm going to briefly explain to the system will be here. 445 00:59:41.100 --> 00:59:56.370 William Cheng: Before I sort of tell you that the address space go from zero all the way to zero, x F, F, F, F, F, F, F, F, as it turns out that truly not very correct. Okay. That's your that's your entire address space, but they are part of your address space that you're not allowed to use 446 00:59:57.420 --> 01:00:12.450 William Cheng: Okay. So as it turns out, you know, for Linux system of a UNIX system. Well, you're allowed to do that you address paces four gigabytes, you're only allowed to use the first three gigabytes of your address space. Okay. The bottom one gigabyte of space. They are reserved to be used by the Colonel. 447 01:00:14.250 --> 01:00:22.200 William Cheng: Okay, so, so I don't know if you have noticed that right. If you haven't, you know, gone through GDP assignment number one over here. Will you try to print out the variable that live inside the staff space. 448 01:00:22.470 --> 01:00:27.030 William Cheng: The address that you're going to get a zero x be F, something, something, something, something, something. 449 01:00:28.530 --> 01:00:40.410 William Cheng: Okay, so. So if you look at the first three gigabyte your address space over here. You start with memory location zero. The last memory address that you're allowed to use is going to be zero, x be F, F, F, F, F, F, F. 450 01:00:41.790 --> 01:00:49.110 William Cheng: Okay, starting with the last gigabyte over here. The first addresses are the kernel is going to be zero FC 0000000 451 01:00:50.010 --> 01:00:58.860 William Cheng: Okay, so this is why when you try to print out the variable inside your stack, they're going to start with zero XP or something because that's the part of the bottom of your address space. Are you allowed to use 452 01:01:00.210 --> 01:01:06.990 William Cheng: That alright so therefore, when you try to make a system called right. So the example that we saw over here is right. Right. You know, use three arguments over here. 453 01:01:07.200 --> 01:01:13.530 William Cheng: When you try to make the racism call we showed you before, you know, for Intel CPU. He IP over here is going to point to 454 01:01:13.770 --> 01:01:24.000 William Cheng: Point to the text like man ESP over here will point to the top of your the the the the top of your stack ABP over here. Again, your, your stack spaces dividing to stack friends. 455 01:01:24.570 --> 01:01:31.860 William Cheng: Where you are, you know, making the right system call again it's a thin wrapper on a machine instruction EDP over here will point to the middle of the stack. Right. 456 01:01:32.880 --> 01:01:40.020 William Cheng: Okay, why do more things that the CPU over here is that the mode of the CPU over here will be in the user mode because you're running the user space program. 457 01:01:41.760 --> 01:01:46.890 William Cheng: Okay, when you make a system called right you're going to trap inside the operating system over here. So, in that case what will happen right 458 01:01:47.190 --> 01:01:50.850 William Cheng: Okay. So in this case, Robin. Is that the way we sort of think about it in this class. 459 01:01:51.210 --> 01:01:59.370 William Cheng: Okay, is that when you execute this code over here. You are a user threat that's running inside your process right process could be a multi threaded process. 460 01:01:59.760 --> 01:02:01.860 William Cheng: So in our simple example. You don't have one threat. 461 01:02:02.490 --> 01:02:10.440 William Cheng: Okay, so if you have one slide over here inside your process. You are a user threat making a racism call and again the racism pauses thin wrapper around our chaplains instruction. 462 01:02:10.680 --> 01:02:17.130 William Cheng: You're going to set up for the trap. When you execute the tribe machine instruction. Boom, you're inside the Colonel. Okay, so what does that look like 463 01:02:18.210 --> 01:02:25.950 William Cheng: Yeah, so we're going to throw this picture over here. When you go inside the Colonel. The Colonel over here. As it turns out, you look just like the user space program. 464 01:02:26.370 --> 01:02:30.450 William Cheng: Now guys, it's just that you're going to be very disappointed because inside the Colonel. It looks just a user space for 465 01:02:30.930 --> 01:02:36.210 William Cheng: The Colonel has a text that grand has, you know, has a data segment right here and also the Colonel. The Colonel stack. 466 01:02:36.780 --> 01:02:47.880 William Cheng: And guy. So therefore, what's going on over here is that will you make the right system call. It's called a system called because you because India, what you're trying to do is that you tried to reach the kernel version of the racism call 467 01:02:48.270 --> 01:02:58.890 William Cheng: Okay, so. Okay, so to imagine that inside the current over here. There's another function is that a kernel. I'm going to call a k, right, which is the kernel version of the Rice's and call it takes three arguments FD buff lane or over here. 468 01:02:59.730 --> 01:03:13.170 William Cheng: Okay, so when you make a user space system call you're going to try to reach the kernel version of the system call. So in this case you trapped inside a kernel and now your user thread changes personality into a kernel thread and now you start executing that a carnal. 469 01:03:14.910 --> 01:03:23.430 William Cheng: guys again for the, for the purpose of this class will you make a system call your user threat simply changes personality and now they become a colonel threat. 470 01:03:24.660 --> 01:03:30.060 William Cheng: Okay. So over here I sort of asked the question over here is that, is this the same threat of execution. 471 01:03:31.380 --> 01:03:37.200 William Cheng: Because, again, we will talk about threat right these are the threats of, you know, these are called him thread there. Also, the sort of execution. 472 01:03:37.800 --> 01:03:45.450 William Cheng: So threat is an abstraction. Okay, so in the beginning we're executing code in the user space program. And now we're coming out of Colonel is this exactly the same threat. 473 01:03:46.680 --> 01:03:54.000 William Cheng: Okay, the answer as far as this causes concern is that the answer is yes. Okay. Also, we asked a question over here is, is this the same process. 474 01:03:54.660 --> 01:04:00.510 William Cheng: What we saw this picture over here we're using exactly the same address space. So therefore, we are the same process. 475 01:04:01.350 --> 01:04:10.110 William Cheng: Okay, so therefore, when you come into the corner where you make a system call you are still the same thread you still run into the same process. The only thing is that your user threat become a colonel threat. 476 01:04:11.160 --> 01:04:12.210 William Cheng: Because of what the colonel threat. 477 01:04:12.870 --> 01:04:19.050 William Cheng: Well guys, the kernel thread is just like the users, right, because to be the over here. It's just an extension of our threat or third we're executing. Is that a user space. 478 01:04:19.290 --> 01:04:24.390 William Cheng: Now in common is that a cardinal is still the same thread is to actually use that same process, but we just call it a kernel threat. 479 01:04:25.110 --> 01:04:31.980 William Cheng: Okay, so what is the real, the real difference right so now we're executing code over here. So instead of the CPU the CPU registered will look like this. 480 01:04:32.700 --> 01:04:40.800 William Cheng: Okay, the CPU mode over here will change to a different mode. Now it's going to be the privilege mall or, you know, in 16 units you will be the kernel mode. 481 01:04:42.330 --> 01:04:49.260 William Cheng: Okay, because remember you six fishing you next. The entire organism in a run and privilege model. So in this case, we're going to say we're gonna call this the kernel mode. 482 01:04:50.010 --> 01:04:57.780 William Cheng: Okay. And instead of CPU. We have VIP SP VP so yeah i p now point to the colonel portion of the address space. 483 01:04:58.410 --> 01:05:02.190 William Cheng: Okay, so he said kernoviae. We also have a kernel staff. What does the carnal cycle where 484 01:05:02.340 --> 01:05:09.810 William Cheng: It looks just like the user space that we're going to start it off with some function over here is going to make another function call is going to make another function call. We're going to build out all the stack frame. 485 01:05:10.050 --> 01:05:23.880 William Cheng: Where you're executing code into the kernel. So now we're executing the function kernel version of the right. It's called K. Right, right. It has the same arguments over here. So, therefore, we also need a stack rank. Okay, why do we need a stack rank because K RY is a see function. 486 01:05:24.990 --> 01:05:34.890 William Cheng: Okay, whenever you try to execute a see function, you need a stock rain because inside that start playing live the function argument. These are the function arguments over here. And if you have local variable. They also live in satisfactory. 487 01:05:35.820 --> 01:05:45.570 William Cheng: Okay, so therefore, instead of Colonel, it has to look exactly the same. We have ESP point to the top of the stack add point in the middle of the stack over here and now we're executing the function K right inside the Colonel. 488 01:05:46.830 --> 01:05:56.460 William Cheng: Okay, so you can see that inside the CPU everything else exactly the same. The only difference is that the CPU mode is different. All these register now point to the colonel portion of the address face. 489 01:05:57.480 --> 01:06:10.080 William Cheng: Okay, so instead of your other stuff over here. What are the other stuff. Stuff. Well, the other step of the process control blog right mentioned before, the process control blogs. The Colonel data structure. So where is it what's inside the colonel portion of the address space. 490 01:06:12.540 --> 01:06:22.230 William Cheng: OK. So again, the colonel over here. It looks very much the same as the users, the user space address. Okay. He has a text document. He has data set where he has 491 01:06:22.890 --> 01:06:34.260 William Cheng: As it turns out, the colonel doesn't really have a heap segment we're going to talk about that a little later, the current also have a stock space. It also has other data structure over here. So what is this other stuff. OK, so maybe this other stuff over here. Look inside the colonel heap. 492 01:06:35.310 --> 01:06:42.600 William Cheng: But the colonel doesn't really need a heat right so so again we're gonna clarify that all that we talked about will go to chapter three. Yeah. 493 01:06:43.380 --> 01:06:49.830 William Cheng: Alright, so, so, so this is the picture for 1241 process. What if you have multiple user processes. 494 01:06:50.550 --> 01:06:58.230 William Cheng: Okay, so in that case the future will look like this. Okay, so this one I show you three different user process. Here's process. A here's process be here's process. See 495 01:06:59.130 --> 01:07:10.470 William Cheng: So each one of them has a different address space where you can see that this picture look a little different from here, they'll get to the bottom here. So we're sort of show you that you know they are you know they're different processes. If I have three CPUs. 496 01:07:11.190 --> 01:07:18.210 William Cheng: I have CPU, number one, number two, number three over here. Every one of them has their own separate VIP s p BP IP IP SVP 497 01:07:18.450 --> 01:07:26.220 William Cheng: Yeah I PSP MVP, I can actually run all these three things simultaneously. Right. Yeah, it will point right here. Yes. People part right here up or I hear 498 01:07:26.430 --> 01:07:34.770 William Cheng: The second CPU. Yeah, it will point right here ESP or here. If you feel right here. And then the third one, yeah I people part right here yes people, particularly people right here. 499 01:07:35.910 --> 01:07:45.390 William Cheng: Okay, so when you have multiple CPU, you will be able to run all this program in parallel. Well, one of them make a system called local Minnesota current also again for this CPU, it will point to the colonel tech stack, man. 500 01:07:45.600 --> 01:07:51.480 William Cheng: And then he will point to the the colonel stack over here. What is the second process over here also make a system called 501 01:07:51.810 --> 01:08:04.620 William Cheng: Why exactly the same thing happen, you will come into the kernel via for the second CPU yeah IP will point to the colonel tech segment ESP over here will point to the currency. So what's important to understand over here is that every user process has a separate Colonel stack. 502 01:08:05.940 --> 01:08:12.870 William Cheng: Okay, well why does that have to be the case. Right. So this way when all these three processes. When they all make system call 503 01:08:13.080 --> 01:08:27.240 William Cheng: They will be able to run inside a colonel in parallel, because all these three policies over here. He IP, they will all point to the colonel tech side man and ESP will point to the top of a different stack and the VP over here will point to the top stack rank of a different stack space. 504 01:08:28.380 --> 01:08:31.860 William Cheng: Okay, so this way all of them over here, there will be able to run in parallel. 505 01:08:34.080 --> 01:08:47.640 William Cheng: OK. So again, this picture is very important understand that when they come inside the colonel over here, they all use a separate stack space over here inside the Colonel. Okay, but they all share exactly the same kernel version of the current tech stack, man. 506 01:08:48.090 --> 01:08:53.220 William Cheng: Because the Colonel, the segment over here, they will have all the kernel version of all the system calls 507 01:08:54.810 --> 01:09:02.190 William Cheng: Whereas over here. So, to draw them in tiny area of here, but in this case it's pretty pretty big. It has the entire current over here. All the code for your kernel, they're all sitting right here. 508 01:09:03.000 --> 01:09:08.910 William Cheng: Okay, so when all these three processes. When they all make system called they will be able to execute concurrently. He's not a carnal. 509 01:09:09.480 --> 01:09:19.530 William Cheng: Okay, and every CPU will point to a different stack. And then again, they're all these yellow stuff over here or difference now every one of them have their own separate process control blog. They all have all their data structure. 510 01:09:19.860 --> 01:09:27.360 William Cheng: They need to maintain the context for that particular process, all that other stuff we saw before. Now, they all merged together inside of inside of big picture. 511 01:09:28.350 --> 01:09:33.720 William Cheng: That. So in that case, when you come into the kernel. They actually they won't get a separate address space inside the Colonel. 512 01:09:34.140 --> 01:09:40.950 William Cheng: Okay. So, is that correct, I mean the colonel is very, very powerful. The Colonel is actually not confused. It knows that when you come into the kernel. He knows who you are. 513 01:09:41.340 --> 01:09:54.630 William Cheng: Okay. He knows whether you belong to the process or process be a policy and then when you come into the kernel. There's only one kernel tech segment. So the way you can sort of think about is that inside the Colonel, there's only one address space that's shared by all the colonel threats. 514 01:09:56.160 --> 01:10:07.500 William Cheng: Okay, so you know way the colonel is one giant process that are shared by all the colonel threats. Right. And what do you get when you get a cut off there from when the user through our program. Make a system called the companies that are currently become Colonel threats. 515 01:10:08.700 --> 01:10:20.730 William Cheng: Okay, so instead of Colonel, you know, you can sort of think about is just one giant process and this giant process is a multi threaded Colonel process because all these threads over here. There'll be able to run run in parallel inside, Colonel. 516 01:10:22.080 --> 01:10:32.340 William Cheng: Okay, so even though you use your space program, you don't have multi threaded program now maybe every Pocahontas only on one thread. When you come inside the Colonel. The Colonel has to be a has to support multi threading. 517 01:10:33.780 --> 01:10:38.070 William Cheng: Right because inside the Colonel, we only have one tech stack, man. And we have all these other 518 01:10:39.000 --> 01:10:46.050 William Cheng: Stack, so he can run multiple Colonel threats and parallel. Right. So again, we're going to talk about threading a little more in colonel in chapter two. 519 01:10:46.590 --> 01:11:01.290 William Cheng: Guys, so now we finished process. Other the introduction to process the threads in chapter one and the next thing I'm going to talk about is the file system. Yeah. Alright, so this is a good time to break and next time we're going to continue by looking at Chapter one will look at it faster.