WEBVTT 1 00:00:02.399 --> 00:00:12.480 William Cheng: Hi. Welcome to lecture for so starting with this lecture I'm going to, you know, sort of record a lecture into three different videos because I'm suspecting that 2 00:00:12.929 --> 00:00:19.200 William Cheng: When the video gets too long the audio transcript doesn't get generated by zoom. Okay, so I'm going to sort of try to 3 00:00:19.800 --> 00:00:26.190 William Cheng: Do with the shorter video maybe on the order you know 4045 minutes somewhere around there. Yeah. 4 00:00:26.790 --> 00:00:35.670 William Cheng: Alright so warm up to his do this Tuesday. If you have code from previous semester, please don't look at them. Don't copy them best to get rid of it. 5 00:00:36.180 --> 00:00:44.940 William Cheng: Grading gamma is the only way we'll gray. We can only great on graders 32 bit Ubuntu 16.04 systems. So you shouldn't really install any extra packages. 6 00:00:45.480 --> 00:00:55.110 William Cheng: Because if you asked the greater to install them, the greater will have to refuse and you need to also run your code against the grading guidelines on this, you know, on your system. 7 00:00:55.770 --> 00:01:01.770 William Cheng: So again, the grading gamma is part of the spec. If you make a submission. Make sure you understand the ticket. 8 00:01:02.160 --> 00:01:11.970 William Cheng: It makes sure you'll find him as corral your file size makes sense and also look at the timestamp, they will tell you when the, you know what, when, when did my server. 9 00:01:12.390 --> 00:01:23.460 William Cheng: Receive your submission. If you have the ticket, the ticket is proof that your that your submission has made it to my server. Okay, so you don't need an email. All you need is the ticket. Okay. 10 00:01:23.970 --> 00:01:32.430 William Cheng: And as soon as you make your final submission, make sure that you should follow the verify your submissions, the submission procedure immediately. 11 00:01:32.970 --> 00:01:36.240 William Cheng: To make sure that everything you have submitted is everything you want us to grade. 12 00:01:36.780 --> 00:01:47.040 William Cheng: Okay, so does include going through every step inside the grading guidelines. Check your Readme file, you know, go step by step and make sure that you know everything's perfect yeah so 13 00:01:47.850 --> 00:01:54.540 William Cheng: The reason, again, is that due to my fearless policy after the submission deadline, there's really nothing I can do. Okay. 14 00:01:58.170 --> 00:02:03.060 William Cheng: All right. So just a reminder in lecture one I mentioned that there's free modification after the deadline. 15 00:02:03.330 --> 00:02:14.700 William Cheng: But this only applies to the day after the submission deadline and not later okay and only the first three lines of changes are free. You can make additional changes going to cost us is not costing you points. Okay. 16 00:02:15.060 --> 00:02:26.130 William Cheng: And also, again, if you read the webpage, very, very, very, very carefully, it will tell you that making modification does not change the submission timestamp and we use a submission timestamp to determine whether you have 17 00:02:26.460 --> 00:02:37.920 William Cheng: You know, early submission extra credit or late submission penalty. Okay. And please. Also, if you want to make changes, you must follow the procedure on the projects web page, and then, you know, 18 00:02:38.310 --> 00:02:42.240 William Cheng: Send me send me the required email don't send it to anybody else. And me the email. Yeah. 19 00:02:43.020 --> 00:02:52.110 William Cheng: If for some reason you cannot finish one more point you should try to get as many partial credit points as you can answer it. Again, you need to remember that the greater has to follow the grading guidelines. 20 00:02:52.560 --> 00:02:59.910 William Cheng: So for example, if you only have part a working and you'll be just doesn't work. And these are my words that if you run it, you're gonna you're going to crash. 21 00:03:00.300 --> 00:03:08.400 William Cheng: You're going to end up losing a lot of points in the minus point section. So one thing that you can do is that you can tell the greater to skip everything in warm up one 22 00:03:09.390 --> 00:03:17.100 William Cheng: Okay, so, so I'm not not not not part of the woman won the popular the warm up. Why so if you give yourself so so put the README file has a self grading section. 23 00:03:17.370 --> 00:03:23.910 William Cheng: If you give yourself zero in any of the data that the grading IDA, the greater will not run those tests. 24 00:03:24.240 --> 00:03:34.200 William Cheng: Okay. I mean, you're not going to get, you're not going to get points for that. But again, if you do that, the greater when I run this command for the greater will not run those command. If it turns out there's some additional failure, you won't lose, lose 25 00:03:34.980 --> 00:03:46.470 William Cheng: Lose those points. Okay. If it turns out that greater run some commands. And it turns out that he crashes or something like that. The greater has followed the grading Ghana and start deducting points. Okay, so if you're confused about this, send me email. 26 00:03:47.820 --> 00:03:54.390 William Cheng: Alright, so, you know, I guess after today's lecture, you can start writing a little bit of code about warm up to 27 00:03:54.900 --> 00:03:58.890 William Cheng: Then you won't you won't really you know know everything about warm up to until a little later. 28 00:03:59.520 --> 00:04:07.140 William Cheng: But, but in order for you to get ready for warm up to what you should do is that you should copy some of the code that that that's on the lecture slides and try to run that 29 00:04:07.350 --> 00:04:17.550 William Cheng: Especially run them under the debugger, so you can understand how multi threading works okay multi threading using a debugger is kind of weird. Okay. Because you have you have many things supposed to run in parallel. 30 00:04:17.970 --> 00:04:22.740 William Cheng: So we get a breakpoint. You know, we get a breakpoint inside one threat. What other threats doing 31 00:04:23.220 --> 00:04:28.980 William Cheng: OK. So again, you need to you need to start running it. Get a feel of what they're doing. So this way you understand 32 00:04:29.310 --> 00:04:33.240 William Cheng: You know what happens when you debug your code under multithreading okay so if you think 33 00:04:33.600 --> 00:04:41.400 William Cheng: Debugging have a single threaded code and warm up. Why is going to be tricky multi threaded code devalue model theoretical, it's got to be a lot more complicated. 34 00:04:41.850 --> 00:04:49.650 William Cheng: Okay, so therefore you're going to start getting used to it as early as possible. Yeah. All right. Again, as usual, if you have a question, send me email. 35 00:04:50.250 --> 00:05:05.370 William Cheng: And last week I asked you to do GDP assignment number one if you haven't done it yet, you should do it as early as possible. Again, it's very, very important to understand the nature of our pointers, the nature of addresses and be able to draw pictures and make sense out of it. Okay. 36 00:05:07.530 --> 00:05:15.870 William Cheng: All right. Um, so now some over here, we, you know, briefly go through the first function in the piece of our library is copies to recreate it creates a bunch of threads. 37 00:05:16.260 --> 00:05:22.890 William Cheng: The code over here, create 100 threads and the one that starts in them. The main function. We call it the main threat now. So if you call this 38 00:05:23.400 --> 00:05:26.670 William Cheng: Piece or create 100 times you're going to end up with 101 threat. 39 00:05:27.450 --> 00:05:38.310 William Cheng: Okay, so, so one of the most important thing I need to understand in peace or create is that as soon as you finish calling P3 create if it returns a value just say the threat creation was successful. 40 00:05:38.700 --> 00:05:48.390 William Cheng: The threat can actually start running at any time. Okay, you have absolutely no control when these threads going to start running. Okay, so for example over here. I'm going to end up with 101 threads. 41 00:05:49.680 --> 00:05:53.490 William Cheng: Okay, so we should are running. Okay, the answer is, I don't know. 42 00:05:53.910 --> 00:06:02.130 William Cheng: Okay, I don't know, is the correct grammar, this is Greg answer if you have 20 different CPUs, you know, maybe some of these threads are running, which ones I have no idea. 43 00:06:02.340 --> 00:06:09.390 William Cheng: Maybe the thread that you create the at the end will be the first one to run. Maybe the thread that you have created the beginning will run first I have totally no control. 44 00:06:10.230 --> 00:06:19.770 William Cheng: Okay, so some people will try to control it. Okay, my recommendation to you is not to try not try to control that, but try to understand what it means when we create a bunch of threats. 45 00:06:20.160 --> 00:06:27.540 William Cheng: Okay. You need to learn to think about all these fields going to run in parallel. And then how do you write code to make sure that they interact with each other perfectly 46 00:06:28.590 --> 00:06:38.970 William Cheng: Okay. So remember when you write, you know, when you write a program like this you are writing the code for your program. So all these threats belong to you. Okay, it's your job to make sure that the threads work nicely together. 47 00:06:39.420 --> 00:06:48.780 William Cheng: Okay, don't assume that the threads going to do something you have to make sure you have to write your code in such a way to guarantee that these threads or interact with each other correctly. 48 00:06:49.380 --> 00:06:58.560 William Cheng: Okay, so that's the purpose. I'm going to spend, you know quite a bit of time on Chapter two, to make sure you understand that abstraction of threads, how they interact with each other. 49 00:06:59.010 --> 00:07:02.250 William Cheng: Okay. Later on chapter floor was a little girl is going to show you how to implement threads. 50 00:07:03.180 --> 00:07:12.540 William Cheng: But if you think about to go on interviews and stuff like that. It's much, much more important that you understand the abstraction of threads. And when you use the P3 library function. 51 00:07:13.410 --> 00:07:20.640 William Cheng: We will we use these pieces at every function to to to to write your threading code, you got to be able to understand exactly what they're doing. 52 00:07:21.210 --> 00:07:25.860 William Cheng: Okay, that's a lot more important than implementing so let's. Okay. Alright. 53 00:07:26.850 --> 00:07:36.870 William Cheng: So let's take a look at it a little more example here the last argument over here in the previous slide, I just called him argument. I don't say what they are. Right. So I sort of mentioned that maybe you can pass I or something like that. 54 00:07:37.290 --> 00:07:42.750 William Cheng: So one thing that you can do is, I will be here and get the last arguments of uptight boy star. What if I pass it over here. 55 00:07:43.230 --> 00:07:52.140 William Cheng: Okay, so, so, so what you need to understand is that, you know, the third argument over here is the first procedure. So the first procedure over here is server right server is a function that returns voice star. 56 00:07:52.350 --> 00:08:00.330 William Cheng: And take a single argument voice da and by now since you've been doing warm up one you know that you can easily typecasting integer into voice start because they are exactly 57 00:08:00.870 --> 00:08:11.250 William Cheng: Exactly compatible. They're both 32 bit integers. Yeah. So I can take the number. I'll be here in podcast voice or when I tried cast, nothing really happens. Right. So in this case I 58 00:08:11.580 --> 00:08:19.590 William Cheng: The first time when I call this function is equal to zero. So zero is going to be the the you know the the the argument to the first procedure. 59 00:08:20.160 --> 00:08:27.120 William Cheng: There. The first argument. The first picture is right here. This one is art is a high voice or so, at the time, will you call P three create 60 00:08:27.690 --> 00:08:35.220 William Cheng: So this is the piece or library calls right so inside Python library what he will do is that it will create a threat and running that threat anytime 61 00:08:36.180 --> 00:08:42.360 William Cheng: Okay, well, that's where I started running it start running at the beginning of this function. And again, when this function return that thread dies. 62 00:08:43.110 --> 00:08:51.000 William Cheng: Yeah, so when the threat start running. What is the argument over here, what the argument over here is the fourth argument that you use the copies or create 63 00:08:51.330 --> 00:08:55.740 William Cheng: There. So if I pass zero over here and and high pass the voice star, what is art. 64 00:08:55.950 --> 00:09:07.230 William Cheng: What are these going to be exactly zero right so if I type cast it back to an integer. So in this example, we're going to take art I typecast it back to integer and k. So, what is going to be k, what k is going to be equal to zero. 65 00:09:08.790 --> 00:09:19.230 William Cheng: Okay, so, so, so next time when I call this function. The second time is going to equal to one. And now I'm going to execute exactly the same code. So basically, you know, all my thread, they can be running exactly the same function. 66 00:09:19.860 --> 00:09:27.930 William Cheng: Okay. So in this example, every time when I create a new thread the ARG will be different. The first one will be zero. The second half will be one, the third time will be two and all the way to 99 67 00:09:29.160 --> 00:09:35.820 William Cheng: Okay. All right. So. So on the right hand side over here, I sort of joy you the address. So what is the address is going to look like when I have multiple threads. 68 00:09:36.570 --> 00:09:44.040 William Cheng: That. So remember that all the servers that belong to the same process. That'd be on to the same process they belong to the same address space they will use exactly the same address space. 69 00:09:44.190 --> 00:09:53.250 William Cheng: Okay. So in this case, they will have this they will be sharing the same text admin, there will be sharing the same data segment. They will be sharing the same BSS like, then they will be sharing the same 70 00:09:53.670 --> 00:10:00.900 William Cheng: Into the same keep segment. Okay. The only the difference between all your thread is that every threat will get their own stack. 71 00:10:02.040 --> 00:10:09.270 William Cheng: Okay, so. So in a way, every three, I will get their own stack space, right. Get the stack space. It's pretty big at the beginning or you use the bottom part your stack space. 72 00:10:09.480 --> 00:10:15.120 William Cheng: And we call function and we make function call the top of the stack of here's gonna go up and down like crazy because you're making a function called 73 00:10:15.600 --> 00:10:17.760 William Cheng: Yeah. So let's take a look at this example over here. 74 00:10:17.970 --> 00:10:29.220 William Cheng: The red arrow over here is your tip pointer, the IP point to the CO. Where is this co while the code is in the text admire him as a co sitting right here, over here, so yeah I POV is actually pointing to co right here. And this is going to be the code that you execute 75 00:10:29.760 --> 00:10:39.810 William Cheng: Now, so at this point you are running the main threat right so the main threat has his own stack. So here is the stack space for the sack space for the main threat. 76 00:10:40.380 --> 00:10:43.860 William Cheng: That means that instead of slack. So there's again there's there's going to be many, many stack rank. 77 00:10:44.070 --> 00:10:51.570 William Cheng: The bottom one is going to be the one correspond to main, main is going to call some function and that function is I'm gonna call star server and start servers, right here. 78 00:10:52.410 --> 00:10:56.340 William Cheng: Okay, so again inside the stack frame this function argument and local variable. 79 00:10:56.490 --> 00:11:02.010 William Cheng: The function argument over here I have no function argument I have local grab. Well, I have two of them. One is called thread you have what's called I 80 00:11:02.160 --> 00:11:10.800 William Cheng: So here is threat. I mean I you know I didn't really draw a box because when we don't really care about it i over here at the time when you call piece or create the first time I go to zero. 81 00:11:11.760 --> 00:11:23.370 William Cheng: So therefore, the way you know so. So this is what the address is going to look like inside the stack space for the main threat the top stack frame is going to correspond to start server and is that the star server I over here is going to be go to zero. 82 00:11:24.210 --> 00:11:30.510 William Cheng: Yeah, so will you call piece or create your inside the library and. And again, just want to stress, stress this point again. 83 00:11:30.780 --> 00:11:42.060 William Cheng: P3 library can be implemented in the user space and the, sorry, the threat can be implementing user space can be employees that occur. And also, we don't really know exactly what's implemented, we only talked about the abstraction right here. 84 00:11:42.420 --> 00:11:51.600 William Cheng: Okay, so we called Peter create your in the piece or a library. The library. What it will do is that it will create a new thread. It will also create a stack space for the new threat. 85 00:11:52.260 --> 00:11:55.140 William Cheng: Okay, so where would just create the stack space for the for the new threat. 86 00:11:55.410 --> 00:12:05.700 William Cheng: Right, if you look at the address space over here, the obvious places right above the current stack space, right. So the current stack space. So let me the clutter this a little bit. The current stack space is right here. 87 00:12:06.420 --> 00:12:16.170 William Cheng: Okay, so what I will do that will create another stack space right on top of it. And this one is for child thread number zero right chapter number zero over here. This is going to be the tax base. 88 00:12:16.500 --> 00:12:20.370 William Cheng: Again, what is the use of with a snack space instead of stack space. I'm not going to be a stack. 89 00:12:20.670 --> 00:12:27.330 William Cheng: Going to be a stack frames the bottom stack frame is going to correspond to the server function. Right. So the ceremonial Olivia is going to be at the bottom over here. 90 00:12:27.510 --> 00:12:37.080 William Cheng: And instead, the server function over here lives the function argument and local variable, the function argument over here is art. Right. So are these right here, the local variable is key, right here. 91 00:12:38.310 --> 00:12:45.630 William Cheng: Okay, so when your co star running arc has been set up already. And now the next thing that you need to do. So you need to execute a code say k equals to arc 92 00:12:46.710 --> 00:12:57.480 William Cheng: Okay, so therefore the picture look like this. Then here is, you know, over here. Here is the stack space for the child threat. Okay. And then at the bottom over here. It's going to be 93 00:12:57.900 --> 00:13:02.550 William Cheng: The stack rank for the server function right the first procedure is just a function call. So over here. 94 00:13:03.060 --> 00:13:07.470 William Cheng: You know, here's the Stack Overflow for server. There is there is a function argument, which is our right here. 95 00:13:08.160 --> 00:13:15.900 William Cheng: Okay, so at the time when you call Peter create what the piece or a library will do is that they will prepare the stack space and set up the bottoms that crap. 96 00:13:16.650 --> 00:13:28.110 William Cheng: Okay. It will also copy the fourth argument into the function argument right here right because over here at the beginning. Over here is equal to zero. Right over here says i equal to zero right here. Okay, the Argo. We hear a washing RB 97 00:13:28.590 --> 00:13:35.640 William Cheng: Right. So yeah, all that happens over here, we called Peter Korea is that the fourth argument over here is going to get copy into art. 98 00:13:36.510 --> 00:13:46.020 William Cheng: Okay, so, so, so, how to do that. Right. You take the fourth argument, which is i right here I has the value of zero and then you copy into art. So now our will also be equal to zero. 99 00:13:49.170 --> 00:13:54.180 William Cheng: Okay, so. So again, it's very important. So, by the way, why do I draw to red arrow, arrow right here. 100 00:13:54.960 --> 00:14:03.480 William Cheng: Okay, because from this point on, I will have to thread running two different CPU simultaneously the main thread will continue to call Peter create and now i equals two, one 101 00:14:03.720 --> 00:14:14.820 William Cheng: What the child through egg created over here is the Chancellor running all the answers. I don't know. Okay, if you have extra CPU, it might be running already if it runs at the time when you start running our gaze equal to zero. 102 00:14:15.420 --> 00:14:27.660 William Cheng: Right, because this valuable to zero I copy them go to zero over here. So now when the child has started or equal to zero because that's what piece or create does it create a stack space is set up the stack rain and now the Charter will be ready to run in the second CPU. 103 00:14:29.040 --> 00:14:29.310 William Cheng: Okay. 104 00:14:30.630 --> 00:14:39.510 William Cheng: All right. What if the mainstream over here or is it okay if we only have one CPU, we can only do one thing at a time. If you have multiple CPU can do multiple CPU. I mean, we're on the slack and only job one 105 00:14:40.440 --> 00:14:47.460 William Cheng: Job to a long way. So, so, so let's say that you know the, you know, the second, third over here is ready around but for some reason we're gonna 106 00:14:49.140 --> 00:14:56.520 William Cheng: We're gonna make it go to sleep. Then while the main thread over here, continue to call Peter create and now is equal to one, right. So now over here. 107 00:14:56.760 --> 00:15:01.350 William Cheng: Inside the Star Trek or Star server I over here is going to be equal to one. OK. 108 00:15:01.530 --> 00:15:12.720 William Cheng: And now we call Peter create. So again, and this time. What's going to happen. Right. You're going to create a new stack frame over here and now i equals two, one. So I'm going to copy this i over here to the art. So this article here will equal to one. 109 00:15:14.190 --> 00:15:24.270 William Cheng: OK. And now if I again if I have another CPU and other CPU will be running the server threat over here. So in this case, I don't know if you can actually see it over here I have two arrows over here pointing at 110 00:15:24.450 --> 00:15:30.660 William Cheng: The first instruction inside server. So if I end up having three CPUs, all these three things can be running parallel 111 00:15:32.100 --> 00:15:35.820 William Cheng: Then, so again, when I'm finished doing that it will look like this right side over here. 112 00:15:36.240 --> 00:15:38.850 William Cheng: So, so this figure over here, this will be one this will be zero. 113 00:15:39.120 --> 00:15:44.940 William Cheng: And now, you know, the main thing I will continue to go over there and do the next time and now is going to equal to two. 114 00:15:45.120 --> 00:15:53.460 William Cheng: And if you call pizza. Great. Again, I'm gonna you know it with creating another stack space and over here are will be equal to two and then we can keep creating stack space over and over and over again. 115 00:15:54.960 --> 00:15:58.950 William Cheng: Alright, so again, what you need to understand over here and this code that you need to use your imagination to 116 00:15:59.310 --> 00:16:11.340 William Cheng: Imagine that. If I create 100 healthcare over here. I'm going to end up with 101 stack space. If I have 101 CPUs that are available to me, I will be able to run all them in parallel. 117 00:16:11.610 --> 00:16:19.110 William Cheng: Right. So in that case, what will be all the CPU to be doing right every CPU have their own separate VIP. So the IP may be executing different instruction. 118 00:16:19.380 --> 00:16:21.000 William Cheng: And then what about ESP 119 00:16:21.210 --> 00:16:29.700 William Cheng: Right, if you know is that a server code over here. They make other function call. So for that particular stack space. And I'm going to end up the top of the stack the stack over here. 120 00:16:29.850 --> 00:16:34.620 William Cheng: Is going up and down, it's going to make function call is going to be simple function is gonna go like crazy what another 121 00:16:34.950 --> 00:16:44.190 William Cheng: Might be sq is a different piece of code independently. So therefore, for another CPU the IP will point to a different space, a different spot inside the text that man. 122 00:16:44.430 --> 00:16:51.510 William Cheng: While the stack segment. This is it. Yes. People want to appoint the top of their stack rank a BP work will be pointed to top the middle step right 123 00:16:52.920 --> 00:17:02.670 William Cheng: Okay, so all these 101 CPU that can be. You can be sharing the same address space together or they can be sharing this the address space simultaneously. 124 00:17:03.360 --> 00:17:05.070 William Cheng: Okay, so this is a big mess right 125 00:17:06.060 --> 00:17:12.240 William Cheng: Okay, so, so it's important for you to understand what the speak mess look like. Okay. So later on we're going to learn how to control this big mess. 126 00:17:12.450 --> 00:17:18.000 William Cheng: But right now, you need to understand that if you write your code like this, we're going to be doing this 101 CPU is all together. 127 00:17:18.810 --> 00:17:23.640 William Cheng: Okay, if we have only one CPU in which that is running, right. The correct answer is, I don't know. 128 00:17:24.150 --> 00:17:31.470 William Cheng: Okay, because there's no way for you to figure out which says, Renee. Okay, so you have to trust that the operating system. The piece of our library, they will do the right thing. 129 00:17:31.710 --> 00:17:39.510 William Cheng: They will prompt, they'll basically the implement all these functions, they're providing a certain kind of service. So they basically they have to stick to 130 00:17:39.930 --> 00:17:48.930 William Cheng: Their implementation to make sure their implementation is correct. Okay. So that's the only thing you can ask for them to make sure that the implemented these p three libraries correctly. Okay. 131 00:17:50.910 --> 00:18:02.640 William Cheng: All right, so here's a slightly different example. Right. Some people say oh you know i is an integer. It's not compatible with voice star. So, therefore I'm going to pass the address of I instead. Okay, is that okay 132 00:18:03.240 --> 00:18:08.430 William Cheng: Okay so address of I'm over here is going to be an address of integer. So it's a pointer data type is compatible with voice star. 133 00:18:08.760 --> 00:18:16.380 William Cheng: So that will also work right. So in this case, inside your server code now. Will you try to type cast our artists voice right you can really use voice start 134 00:18:16.800 --> 00:18:21.060 William Cheng: Okay. So in this case, you know why because you're writing your the programmer who wrote this code. 135 00:18:21.390 --> 00:18:29.010 William Cheng: Okay, so therefore you know that our goal here is the address of it, so therefore it's data type is actually in a pointer to an integer. 136 00:18:29.460 --> 00:18:39.210 William Cheng: Okay, so in this case I'm going to type cast. Argh. To install right because address by over here is in store right so I'm going to assign them to I pointer over here. And now I'm going to start using I pointer. 137 00:18:40.080 --> 00:18:50.880 William Cheng: Okay, are you allowed to do this again. So, of course, you know, they're all 32 bit value. So in this case, they will all be compatible. But what does it mean when I started running my code over here. I'm going to copy the address I over here to our 138 00:18:51.510 --> 00:18:59.370 William Cheng: Guys that again. The fourth argument simply get copy into the stack space of the into the into the stack space into the stack frame. 139 00:18:59.820 --> 00:19:07.500 William Cheng: You know of the child threat. So if I copy over here address or buy into art so inside the article we here. I'm going to end up with address a lie. 140 00:19:08.400 --> 00:19:15.780 William Cheng: Okay, I'm going to end up with address of it for a child number zero. What about shop for Challenge number one child, number one, also get address right 141 00:19:16.140 --> 00:19:22.830 William Cheng: Chop chop number to also get ideas about everybody get address of I. What does it mean over here just for you know for the art to have the address of it. 142 00:19:23.490 --> 00:19:27.960 William Cheng: Well, it's a pointer variable. So over here. So, what it will do is that you know you want to draw a picture is that 143 00:19:28.140 --> 00:19:32.430 William Cheng: If you contain the edges of it, you will draw a pointer to the memory location has no si 144 00:19:32.610 --> 00:19:40.620 William Cheng: So in this case, this will be here will be appointed the point that I have here. So, especially if you also draw the eye pointer it I appointed over here will be another variable over here. 145 00:19:40.860 --> 00:19:52.500 William Cheng: Inside the stack frame it will point to the variable i. So for the next chart over here. They will also pointed a variable is so every one of these pointer, they will all point to the same memory location or is it. Okay, is that okay 146 00:19:53.640 --> 00:19:58.200 William Cheng: Okay. So over here I have a pointer that point on one stack space to another stack space. 147 00:19:58.410 --> 00:20:08.370 William Cheng: Was since they're all part of your address space you can point anywhere you want, right, because your address space is the the space of addressable memory location anything inside your address space. They're all addressable 148 00:20:08.940 --> 00:20:19.770 William Cheng: Okay, so it's perfectly okay for all these points are they all point to the same place. So in this case, which when your child number we are China, but one over here, try to run this code over here, what will be the value by 149 00:20:21.720 --> 00:20:33.390 William Cheng: OK. So again, you know, over here I pointer is so this is show number zero right right here, this show. Number one over here. So here is the I pointer I pointed pointed inside over here. When this course starts to run, what is the value by 150 00:20:34.440 --> 00:20:44.340 William Cheng: What the answer is, I don't know. It could be, you know, so, so I know for sure it's going to be bigger than equal to one could be to could be three could be 99 could be 57 I have no idea. 151 00:20:45.090 --> 00:20:50.430 William Cheng: Okay, because you write code. This way you will have no control of you know what the, what the value bias. 152 00:20:51.840 --> 00:21:04.470 William Cheng: Okay. So, therefore, you get to decide whether you want to write your code. This way or you want to radical the previous way. I will actually typecast I to avoid star and not become an integer. So this way every time I when I start a new thread the art will be different. 153 00:21:05.340 --> 00:21:15.450 William Cheng: Okay if I do it this way. Okay. The art will be exactly the same, even though they point to the same memory location. So in this case, you know, when you try to get the you know the value that is pointing to. 154 00:21:15.690 --> 00:21:24.630 William Cheng: I there's no way for me to predict what kind of value I get okay if I, you know, it is possible that all my child will all see the value by over here, equal to 999 155 00:21:25.650 --> 00:21:36.120 William Cheng: Okay, because they only get to run when all the 100 shelter has been created. IT'S POSSIBLE. IT'S NOT sides in this case it would be 100 because that that in that case, I actually could go to 100 156 00:21:37.560 --> 00:21:45.120 William Cheng: Or so it's very, very important to understand these threading model is seen a little bit chaotic but this is done intentionally. You just need to understand that this is what happens. 157 00:21:45.330 --> 00:21:50.640 William Cheng: When you write code like this, right. So you get to choose what you want to write code like this, or you want to write code like the previous slide. 158 00:21:52.320 --> 00:21:59.220 William Cheng: Okay, so this is a very important function piece or create right so you know warm up to you need to create a bunch of threads. So again, you can you get to decide what to do. 159 00:21:59.970 --> 00:22:09.180 William Cheng: Right. All right, we'll have really talked about the first argument over here. The first argument is a return value. Right. So what you would normally do is that you will create a local variable past the address of the local variable. 160 00:22:09.360 --> 00:22:16.350 William Cheng: And this one is called My. I mean, again, the terminology that we use is that the first argument is going to return a threat identifier. 161 00:22:16.890 --> 00:22:23.610 William Cheng: Okay, so what is the threat identifiers which identifies the threat. So the variable that we use the mobile typically we just call it a threat. 162 00:22:24.360 --> 00:22:33.750 William Cheng: Okay, so when this function returns successfully threat the you know the name of this variable, this variable will contain a threat identifier that will identify the sweat. 163 00:22:34.200 --> 00:22:43.140 William Cheng: Okay, so later on we're going to see how you use this identifier. I mean, so far, the code that doesn't really use the three identifier. So it's going to get wiped out every time when you call a piece of recreate and that's perfectly fine. Yeah. 164 00:22:43.680 --> 00:22:55.740 William Cheng: All right, so some people will take a look at the first argument over here says, oh, it's a three pointer. So I have to use a dynamically allocate the object. So you can call Malik right size of P thread to a UK typecast to a pizza T pointer. 165 00:22:56.070 --> 00:23:02.610 William Cheng: You can do it this way. So in this case you will pass it a point to that point to the dynamic dynamic region. 166 00:23:03.090 --> 00:23:10.560 William Cheng: Okay. Both ways are exactly the same, right, if you think about your address space over here. One of them is a local variable. Alright, so this is the thread local variable we hear 167 00:23:10.770 --> 00:23:16.500 William Cheng: That. So in this case, you pass the address of the threat over here. So this one is going to point to a thread object somewhere. 168 00:23:17.130 --> 00:23:22.080 William Cheng: So, so in this case what happens when this function returns successfully the thread identify will be store right here. 169 00:23:22.470 --> 00:23:30.990 William Cheng: Okay, if you call Malik one in that case you allocate a memory inside the heap space over here and then you allocate this data structure and now when you call Peter create 170 00:23:31.200 --> 00:23:37.440 William Cheng: This is a three pointer that point right here. So in the end, this memory location inside the heap will store the three identifier. 171 00:23:38.490 --> 00:23:42.450 William Cheng: Okay, does it really matter. You know your three identify you know live inside the Hebrew. 172 00:23:43.980 --> 00:23:49.890 William Cheng: Or live inside the stack space. Okay. I mean, it doesn't really matter where you live. You just need to understand where this 173 00:23:50.550 --> 00:23:57.360 William Cheng: Okay, if it's inside the heat you are managing the memory for so you know the memory location is going to stay there until you call free on it. 174 00:23:57.840 --> 00:24:13.320 William Cheng: Okay, otherwise that memory location is going to the until then you own that memory. So, therefore, you get to manage it. Okay. But if you have this variable in the inside your stack space, especially this one is a local variable. So, 175 00:24:14.460 --> 00:24:27.330 William Cheng: Sorry. So you live inside the stack friend. As soon as you return from this function you're struggling with destroy. So therefore, in that case, you know that this variable we here may be short lived computer. The one that inside the heat. 176 00:24:28.800 --> 00:24:34.530 William Cheng: OK. So again, you need to understand the lifetime of your variables. Okay, so this way you the way 177 00:24:35.220 --> 00:24:43.350 William Cheng: When you write your code a variable is not going to all of a sudden disappear or, you know, the variable is going to be a Gary okay get replaced with garbage. 178 00:24:44.130 --> 00:24:55.680 William Cheng: Okay, so that's very, very important to understand is the lifetime of all these variables. Yeah, right, if you if you create a variable inside your stack stack rank as soon as the function return 179 00:24:56.010 --> 00:24:58.350 William Cheng: The you know that the local variable become garbage. 180 00:24:58.860 --> 00:25:11.340 William Cheng: If you create a variable inside your heap. If you don't call free on it. Well, then it will stay there forever if you create a variable inside your data segment or your BSS one. In that case, you know, the, you know, the memory location never disappear. 181 00:25:12.630 --> 00:25:20.040 William Cheng: Okay, so when you create a global variable. Once you program start running the global variable stay exactly where they are until your program terminates. 182 00:25:20.850 --> 00:25:26.790 William Cheng: OK. So again, these three different types of variable has different lifetime. It's very important to understand the difference between them. 183 00:25:27.180 --> 00:25:31.860 William Cheng: Yeah, alright. So, you can write your code, any way you want. Right. You just need to understand what you're doing. Yeah. 184 00:25:32.610 --> 00:25:40.650 William Cheng: Alright, if you Malik it. We're done with that you're supposed to free it okay if you're free to early well then then then then as somebody else's still using it. 185 00:25:41.130 --> 00:25:48.780 William Cheng: In that case, one word. If you return from a function and somebody else are still using the memory location inside the stack frame. Well, then, in this case, you'll be using garbage. 186 00:25:49.890 --> 00:25:51.690 William Cheng: Okay, very important to understand. Okay. 187 00:25:56.160 --> 00:26:03.270 William Cheng: All right, we're not going to talk about windows too much windows, creating a thread, you know, a little more complicated. Again, wow, you know, we don't have to know 188 00:26:05.010 --> 00:26:08.760 William Cheng: All right, so let's take a look at our co our login D over here. 189 00:26:08.970 --> 00:26:19.290 William Cheng: So there are two questions that we need to implement that need to answer right we mentioned before, are logging, do you just create two threads. Want to call incoming thread right that that copy data this way. Another one's called our 190 00:26:19.860 --> 00:26:29.040 William Cheng: I'll going through the copy data that way. So, so we need to call piece of create that. So in this case, when we try to create a thought. We need to pass two arguments into it. 191 00:26:29.400 --> 00:26:39.780 William Cheng: Okay, so how do you pass two arguments in into peace recreate right so so that's the first problem over here, we cannot pass five arguments into pieces. So therefore, we need to do something else. Yeah. 192 00:26:40.320 --> 00:26:50.070 William Cheng: Number to over here is that we're going to create these local variable. We're here to store the threat identifier. As soon as you return for this function we mentioned before this local variable here is going to become invalid. 193 00:26:51.510 --> 00:27:00.780 William Cheng: Okay. So, therefore, you know what, what should we do, right. So one thing that we can do over here is that we can make sure that our function doesn't return until we're done with all these local variables. 194 00:27:01.080 --> 00:27:05.760 William Cheng: Okay, because as soon as you read up on this logo, the finance function everything the function argument or 195 00:27:06.360 --> 00:27:17.820 William Cheng: The function of the copy of the funky argument and the local variable, they all become invalid. Okay, so therefore you cannot use it anymore. Right, so if if my child threads are using this information whether, in that case, they will turn into garbage. 196 00:27:18.930 --> 00:27:26.580 William Cheng: Okay, so that would be another you know thing I need to address. I need to do. Maybe the right thing for me to do is to wait for the charter to die before I returned from this function. 197 00:27:27.300 --> 00:27:31.710 William Cheng: Right. Alright, so let's take a look at these two example number one is, how do you handle multiple arguments. 198 00:27:32.400 --> 00:27:36.360 William Cheng: So, since it could economical Peter careers with For argument's 199 00:27:36.750 --> 00:27:44.010 William Cheng: If you want to pass multiple arguments. What you can do that. You can create a data structure and pass the address of the data structure in the fourth argument. 200 00:27:44.250 --> 00:27:50.760 William Cheng: And the state of Georgia can be as big as you want. So if you want to pass 10 things into it was just create a large enough data structure, you know, for it. Yeah. 201 00:27:51.210 --> 00:28:01.650 William Cheng: So in this example, we're passing two arguments into both of them I integer. So we're going to create this data structure know as to integer data structure and the first arguments go coffers. The second one is going to cause second 202 00:28:02.430 --> 00:28:10.140 William Cheng: Not very creative there so we hear what we need to do is that, you know, for the the incoming sweater over here. We're going to pass. 203 00:28:10.560 --> 00:28:17.040 William Cheng: You know, we're going to pass the first argument over here. And the last argument into the incoming threat. 204 00:28:17.610 --> 00:28:24.120 William Cheng: OK. So again, these are file descriptor. So maybe Aryan is equal to five are all equal to six now than equal to seven and an 205 00:28:24.420 --> 00:28:31.170 William Cheng: equal to eight. Right. So in this case, when we try to create the incoming threat the you know the you know the the the the input. 206 00:28:31.500 --> 00:28:35.010 William Cheng: The input file descriptor should be five and the alpha file descriptor is equal to eight. 207 00:28:35.490 --> 00:28:41.580 William Cheng: Grand when we create the outgoing threat the input file descriptor over here is going to be go to seven and the amplified scrambled six 208 00:28:41.970 --> 00:28:45.600 William Cheng: Okay, so we can just simply create a data structure to hold these two integers. Yeah. 209 00:28:45.750 --> 00:28:50.760 William Cheng: So the way we do this is that we've defined this data type over here and there would be defined these local variable. 210 00:28:50.910 --> 00:28:57.900 William Cheng: One of them is called in the other ones called out. So in over here. It's going to contain to integer. Are you know be equal to five. Ella over here equals eight. 211 00:28:58.290 --> 00:29:03.780 William Cheng: Okay out over here content. The other two variable L equal to seven are all equal equal to six. 212 00:29:04.590 --> 00:29:13.680 William Cheng: Okay, so this is again how you actually create some data structures, the whole bunch of integers. So now when I call piece of it create I can use the address of in and now address of in is compatible with voice start 213 00:29:14.010 --> 00:29:17.520 William Cheng: There. So therefore, in this case, the first procedures, no one's incoming 214 00:29:17.730 --> 00:29:29.070 William Cheng: Incoming will take voice and pikas to the right data type. Right. So again, you are the programmer, you know exactly what data type. This. So, therefore, what you will do the yo typecast org into to integer data. 215 00:29:29.490 --> 00:29:34.350 William Cheng: The data structure pointer right over here because it's the address of in so therefore you do that and he happy. 216 00:29:34.560 --> 00:29:44.670 William Cheng: And now will p will be appointed as two fields one. It's called first one it's a second so you access P or first, you should you should get the number five, and we access PF second you should get the integer eight 217 00:29:46.140 --> 00:29:53.280 William Cheng: OK. So, again, to understand how this actually work. We need to go to our address space again. So the I just feel will look like this. 218 00:29:53.730 --> 00:29:59.340 William Cheng: Guy's. So here is our main function, our main function is going to cause some other function eventually is going to call our login D. 219 00:29:59.580 --> 00:30:04.830 William Cheng: Are logging D is the code that we show you before, right. This is the login D is going to copy three create twice. 220 00:30:05.100 --> 00:30:10.860 William Cheng: Yeah, so therefore, when a car. Peter create the first time we're going to create the stack space for the child through 221 00:30:11.040 --> 00:30:15.810 William Cheng: Again, I'm not going to sort of draw them right next to each other, you know, they're inside your address space somewhere. 222 00:30:15.990 --> 00:30:22.620 William Cheng: So over here, the stack space for the incoming thread and inside the stack space over here. We're going to have the function argument and local variable. 223 00:30:22.920 --> 00:30:31.080 William Cheng: That. So, by the way, the are logging D over here. It's going to contain also function argument, a local variable. So in this case, the function arguments over here is going to be these for integers. 224 00:30:31.320 --> 00:30:39.660 William Cheng: Okay, what about local variable right over here. There is the day the thread identifier for the incoming threat the threat identifier for the outgoing threat. 225 00:30:40.020 --> 00:30:45.210 William Cheng: The in data structure and our data showed you. So it's a pretty good. It's a pretty big stack space. 226 00:30:45.660 --> 00:30:56.370 William Cheng: Okay, so I'm only going to draw the in and out, because that's the one I'm focusing on right now. So in over here contain to integer are in and out. Right. So, so in this example again 5678 right here. 227 00:30:56.520 --> 00:31:09.090 William Cheng: So in contains two integers one is five and the other one is a right here I'm drawing a vertical bar to separate the two integers that may actually use comma over here. So the out of Arizona contains seven comma six 228 00:31:10.470 --> 00:31:15.540 William Cheng: Okay, so when you write your code like this when you need to draw your address space, you should know they should draw like this way. 229 00:31:16.710 --> 00:31:24.120 William Cheng: Okay, so again, in this case, what I call, Peter create I'm going to copy the address in address of in into arc. So in this case, what would the art. 230 00:31:24.420 --> 00:31:34.980 William Cheng: Are will get the address of in over here, right. So in this case, again, when we joined an address of a in which all pointer. So therefore, we draw a dot over here that points to the address of in over here. 231 00:31:36.060 --> 00:31:44.190 William Cheng: Okay, then the instruction. Over here we execute is going to be p equals to our when we copy pointer. They point to the same place over here p will be pointing this one. 232 00:31:44.490 --> 00:31:58.590 William Cheng: OK. So again, we cannot use art because art is over time voice voice doesn't have first and second feel, but when we copy the pointer over here now piece of the right time. So now we can actually access P right arrow. First, when you say p right arrow first 233 00:31:59.490 --> 00:32:09.150 William Cheng: There. So again, if you look at the pointers web page that I prepare, right, the right arrow over here is a shorthand for star p doc first right 234 00:32:10.410 --> 00:32:18.420 William Cheng: There again, start PM mean over here is that P is appointed variable star p, meaning that follow the point to point a word point and now it's a data structure. 235 00:32:18.570 --> 00:32:26.910 William Cheng: Is that a data structure. There are two fields. There's the first and second, so start Pete offers will give you the five. Okay. Start Peter second will give you the eight 236 00:32:28.290 --> 00:32:37.830 William Cheng: Or so, again, it's very, very important to know how to draw these picture and connect the dots over here so that when you copy data you need to sort of figure out whether you're copying pointer, a couple copy the content. 237 00:32:38.340 --> 00:32:41.880 William Cheng: So this way you know exactly what's going on when you run code like this. 238 00:32:43.020 --> 00:32:51.480 William Cheng: Okay, so again I'm going over this pretty quickly. You should take a look at it. And in the end, if you don't understand, feel free to ask me question during the live lecture section. Okay. 239 00:32:53.790 --> 00:32:55.320 William Cheng: Alright, so again, over here. 240 00:32:55.590 --> 00:33:05.910 William Cheng: That's what I talked about. Right. You know, you will use these arguments, you know, they can be local variable. They can you could be global variable that can be he variable, you need to understand the lifetime all these variables. Okay. 241 00:33:06.150 --> 00:33:13.950 William Cheng: For a global variable a static variable their lifetime is the lifetime of your program. Once your programs are running these addresses don't change. 242 00:33:14.490 --> 00:33:22.920 William Cheng: For dynamically allocate a variable. Why don't you allocate it to address the adjutant and change. And then when you free them you return that you return the object. 243 00:33:23.220 --> 00:33:28.980 William Cheng: So you return a data structure back into the memory alligator. Next time, will you call Malik again maybe you'll get a different address 244 00:33:29.640 --> 00:33:34.980 William Cheng: OK for local variable as soon as you return from the function in the local variable is going to be considered invalid. 245 00:33:35.250 --> 00:33:43.440 William Cheng: Okay, some people say, wait a second. But as soon as I returned from a function. If I have a point to a point to, you know, pull up will point to the stack frame I can continue to use it. 246 00:33:44.130 --> 00:33:57.600 William Cheng: Okay, that that's because you're lucky. Okay. You're lucky that the data inside the stack frame that you already returned from that data is still valid. But, you know, next time, will you run a different you know 247 00:33:58.260 --> 00:34:05.790 William Cheng: Some of your program timing is a little different. Maybe the data will not be there. Okay, just because you see that the data is good, doesn't mean that it's always there. 248 00:34:06.510 --> 00:34:19.800 William Cheng: Okay, so. Okay. You need to learn to write your code correctly and you need to understand that if you create a local variable as soon as you return on a phone for a particular function, you should consider all the local variable become garbage, even though they don't look like garbage. 249 00:34:21.300 --> 00:34:25.590 William Cheng: Okay. See, it's important to understand that they should be treated as garbage. Right. 250 00:34:27.090 --> 00:34:37.440 William Cheng: All right, so, so, so again if you don't know this kind of stuff. You're going to end up with memory corruption bug. So again, what is memory corruption right memory corruption bug happened where memory is reuse unexpectedly. 251 00:34:38.280 --> 00:34:45.690 William Cheng: Okay, so some people will have a pointer to a local variable that you wish for this function. And later on, you find out the value has changed is that, oh, that's a memory corruption bug. 252 00:34:46.140 --> 00:34:53.790 William Cheng: Because the memory location changes unexpectedly. Okay, why did they change it unexpected should you expect expect memory supposed to change. 253 00:34:54.180 --> 00:35:03.060 William Cheng: What if you return from a function and then you're still using the local variable what they should expect that to change. Right. It's so, so the problem is that your expectation was incorrect. 254 00:35:03.930 --> 00:35:12.450 William Cheng: Okay, so these kinds of stuff. It's called memory corruption bug you corrupt some of your address space because you didn't expect something to change while that's because you'd be because the amount misunderstood. 255 00:35:12.900 --> 00:35:14.190 William Cheng: What functions are supposed to do. 256 00:35:14.580 --> 00:35:22.680 William Cheng: Okay, there are other kind of memory corruption. For example, if you have an array. So if you have a data structure that's only you know exabytes long right x by flown over here. 257 00:35:22.920 --> 00:35:31.260 William Cheng: If you have a buffer overflow you right beyond this, the odd the end of this buffer when you write beyond this, the end of this month, or where did you ride into 258 00:35:32.220 --> 00:35:42.630 William Cheng: Okay, so let's say the X obvious as long. Right. So instead of riding a bike I ride all bites into the, into this memory buffer. Okay, what is going to be the next four bytes. The next four bytes is another variable. 259 00:35:44.130 --> 00:35:49.140 William Cheng: Okay. Then the next four by, you know, it's part of your address space. So therefore, the next poor by is not thin air. 260 00:35:49.470 --> 00:35:56.760 William Cheng: Okay, so, so when you write more than eight bytes, you're wiping out some other variable and which you don't expect. So again, these, these bugs are nice memory corruption bugs. 261 00:35:57.330 --> 00:36:03.000 William Cheng: Okay. All right. So, so when you when you write your code, you got to be very careful. Every memory location, you need to watch out for. 262 00:36:03.240 --> 00:36:10.980 William Cheng: Okay, make sure you don't have any memory corruption bug. I mean, some people, you know, come to me as question into my office hours to say, you know, and they showed me some of their co 263 00:36:11.160 --> 00:36:18.240 William Cheng: I look at their code. It says, I asked them questions that are you sure this code works. The students, as I hope so. Okay, that is the wrong answer. 264 00:36:19.260 --> 00:36:23.460 William Cheng: Okay, it's your code you need to know whether the memory is going to be there. 265 00:36:23.970 --> 00:36:29.250 William Cheng: You know when when we access the memory location. Okay, if that memory location is going to turn into garbage. 266 00:36:29.460 --> 00:36:40.470 William Cheng: Or you know you have memory location that you don't know that you are actually destroying them. Well, then in that case, you shouldn't be surprised that the code doesn't work. Okay, write code that you're sure about. 267 00:36:41.550 --> 00:36:41.820 William Cheng: That 268 00:36:42.870 --> 00:36:55.740 William Cheng: I mean, this sounds almost impossible. But again, if you every time when you write code, you think about your co very, very carefully, you should be able to you know after. Why should be able to have develop a good habit and note exactly what your code is doing. OK. 269 00:36:58.440 --> 00:36:58.950 William Cheng: All right. 270 00:37:02.310 --> 00:37:11.310 William Cheng: OK. So again, another example over here. What will happen if we return from our login d, right. So this example I'm going to copy to create once. And now I'm going to return from our log end 271 00:37:11.730 --> 00:37:19.260 William Cheng: OK. So again, all these data over here that live inside a stack rank as soon as you return from our login de de this stack frame is popped. 272 00:37:20.190 --> 00:37:27.690 William Cheng: Okay, so with the stack, whereas pop ESP. He's got a point right here, HTTP is going to pray. Here you return back to the function that caught our login D. 273 00:37:28.020 --> 00:37:36.210 William Cheng: In that function call another function, what will what will happen okay that function call another function, it will build another stack frame right on top of this one, and it was gonna wipe all the data. 274 00:37:36.870 --> 00:37:40.920 William Cheng: Okay, if the other functions just happened not call any other function. Well, in that case, you're lucky. 275 00:37:41.190 --> 00:37:54.870 William Cheng: This data will not be modified. But if you if you change your code to have a call another function, then everything will be all the way down. Okay, so it's very important to understand that when you return from this function, all these data will disappear. And now what do you tell threat. 276 00:37:56.340 --> 00:38:02.640 William Cheng: Well, you don't really know if your child is running yet, right, because I mentioned before, as soon as you create a child through that you have no idea when your child is going to run 277 00:38:03.000 --> 00:38:12.450 William Cheng: Okay. What if you return from this function right away and your child has a Mr. Ronnie. Yeah, well, your shelter start running this code says P over here, point to the you know the data right here. 278 00:38:12.690 --> 00:38:22.440 William Cheng: And now, should you expect into a five and a sitting, you know, sitting in this memory location. Well, no, because as soon as you return but this function over here. It is possible. All these data will be wiped out. 279 00:38:23.700 --> 00:38:31.290 William Cheng: Okay. So, therefore, if you write your code. This way, will you start running your child threat. You have no idea where you're going to get in terms of P right arrow first and P rival second 280 00:38:32.970 --> 00:38:45.180 William Cheng: Okay. The only thing that guarantees over here is that are will point to this memory location that used to be, we're in is OK. But there's no guarantee. What's going to be inside our memory location. Right. Instead, I remember location if you return on this function or better off. 281 00:38:46.260 --> 00:38:50.670 William Cheng: Now, so therefore the right thing to do is that if you want to make sure that when your child starts running 282 00:38:50.940 --> 00:38:57.450 William Cheng: The data is always there. What did you do is that you need to keep meeting you need to make sure that the stack rank stay exactly where it was. 283 00:38:57.900 --> 00:39:02.370 William Cheng: Okay, how do you make sure that this document stay exactly where it was. You don't return from that function. 284 00:39:02.940 --> 00:39:13.080 William Cheng: As soon as you return on our function. They're all bets are off. Okay. So in a way, what we need to do over here is that is that in our login D. We're not allowed to return until both child threats are dead. 285 00:39:13.710 --> 00:39:17.880 William Cheng: Right, because when both child thread I did, then we know that this code can execute anymore. 286 00:39:19.710 --> 00:39:24.120 William Cheng: Okay, so therefore, in this case, we need to wait for the charter to die for Peter library. 287 00:39:24.540 --> 00:39:32.370 William Cheng: As it turns out, there's no function called Peter. Wait, wait, wait. Sorry. Wait for child process to die. So, you know, you might guess, that is the function called called Peter away. 288 00:39:33.060 --> 00:39:39.960 William Cheng: As it turns out, it's called peace, joy. Okay. So the idea here is that we're going to join with a specific. So we're going to wait for that threat to die. 289 00:39:40.800 --> 00:39:45.150 William Cheng: Okay, so Peter and john is a blocking call this function, you know, it takes two arguments. 290 00:39:45.360 --> 00:39:56.730 William Cheng: The first argument is going to be the three identifier. So if you want to wait for us to have to die, you get you get to specify which threat. You want to wait you you were still you want to wait for for for that were to die. 291 00:39:57.450 --> 00:40:05.370 William Cheng: Okay, so that's why this function is not is not called piece right away because what's the difference between we're enjoying what we call way. You have no idea who you're waiting board. 292 00:40:06.390 --> 00:40:14.160 William Cheng: Okay, so remember the way system call right away some content returned with any child who has process ID. Okay, that that is the child processes that 293 00:40:14.430 --> 00:40:19.110 William Cheng: Okay, but we call peace, joy, you can actually said, that is the thread I want to wait for today. 294 00:40:19.950 --> 00:40:26.820 William Cheng: Okay, so therefore, again, this is why you know when you call piece of creative, you need to keep the thread identifier. Because later on you can say, I want to wait for that to die. 295 00:40:27.090 --> 00:40:30.240 William Cheng: Yeah. So this guy is what I call piece of creative. I'm going to keep 296 00:40:30.600 --> 00:40:35.910 William Cheng: The return so identifier into the, into arguments over here. So, this will be the incoming thread. 297 00:40:36.120 --> 00:40:48.480 William Cheng: And then when I copy their career for the outgoing thread. I'm going to store the three identify in this variable. So now what I tried to wait for them to die. I can call peace, joy in there. So this function will not return until the incoming thread is that 298 00:40:49.320 --> 00:40:53.850 William Cheng: Okay, the second argument over here is also a return value, just like the way system call 299 00:40:54.900 --> 00:41:01.650 William Cheng: Okay, but, you know, remember the way system car. Either way system call you pass the address of an integer. So in this case, you know, so, so since 300 00:41:01.890 --> 00:41:10.110 William Cheng: The child threat the return value is going to be a voice star and you need an address of of oyster variable one this case that the argument type is going to be boys, star, star. 301 00:41:10.890 --> 00:41:18.150 William Cheng: Okay, what is the boy, star, star okay voice, star, star. All it means that it's a pointer. If it's a pointer. It's just a 32 bit value again. 302 00:41:19.170 --> 00:41:26.370 William Cheng: Okay. So in this case, what you are expected to do is if you provide a zero. That means I'm not interested in the return value or the x value for this thread. 303 00:41:26.670 --> 00:41:34.410 William Cheng: Okay. Otherwise, it's going to be an address, have a voice star. Alright. So again, what is that what is the voice tone of voice are simply a 32 bit value. 304 00:41:34.860 --> 00:41:39.450 William Cheng: Okay. It doesn't have to be a pointer, it just a natural preservative evaluate. Okay. 305 00:41:40.320 --> 00:41:48.030 William Cheng: Alright, so again, peace or joy return to values. One is, whether they were successful or not 010 minus one, I guess. 306 00:41:48.210 --> 00:41:54.420 William Cheng: The other way they want the other one, the other the other return value over here. It's going to be the return, Mexico, right. So what is the return Mexico. 307 00:41:54.720 --> 00:42:02.250 William Cheng: Right, we saw before we hear when a threat. I will. What a thread dies. Right. The last line over here is going to be returned going to return something or voice start 308 00:42:02.640 --> 00:42:09.120 William Cheng: OK, so the again the idea here is that this value is going to get copy into the threat control blogs or the process control, blah. 309 00:42:09.540 --> 00:42:20.160 William Cheng: Okay, so in this case where you copy. So john we're going to go to the threat control blog way for the threat to die. And when it dies. We're going to copy the thread return Mexico and back into this memory location. 310 00:42:21.660 --> 00:42:27.690 William Cheng: All right. Okay, so here's an example. All right over here for peace or joy, talk about these things already 311 00:42:28.290 --> 00:42:37.080 William Cheng: So here's the example for the parent to child three and then wait for the child threat to die. Okay. All right. So over here, as the question. How's the threat terminate. 312 00:42:37.800 --> 00:42:45.270 William Cheng: Okay, so for the threat they can return from the first procedure call okay when a thread returned from first procedure that's how a threat with self terminate. 313 00:42:45.660 --> 00:42:54.690 William Cheng: Okay, but just like a process. The process and also called the excess Simcoe and anytime he wants. So forthright yourself terminate the second also call a function called P threat. Exit 314 00:42:55.110 --> 00:43:03.600 William Cheng: Okay, so here's a valuable piece of Alexa, so we copied to exit the argument over here is going to be called voice star, will you call piece to exit the thread will also self terminate. 315 00:43:05.070 --> 00:43:11.010 William Cheng: Alright, so let's take a look at example we have a parent threat over here with child threat. Right. So these are the parents right over here. 316 00:43:11.430 --> 00:43:18.870 William Cheng: We're going to have a threat identifier over here is a local variable. And then we also going to have a voice star to receive the return Mexico. 317 00:43:19.170 --> 00:43:24.660 William Cheng: When we call P three joy, guys. But again, this is the voice start over here. So we're going to type cast to a voice to have zero 318 00:43:25.110 --> 00:43:33.450 William Cheng: So again, it just means that the 32 bit value. Okay, we're gonna call P3 at create the first argument is over here is going to be the three identifier on return 319 00:43:34.110 --> 00:43:40.650 William Cheng: The second argument is always know and their argument is going to be the first procedures, again, the first procedure is a function that returns voice star. 320 00:43:40.920 --> 00:43:46.440 William Cheng: Take an argument voiced are the fourth arguments going to get copy over here. So, therefore, our is going to be equal to zero. 321 00:43:46.650 --> 00:43:58.830 William Cheng: When the child stars that are you know in our example over here, the child so it doesn't use that argument. So it doesn't really matter where you go when you use, and now the parent over here as soon as you create the child, where it's simply wait for the child threat to die. 322 00:44:00.180 --> 00:44:11.130 William Cheng: Okay, so it called peace or joy waiting for this chapter to die and then they pass the address of result to receive that other the return the return or the ethical for the child threat. OK, so now 323 00:44:11.520 --> 00:44:21.480 William Cheng: You know who's going to run. Next, right, the parent over here. A copy through a joy and peace or a joint is a blocking call. So the parent give up the CPU the CPU can be given to other program or the processes. 324 00:44:21.660 --> 00:44:28.710 William Cheng: But eventually, it will give to back to this program and now inside this program with the parents who had a sleeping so therefore you will have to run the child threat. 325 00:44:29.370 --> 00:44:36.630 William Cheng: Okay, so in this case we know that sooner or later the chocolate will start to run. We have no idea when it's going to run, but we know that sooner or later is done show running 326 00:44:36.990 --> 00:44:49.860 William Cheng: Okay, so this shelter over here, start running at some point over here if it decided to terminate early, it will call Pisa exit and then in this case is going to, you know, call it an argument and this argument will become the threats return exit code. 327 00:44:50.520 --> 00:44:54.900 William Cheng: OK. So the idea here is that it's very analogous to the process returned Mexico. 328 00:44:55.260 --> 00:45:02.940 William Cheng: Yeah. So in this case, we're going to draw a picture of something like a threat control block. So again, this could be a kernel data search. You can be a user to a distortion. We don't know where it is. 329 00:45:03.180 --> 00:45:06.270 William Cheng: Right, what's important to understand is that what's inside a threat control, blah. 330 00:45:06.870 --> 00:45:12.540 William Cheng: Okay, so it'll get easier to control blah, while we have to have a three identifier nice over here. We're going to call it a TI ID over here. 331 00:45:12.780 --> 00:45:19.830 William Cheng: That's a threat identify that should be equal to the same value as the local variable right here. Okay. And also we need a place to store the return Mexico. 332 00:45:20.070 --> 00:45:30.000 William Cheng: So what you call pizza at exit. This was going to die. And when it dies, he will update the threat control block with the return of Mexico. So in this case, I'm going to copy the value one over here. 333 00:45:30.660 --> 00:45:34.890 William Cheng: Okay, so the return actually go over your be set to what if, what if the child is not the right guy right here. 334 00:45:35.520 --> 00:45:41.400 William Cheng: Okay, also inside the library. When it does, he will notice that the parent is actually waiting for the child threat to die. 335 00:45:41.610 --> 00:45:49.020 William Cheng: And the parent over here is interested in the in the in the Mexico and the address over here that it provides the address of resolved. So again, where's the result. 336 00:45:49.380 --> 00:46:00.630 William Cheng: That. So again, you need to go back to the address space over here. Here is the parents stack space inside the parents diagram over here as a function called parent. And then there's a local variable called result. 337 00:46:01.110 --> 00:46:03.390 William Cheng: Okay, and now the parents that have you're sleeping. 338 00:46:03.990 --> 00:46:14.970 William Cheng: Okay. So, therefore, what we need to do is that we need to copy the return exit code over here into the address that are specified in peace or joy. So therefore going to copy this into this memory location over here and now results can be good one. 339 00:46:16.260 --> 00:46:23.580 William Cheng: Okay, and then we're going to wake up the parents threat. When the parents are awake so up it's gonna is going to return from peace, joy, right. So peace or joy is a blocking call 340 00:46:23.760 --> 00:46:30.600 William Cheng: The only return when the child or die. And when you when you copy the child's Mexico into this argument over here. 341 00:46:31.080 --> 00:46:37.620 William Cheng: Okay, so now the child who had is dead already. So this is the stats for the child over here. So again, the stack space over here will get freed up 342 00:46:37.830 --> 00:46:46.920 William Cheng: The child who has completely dead now the parent is going to return on this function and you can actually do a switch on the result podcaster integer and then look at the value over here, equal to one or two. 343 00:46:47.550 --> 00:46:51.840 William Cheng: Okay, so this way, the parent can do different actions depends on how the child thread is dead. 344 00:46:53.160 --> 00:47:01.110 William Cheng: Okay, one of the child thread doesn't die by calling FedEx it. So in that case, the chapter will see you all the way to the end over here called return with to 345 00:47:01.740 --> 00:47:13.980 William Cheng: Win the shelf. There we turn. What does it return to. OK. So again, there's going to be a child, I start a function that called the first procedure. So inside that function over here. What it will do is that it will take the return value and it will call Pisa exit on it. 346 00:47:15.450 --> 00:47:21.240 William Cheng: OK. So again, what's important to understand over here is that when you return from the first procedure or when you call piece of x. 347 00:47:21.540 --> 00:47:29.580 William Cheng: Piece of exit directly. The end result is exactly you're saying you're going to end up calling peace because peace or exit is the only way for threat to die. 348 00:47:31.380 --> 00:47:37.230 William Cheng: OK. So again, that's very important to understand. That's the only way for her to die. So this guy's, will you call return to over here. 349 00:47:37.410 --> 00:47:42.450 William Cheng: And then again, what happens at the threads going to tell us off terminate is going to copy through. So the argument of to 350 00:47:42.660 --> 00:47:45.300 William Cheng: Two is going to get copied over here to the return Mexico. 351 00:47:45.480 --> 00:47:53.400 William Cheng: And he will notice that the parent is waiting. So they will copy this to all we get into the result right here. Wake up the parent and now when the parents are running code over here. 352 00:47:53.580 --> 00:47:57.930 William Cheng: When you look at the result. The result has value of two. So, therefore, this case you will handle the second case. 353 00:48:00.420 --> 00:48:14.370 William Cheng: Okay, so again, try to understand this separate example, write your code. This way compile it and run it to understand exactly why open and to understand what the address space look like local variable function arguments over here and then also see you know how 354 00:48:15.150 --> 00:48:19.440 William Cheng: Well, you run a yo. Yo, you will not be able to see the threat control blog because that's inside the 355 00:48:19.950 --> 00:48:27.480 William Cheng: Inside the library and again through it can be a little planes out of Colonel. So if you just sit data structure actually inside a colonel. I mean, clearly, you will not be able to see it. 356 00:48:27.900 --> 00:48:30.570 William Cheng: Okay. So over here we just imagine what the third control blocked by 357 00:48:31.140 --> 00:48:36.450 William Cheng: The tangible must look like it must have a threat identifier. It also must keep the return of Mexico. 358 00:48:36.690 --> 00:48:47.010 William Cheng: Okay, what about all the other stuff again throughout the rest of the remaining of the semester. We're going to continue to talk about what, what else that well. Other stuff going to the second job blog. Well, other stuff going to the process control block. 359 00:48:48.540 --> 00:49:01.560 William Cheng: Alright, so I'm going to sort of stop the first part of the video and then next time when we come back we'll continue to look at, you know, threat termination, and then they're gonna look at, you know, a threat synchronization. Yeah.