WEBVTT 1 00:00:02.070 --> 00:00:15.929 William Cheng: Okay, this is the second part of lectures six. So at the end of the last video, we talked about the solution to the first example, what is the correct way to to away for a 2 00:00:16.830 --> 00:00:26.940 William Cheng: Asynchronous signal. Right. And then in the last slide that we went over. We talked about the only correct way to wait for a synchronous event. OK. So again, the slides. Very, very important. 3 00:00:27.690 --> 00:00:37.230 William Cheng: You know, you need to sort of remember how how this actually works. Okay. All right. The second problem was the long running procedure. So the solution over here is very simple. 4 00:00:37.800 --> 00:00:42.690 William Cheng: Because when you try to update the limitless. And we, one thing that you can do so you can simply block the signal. 5 00:00:43.260 --> 00:00:48.960 William Cheng: So in this case you can block the signal and then you go update the link list. And when you finished updating linguists and the you cannot block the signal. 6 00:00:49.320 --> 00:00:56.250 William Cheng: OK, so the solution over here. So what we're going to do that in the main thread. Over here we've got two blocks again. Right. And then we set up the signal handler over here. 7 00:00:56.490 --> 00:01:04.320 William Cheng: And then what we call the long running process over here. Again, the idea here is to make sure that this code and this code, these two functions are mutually exclusive. 8 00:01:04.710 --> 00:01:07.920 William Cheng: Yeah, but again, since these two are these two are not threats. 9 00:01:08.550 --> 00:01:14.910 William Cheng: One of them is a signal handler. The other ones a regular threat Co. So, therefore, you can't really use peacetime new tax law computer miss out on lock. 10 00:01:15.300 --> 00:01:23.040 William Cheng: So we should do, is that right before we're going to, you know, mess around with the link this we simply blog. The signal. What I in that case this function cannot be called 11 00:01:23.550 --> 00:01:25.170 William Cheng: Okay, so they have what we will do is that 12 00:01:25.830 --> 00:01:33.180 William Cheng: You know, we're going to call sick proc mask with a cig blog over here and the set over here only has a single bit set forth for for for segment. 13 00:01:33.480 --> 00:01:43.860 William Cheng: Right. So, therefore, as soon as we're finished calling this function and now. So, and will happen is that this one. The signal is blocked okay if signal is blocked. 14 00:01:44.880 --> 00:01:55.860 William Cheng: If the signal is blog. That means that this function can execute. Well, which means. So when the, when the user press Control C, Control C is going to become pending until the signals unblock right so therefore the main threat over here. 15 00:01:56.910 --> 00:02:01.650 William Cheng: Is going to safely update state mess around with the link less over here take its time. 16 00:02:02.610 --> 00:02:08.820 William Cheng: In the meantime, if the user press Control see nothing happens right and then why because that will be here. We're going to restore the signal mass 17 00:02:09.240 --> 00:02:11.520 William Cheng: To what it was before. And then in that case. 18 00:02:11.880 --> 00:02:19.470 William Cheng: In that case it will be you on blocks again. So as soon as you do that again is on blah, if there's a pending. So again, and now singing will get deliver 19 00:02:19.650 --> 00:02:26.700 William Cheng: On in our example over here when you deliver this again, you're going to call the you can call the signal handler. So, therefore, you can call it display stayed over here. 20 00:02:27.810 --> 00:02:37.740 William Cheng: You know. So in this case, your main thread over here doesn't have the you know the method over here has finished updating this so therefore it's safe to actually execute this code. 21 00:02:38.610 --> 00:02:46.290 William Cheng: Okay, so what will happen in this case either method over here runs pretty fast. And then what it will do is it will it will try to update the state again. 22 00:02:46.830 --> 00:02:56.940 William Cheng: Okay, so remember that since the bottom code over here is it's a signal handler and we borrow this thread to to run to handle the signal handler here. So therefore, the main thread is not running anymore. 23 00:02:57.750 --> 00:03:05.010 William Cheng: Okay, so once we are in the same signal handler over here. We're going to execute display function to walk down the links to play the print some information. 24 00:03:05.670 --> 00:03:16.500 William Cheng: So the CPU. So, so in this case our thread is executing the signal handler over here, so therefore it will not be able to run the code inside means. Okay. So, therefore, this could actually works. Okay. 25 00:03:18.570 --> 00:03:25.800 William Cheng: All right, so, so talk about the sort of the signal about sort of blocking and unblocking 26 00:03:26.400 --> 00:03:43.080 William Cheng: Unix the signals was were invented, long time ago. So they were delivered to processes and not threats right so now even individual threads can signal block and block. Okay. So, therefore, when you try to deliver a signal. What if you have multiple threads, both of them had the signal unblock 27 00:03:44.430 --> 00:03:48.420 William Cheng: Okay, so, so the the positive specification says that in that case. 28 00:03:49.530 --> 00:03:55.200 William Cheng: The policy library, what it will do that they will pick a random threat that have the signal unblock to deliver the signal. 29 00:03:55.620 --> 00:04:04.410 William Cheng: Because it again. What does that mean deliver the signal right deliver signal meaning and the meaning that it's going to execute a signal handler. So again, what it will do is it will, you know, out of all the threads that have the signal unblock 30 00:04:04.650 --> 00:04:09.210 William Cheng: It will pick one. Pick one of them at random and then use that threat to execute a signal. Hello. 31 00:04:10.380 --> 00:04:24.720 William Cheng: Okay, so, so, so what if that's not the thread that you want to get borrow to execute a signal handler. Why, in that case, then it doesn't work. Okay. So, therefore, one thing that we should we need to be able to do is to try to control this particular 32 00:04:26.040 --> 00:04:29.580 William Cheng: Control. Control the stress election. Okay. 33 00:04:31.260 --> 00:04:40.950 William Cheng: Alright, so, so let's so sort of take a look to see how this one is done. So the basic idea over here is that we're going to designate a thread, just to catch segment. 34 00:04:41.580 --> 00:04:50.250 William Cheng: Okay, so in our process all the other threads going to have second block and we if we only have one thread that has singing unblock. Well, in that case, if you've got a control. See, which 35 00:04:50.580 --> 00:04:57.720 William Cheng: Are you going to borrow to execute a signal handler. Well, in that case, there's only one thread to choose from so you know exactly that. So it will be borrowed to execute a signal delivery. 36 00:04:58.320 --> 00:05:12.240 William Cheng: OK, so the way that this works is that, you know, so, so, so, so, yeah. In this example we're going to handle the signal synchronously. Okay, by making us a system called see wait now. Alright, so, so the key it over here is that we're going to make sure that our threats have Seguin blocked. 37 00:05:13.260 --> 00:05:18.990 William Cheng: There's okay how do you do that, right. So what you do that is that you block singing in the main threat. And we also know that whenever you create a child. 38 00:05:19.440 --> 00:05:25.470 William Cheng: The child thread will inherit the signal mass when the parent, the parent has singing blah, so therefore the child to have the cities. 39 00:05:25.920 --> 00:05:36.420 William Cheng: Will also have the second block. OK, so the CO will look like this. So I'm going to start with CMT said over here. We're going to set a single bit over here corresponding just again and then we're going to block Sagan in the main threat. 40 00:05:36.720 --> 00:05:40.290 William Cheng: And then so from this point on any thread that we create. They all have Sagan, blah. 41 00:05:40.590 --> 00:05:48.510 William Cheng: Then, so this guy will create a child to add the child. First. First. First. Procedures going to call monitor. So we call piece right create the manager over here and now. 42 00:05:48.840 --> 00:05:52.320 William Cheng: That the chapter is going to start running over here and the child who is a separate thread. 43 00:05:52.800 --> 00:05:59.310 William Cheng: OK, so the main sort of you is going to call long running process. And now we're going to end up with two threads potentially running in parallel. 44 00:05:59.730 --> 00:06:03.570 William Cheng: Okay, since there are separate threads, whatnot can use PHP tags off and piece of music. 45 00:06:04.380 --> 00:06:11.430 William Cheng: Okay. In the previous case, I only have one thread. So therefore you if you use P three meters log on, log, you're going to end up dead lucky with yourself. 46 00:06:11.940 --> 00:06:17.340 William Cheng: Okay, so therefore, in this case, you know, if I, you know, in order for me to provide mutual exclusion. 47 00:06:18.090 --> 00:06:25.230 William Cheng: The upstate upstate over here will be executed inside of critical section and the display stayed over, he will also get executed inside of critical section. 48 00:06:25.980 --> 00:06:33.390 William Cheng: That alright so in order for you to deliver the signal you know synchronously. At this point, both have singing block. 49 00:06:34.350 --> 00:06:41.400 William Cheng: Okay, so therefore you do press Control C control sees is going to become pending right so they will never get to liver. So at some point, we need to unblock again. 50 00:06:41.580 --> 00:06:50.460 William Cheng: OK, so the way this is does that inside the child read over here. I'm going to go instead of internal wow wow one over here is going to be an infinite loop and then I will call this function called segue. 51 00:06:51.000 --> 00:07:01.470 William Cheng: Okay, so at the time when you go sideways. Second is blocked and in this case the set over here is going to be this global variable. And we know that this one has ever been equal to zero, except once again that will be the one that equal to one. 52 00:07:02.040 --> 00:07:10.680 William Cheng: Okay. So in this case, when he goes subway, what happened is that we're going to wait for the signal inside the set and we're going to unblock that signal and wait for the signal in one atomic operation. 53 00:07:11.910 --> 00:07:16.200 William Cheng: Because it again in order for this to work. You need an atomic operation so sideways and then hopping operation. 54 00:07:16.560 --> 00:07:24.600 William Cheng: At the time when you can see where you got to make sure the signal is blocked so when you call see where he will unblock the signal and wait for the signal in one atomic operation. 55 00:07:25.410 --> 00:07:31.440 William Cheng: Well guys, it's almost like six. So then I have a six is about work a little differently, right. So this one of the argument over here so so so 56 00:07:31.650 --> 00:07:35.280 William Cheng: So this will be the sooner they get blocked and this will be the signal that it will be waiting for 57 00:07:35.670 --> 00:07:47.580 William Cheng: That. So if the signal said oh yes multiple it's beset it will unblock multiple signals and it will wait for either of the signals that and when signal get deliver, even if you have specify signal handler they will all get ignored. 58 00:07:49.110 --> 00:07:58.980 William Cheng: Okay, so in this call over here, we don't we don't even specify a signal handler, because we know that if we specify signal handle it will be ignored. So when the signal get delivered. The only thing that will happen is that this to I will return from segue. 59 00:07:59.340 --> 00:08:02.220 William Cheng: Okay, what are we talking about segue Sagan is blocked again. 60 00:08:03.360 --> 00:08:09.090 William Cheng: Okay, so if you look at this entire code over here. The only time signatures unblock is going to be in the middle of segue. 61 00:08:09.210 --> 00:08:14.910 William Cheng: So, therefore, you know exactly what the signal is going to get delivered right. The only place the signal going to get delivery is going to be in the middle of subway. 62 00:08:15.600 --> 00:08:28.320 William Cheng: Okay, so, so, so, so that's why this is consider you know as sort of a sort of waiting for the signal in a in a synchronous fashion because we know exactly what signal will get deliver it will get deliver inside this particular system called 63 00:08:29.100 --> 00:08:39.180 William Cheng: Yeah, alright. So, so if control see get press over here we're going to return on subway and then we're going to wait for the new tax to be unlocked and then we're going to displace day and this code. And this guy over here, they're mutually exclusive. 64 00:08:39.360 --> 00:08:46.740 William Cheng: So therefore, this code is going to be very safe. All right. In our example over here, once we've done this over here, we're going to go back to the infinite loop. And we're going to go see wait again. 65 00:08:47.220 --> 00:08:52.650 William Cheng: Okay, so, so the the job of the child read over here is simply to wait for control see to get pressed 66 00:08:53.250 --> 00:09:03.150 William Cheng: OK. So I guess the lecture, only one or two I call this one the control see catching threat because you can see that this thread doesn't do anything other than waiting for control see to be pressed right 67 00:09:04.440 --> 00:09:13.200 William Cheng: Alright, so for your warm up to. So please understand that this is the preferred way to wait for a signal. Okay, you create a thread, just to catch one signal. 68 00:09:13.620 --> 00:09:19.050 William Cheng: Okay, if you need to catch what will signal, what would you do you create multiple threads each one of them deal with a single signal. 69 00:09:19.530 --> 00:09:22.320 William Cheng: Okay, so that will be the really clean way to wait for signal. 70 00:09:23.100 --> 00:09:36.060 William Cheng: Okay, so, so, so, so I'm going to sort of strongly discourage you to use a signal handler, because they will have learned how to deal with the signal signal asynchronously. Also, there are other reason not to do that. Okay. And that will be the next thing I'm going to talk about. Yeah. 71 00:09:37.590 --> 00:09:43.140 William Cheng: Oh, so let me finish the way over here the segue over here is an atomic operation, you will unlock the signal. 72 00:09:43.380 --> 00:09:54.720 William Cheng: Specify in the set and wait for the signal delivery and one atomic operation. Right. So look at the picture over here is that we're going to unblock the signals in the argument and then we're going to wait for the signal in the argument in one atomic operation. 73 00:09:55.140 --> 00:10:02.580 William Cheng: Okay, so therefore signal. Can I be a delivery in the middle and why signal cannot be delivered before that, because you have the signal block. 74 00:10:03.330 --> 00:10:12.390 William Cheng: Okay, so therefore before you can see where you got to make sure the signals block. So therefore, the signal. Can I get there before signal. Can I be delivered in the middle. So therefore, the signal has to has to be delivered 75 00:10:12.660 --> 00:10:16.800 William Cheng: After the atomic operation. So in this case you are waiting for it. So, therefore, you will not miss it. 76 00:10:17.970 --> 00:10:20.610 William Cheng: OK. So, again, very important to us atomic operation over here. 77 00:10:22.770 --> 00:10:31.020 William Cheng: Alright, so over here should ask the question, what is this, you know, what is the signal is generated what the process is blocked in the system call 78 00:10:32.010 --> 00:10:45.780 William Cheng: Okay, so the way we sort of talked about is that you know you know what we press Control C. Right. You know, it happens at the Colonel. The Colonel is going to come into user space. Try to borrow one of your threat to to execute the signal handler. What if your threat is inside the Colonel. 79 00:10:46.830 --> 00:10:56.730 William Cheng: Okay, so we have really talked about that case. Right. We talked about the cases that you know you're inside the Colonel, you come to make up call. You're going to borrow one of the user threat over here to call the signal the signal handler user space. 80 00:10:57.300 --> 00:11:04.260 William Cheng: If you're, if that they're already made it made a system call wasn't that there is going to be inside the Colonel. So, so in that case not gonna work out. 81 00:11:05.040 --> 00:11:10.620 William Cheng: Okay, so we'll be here. So they asked the question, well, how do you deal with it if the threads already inside of Colonel now. 82 00:11:11.130 --> 00:11:16.140 William Cheng: So as it turns out that you know there are, there's this design decision to be made for operating system design people 83 00:11:16.560 --> 00:11:20.160 William Cheng: So there are three different choices. One is the deal with it when the system called completes. 84 00:11:20.820 --> 00:11:26.100 William Cheng: Okay, so therefore, in this case, when a signal is going to get the liver. Well, you won't be able to deliver it. You're gonna you're gonna you're gonna stay pending. 85 00:11:26.400 --> 00:11:33.690 William Cheng: Until this thread returning to the user space and then you borrow it and to deliver signal. Okay. As it turns out, you know, people don't really do that. 86 00:11:34.500 --> 00:11:40.890 William Cheng: The other approach is that you're going to interrupt the system call right you're in them in the middle of the system call what you would do is that 87 00:11:43.650 --> 00:11:48.720 William Cheng: Is it going to tell tell tell the Colonel, so to say hey you know whatever you're doing, don't do it. Now go deliver the signal. 88 00:11:48.930 --> 00:11:59.190 William Cheng: Okay, how do you deliver a signal. Right. So in that case, your kernel said, well, why did you return back into the user space to execute the central handler and then come back into the kernel and then finish what he was doing before. 89 00:12:00.330 --> 00:12:09.270 William Cheng: Okay, so that sounds a little weird. So typically again criticism designer don't don't don't do like that like that, typically a modern operating system. They take the third choice. 90 00:12:09.990 --> 00:12:19.410 William Cheng: And then. So the third show is over here that we're going to interrupt the system calls okay your threat is inside, Colonel. And now we're going to interrupt the system called we actually we're going to abort the system call 91 00:12:20.430 --> 00:12:24.270 William Cheng: Okay, we're going to tell the system called as a human aboard. So whatever you're supposed to do. Don't do it anymore. 92 00:12:25.200 --> 00:12:38.160 William Cheng: Okay, sounds really bad. Right. But again, this is the this is the usual choice of audiences in their, their designer. So what they will do is that aboard the system call they will deal with the signal and then they will return from the system called to indicate that something has happened. 93 00:12:39.540 --> 00:12:49.530 William Cheng: Okay, so in this case law, but is that we've got, you know, so, so, so, so if the guys actually inserted, Colonel. What we're going to do that we're going to make it go into the user space and in this case it will actually get a signal handler. 94 00:12:49.890 --> 00:12:57.210 William Cheng: And then wipe us out when it finishes is going to come back into the kernel. And then he will aboard the system call it's going to return from the system call with the error code. 95 00:12:57.780 --> 00:13:10.950 William Cheng: Okay, so in that case, you know, you know, this isn't going to return minus one to indicate errors are also indicated will return minus one and the error number will say that this particular system called was a board it because you know the third was borrowed to deliver signal. 96 00:13:12.150 --> 00:13:17.460 William Cheng: Okay, so why does our criticism do that okay because the problem here is that, you know, 97 00:13:18.210 --> 00:13:29.340 William Cheng: This kernel sale was inserted Colonel right it was executing on camera system call if we need to have this this kernel to go back into the user space to actually a signal handler inside the signal handler, you can make more system call 98 00:13:31.230 --> 00:13:41.010 William Cheng: Okay, so please understand that the system, you know, the signal handler, you can make more system called so you go into the user space one if you make the same system called as before. So now you're going to end up with a really weird state. 99 00:13:42.000 --> 00:13:42.900 William Cheng: Okay, so therefore the 100 00:13:43.140 --> 00:13:48.300 William Cheng: System designed to say, well, what we're going to do that we're going to abort the system call you know so that when we finished delivering the signal. 101 00:13:48.420 --> 00:13:55.290 William Cheng: If we end up making multiple system called we hear a mess up, you know, all these state inside of Colonel is going to be okay because the previous system call is going to get a board. 102 00:13:56.550 --> 00:14:07.200 William Cheng: Okay, so therefore all the modern operating system. So to choose this option. Okay, here's a nodal over here says this may be the reason why piece to a condition way may return for no good reason. 103 00:14:07.860 --> 00:14:16.680 William Cheng: Okay, so when you copy definition way right again depends on whether the threat is implementing the user space or is that a colonel. It is possible. Peter condition way is implementing that Colonel 104 00:14:16.980 --> 00:14:27.180 William Cheng: Why, in that case, your threat can be borrowed to deliver signal. Well, so, so, so, so if we get baro baro to deliver signal. Well, it needs to return from Peter condition way with an area code. 105 00:14:28.650 --> 00:14:38.220 William Cheng: Okay, so therefore, in this case, Peter conditional returned for no good reason. And now we know what the reason is. The reason is that that's there was borrowed to deliver signal and now it has returned Erica. 106 00:14:39.570 --> 00:14:46.140 William Cheng: Okay, so that's why you know when Peter conditionally return, you have to check the GAR I just in case of return, really, for no good reason. Okay. 107 00:14:47.580 --> 00:14:48.120 William Cheng: All right. 108 00:14:49.290 --> 00:14:52.680 William Cheng: Okay, so, so, so, so, so, so, so let's take it. 109 00:14:53.190 --> 00:15:02.100 William Cheng: Take an example over here. So now you know that when you make a system called your threat. I can get both the delivery signal. So therefore, every system called can possibly fail. 110 00:15:02.370 --> 00:15:05.700 William Cheng: Okay, even though you think about a system called that you never fail. Well not it can fail. 111 00:15:06.120 --> 00:15:16.950 William Cheng: Okay, so for example we you know we make a recent call there. The reason why I can fail because your threat can be both to deliver SIGNAL SO EVERY TIME WHEN YOU CALL read, you have to put read inside an infinite loop. 112 00:15:18.960 --> 00:15:30.270 William Cheng: Okay, so, so every time when you call read every period is infinite loops and it will look like this. Right. So, so really check the return code is the return code is equal to minus one that you need to find out whether, you know. So what 113 00:15:31.380 --> 00:15:38.310 William Cheng: So why did we fail it refill because he was bound to to to to to deliver a signal. Well, then we just have to read again. 114 00:15:39.300 --> 00:15:52.140 William Cheng: Okay, so therefore it returned mine as well. We need to check to see if the error number equals to EI n Tr Tr meaning that this system called was interrupted okay it be due to due to due to a signal delivery. 115 00:15:53.490 --> 00:15:56.880 William Cheng: OK. So again, this is why people confuse you know interrupt with 116 00:15:57.450 --> 00:16:09.210 William Cheng: A pseudo delivery. They say, oh, you know, secret. Little bit of suffering around but know why. It's because the, the, the name over here. See I MTR. So again, it means that the system call was interrupted or board it because the threat was borrow to deliver signal. 117 00:16:10.080 --> 00:16:12.870 William Cheng: Okay, it doesn't mean that you know signals that yourself. We interrupt now. 118 00:16:13.530 --> 00:16:20.070 William Cheng: Right. So in air anomalies. He is TR, that means that this particular system called was not perform. And then you have to do it again. 119 00:16:20.520 --> 00:16:31.860 William Cheng: Okay, so in this case what I would do is I will continue and we'll try to read again. So it is possible. You will call read and you go, Eric. Eric, can keep doing this many, many times and eventually read is going to return an error. That's not equal to one. 120 00:16:32.610 --> 00:16:41.040 William Cheng: Okay, so in that case you jump out of the envelope. Okay, so what if we return at minus one over here and there is, as it turns out, the air number is not yeah MTR 121 00:16:41.400 --> 00:16:52.560 William Cheng: Okay, so in that case you have to read the man, the man pages will read and find out what other error codes are so if all the articles are bad news. When the case, again, we're gonna we're gonna copy error. And then we can access our program. 122 00:16:54.360 --> 00:17:00.810 William Cheng: Okay, so, so again now every time when you call read is going to be more complicated okay because you need to put read inside the infinite loop. 123 00:17:01.770 --> 00:17:12.330 William Cheng: Okay, so, so you only have to do this if there's a chance that you will your will get borrow to deliver signal. Okay. If it turns out that if your threat guess Sagan blocked. 124 00:17:13.140 --> 00:17:18.600 William Cheng: Well, then in that case, you know that you said when I get when I get borrow to deliver signal. But in that case you can just simply call read 125 00:17:20.130 --> 00:17:23.940 William Cheng: That. So that's another reason we like the way to to have to talk as 126 00:17:25.230 --> 00:17:30.900 William Cheng: We would like it better to use subway, because in that case all the threat they all have second blocked. 127 00:17:32.010 --> 00:17:44.100 William Cheng: Okay. So, therefore, we know that the only threat that will get borrow to deliver signal is the control see catching threat. Okay, so only. That's why we have to worry about calling read and calling right okay all the other thread when they call me. You can just call me directly. 128 00:17:46.140 --> 00:17:53.070 William Cheng: Right. So again, this is read and write is even worse. Okay. Because every time we call right so so so how does it work, right. So when I 129 00:17:53.460 --> 00:18:01.290 William Cheng: Made the racism call the first argument is file descriptor. The second one is the buffer. And then the third argument is, how many fights. I have to write to the 130 00:18:02.070 --> 00:18:07.860 William Cheng: Needs right right to the desk. So let's say that over here buffer point, a point right here and the remaining or equal to 1000 bytes. 131 00:18:08.640 --> 00:18:17.880 William Cheng: Yeah. So what I will do is I will call the right system called the racism called we don't really know how to implement inside of Colonel, so maybe the Colonel. What it will do is that they will write 200 bytes. The desk at a time. 132 00:18:19.020 --> 00:18:23.880 William Cheng: Okay, so good. I just made up number. Okay, so what kind of what is the right 200 bytes, you know, 133 00:18:24.180 --> 00:18:33.000 William Cheng: The over here. Here's the desk right you will take the first one goodbyes over here, right to the desk and then it will take another 200 bytes over here, right to the desk and now this thread is proud to deliver signal. 134 00:18:33.840 --> 00:18:39.990 William Cheng: Okay, so in that case, we're going to upload this you know aboard the system call. So when we bought the system is what should be the return value. Right. 135 00:18:40.530 --> 00:18:55.980 William Cheng: Because what again. What are the racism call when he returns it returns a number of bytes has been written out to the to the desk right so in this case we have written 400 bytes to the desk so therefore it will return 400 136 00:18:58.140 --> 00:19:04.950 William Cheng: Right. So when we return 400 over here is that we asked the organism to write 1000 by to the dis opportunities is that I can only do 400 137 00:19:05.550 --> 00:19:12.780 William Cheng: Okay, so they have a, what should you do well you should move the buffer pointer to 400 bytes downwards and then next time you should only right 600 bytes. 138 00:19:13.020 --> 00:19:23.190 William Cheng: Okay, so therefore, in that case over here. If the number of bytes that get written out to the desert is less than 1000 what you will do is that is that is that your detriment. The remaining by the amount of 139 00:19:23.790 --> 00:19:28.410 William Cheng: By the number of bytes. I have written to the desk and then you also events, the buffer pointer over here. 140 00:19:28.770 --> 00:19:36.750 William Cheng: by that amount. Okay, so this way, the buffer partner will fly right here. And next time when you call right over here. You're going to start writing from here and you only write 600 bytes law. 141 00:19:38.250 --> 00:19:46.440 William Cheng: OK. So again, if you're through it can be borrowed to deliver signal every time when you need to call right, you need to put it in an infinite loop because you can actually get interrupted anytime 142 00:19:47.160 --> 00:19:50.880 William Cheng: That if it turns out you didn't write any data to the desk. You then they will return minus one. 143 00:19:51.510 --> 00:20:00.900 William Cheng: Minus one. What they in that case you need to check. He is it see the numbers, it will be. It is equal to that means that you didn't write anything down to a desk or in that case, again, you need to write again. 144 00:20:02.580 --> 00:20:09.120 William Cheng: Okay. So pretty much every system call where your threat can be borrowed to deliver signal and now you all have to put inside of the infinite loop. 145 00:20:09.810 --> 00:20:19.440 William Cheng: Okay, who wants to record like that or nobody wants to write code like that. So that's why the best thing to do is to have a control C catching threat. So this way all your other thread. You don't have to do infinite loop like this. 146 00:20:20.580 --> 00:20:31.650 William Cheng: Okay, so again I would strongly recommend you to use a control, control see catching threat information two, okay. Don't believe whatever that you see on the internet. Okay, those are really not the right way to do it. 147 00:20:34.560 --> 00:20:46.980 William Cheng: Alright, so here's sort of summary right if a threat can see signal delivery, which means that it can be borrowed to Otter to deliver a signal that every one of the system called over here, you know, they all have to be, you know, putting out of the loop. 148 00:20:47.700 --> 00:20:55.800 William Cheng: Okay, so the best way to do it is that you want the blocks again and all the other threads all the other thing I will call read and write the normal way only the control see catching fire over here will give 149 00:20:56.520 --> 00:21:07.470 William Cheng: You the control it catches read over here, you know, we'll be able to see it to see that you know control. So you get delivered right so it's okay if you segue. Then in that case when congested liver is simply return on segue. 150 00:21:08.250 --> 00:21:16.680 William Cheng: Okay, so, and also as soon as he returned my Segway controversies blocked again and now the not everything will have it will have control. See, blah. If you press Control. See the second time, nothing's gonna happen. 151 00:21:17.550 --> 00:21:20.970 William Cheng: Well, because controls. He's going to the biggest thing is going to become pending. Okay. 152 00:21:21.360 --> 00:21:29.370 William Cheng: Alright, so for warm up to what do you have to do once controversies press. So you have to be the spec right instead of spec. What it will say is that you need to 153 00:21:29.670 --> 00:21:37.530 William Cheng: You know, cancel the packets through and the and the tokens, but you are not allowed to cancel the server thread, because the silver thread need to finish whatever they're doing 154 00:21:38.220 --> 00:21:45.660 William Cheng: Okay, so, so the surface, the surface any if the server is transmitting a packet. You must allow the server to finish transmitting that packet net 155 00:21:46.230 --> 00:21:51.630 William Cheng: So in this case, what, what, what should you do right so in this case. Well, you know, since you are interacting with, you know, 156 00:21:52.470 --> 00:21:58.980 William Cheng: With with with all the other thread over here. What you should do is that you should let everybody else know that control C has been pressed 157 00:21:59.430 --> 00:22:07.560 William Cheng: Know, guys. And this guy. He was at a global variable. But in order for you want to set a global global the global variable is being shared by all the other thread. So you have to lock them your tax. 158 00:22:08.160 --> 00:22:11.550 William Cheng: That. So as soon as cigarette return what you should do is that you lock them in tax. 159 00:22:11.760 --> 00:22:17.430 William Cheng: And you do everything that you need to do with the new tax law and then you unlock the new tax and now this track and actually self terminate. 160 00:22:17.610 --> 00:22:21.150 William Cheng: Right. Because, Control C has been pressed. Okay, so what you would do is that you would like to be a text. 161 00:22:21.330 --> 00:22:28.260 William Cheng: You said that global flag is that control C has been pressed. And again, if you read the spec, he will tell you what is supposed to happen. So, for example, 162 00:22:28.560 --> 00:22:43.350 William Cheng: The package thrash and every January. Another packet once, Control C has been pressed and also the, the, the token three is not well generally not a token. And then the server threat if they are serving a packet. It gets to finish the packet, but it's not allowed to to serve a new package. 163 00:22:44.580 --> 00:22:54.870 William Cheng: Okay, so, so, so what did you lose that you need love to be text over here printer. The printer message to say, Control C has been press OK. So now you know nobody should do do do do anything extra. 164 00:22:55.530 --> 00:23:04.050 William Cheng: You said a global flags that control C has been pressed, you can cancel the packet arrival thread a toga deposits were using the P threat cancellation mechanism with them, talk about 165 00:23:04.950 --> 00:23:09.510 William Cheng: Okay. And then what you will do is that in case the server. There are sleeping because Q2. 166 00:23:09.870 --> 00:23:15.390 William Cheng: Q3 is actually empty. So what you need to do that, you need to wake up all the server thread so that they can they can self terminate. 167 00:23:15.840 --> 00:23:19.890 William Cheng: Okay, so therefore with them. Your tax law. You should also broadcast the condition variable. 168 00:23:20.280 --> 00:23:31.140 William Cheng: Okay. And also, if there's other thing that you need to do, again, you do you do all these things and then you unlock the new tax and then you finally use the in this case the you will, you know, a self terminate. 169 00:23:32.400 --> 00:23:36.600 William Cheng: Alright, so again if you have questions about this particular process, you know, get semi now right 170 00:23:38.700 --> 00:23:49.650 William Cheng: Alright, so the textbook. I shouldn't give you a bunch of function to say that these are the kind of functions that are safe inside of signal handler and you can see that they're resilient functions. Okay. Nobody can remember this list. 171 00:23:50.100 --> 00:23:55.290 William Cheng: Okay, so therefore, in general, what you should do is that if you want to use the signal handlers. Okay, that's not recommended anymore. 172 00:23:55.860 --> 00:24:02.280 William Cheng: Okay, but if you're happy as a signal handler, you need to make this simpler handler as simple as possible. Right. As soon as you get into a sigma handler, you need to get out. 173 00:24:03.030 --> 00:24:09.510 William Cheng: Okay, so one thing that you should try is that you should, if you are playing around with a signal handler. You can set a breakpoint inside of signal handler. 174 00:24:09.720 --> 00:24:16.710 William Cheng: Where you get inside a signal handler over here. When you get a breakpoint in there. You type where and you're going to see that GDP is confused. It doesn't know where it is. 175 00:24:18.000 --> 00:24:28.470 William Cheng: OK, because typically when when when you type where it will show you that you know main core function called x x or y or y equals z. And now you're in Z and it will show you show you the stack trace. But if you're inside the signal handler. 176 00:24:28.890 --> 00:24:40.380 William Cheng: GDP is confused. Okay, so therefore it's trying to tell you that the inside the signal handler is actually not a very good place. So therefore you should stay out of it as much as possible. Okay. Alright. So again, always you sing in now you signal. 177 00:24:41.400 --> 00:24:46.350 William Cheng: Alright, so now we're going to talk about the next topic over here which is peace through a cancellation. Okay. 178 00:24:46.740 --> 00:24:53.310 William Cheng: So in this case, again in warm up to we we press Control. See you like to be times that you're supposed to cancel all these other threats. OK. 179 00:24:53.880 --> 00:25:02.700 William Cheng: So the terminology of you is going to be called cancellation. So you have one third running over here. Here's a another three years. Three be there so through a is going to cancel therapy. 180 00:25:03.090 --> 00:25:08.700 William Cheng: Is going to tell you, happy to say, hey, why don't you, why don't you self terminate. OK. So the idea here is that 181 00:25:10.200 --> 00:25:17.940 William Cheng: Three is going to cancel therapy and therapy is going to deviate. It's normal for execution and then what it will do is that it will call P threat. Exit 182 00:25:19.920 --> 00:25:29.340 William Cheng: Okay so through a cancel therapy and thread be will copies or access or the terminology that piece for us over here is that you know so so thread as well as piece or it 183 00:25:30.060 --> 00:25:42.990 William Cheng: Will cancel do A, B, and C A b will act on cancel by calling piece at exit. There's a one star be copyrighted. Then it never goes back, whereas over here is your uncle Peter likes it. And then at the end over here, it will terminate at the end of Peter exit. 184 00:25:44.010 --> 00:25:48.450 William Cheng: Record. Again, the only way for her to die is for the threat to copy to exit itself. 185 00:25:48.810 --> 00:25:59.010 William Cheng: You cannot have another thread copies of exit for for for another thread. There is no way for you to actually kill another thread. So therefore, the only thing you can do is that you you ask the other thread to die nicely. Yeah. 186 00:25:59.850 --> 00:26:07.380 William Cheng: Alright, so, so what do we have to worry about. Right. So there are two things we have to worry about, you know, that we can actually, you know, get get get cancelled done 187 00:26:08.040 --> 00:26:13.980 William Cheng: In a bad time. Okay, so for example threat be over here, what in therapy over here is in the middle of modifying linguists 188 00:26:14.370 --> 00:26:25.470 William Cheng: Okay therapy is calling my phone to lift a pen is trying to modify the lenders and then you tell services. The. Hey, stop what you're doing over here. Go call piece right exit. As soon as he does that, the link is going to be in bad shape. 189 00:26:26.160 --> 00:26:30.360 William Cheng: Okay, so therefore later on some other things going to use that lingo is going to end up with a segmentation call 190 00:26:31.440 --> 00:26:38.310 William Cheng: Okay, so therefore we're going to make sure that the first thing we need to make sure is that you know threat be over here needs to make sure that it's not in the middle of modifying linked lists. 191 00:26:39.150 --> 00:26:44.190 William Cheng: Okay, so he has to wait wait until they finish modify and English and then maybe you can act on Castle. 192 00:26:45.150 --> 00:26:51.450 William Cheng: Or so this is that we're gonna see how to do that. And the second one over here is going to be cleaning up. So, so what if he has a new tax law. 193 00:26:52.320 --> 00:26:58.320 William Cheng: Okay, Serbia, you know, so every call piece of meat. I love over here. You tell us through. I've been to die and therapy called Pisa Pisa. Exit 194 00:26:58.560 --> 00:27:04.680 William Cheng: And now, who's going to unlock them attacks. There's nobody can unlock them attacks only threat beacon unlocking the text. Okay. So, therefore, this 195 00:27:05.010 --> 00:27:13.140 William Cheng: Is no as cleaning up in therapy has been piece of it as law. You got to have a way for therapy to call piece for me to unlock. Otherwise, you're gonna you're gonna end up 196 00:27:13.500 --> 00:27:25.050 William Cheng: Which will get another case is that what if I be has a file open. Okay, so I'd be open a file. So in that case, you knew you want to repeat also close the file. What if thread be over here called Malik. 197 00:27:25.860 --> 00:27:33.390 William Cheng: OK, so the allocate memory. But now you want to happy to die. So therefore, there's no use for that memory. If you don't free up the memory where you can enable the memory leak. 198 00:27:34.320 --> 00:27:43.860 William Cheng: Okay, so I get this is sort of the first time we can worry about memory leak. So it's therapy allocate memory, what we need to get give me a chance to to actually clean up the memory. Yeah. 199 00:27:44.910 --> 00:27:53.190 William Cheng: Alright, so let's talk about the first case over here. Okay. We want to make sure that it is not modified and wingless you know when it's when they don't cancel. Okay. 200 00:27:54.960 --> 00:28:07.320 William Cheng: Alright, so, so the way you can sort of threat is to call the function PCI council. So in this case to a over here will be calling PCI council and thread be over here will be the, the argument. So in this case, through a will call Peter Cancel, cancel therapy. 201 00:28:07.740 --> 00:28:17.790 William Cheng: Okay, this function is not a blocking call. So, therefore, this function return right away. So if you have one only one CPU that three acres to continue to execute. So I be never get the execute 202 00:28:18.690 --> 00:28:26.790 William Cheng: Because again, if you don't have one, one CPU. Right. So a copy of our class over here through a continue to run inside a CPU without giving the CPU to any other threat. 203 00:28:28.170 --> 00:28:35.970 William Cheng: Okay. Now of course the obvious isn't can always take away the CPU, but in this example, it is possible. You will call PCI council and then nobody else gets to run. And so I 204 00:28:36.390 --> 00:28:44.760 William Cheng: Will continue to execute. Okay, so I just want to point out that it's important to understand that P threat cancel is not a block and called it always return right away. Okay. 205 00:28:45.630 --> 00:28:56.610 William Cheng: All right, so to control when thread be should act on cancel. Okay, we're going to sort of talk about two things that that piece of create that plethora of provides 206 00:28:57.030 --> 00:29:02.040 William Cheng: One is called cancellation state. So throughout be can have the cancellation state and enable or disable 207 00:29:02.370 --> 00:29:10.200 William Cheng: Okay, so if he has cancellation disable and re try to cancel therapy. What's going to have a while the cancellation is going to become pending. 208 00:29:10.710 --> 00:29:20.160 William Cheng: Or as again. We use the same terminology right if therapy has cancellation disable and you try to cancel it. Well, in that case, cancellation become disabled, as I can. So this is going to become penny. 209 00:29:20.520 --> 00:29:31.830 William Cheng: When will cancel it. You can't deliver well when thread be enable cancellation or in that case, you know, cancellation. So, you will get deliver when cancellation get the liberal happens throughout be will copy the exit. 210 00:29:32.940 --> 00:29:40.890 William Cheng: Okay, whereas okay we use very similar terminology, as it turns out that, you know, there's another thing you can control for three IP. You can also set a cancellation type 211 00:29:41.820 --> 00:29:52.140 William Cheng: Okay, so when the cancellations disable it doesn't really matter, matter what type it is when the cancellations enabled through I be can actually control the cancellation type. The only two types. One is called asynchronous. 212 00:29:52.770 --> 00:29:57.390 William Cheng: Okay, so if the cancellation type is asynchronous, then you can actually, I can cancel anytime 213 00:29:58.410 --> 00:30:00.480 William Cheng: Guys. So that's a really bad idea, right, because if therapy is 214 00:30:01.350 --> 00:30:07.470 William Cheng: Manipulating a linguist one. In that case, if we can tie anytime on in that case, you're gonna you're gonna end up with a with a linguist has completely messed up. 215 00:30:07.740 --> 00:30:14.370 William Cheng: Okay, so therefore you should probably should probably never set this cancellation type to be asynchronous. Now the other type is called differ. 216 00:30:15.030 --> 00:30:23.610 William Cheng: Okay, so when the cancellation types of different when you try to cancel that a for cancellations enable threads going to continue to execute co until it 217 00:30:24.390 --> 00:30:31.200 William Cheng: Until we get to something called a cancellation point okay if it hasn't reached the cancellation point the cancellation will continue to become pending. 218 00:30:32.490 --> 00:30:41.100 William Cheng: Okay. So, so what it, what is this is when the cancellations differ so it would defer until thread be get into a cancellation point where I was going to see a list of cancellation point 219 00:30:41.730 --> 00:30:47.280 William Cheng: So, so with Ruby is going to execute to cancellation point that therapy will act on Castle by calling piece right exit. 220 00:30:48.540 --> 00:30:54.240 William Cheng: Okay, so one thing that's good to know is that Malik and free. They are not cancellation point. Okay. So, therefore, if you know if 221 00:30:55.800 --> 00:30:59.580 William Cheng: You and also when you're manipulating manipulating link list again. 222 00:31:00.570 --> 00:31:08.880 William Cheng: Those kind of operation. You just modify memory locations over here. They are not cancellation point typically cancellation points are some of the pieces function call and some of the system call 223 00:31:09.510 --> 00:31:17.430 William Cheng: Okay, so if therapy or just modify memory. In that case it will continue to run instead of CPU until eventually get to a cancellation point Denny Waxman castle. 224 00:31:17.970 --> 00:31:25.980 William Cheng: Okay, so therefore, in that case, when therapy is modifying linguists. And if you think about your coding in warm up one inside my for to this. 225 00:31:26.820 --> 00:31:31.530 William Cheng: When you read a call from my phone to list a pen, do you actually make any system call 226 00:31:32.100 --> 00:31:41.670 William Cheng: Well, you don't make any system for all you do is called Malik. And again, I just mentioned Malik is not a cancellation point. Okay, so you Malik, and then you manipulate these addresses up like that and then you return 227 00:31:42.480 --> 00:31:56.220 William Cheng: Okay, so therefore inside my afford to lose a pair, you're adjusting all these pointers, so, so, so, so if you'll still get cancel it actually get to finish my fortune is a pan. OK, so now you're listening to this state. Well then, then it's possible for you to get canceled. 228 00:31:57.330 --> 00:32:04.770 William Cheng: Right, right. So by default, a threat has cancellation enable and defer. Okay. And this is this down for a good reason. 229 00:32:05.010 --> 00:32:12.330 William Cheng: Right, because you want to be able to cancel your threat because whatever you're doing is doing, you know, another set to say we don't need that results anymore. So you need to die as soon as possible. 230 00:32:12.630 --> 00:32:20.220 William Cheng: That it needs to be different because because we want to make sure that you are not, you know, modifying the limitless, so therefore it's always good to have cancellation and NATO and defer 231 00:32:20.430 --> 00:32:25.200 William Cheng: That if you want to change it. You better have a really good reason. Okay, otherwise don't change it. Okay. 232 00:32:27.690 --> 00:32:30.150 William Cheng: All right, so I get this is the stuff I just 233 00:32:30.690 --> 00:32:42.720 William Cheng: I just mentioned right when he tried to cancel thread be therapy can have cancellation enable or disable it therapy has cancellation disable what then in this case consortium become pending, and then when 234 00:32:43.080 --> 00:32:51.300 William Cheng: You know when when when when therapy has cancellation enable what, in that case, we have to look at cancellation type guy if the cancellation type is asynchronous then 235 00:32:51.630 --> 00:33:08.070 William Cheng: You know, they, the, the therapy will act on cancel immediately by calling people at exit. Okay. If the cancellation tight is differ therapy will continue to execute until to get to a cancellation point when you get to a cancellation point what he would do that. It will copy to exit. Yeah. 236 00:33:10.350 --> 00:33:18.780 William Cheng: Alright so here is a short list of cancellation point. I think this list is coming from the textbook textbook was written written in 2011 2012 237 00:33:19.470 --> 00:33:27.360 William Cheng: So right now, if you look at it, look for cancellation point in a piece of library, the list is much longer. Okay, so those those people keep adding stuff to this list. 238 00:33:27.840 --> 00:33:37.950 William Cheng: So we're just going to sort of briefly take a look at what's inside this list all the file system related system called they're all cancellation points. Okay, so, so over here. There's open right and there's close 239 00:33:38.430 --> 00:33:45.990 William Cheng: There's read, and then there's ways right or writers right here. Okay, so, so, so again if you make a file system call well then that case. 240 00:33:47.220 --> 00:33:55.080 William Cheng: If you send a cancellation penny. What do you, what do they will act on Castle. Yeah, and also the piece or a library called over here. A Peter condition way. 241 00:33:55.500 --> 00:34:02.100 William Cheng: Okay, so if you're waiting for condition to to to to to to become true. In that case, you can actually act on Castle. 242 00:34:02.880 --> 00:34:10.710 William Cheng: Piece to join. So again we your threat goes to sleep that will kind of that they'll kind of be a good place. You know, for, for the actual cancel that. 243 00:34:11.160 --> 00:34:20.580 William Cheng: There's a weird one over your core piece or a test cancel that. So what happened is that, you know, when let's say you're a over here is gonna cancel threat be, why does it a try to cancel their be 244 00:34:21.060 --> 00:34:24.180 William Cheng: Well, because whatever third be is doing. We don't need the results anymore. 245 00:34:24.630 --> 00:34:30.750 William Cheng: Okay, so for example if you have a web browser, you click on some you click on something you want to do something and then you say, I want to cancel it. 246 00:34:31.290 --> 00:34:38.010 William Cheng: Okay, so when you want to cancel that means that I don't really care about that result anymore. I want this operation to get castle as soon as possible. 247 00:34:38.730 --> 00:34:48.180 William Cheng: Okay, so, so, so, so, so in this case over here. We're gonna cancel it and then we want to act on cancel right away. What if VoIP is doing something that will take a very, very long time. 248 00:34:49.050 --> 00:34:50.460 William Cheng: Okay, and there's no cancelation point 249 00:34:50.820 --> 00:35:01.980 William Cheng: Okay, so, so typically through IP. What I'll do is, if you think about all your co, you can always stay inside Hulu. So if you stay inside a loop over here inside the entire loop. There is no cancelation point well their therapy over here will be actually for very long time. 250 00:35:02.760 --> 00:35:04.800 William Cheng: Okay, so what kind of code doesn't have any cancellation point 251 00:35:05.790 --> 00:35:12.750 William Cheng: Those of you who are, you know, sort of familiar with your encryption, decryption when you run encryption, decryption code you're doing you know number crunching 252 00:35:12.990 --> 00:35:21.900 William Cheng: So therefore, none of the operation of their system call. Okay, so, so you will see none of these function there. So, therefore, in that case, you can take a very long time. When you're doing encryption, decryption. 253 00:35:22.860 --> 00:35:27.390 William Cheng: Okay. So, therefore, what you should do is that if you see things that are type new without any 254 00:35:28.410 --> 00:35:36.240 William Cheng: Any cancellation point you can add an artificial cancellation point. So I will piece to a test cancel right here. So, therefore, every iteration. What it will do is it will 255 00:35:37.140 --> 00:35:47.220 William Cheng: It will hit a cancellation point if it turns out cancellation is pending. Why, in this case, your, your therapy over here will copy threat exit right away. And now you know that that the operation will be cancelled. 256 00:35:48.930 --> 00:36:01.620 William Cheng: Okay. The other example I can think of is that, you know, if you are a W person you do some kind of a simulation. When you do a simulation. You know you special event driven simulation. Again, there's no cancellation point inside this this big event loop. 257 00:36:01.860 --> 00:36:09.120 William Cheng: So in that case, maybe you want to add P three tests can sew ins are loop over here. So this way. When you say I'm not interested in the simulation anymore. Please die right away. 258 00:36:10.050 --> 00:36:13.140 William Cheng: Okay, so this guy will act on Castle when you hit a cancellation point 259 00:36:13.800 --> 00:36:22.020 William Cheng: That a bunch of the signal or related a function again if you throw it goes to sleep, you know, chances are, they are cancellation points will be. Yeah. 260 00:36:22.560 --> 00:36:30.240 William Cheng: So please notice I please read new tax law piece of meat along a lot. They are not on the list and also Malik and free and they're not on the list right 261 00:36:32.280 --> 00:36:36.270 William Cheng: Alright, so the second part over here is that, you know, how do you 262 00:36:37.020 --> 00:36:48.420 William Cheng: How do you make sure that you can actually clean up your co when you have to meet tax law he want to be able to unlock it where you, you know, Mel allocate memory and you need to free air when you open a file you want to be able to close it. 263 00:36:48.810 --> 00:36:56.820 William Cheng: There. So therefore, the way that this is done in peace, right, is that when you act on Castle, which means that you start executing P through and exit. So instead of corporate piece of 264 00:36:57.420 --> 00:36:59.490 William Cheng: What he will do is that it will do the following. 265 00:37:00.270 --> 00:37:11.340 William Cheng: That's an insight. Peter exit. Right. So, so either you call it directly or you ask them Castle, or whatever, it doesn't really matter. So inside piece of access, what do you do that it will first walk through a stack of cleanup handlers. 266 00:37:11.790 --> 00:37:15.810 William Cheng: Okay, so, so, so, so again I start to look like this. Right. So here's a stack of stuff over here. 267 00:37:16.500 --> 00:37:25.260 William Cheng: Inside the stack over here, they are clean up handlers. Okay. So for example, if you want to clean up allocate memory, what would you do well, you will call free on the pointer. 268 00:37:25.830 --> 00:37:38.730 William Cheng: Okay, so therefore, one of the stuff inside the inside the clear header over here will be the function pointer free. OK. So again, free. It's just a function. Right. So all you need to do a store the address of free. And then the second one second argument over here will be pointer. 269 00:37:40.020 --> 00:37:51.090 William Cheng: Okay, so this will be a stack of clear routine. So maybe this is pointer one maybe also want to free up a point or two. I'll be here is I'm going to say three pointer to over here. So when I act on Castle. 270 00:37:51.270 --> 00:38:02.250 William Cheng: I'm going to walk through the stack of cleanup handler over here. I'm going to go this way and start calling the function. Okay, so I'm going to call free point or two and 3.21 and when I get to the bottom over here. My threat was self terminate. 271 00:38:03.930 --> 00:38:11.310 William Cheng: Okay, so how do you actually build up this stack of cleanup handlers. Okay, so there's a there's a function that you can you can call. It's called P through a cleanup push 272 00:38:11.490 --> 00:38:17.220 William Cheng: It takes two arguments, the first one. The girl is going to be a function pointer and the second one over here is going to be a voice star. 273 00:38:17.760 --> 00:38:22.740 William Cheng: Okay, so therefore, again, you can see that these two things actually compatible with that free is a function pointer. 274 00:38:23.130 --> 00:38:30.390 William Cheng: The function returns, nothing. And one of the, it takes one argument that's the voice Dar, and the second argument over here is going to be a 32 bit of value, which is the voice. 275 00:38:31.050 --> 00:38:39.600 William Cheng: Okay, what other things can you push. Okay. So for example, if you open a file, right. So again, you know, inside this does the cleanup stuck over here. We're going to have free 276 00:38:39.840 --> 00:38:48.060 William Cheng: Followed by a pointer. Okay, if you open a file. You can also call close on it. So, therefore, in this case we can push on to the stag over here with the function close 277 00:38:49.080 --> 00:38:56.430 William Cheng: That. So what is the argument for clothes. It's going to be a file descriptor right the file descriptor is an integer is a 32 bit number. So again, it's compatible with voice. All right. 278 00:38:56.850 --> 00:39:03.300 William Cheng: Okay, if you call piece of your tax luck. Well, then in that case the cleanup. Thumbs up for me to unlock right so we had an appeal to a 279 00:39:03.600 --> 00:39:12.750 William Cheng: New tax unlock over here a lot right here. And what is the argument. The argument is the address of a new tags. Again, that's a 32 bit value again compatible with voice. 280 00:39:14.070 --> 00:39:19.770 William Cheng: Okay, so all these function will happen is that you know you know Ii Ii Ii 281 00:39:20.430 --> 00:39:25.500 William Cheng: If you'd like me to ask you got to remember that you put push piece of me to unlock into the cleanups that 282 00:39:25.830 --> 00:39:34.890 William Cheng: If you open a file you to call you to push onto the stack the clothes function and also if you allocate a pointer than in this case you need to free it but 283 00:39:35.400 --> 00:39:41.370 William Cheng: What if your threat doesn't. What is your threat you know doesn't you know the castle. 284 00:39:42.120 --> 00:39:51.150 William Cheng: Okay, if you're still, it doesn't get cancer one. In that case, what happened is that you know you lock them you texting you unlock them attacks and now when your self terminate. When you self terminates can call piece of an exit. 285 00:39:51.840 --> 00:39:57.240 William Cheng: Okay, so in that case when it called piece of access. What if you unlock me times a second time for them is not good. It's no good. 286 00:39:58.290 --> 00:40:05.370 William Cheng: Then you progress gonna crash. What if you close the same file twice. Well, as it turns out. Clothes. Clothes in the same file twice, it's actually, it's not a big deal. 287 00:40:05.940 --> 00:40:09.390 William Cheng: Okay. What if you free, the same point or twice. Well, again, that's really terrible. 288 00:40:10.050 --> 00:40:17.670 William Cheng: Okay. So out of these three operations over here. You are not allowed to allow me to access the same the same title is also, you are not allowed to free the same point or twice. 289 00:40:18.420 --> 00:40:22.800 William Cheng: Okay, so therefore, in this case, we need to pop them off the cleanups that because otherwise. 290 00:40:23.160 --> 00:40:31.890 William Cheng: The you your programs gonna crash. Yeah. So the function that we use is going to copy through a clean up pop up called Pisa clean I'll pop it will go to the cleanup stuck over here. 291 00:40:32.100 --> 00:40:43.440 William Cheng: And it will pop off the first item over here off the stack and that the argument of your can be zero or one. If it's equal to zero, that means that don't execute that function if this argument over here equals two, one, that means that execute function. 292 00:40:44.220 --> 00:40:50.070 William Cheng: Okay. So, therefore, if you if you say actually go to one. Then in this case, he will actually call piece of music on luck with the argument of the new tax. 293 00:40:50.250 --> 00:40:56.310 William Cheng: And then if it turns out that what's on the top of the stack up here is the clothes function. Well, in that case over here, you're going to be don't close the file. 294 00:40:56.520 --> 00:41:02.280 William Cheng: And if it turns out that you know the stack will be only has one element over here that's called free the pointer. So in that case three pointer will be called 295 00:41:02.670 --> 00:41:11.430 William Cheng: That. But if the argument over here is zero. The you simply pop it off the stack and then you don't do anything. Okay, so, so you as a programmer. You decide whether you want to execute a function or not. 296 00:41:12.900 --> 00:41:15.960 William Cheng: Alright, so let's put all these things together and let's take a look at an example. 297 00:41:16.380 --> 00:41:22.920 William Cheng: The example over here. It's called gather data. So the, so the idea here is that here is a thread. Okay, this is a web crawler 298 00:41:23.190 --> 00:41:31.050 William Cheng: Guys, what it will do is that it will visit a lot of sites over here, gathering some data and then it will add data to a global list and this would self terminate. 299 00:41:31.440 --> 00:41:42.030 William Cheng: That are so the way this will work is I'm going to create a data structure over here. I'm going to call Malik to create this kind of item over here. And then we're going to call the function and this is the sort of the, the workhorse 300 00:41:42.510 --> 00:41:54.630 William Cheng: For the you know the full for the web crawler so in the so what I call it did. I'm over here. I need to pass the data structure so that when it collect a bunch of data, it will add data, you know, to, to, to this data structure. 301 00:41:55.260 --> 00:42:02.430 William Cheng: Okay. When I finished gathering data over here to get gather data will return. So in this case, I'm going to add the item which was allocated right here. 302 00:42:02.640 --> 00:42:13.650 William Cheng: I'm going to add it to the globalist right over here called insert. You can also get a pen, doesn't really matter. So, so the idea here is that this threat. When it finished crawling the web is going to take all that data editable globalist 303 00:42:14.070 --> 00:42:26.940 William Cheng: Okay, so another threat will process the data will process the data. There are so, so when it's finished doing this. I'm going to print a message onto the screen to say I'm done. And then I will return from your phone. The first procedure and now the server that 304 00:42:28.620 --> 00:42:37.800 William Cheng: Okay, so the question over here is that we look at this code over here, right. So this, this is the first procedure of the threat is nice and simple. You should be able to figure out where all the cancellation points are 305 00:42:38.790 --> 00:42:47.730 William Cheng: Okay, so where are the cancellation points with this call over here so you can actually go through it line by line by line Malik Malik is not a cancellation point get data item over here. 306 00:42:48.960 --> 00:42:57.630 William Cheng: That does it contain a cancellation point. Okay, the answer is yes because get did item actually contain Brazilian cancellation point right because because 307 00:42:58.320 --> 00:43:07.830 William Cheng: How does a web crawler work what opens up an Internet connection is going to go crawl crawl around the web. And then it's going to go. So when you open up Internet. Internet connection is going to open 308 00:43:08.370 --> 00:43:12.060 William Cheng: Right, because that's the only way to talk to any devices. So if you want to talk on the internet. You gotta call back 309 00:43:12.270 --> 00:43:20.970 William Cheng: And we try to read data from the internet. You're gonna call read right he's gonna call. We're going to call right going to do a bunch of that Indian ago called close so there's plenty of get 310 00:43:22.380 --> 00:43:33.780 William Cheng: So, so every time when you try to connect to a website, you're going to open read, write, rewrite rather than close. OK. And then you go to another website going to do the same thing over and over again. So inside get that item there zillion cancellation points. 311 00:43:34.230 --> 00:43:43.800 William Cheng: Okay, what about insert. Right. So again, think about your calling my for tool is a pen. There's no cancelation point there's Malik I'm Alexander cancellation point okay printer. 312 00:43:44.340 --> 00:43:49.260 William Cheng: Printer is a rapper function around the right system calls. So therefore, there's a cancellation point there. 313 00:43:49.560 --> 00:43:59.250 William Cheng: Okay, and then we return over here while then return it doesn't have a cancellation point. So inside this entire code over here. There's only two places that had cancellation point. And those are the two places. I have to worry about. 314 00:43:59.670 --> 00:44:03.840 William Cheng: Right, because those will be the only two places I can act on Castle. Okay. 315 00:44:05.220 --> 00:44:12.090 William Cheng: So, so, so, so, so what is the problem we're trying to solve what we hear it. Okay, so let's say that your favorite Castle is targeted at me, excuse 316 00:44:12.480 --> 00:44:17.250 William Cheng: Me Your web portal is going to take a long time. What if your thread dies right here, you're going to end up with a memory leak. 317 00:44:17.880 --> 00:44:22.440 William Cheng: Why, why would he end up with a memory leak, right, because the item over here is Malik, but you're nobody freeze it. 318 00:44:22.950 --> 00:44:29.460 William Cheng: Okay, so therefore, in that case, you know, you're gonna end up with a memory leak. Okay, so how do we prevent this code from getting a memory leak. 319 00:44:30.060 --> 00:44:40.770 William Cheng: Know what the solution is very simple. Right. One of the simple solution over here is that I can simply call I can simply disable cancellation right here and then enable cancellation. Once I finish getting the data item. 320 00:44:41.340 --> 00:44:50.970 William Cheng: So therefore, one solutions like this, right, I'm going to cause a piece of ice that castle state. And then I'm going to disable cancellation. So now when you're inside get that item over here you can see 321 00:44:52.140 --> 00:44:54.690 William Cheng: Somebody tried to cancel. You can select is going to become pending. 322 00:44:54.930 --> 00:44:57.390 William Cheng: And then eventually, whether you've returned from get that item over here. 323 00:44:57.570 --> 00:45:05.730 William Cheng: We're going to set a council estate over here to be enabled. So again, by default cancellations differ. So therefore, at this point, nothing's going to happen right cancellation is still going to be pending. 324 00:45:05.970 --> 00:45:11.610 William Cheng: And then when you call insert, there is no cancelation pointing is I insert. Well, so therefore you continue to execute 325 00:45:11.850 --> 00:45:24.360 William Cheng: And finally we call Pranav, you're going to end up calling the racism called and racism call is a cancellation point. So now your threads going to die inside you know again die means that act on Castle is going to call piece or exit inside the racism call 326 00:45:25.200 --> 00:45:26.880 William Cheng: Okay. So in this case, is there any memory leak. 327 00:45:28.530 --> 00:45:41.220 William Cheng: Okay, so there's no memory leak over here because while Vanessa. I call insert the data item over here is edit, you know, is added into a global list. So another threat when it's finished processing the data that thread will free this data item. 328 00:45:42.450 --> 00:45:53.190 William Cheng: Okay. So, okay, I have to trust working together. This one is sort of the producer is producing data editor globalist and nothing is going to pick it up, so therefore it's going to be the responsibility of another threat to free up this data structure. 329 00:45:53.850 --> 00:45:59.310 William Cheng: Okay, so therefore, in this example, we hear, you know, I have no memory leak. Okay. So, problem solved. Right, 330 00:45:59.880 --> 00:46:09.900 William Cheng: The problem with this, with this solution is that this is really not a good solution, right, because you know that are the web crawl over here. One of the work call is going to take hours to, you know, to to to to to crawl the web. 331 00:46:10.740 --> 00:46:16.500 William Cheng: Okay, or even minutes or so that's that this is going to take you two minutes. Okay. So, therefore, when I try to cancel this thread why 332 00:46:16.830 --> 00:46:22.410 William Cheng: Why does not, if they're trying to cancel this where you get it says, I'm not interested in the result of the web crawling anymore. 333 00:46:22.980 --> 00:46:27.270 William Cheng: Okay, so therefore, in this case, when you try to cancel this thread and you need to wait for two minutes. You're gonna be very upset. 334 00:46:27.750 --> 00:46:36.690 William Cheng: By me. We all seen you know you know application to do that. Right. You try to click on the Cancel button and they just refuse the castle. Okay. It's very, very frustrating because they didn't do this properly. 335 00:46:37.560 --> 00:46:48.810 William Cheng: Okay, alright. So the idea here is that even though this is the correct implementation. Right. We don't have any memory leaks. And in the end, everything works out perfectly except that, you know, you know, you try to cancel it this way and this very refused to die. 336 00:46:49.530 --> 00:46:56.040 William Cheng: Okay, so therefore this solution is really not acceptable if we get get daylight and can take a very long time to process. 337 00:46:56.700 --> 00:47:00.690 William Cheng: Okay. What if you know that get data, Adam, can be finished in one microsecond 338 00:47:01.290 --> 00:47:06.720 William Cheng: What, in that case, you know, over here, you're going to disable cancellation and Erica says you only for one microsecond way that is not a big deal. 339 00:47:07.650 --> 00:47:15.390 William Cheng: Okay, so, so, so again if somebody you know sort of show button. Only you cancel that while there's no way for you to click the button in one microseconds. So in that case is really not so bad. 340 00:47:16.290 --> 00:47:24.180 William Cheng: Okay. So it really depends on your application that in our example over here to get daylight and since this is a web crawler. So in this case, it will be really bad. Okay. 341 00:47:26.340 --> 00:47:33.570 William Cheng: Alright so what you should do. Over here, right, is that you know you should shouldn't disable cancellation. Right. So in this case, what you should do is that, you know, in order for you to 342 00:47:34.080 --> 00:47:42.630 William Cheng: Do not leave any memory. You need to call a piece of cleanup push and then the argument over here will be the free function, followed by them. Right. So again, instead of cleanup stack over here. 343 00:47:42.810 --> 00:47:45.600 William Cheng: We're going to have two things in there. I'm going to be free, followed by them. 344 00:47:45.990 --> 00:47:52.890 William Cheng: Yeah. So in this case, when you act on Castle inside get data. And what's going to happen right when you act on cancer, you're gonna call piece right exit. 345 00:47:53.100 --> 00:47:59.190 William Cheng: Inside, Peter. Excellent. We're going to walk down the cleanup stack over here and call all the function there, one after another. 346 00:47:59.730 --> 00:48:10.020 William Cheng: Okay. So in this example, there's only one function in there. So we're going to call free item and now there's no more memory leak. OK. And then as soon as they go to the bottom of the cleanup stack over here and now your threat or self terminate. 347 00:48:11.640 --> 00:48:15.720 William Cheng: Okay, so in this case the coercion work perfectly. But what if you have to. It doesn't get cancelled. 348 00:48:16.680 --> 00:48:21.240 William Cheng: OK. So again, you know, you need to consider all possible cases. If you'll see it doesn't get canceled over here. 349 00:48:21.360 --> 00:48:27.120 William Cheng: Why, in that case what it will do is that it will insert this item to a global list over here and then it will print the message over here to say done 350 00:48:27.300 --> 00:48:32.910 William Cheng: And then when it called return zero over here again will you return for the first procedure, you can end up calling Peter exit. 351 00:48:33.390 --> 00:48:34.590 William Cheng: Okay, so when we got pizza. 352 00:48:34.920 --> 00:48:45.420 William Cheng: So we have, we're going to free the item because we haven't pop it off the cleanup stack. So we're going to call free the item over here and then later on another thread is going to work on the item over here. Now the either can turn to garbage. 353 00:48:45.690 --> 00:48:55.500 William Cheng: Right, because once you free it, it might be reallocated or, you know, if the item doesn't does going to reallocate it when the other three guys that have called free for the second time while they're going to end up getting a segmentation fault. 354 00:48:57.480 --> 00:49:07.440 William Cheng: Or. So in this case, you know, it will get doesn't work if you don't cancel the threat, guess what you should do is that as soon as you return from get that I'm over here. You should call piece right clean up pop 355 00:49:07.800 --> 00:49:12.210 William Cheng: Get to to get rid of this guy over here, then in that case it will work nice over here. I'm gonna call Peter 356 00:49:12.660 --> 00:49:18.390 William Cheng: So is that what we should do is that I need to call Peter cleanup pop over here. Okay, the argument should I use 01 357 00:49:19.110 --> 00:49:28.860 William Cheng: So remember, at this time, I have free item in here item over here instead of cleanups that if I set it to one, I'm going to call the free item in that case, I'm going to end up with an item price again. 358 00:49:29.520 --> 00:49:37.620 William Cheng: Okay, so therefore I need to use zero so that this thing will get pop off the stack over here. And then in that case it will not you know that you will not get executed now. 359 00:49:38.220 --> 00:49:40.740 William Cheng: So in that case, you know, what if you actually get a cancellation right here. 360 00:49:41.490 --> 00:49:49.680 William Cheng: Okay, if you get if you got a third Telecaster you right here. We're going to still insert the item over there to a global list. And then when we call printer will be here. 361 00:49:50.430 --> 00:50:00.300 William Cheng: Is a prenup there's racism called going to add on castles. Again, there's no memory leak and everything will be fine. Okay. And also if you if you don't get cancel here still everything will be executed just fine. 362 00:50:01.380 --> 00:50:04.320 William Cheng: Okay, so therefore this code over here will be the right way to do it. 363 00:50:04.950 --> 00:50:08.100 William Cheng: Okay. All right. So in Peter library. They want to make sure that 364 00:50:08.340 --> 00:50:18.690 William Cheng: You know, every time when you call piece or clean out, push you don't forget a piece of clean up a pop. They actually going to require you to put piece right cleanup push and Peter clean our pop. They need to match. 365 00:50:19.440 --> 00:50:30.330 William Cheng: They need to match with each other like a pair of curly brackets. OK. So the way you should think about is that in piece of cleanup push over here, there's a there's a hidden left curly bracket and 366 00:50:31.110 --> 00:50:34.440 William Cheng: Right after the piece of cleanup pop over here, there's a hidden right curly bracket. 367 00:50:34.800 --> 00:50:44.400 William Cheng: Okay, if you have only one of them and don't have the other way. And while the duck is going to get a compiler error and you're gonna get a real really we're compiling mentioned to say hey you know there are some a curly bracket that doesn't match up. 368 00:50:44.760 --> 00:50:48.600 William Cheng: Okay, so in that case, the compiler trying to tell you that you forgot you forgot to mash mash them up. 369 00:50:48.990 --> 00:50:56.670 William Cheng: Okay. So, therefore, what you are not allowed to do is to to have inside one function. So, so you call a function to do, Peter. Peter can now create a push. 370 00:50:56.880 --> 00:51:04.140 William Cheng: And then you call another function to go Peter can pop. So in that case, again, the parentheses, the curly brackets not going to match you gotta get a compiler. Compiler error. 371 00:51:04.950 --> 00:51:05.250 William Cheng: Then 372 00:51:05.790 --> 00:51:12.690 William Cheng: Alright, so what if you caught piece of it cannot push a three times right the first time you called free and the second one you call close and the third party copies of john law. 373 00:51:12.810 --> 00:51:18.240 William Cheng: So in this case, you got to make sure that you call a piece of clear pop three times, otherwise you're going to get a compile time air. 374 00:51:19.140 --> 00:51:26.670 William Cheng: Well, alright, so in this example we push the free function. Right. You can also write your own function as over here I have a function called close file. 375 00:51:26.850 --> 00:51:36.510 William Cheng: As long as this function returns nothing and take one argument that's a 32 bit value. So in that case, I can copy three clean up, push you know for my own function and then I can supply any argument I want 376 00:51:36.990 --> 00:51:44.520 William Cheng: OK. So again, it could be address of a data structure or whatever. So what so you can read your own program and he can do all your clean out using one function. Okay. 377 00:51:45.960 --> 00:51:50.310 William Cheng: All right. Finally, the last one over here is you know is peace or condition. Wait. 378 00:51:51.180 --> 00:51:56.010 William Cheng: So I mentioned before, if you call piece of it clean up you know if you call piece of meat AX log over here and then 379 00:51:56.400 --> 00:52:02.820 William Cheng: You know, you can get Castle, you're going to end up with a new tax law, right. So one thing that you should do is that you need to call piece of it clean. I'll push 380 00:52:03.000 --> 00:52:09.960 William Cheng: And then over here, you're going to call P threat. You know, so P thread new tax unlock right and then put the address of meetings over here. 381 00:52:11.310 --> 00:52:18.840 William Cheng: Okay but but in that case, you know, is the code is going to work. So, so, so let's take a look at this example here. So in this case, we're using a garden command. 382 00:52:19.530 --> 00:52:34.350 William Cheng: That so over here says well Nagar P through a condition where were you at and we mentioned before, a piece of a condition ways actually cancellation point, they will your third excellent cancel inside Pisa condition way. There's your thread has the meat locker on lot 383 00:52:36.660 --> 00:52:44.340 William Cheng: OK, so the, you know, so, so, you know, so if you don't know any better. You will say, I don't know, right, because inside pizza condition way. It's a one atomic operation. 384 00:52:44.520 --> 00:52:52.680 William Cheng: It will unlock the meetups and wait for the matrix. And one of the one of the operation. So, you know, maybe let me take some law okay if meters unlock. What if you know 385 00:52:53.370 --> 00:52:59.760 William Cheng: This case if you act on cancel and you call Pisa Pisa when you don't have the law. Well, then, in this case it's not gonna work. 386 00:53:01.440 --> 00:53:04.830 William Cheng: Okay. So, therefore, if you don't put, you know, a push piece every time. 387 00:53:05.520 --> 00:53:15.630 William Cheng: With the address with meetings on to the cleanups that what if at the end of Peter condition way right so so you know somebody called Peter condition a signal broadcast and then you get picked out and move into the 388 00:53:16.050 --> 00:53:22.200 William Cheng: Move the move into the middle of the meat AX you and pretty soon you're going to have a new tax law. What if you get one of your 389 00:53:22.470 --> 00:53:27.570 William Cheng: Castle Rock right there right before you return from Peter condition way. Well, in that case, you have to meet next Lok 390 00:53:28.380 --> 00:53:34.530 William Cheng: Okay, so therefore, so over here. That's why we sort of write this function over here when we call it cleaner push over here, what should be the right argument. 391 00:53:35.100 --> 00:53:44.640 William Cheng: Okay, because if you can act on Castle in both scenario one cases that you have the new tax law and the other ones that you have to unlock while then in this case, there's no way for you to write your code. 392 00:53:45.060 --> 00:53:52.170 William Cheng: To to to satisfy satisfy that requirement over here. Okay, there's no piece of a function to check whether whether meet x lock or not. 393 00:53:53.370 --> 00:54:00.240 William Cheng: Okay, so therefore, in this case, it doesn't seem to be any solution. So what happened is that the P3 library. People say, okay, we will have to take care of it. 394 00:54:01.140 --> 00:54:06.630 William Cheng: Okay, so, so, so the modern piece where implementation basically the pizza library made the following promise. Okay. 395 00:54:06.870 --> 00:54:16.740 William Cheng: If you're a threat act on or cancel inside piece or a condition way what the piece or a library will guarantee is that it will only call the cleanup routine when the mutates is locked 396 00:54:18.360 --> 00:54:28.200 William Cheng: OK, so the piece of library implementation that will guarantee that if you act on cancel inside piece or a condition way when you know when you copy to exit. He will guarantee that the new Texas lot 397 00:54:28.680 --> 00:54:37.050 William Cheng: Okay, so therefore you become 100% safe to where you copies are coming up, push over here, the argument should be peace new tax law, followed by the address in the text. 398 00:54:37.380 --> 00:54:41.190 William Cheng: There so therefore this will be the right way to do it right. So you got Peter clear push over here. 399 00:54:41.430 --> 00:54:50.400 William Cheng: The first argument we piece of me that unlocked. And then the second argument over here will be the address is a new tax. So, therefore, if your program die inside piece or condition where we have to cancel. 400 00:54:50.670 --> 00:54:56.940 William Cheng: Well, when it goes to the cleanups that it will actually you know you watch ultimate X okay so this will be the right way to do it. 401 00:54:57.690 --> 00:54:59.640 William Cheng: That. But what did you say it doesn't get canceled. 402 00:55:00.120 --> 00:55:11.220 William Cheng: Okay, if you threaten that cancer, they will continue to execute. Eventually the guard is going to be true. And then at the end over here. Were you supposed to do is to call piece of meat has unlock that, and then you also need to pop this guy off the stack. 403 00:55:12.450 --> 00:55:19.140 William Cheng: OK. So again, you know, you have this function of the stack. So if you don't pop them off and later on we call pizza. Excellent. We're gonna, we're gonna end up, you know, unlocking the new tax. 404 00:55:19.290 --> 00:55:28.680 William Cheng: That you're not lucky, so therefore we should do over here that we need to combine those two operation into one statement, right, we need to unlock the new tax and also pop it off from the cleanups that 405 00:55:29.190 --> 00:55:31.920 William Cheng: Okay, so you can also write your programming two lines over here that'll be probably fine. 406 00:55:32.370 --> 00:55:34.560 William Cheng: If you want to say one line of code over here you will call 407 00:55:34.890 --> 00:55:45.690 William Cheng: Peter clean I'll pop with the value of one. So, again, one means to execute as function. So the function execute it is peaceful in the text. Ah, I follow by the address of the meat ax and then it will also pop this guy off the stack. 408 00:55:46.410 --> 00:55:49.950 William Cheng: Okay, so you get to choose whether you want to write two lines of code or one line of code. Okay. 409 00:55:52.530 --> 00:55:59.970 William Cheng: All right, so one or two cancellation over here, I just want to sort of briefly sort of mentioned how you should implement, you know, one of the cancellation over here. 410 00:56:00.900 --> 00:56:11.820 William Cheng: So, so one of two over here, you know, weapons that you don't want control sees press you going to use the control see catching threat instead of control the cajon so you need to you need to cancel the packets are at the token threat. 411 00:56:12.720 --> 00:56:20.040 William Cheng: Okay, so in that case the package three and a total throughout. They have piece of meat has unlocked, they will actually allocate memory or something like that is going to get pretty complicated. 412 00:56:20.550 --> 00:56:28.470 William Cheng: Okay, so I'm going to sort of give you a shortcut. Okay, so this way you're co op. Nice and nice and simple. Okay, so, so, so the key here is that if you think about your 413 00:56:28.830 --> 00:56:34.620 William Cheng: You know your package threat and your tokens where when they have the new tax law, do they locked him you test for a very long time. 414 00:56:35.940 --> 00:56:43.110 William Cheng: Okay, the answer is that they don't really locked in, which is for very, very long time. So what it will do is that they were locked in the tax and they will do something and they will not give me tax pretty quickly. 415 00:56:44.160 --> 00:56:52.170 William Cheng: Okay. So, therefore, you know, during the time when they have the new tax law since it's a very, very short period of time. What we can actually do is to disable cancellation. 416 00:56:52.590 --> 00:56:58.590 William Cheng: Okay, and then you locked him attacks. Okay, so this way you know when you allow the new tax over here that you can re enable cancellation. 417 00:56:59.040 --> 00:57:09.120 William Cheng: So, so, so, so, so, so this case, you know, since inside the we have the tax law in the cancellations disable. So in that case, you don't have to worry about acting on cancel. 418 00:57:10.620 --> 00:57:19.890 William Cheng: OK, so my recommendation over here. Is that what you should do is that as soon as your packets throw together when they get started you will disable cancellation right away. 419 00:57:20.460 --> 00:57:33.270 William Cheng: Okay. And right before you go to sleep you enable cancellation. Okay. As soon as you return fall asleep you disable cancellation and now you know when he tried a lot. Can you talk to do everything with the you do everything with a cancellation disable 420 00:57:34.740 --> 00:57:42.060 William Cheng: Okay, so this way when can your thread die. They're working through the admin console. The only time you don't cancel is when it's calling you sleep. 421 00:57:44.190 --> 00:57:52.440 William Cheng: Alright, so again, this way you sort of control exactly what cancellation is happening, you basically use synchronous cancellation with your co to happen only inside the 422 00:57:53.040 --> 00:58:05.670 William Cheng: Inside you sleep. Okay, so this way, your co op. Nice and simple, guys. Okay, this will be my recommendation. But again, you know, there's one caveat over here is that you can still get into a little race condition. 423 00:58:06.870 --> 00:58:12.780 William Cheng: Okay so so anyways you want. Once you start implementing your code over here if you somehow get started, you need to remind 424 00:58:13.320 --> 00:58:18.480 William Cheng: You remember that so that you can still get into a race condition. So, therefore, you need to solve that little problem with a small race condition. 425 00:58:18.900 --> 00:58:27.540 William Cheng: Okay, and the race going to the solution for the requisition over here is that inside your control see catches read over here. Once you like to meet, as you said, a global variable to say it's time to quit. 426 00:58:28.080 --> 00:58:37.860 William Cheng: Okay, so this way, whenever you lock the new tax. The first thing you need to check to see if it's time to quit. If it's time to quit that you don't do anything with a token bucket you is just simply unlocking the attacks and get out 427 00:58:38.640 --> 00:58:45.720 William Cheng: And get out. This means that you should self terminate. Yeah, right. So anyways, if you have question you can ask me when you get there. Yeah. 428 00:58:46.110 --> 00:58:49.590 William Cheng: All right. The last slide for Chapter two over here is a cancellation and c++ 429 00:58:50.070 --> 00:58:58.470 William Cheng: As it turns out, c++ doesn't implement the entire P threat mechanism. Okay. One of the things that the implement is cancellation there. 430 00:58:58.920 --> 00:59:02.760 William Cheng: So for example, if you are ready. Yes. Okay. If you don't know c++ don't have to worry about this. 431 00:59:03.120 --> 00:59:06.870 William Cheng: If you have c++ code over here. So let's say that you know my record here Castle in the middle. 432 00:59:07.230 --> 00:59:11.820 William Cheng: There at the beginning of my function over here. I'm actually going to create an object. So in this case, 433 00:59:12.060 --> 00:59:20.040 William Cheng: The or the construction of the object will get called. And then when I act on cancel in the middle of this function over here. The question is, that is the destruction of this function called 434 00:59:21.030 --> 00:59:23.490 William Cheng: Okay, the answer. The correct answer is, I don't know. 435 00:59:24.270 --> 00:59:29.310 William Cheng: Okay. Why don't you know what it depends on this, the compiler implementation for the c++ compiler. 436 00:59:29.580 --> 00:59:39.570 William Cheng: Some compiler will actually make sure that the structure, will you call and some compiler won't. So if you worry about memory leaks. Well, then you should read the compiler manuals, to find out exactly, you know, the 437 00:59:39.900 --> 00:59:44.100 William Cheng: Exactly. If you write code like this, are you going to going to end up with memory leaks. 438 00:59:45.390 --> 00:59:52.170 William Cheng: Okay. So as it turns out that you know c++ you know has, you know, you know, bee bee bee bee bee bee because because the way 439 00:59:52.860 --> 01:00:00.420 William Cheng: They handle exceptions. Some of the stuff they are actually not compatible with you know with with with with the people with a piece of library. 440 01:00:00.840 --> 01:00:09.930 William Cheng: Okay. So for example, when you're running your superpower a PC postcard in c++ there's a way to, to, to create a threat, but there's no way for you to cancel the threat. 441 01:00:11.040 --> 01:00:16.350 William Cheng: Right because because Patrick has leisure is incompatible with the current c++ standard 442 01:00:16.590 --> 01:00:23.490 William Cheng: Okay. So, therefore, if you want to use some implementation or c++ you need to, you know, read the manual. Find out exactly how they deal with reconciliation. 443 01:00:23.820 --> 01:00:28.890 William Cheng: Yeah. Alright. So anyways, the c++ we don't really talk about people are very, very much. So again, it's not that important. 444 01:00:29.550 --> 01:00:38.340 William Cheng: But alright so this is a good time to break. So next time we're going to go into chapter three. And we're going to look at the beginning of Chapter three, where we talk about context switching. Okay.