WEBVTT 1 00:00:02.490 --> 00:00:09.300 William Cheng: Welcome to lecture six and so we're in the middle, warm up to now it's due. 2 00:00:10.440 --> 00:00:11.730 William Cheng: In next Tuesday. 3 00:00:12.780 --> 00:00:19.680 William Cheng: So if you have co from previous semester, don't look at them. Don't copy them best to get rid of it that you should start as early as possible. Don't wait until the last minute. 4 00:00:20.280 --> 00:00:26.250 William Cheng: Grading guidelines. The only way we'll grey, we can all agree on on the third to move on to 16 point or for grading Ghana, as part of the spec. 5 00:00:26.730 --> 00:00:30.840 William Cheng: If you make a submission read and understand the ticket I you verify your submission 6 00:00:31.110 --> 00:00:39.150 William Cheng: I mean, the reason I keep repeating, repeating this is that because you know some people in the end they make some horrible mistake. And then the claim that they don't really, they don't really know any of these rules. 7 00:00:40.020 --> 00:00:44.100 William Cheng: So that's why I have to keep repeating them in every lecture. Okay, so hopefully I don't spend too much time on that. 8 00:00:44.640 --> 00:00:55.290 William Cheng: I just started looking for partners, where your current Osama work with them, you know, will will will work them on warm up to get work at a high level, and NASA any pizza any line of code. 9 00:00:55.950 --> 00:01:07.620 William Cheng: The team forming deadline is one day after warm up to do that. So lecture today will give you everything that you need to finish warm up to to handle control. See also know a cig in relatable. We talked about signals. 10 00:01:08.580 --> 00:01:14.190 William Cheng: So, and I guess there's some common over here. So today we're going to talk about what is the right way to do it. 11 00:01:14.670 --> 00:01:18.180 William Cheng: Hopefully, I'll give you some reason why, you know why we need to do things in a Certain Way. 12 00:01:18.540 --> 00:01:24.870 William Cheng: And then we also need to talk about something called P threat cancellation, one of the reason you want to learn. Peter cancellation is that 13 00:01:25.170 --> 00:01:30.000 William Cheng: Will you go to Colonel one, one of the things that you have to implement is called Colonel threat cancellation. 14 00:01:30.780 --> 00:01:33.360 William Cheng: So kind of threat cancellation is a little different from 15 00:01:33.720 --> 00:01:45.150 William Cheng: Us or threat cancellation, but by learning user threat cancellation. It will help you to understand. Colonel threat cancellation better. Okay, so if you don't do user to a cancellation where you go to Colonel thread, then, then, then you have to start from scratch. 16 00:01:45.690 --> 00:01:53.190 William Cheng: So in that case is going to take you longer to sort of understand the concept of cancellation. Okay. Alright, so the time to learn cancellation is, you know, right now, okay. 17 00:01:55.050 --> 00:02:06.480 William Cheng: Tentative kind of a warm up to your simulation should work by the end of this Thursday. So again, the goal is to submit before the early submission deadline to get 10% extra credit by end the Saturday. 18 00:02:07.020 --> 00:02:11.310 William Cheng: So if you're done by this Thursday, you still have two days to finish control see handling. 19 00:02:12.090 --> 00:02:18.030 William Cheng: So so so before that, you, you, you need to produce a consistent chase output. 20 00:02:18.330 --> 00:02:30.360 William Cheng: Okay, so, so that the output needs to make perfect sense when you look at every lead inside the output. They all needs to be correct. Okay. So you need to understand what correct means, you know, this way you can look at it and see if something went wrong. Yeah. 21 00:02:31.830 --> 00:02:37.380 William Cheng: The problem with the law to is that there's really no solution because even every time when you run your program you're gonna get a different result. 22 00:02:37.740 --> 00:02:44.490 William Cheng: Okay. I mean, what are you going to get a slightly different result not exactly the same. So, therefore, there's no way for you to compare against a solution. Okay. 23 00:02:44.820 --> 00:02:54.270 William Cheng: Integrating gun I mentioned some information about you know when you run this test. How long should it take or something like that. So if you're running time is way off, you should immediately try to sort of figure out why. 24 00:02:54.510 --> 00:03:01.890 William Cheng: Okay so so chances are you have a buggy code. Yeah. Alright, so again before Thursday, you don't have to worry about control see it. There's a 25 00:03:02.550 --> 00:03:07.410 William Cheng: Piece of code that comes with a warm up to spec. It's called analyze chase dot txt 26 00:03:08.160 --> 00:03:17.670 William Cheng: Okay, so you should look at the grading guidelines to see how you should. Are you supposed to run it, please understand that analyze chase that txt only work with perfect print out 27 00:03:18.450 --> 00:03:29.400 William Cheng: Okay, so if your printer is not perfect, sometimes and allied trades will start printing nonsense. Okay, so don't complain about analyze chase dot dot txt printing analysis is trying to tell you that something's wrong with your printer. 28 00:03:30.480 --> 00:03:36.150 William Cheng: Okay. So, therefore, you know, if everything is perfect and electric txt will will 29 00:03:36.630 --> 00:03:42.180 William Cheng: Will look correct if there's anything wrong with it. Okay. Look into your printer and try to sort of find out what's wrong with your print up 30 00:03:42.420 --> 00:03:47.040 William Cheng: Okay. And again, feel free to send me an email and asked me to help you to look at a printer. Right. So again, 31 00:03:47.610 --> 00:03:58.170 William Cheng: This is a sort of a TC shell script, you need to, you know, it's another programming language is pretty easy to understand. So you need to spend some time trying to understand that. 32 00:03:58.800 --> 00:04:08.700 William Cheng: You know, so this way you can fix your code pretty quickly. Yeah. Alright, so you should only use one new tax at one CV. Right. Make sure you're doing that, you must not do Busy waiting 33 00:04:09.420 --> 00:04:18.900 William Cheng: So again, some people misunderstand Busy waiting right. Is it okay to be busy. If you're doing something useful work if you're doing useful work. You're supposed to be busy. Why not cases. Okay. 34 00:04:19.740 --> 00:04:21.630 William Cheng: Okay, some people say, oh, when I'm running, you know, 35 00:04:21.840 --> 00:04:27.180 William Cheng: Doing a lot of stuff you know the CPU usage is really, really high water that is supposed to be really high, so therefore it's no problem. 36 00:04:27.330 --> 00:04:33.570 William Cheng: It's not what you have nothing to do but waiting for a variable to change value one. In that case, you should give up the CPU and you wait for that way. 37 00:04:34.680 --> 00:04:39.150 William Cheng: Okay, so, so again it's perfectly okay to be busy. If you have a lot of work to do. Yeah. 38 00:04:39.990 --> 00:04:47.160 William Cheng: All right, and also a thread must not be idle when it's supposed to be busy, right, what we're supposed to do something, it needs to do something pretty soon, right. 39 00:04:47.310 --> 00:04:57.540 William Cheng: There's no way for you to do something simultaneous you're right away, especially when one has a new tax law. The other three has to wait for the meetings together in LA. So it's going to be a little bit of delay. Okay, but at the lake and I'd be really long. 40 00:04:57.900 --> 00:05:07.950 William Cheng: Okay, if the delay is too long. Something wrong is going on there. Alright, so, so after you do you have finished all that on the Thursday, you can add a third cancellation code. 41 00:05:08.640 --> 00:05:16.980 William Cheng: So again, we're going to talk about something. How sick. Wait, so, so, so, so, so, so guy gets today we're gonna sort of cover how to all the how to do all these things. Yeah. 42 00:05:17.970 --> 00:05:26.340 William Cheng: Alright, so one of the requirement over here is that you're supposed to cancel the packets thread and the token threat and you must not cancel a server threat. 43 00:05:26.610 --> 00:05:35.010 William Cheng: Guys make sure we do this correctly. All these things are spelled out in the in the spec inside a spec. So again, you have to read the spec very, very carefully. Yeah. 44 00:05:37.020 --> 00:05:46.890 William Cheng: All right, so last time we're talking about signals, right. So we're going to go back and talk about that we went over some of the popular Unix signals out again there are more of these that we don't cover. 45 00:05:47.430 --> 00:05:55.620 William Cheng: So, that's okay. And we also mentioned some important terminology for signals the signal can be blocked. If it's blocked and it's get generated become pending. 46 00:05:56.010 --> 00:06:04.470 William Cheng: Eventually when a signal get on blog. The signal will get delivered when I get delivery. There are four different choices of things that can happen. So again, make sure you understand all that concepts. Okay. 47 00:06:06.240 --> 00:06:09.390 William Cheng: All right, how do you generate a signal or how do you send a signal. 48 00:06:09.720 --> 00:06:14.400 William Cheng: So you can actually make a system call. So if I want to send a signal to a particular process. 49 00:06:14.610 --> 00:06:24.870 William Cheng: I need the process identifier for that process. So in this case, I can make a kill system call for this process. So this is the target process. That's why I want to deliver a signal to and here's a signal number 50 00:06:25.170 --> 00:06:28.170 William Cheng: Okay, so I get the signal number over here, you can use the symbol over here. 51 00:06:28.350 --> 00:06:37.050 William Cheng: To say which signal. You want to get delivered to so will you try to deliver sick in right so sick over here can be sick in. Okay, so when you send segue into another process. 52 00:06:37.230 --> 00:06:49.890 William Cheng: When that process gets again it will be exactly the same as if somebody press Control see on the keyboard. Okay, that process will not be able to tell a difference because in both cases Sagan has been delivered to that process. Yeah. 53 00:06:51.270 --> 00:06:58.830 William Cheng: Alright, so again we saw before, you know, when you send a signal. It doesn't really mean that you're going to kill the process or even though the system calls called kill 54 00:06:59.580 --> 00:07:10.950 William Cheng: You know, so, so it depends on what signal that they use. And the default action, sometimes it's not killing the process or I will mention. We'll talk about that last time. Yeah. And also we press Control C, Control C is it 55 00:07:11.850 --> 00:07:15.450 William Cheng: Is a keyboard interrupt. Right. So inside the Colonel, they will see that you 56 00:07:15.750 --> 00:07:25.350 William Cheng: hold down the shift key and you press the control sorry you hold down the Control key and you press. See, and he was like, oh, you press Control sees. So, therefore, that should generate a cig in. So in this case, again, 57 00:07:25.770 --> 00:07:33.780 William Cheng: If you don't have a signal handler, it will kill your program, you have a signal handler, then in that case it will invoke the signal you will make an appt call and then deliver the signal. 58 00:07:34.620 --> 00:07:37.950 William Cheng: So, so again, these are, these are the possibilities. Now, 59 00:07:38.340 --> 00:07:50.310 William Cheng: You can also use a kill shell command right you're logging show bash TC show. You can use a command. So for example, you can say kill minus followed by signal number. So over here, the signal numbers to the serial number is not minus two. 60 00:07:50.670 --> 00:08:04.080 William Cheng: There. So you say kill sending the signal number to over here for for for process was process ID like this. Okay, so you can actually write this program. This program is very, very simple you parse the command line arguments and you make the kill system call and that's all you do. Yeah. 61 00:08:05.400 --> 00:08:12.570 William Cheng: You can also do something illegal right if you do divide by zero, you're going to get a floating point exception. So in that case, you would generally signal if you know 62 00:08:13.380 --> 00:08:19.650 William Cheng: Because the segmentation for again a signal get generated and if you have a signal handler will get deliver all that kind of stuff now. 63 00:08:20.190 --> 00:08:27.600 William Cheng: There's something for P three kill. Okay, so don't use P three kill right there. The idea. Peter IQ is that once where it's going to kill the other thread. 64 00:08:27.870 --> 00:08:34.200 William Cheng: In P thread that's really not the right way to do it. Okay, I have no idea why the pizza library provide a function called peace right kill then 65 00:08:34.410 --> 00:08:45.510 William Cheng: The right thing that you that you should do is that if once. Do I need to kill another threat the terminology over here. It's called a cancellation okay threat Asia cancel therapy and through Asia not kill therapy. 66 00:08:45.960 --> 00:08:53.010 William Cheng: Okay. So, therefore, you know, so they don't want to talk about peace, like isolation that should be the mechanism that you use to to tell the other thread to die now. 67 00:08:54.510 --> 00:09:04.260 William Cheng: All right, so, so, so, so when we say killing a threat. Okay, so in this class for the rest of the semester when we say killing a threat. It never means to actually kill the threat, which means that we're gonna we're gonna cancel the threat. 68 00:09:04.740 --> 00:09:08.550 William Cheng: OK. So again, Castle. They're basically asking the other says, I could you die nicely. 69 00:09:09.660 --> 00:09:18.060 William Cheng: Alright, so that's that's the meaning of social isolation. We're going to ask that thread to say, hey, you know. Would you be kind enough to to to self terminate that 70 00:09:20.310 --> 00:09:24.870 William Cheng: Alright, so there are two ways to handle signals or one is to handle handle the synchronously. 71 00:09:25.260 --> 00:09:40.230 William Cheng: So these are you know so. So the way you do this is that you specify a signal handler. Right. So for example, you press Control C control so you can happen at any time. So we hear the word sync asynchronous mean that it's asynchronously with respect to what your program is doing. 72 00:09:41.400 --> 00:09:50.400 William Cheng: Okay, so when I press Control see which line of code is your program executing what you have no idea rather controls you can be present anytime. So therefore, Control C is the asynchronous signal. 73 00:09:51.390 --> 00:10:04.920 William Cheng: Okay. You can also handle the signal synchronously. So we're going to learn that there's a function that you can call if you call the function segue. And if you do it perfectly. What happened is that the only time the signal can be deliver is in the middle of segue. 74 00:10:05.580 --> 00:10:18.840 William Cheng: Okay, so this this way you can write a program to say I want the signal to get delivered inside this function. So therefore, the signal can be handle synchronously. Right. So again, synchronously, with respect to what synchronous loosely with respect to the execution of your code. 75 00:10:20.280 --> 00:10:30.300 William Cheng: Okay, so which way is better. Right, so, so, so handle the signals asynchronously seem to be more chaotic because you have no idea. Well, you know what, when to expect that 76 00:10:30.930 --> 00:10:39.510 William Cheng: A better way to do it is to do a synchronously. Okay, so first we're going to talk about a segment of signal and also explain to you why is it dangerous to handle a signal signal and then we're sort of 77 00:10:40.020 --> 00:10:45.000 William Cheng: Going to teach you the modern way of handling signal which is using segue and handle the signal synchronicity. 78 00:10:45.510 --> 00:10:56.340 William Cheng: Yeah, so this way you know exactly, you know, which line of code inside your program that that will be that that will be the place where the signal will get delivered guarantee. So again, it's a better way to go. 79 00:10:57.750 --> 00:11:07.080 William Cheng: All right, to, to, to specify a signal handler. So signal was invented, long time ago before multithreading. Okay. So, therefore, you know, every signal inside a process. 80 00:11:07.350 --> 00:11:14.400 William Cheng: Process can have a most one signal handler. Okay. Some people actually think that if I have multiple threads every thread can actually have a signal handler. 81 00:11:14.700 --> 00:11:23.880 William Cheng: No signal handler, the concept come from processes. So every signal has only one signal handler. Okay, so if you want to, you know, if you have multiple today. You got to be very, very careful, okay. 82 00:11:24.390 --> 00:11:31.530 William Cheng: To specify signal handler. There are two system color. You can make one is called signal that one is an older version of the 83 00:11:32.100 --> 00:11:35.910 William Cheng: Signal handler today, you should use six that okay but they're exactly the same thing. 84 00:11:36.360 --> 00:11:45.180 William Cheng: Okay, so you can say signal, something like that to specify signal handler for particular signal or you can say success. Okay, the function prototype of these two functions are exactly the same. 85 00:11:45.510 --> 00:11:52.650 William Cheng: Okay, so over here. What is the function pull tab over here, right. So this function for that says that the first argument is going to be a signal number 86 00:11:52.950 --> 00:11:59.400 William Cheng: Okay, so basically you're setting up a signal handler for particular signal. So you can say, I want to set up a signal handler for sig and 87 00:12:00.420 --> 00:12:07.680 William Cheng: Then the second argument is going to be a function pointer because, again, a function pointer. You just name a function. It's the address of that particular function. 88 00:12:08.160 --> 00:12:17.040 William Cheng: So this one is called a signal handler type. Okay, so what are the signal handle time. Right. So again, you see the definition over here the signal handler type. Again, it's an asterix you're 89 00:12:17.430 --> 00:12:29.340 William Cheng: Surrounded by peer apprentices. That means I see the. That means that the function pointer this function return what it returns void. It doesn't return anything and it takes one argument over here, which is the integer 90 00:12:30.540 --> 00:12:40.020 William Cheng: Okay, so, so in this case the signal handler is simply a function that returns nothing but he takes one argument that the value of integer 91 00:12:40.320 --> 00:12:49.380 William Cheng: Okay, so this particular function, you know, when the signal is getting deliver this function will get caught. And the argument over here will be the signal that cause the signal. 92 00:12:50.010 --> 00:12:52.740 William Cheng: Is the signal number that cause a signal to get delivered 93 00:12:53.220 --> 00:13:05.550 William Cheng: Okay, so if you have a control C handler we press Control C and you specify a signal handler, where the signal handler start executing this argument will be equal to two, right, because again, or control C is going to be a serial number two. Yeah. 94 00:13:06.360 --> 00:13:11.970 William Cheng: Alright, so these two functions are exactly the same. So the way you use it as a equals six set or seek a signal. 95 00:13:12.210 --> 00:13:20.250 William Cheng: And then you try to set up a signal handler for sake in and then this will be the new handler this function returned to you the previous signal handler. 96 00:13:21.060 --> 00:13:29.280 William Cheng: OK. So the idea here is that what you can do that. You can use that to save the contacts and later on, restore the contacts. Okay. So inside your program. Again, if you think about a progressive 97 00:13:29.940 --> 00:13:42.150 William Cheng: Inside your process. Every signal has has one signal handler. So when you try to change the signal handler, you can actually save the previous signal handler. So later on you can restore back to the previous signal handler. 98 00:13:43.230 --> 00:13:54.240 William Cheng: Okay, so we got six out over here you specify new handler, the return value over here you can store it in a variable to remember the previous handler and if you want to go back to what it was before. What do you do, again, you call six set and you 99 00:13:54.510 --> 00:13:58.830 William Cheng: And the second argument over here will be the OH handler. So now you have restored the contacts. 100 00:13:59.730 --> 00:14:11.730 William Cheng: OK. So again, you should think about the signal handler is part of the context of your program. But you get to manage it. Okay, you can set it right but if you want to remember what it was before then. In this case, it was stored inside a local variable. Now, 101 00:14:13.770 --> 00:14:17.220 William Cheng: Alright, so there are some default. There's some special signal handler. 102 00:14:17.400 --> 00:14:27.240 William Cheng: One is called sick default right so sick default is a table that we saw at the end, the last lecture, right. Those are the default behavior. So you can actually says signal or six set and then you specify signal number 103 00:14:27.420 --> 00:14:30.990 William Cheng: And he said, I want to change the signal handler to the default signal handler. 104 00:14:31.620 --> 00:14:43.560 William Cheng: Okay, so, so you'll do like this. The last arguments, the default. So now it will go back to the default behavior. So it previously you set up a signal handler, you don't want to use it anymore. You can make the system call over here to switch back to the default handler. 105 00:14:43.860 --> 00:14:51.870 William Cheng: That you can also tell the operating system to say you know when this thing will get deliver simple simply ignore it. Okay. So, actually. So, so in this case the 106 00:14:52.470 --> 00:14:59.190 William Cheng: Last time we mentioned that there are four different action. There's actually the fifth action over here simply to ignore the signal. So nothing will happen. You 107 00:14:59.490 --> 00:15:07.470 William Cheng: Will not get killed, nothing will happen. Okay. So in this case, like I said, I you know I can control sees press instead of doing something about it. Please just ignore it. 108 00:15:08.040 --> 00:15:14.850 William Cheng: Okay, so this way with somebody presidential. See, you're probably just won't die again. And also, nothing happens. Okay. So in this case, how can you kill your program. 109 00:15:15.330 --> 00:15:25.050 William Cheng: One. And that's a problem, so, so, so if you tell the you start ignoring signals and the signal or they're supposed to tell you not to, you know, do anything, anything bad 110 00:15:25.530 --> 00:15:33.720 William Cheng: So this way you know bathroom can start happening. OK. So again, be very, very careful with the noise to no signal sometime. Maybe it's not a good idea to ignore signals. Okay. 111 00:15:35.280 --> 00:15:42.180 William Cheng: Alright, so that's a good example. So we here. We're going to specify signal handler for sig in right. So over here we say six sets again. 112 00:15:42.450 --> 00:15:54.810 William Cheng: The handler over here is this function right this function. It's, you know, the function of a return nothing, it takes one argument over here is going to be the signal number. So when control see get press then seek number over here will be equal to 113 00:15:55.710 --> 00:16:04.440 William Cheng: That. So what I do is that, you know, in my program up here. I set up a signal handler. Then I go into an infinite loop. Okay, so my program over here basically will never die. 114 00:16:04.860 --> 00:16:10.590 William Cheng: Okay, because it goes out for a little over here. And now when the user press Control. See what's going to happen right when the user press Control see 115 00:16:11.130 --> 00:16:22.470 William Cheng: You know, again, the organism is going to see that control sees getting pressed inside the kernel. And then he will see that this particular program has a signal handler specify. So therefore, instead of the default action. What it will do is I 116 00:16:22.800 --> 00:16:28.650 William Cheng: Will make an appt call come to the user space program and borrow one of your threat to execute a signal handler. 117 00:16:29.190 --> 00:16:34.320 William Cheng: Okay, the signal hello over here. It's going to be a function. Right. So in order for you to execute function, you need a threat. 118 00:16:34.740 --> 00:16:43.620 William Cheng: Okay. So in this case, which will which will use that we're going to use the threat that was going that's running around over here inside the infinite loop, right. This was running, running 119 00:16:44.040 --> 00:16:50.490 William Cheng: Excellent different Lou. We're going to borrow that throughout to execute a signal handler and when we start executing the signal, however. 120 00:16:50.940 --> 00:16:59.670 William Cheng: During that time, you cannot generate another. See, I didn't notice again. Okay, so therefore the rule over here is that when you are delivering siggins again is also blocked. 121 00:17:00.270 --> 00:17:07.890 William Cheng: Okay, so therefore you press to control seems very, very quickly. The second control see will not have any effect because when you press a second, Control C is going to be pending. 122 00:17:08.730 --> 00:17:15.270 William Cheng: Okay, so therefore we start executing the signal handler over here, Control C is going to get blah. So the second Control C is going to is going to go into the pending State. 123 00:17:15.480 --> 00:17:22.440 William Cheng: While we print this message to say I have received signal number to over here would be is I'm celebrating because my program didn't get killed. 124 00:17:23.280 --> 00:17:35.550 William Cheng: That as soon as I recall return from the signal handler that in that case the signal will get unblocked and then in this case the signal will get deliver the signal delivered. I'm going to see the second message to say I receive signal. Number two will be again. 125 00:17:36.030 --> 00:17:41.520 William Cheng: Okay. So, therefore, if you press Control see twice really quickly, you're going to see that actually appear over here twice. Yeah. 126 00:17:43.290 --> 00:17:48.300 William Cheng: All right, so there's another sort of a fancy way to do a better way. Just want to make a note over here. 127 00:17:48.690 --> 00:17:59.490 William Cheng: This is the behavior of the Linux operating system in some operating system, you know, inside the signal handler. If you want the same kind of behavior for the second signal guess if you press Control see twice. 128 00:17:59.850 --> 00:18:05.220 William Cheng: In some offices and, you know, the first one over here will execute a signal handler, but a second while I should kill your program. 129 00:18:06.030 --> 00:18:13.830 William Cheng: Okay, so in that case what you should do is that inside the signal handler over here you can reestablish the signal handler. So over here, I'm going to say that the signal have little over here. 130 00:18:14.190 --> 00:18:16.680 William Cheng: It's going to go is don't go to this to this function again. 131 00:18:17.070 --> 00:18:24.930 William Cheng: All right, because if I don't do this. As soon as I returned from the signal handler is going to go back to the default handler right and the default handler to handle control sees no kill your program. 132 00:18:25.440 --> 00:18:31.020 William Cheng: Okay. So, therefore I'm going to specify the signal handler again to be this function when I have the signal block. 133 00:18:31.560 --> 00:18:38.940 William Cheng: Okay, so this way. As soon as I returned from this function, then this will be the signal handler and when the second control Sega deliver. Again, this function is going to get call 134 00:18:39.840 --> 00:18:47.520 William Cheng: OK. So again, this kind of behavior is operating system specific I think some Unix operating system you're required to do this ball Linux operating system. 135 00:18:47.880 --> 00:18:55.140 William Cheng: Once you set up the signal handler, it will stay to be your signal handler for this particular signal. So again, different operating system behave differently. 136 00:18:56.970 --> 00:19:06.330 William Cheng: All right. There are other ways to handle signal. So again, you are not responsible for it. So there's another you know system cocoa. Six Sigma action. So the more complicated, but you know, I'm going to skip that. 137 00:19:07.170 --> 00:19:12.060 William Cheng: Alright, so let's talk about these asynchronous signal a signal. The signal is very dangerous. 138 00:19:12.900 --> 00:19:20.130 William Cheng: Okay, why are they dangerous, right. So for example, when I press Control. See what if, when I press Control. See you are actually in the middle hooking up some 139 00:19:20.940 --> 00:19:32.430 William Cheng: Hooking up some some linked lists. Okay, and you I'm hookah. You know, you basically took an x pointer and now all of a sudden you know I press Control. See, so now inside the signal handler. What if I look at that in class. 140 00:19:33.840 --> 00:19:43.140 William Cheng: Okay, so you are in the middle of, you know, messing around with your English and now control sees press, you've got execute a signal handler in the signal handler use that link list, you're going to get a segmentation Paul 141 00:19:44.040 --> 00:19:49.260 William Cheng: Okay. So, therefore, this means that, you know, the way that you handle these asynchronous signal is not safe. 142 00:19:49.800 --> 00:19:56.370 William Cheng: Now, so I'm going to talk about a sync signal safety. Want to learn how to handle the asynchronous signal in a safe manner. 143 00:19:56.940 --> 00:20:04.350 William Cheng: Now, so, so a signal safety mean that you want to make your co safe when you're working with, as the asynchronous signals that 144 00:20:05.130 --> 00:20:15.720 William Cheng: So the general rule to provide a signal safety is that any data structure that signal Heller access must be a signal say right instead of secret hello over here. The example that we saw before. 145 00:20:16.020 --> 00:20:25.470 William Cheng: I mean, it doesn't really do anything, you just called printer okay if I if I need to manipulate linguist link list inside the signal handle over here. What then I got to make sure that 146 00:20:26.010 --> 00:20:30.270 William Cheng: You know the signal handler will only get involved when the link list is in a good state. 147 00:20:31.230 --> 00:20:39.480 William Cheng: Right, because otherwise, when I start executing this code over here. Well, then, in this case, I'm going to end up with a segmentation fault. Then, so we're going to sort of learn techniques of how to deal with that. Yeah. 148 00:20:41.160 --> 00:20:42.900 William Cheng: Alright, so that's one way to the to 149 00:20:43.770 --> 00:20:54.660 William Cheng: The basic rule we hear is that an ACC the asynchronous signal must not corrupt the data structure, right. So if you have a legless us on hawk. Well, now when you try to, you know, visit that link class you're gonna end up with a segmentation. 150 00:20:55.230 --> 00:21:04.770 William Cheng: There an alternative is to make a synchronous, asynchronous signal make it into a synchronous signal. Okay, so this way you don't have to worry about a signal safety. 151 00:21:05.520 --> 00:21:12.060 William Cheng: Okay. So in this case, when I get this is going to be my recommendation for your warm up to you should use a signal catching threat. 152 00:21:12.300 --> 00:21:21.000 William Cheng: This one, they use a single candidate to receive a particular signal and you know exactly where the signal will get delivered. Okay, so this way you 153 00:21:21.570 --> 00:21:32.400 William Cheng: The way you handle signal is going to become synchronous with your code. So it's so in this case there's no a sink signal safety issue anymore okay because a signal safety is kind of nasty. Okay. 154 00:21:34.410 --> 00:21:38.220 William Cheng: All right, we're going to take a look at these two examples when asynchronous signal can actually 155 00:21:38.490 --> 00:21:44.610 William Cheng: Can actually mess things up. Okay. The first one over here is what is the right way to wait for a signal. Okay, so we're going to sort of 156 00:21:44.850 --> 00:21:50.460 William Cheng: You know, we're going to do something. I'm going to sort of wait for a signal to get deliver how, what is the right way to to to wait for it. 157 00:21:50.760 --> 00:21:53.730 William Cheng: Okay. So first, what is it we're going to look at what is wrong with wait for it. Yeah. 158 00:21:54.030 --> 00:21:58.650 William Cheng: Alright, so in this case we're going to set up a alarm clock. When a white up an alarm clock and then the 159 00:21:58.920 --> 00:22:05.790 William Cheng: You know, the, the, the signal handler for sick alarm is going to be this function called do something interesting about whatever that is, we don't really care. 160 00:22:06.060 --> 00:22:10.590 William Cheng: Okay, so why because I'm going to wipe out the alarm clock over here, right. So in this way, you know, 161 00:22:11.430 --> 00:22:18.780 William Cheng: So so so we start over here, we specify what is the signal handler for signal on first game, and then we're going to wind up the alarm clock. 162 00:22:19.080 --> 00:22:25.740 William Cheng: Okay, so in this case we're going to wipe out a clock alarm clock for only one millisecond. Okay, so this data structures. Nice. 163 00:22:26.520 --> 00:22:38.280 William Cheng: Struck time Val kinda has two fields. One is number of seconds and the other ones number of microseconds. So in this case, the number of seconds equal to zero, the number of microsecond is equal to 1000. That means that it's equal to one millisecond. 164 00:22:39.240 --> 00:22:43.050 William Cheng: Okay. So, therefore, what we want to do is that we want to sleep or one millisecond. Okay. 165 00:22:43.950 --> 00:22:57.330 William Cheng: Alright, so we're here, we're going to call it the system call call set a timer so at a timer is the interval timer. So we're going to, we're going to start whining have an alarm clock. So this alarm car will go off one millisecond later. Okay, so, so as soon as we 166 00:22:59.460 --> 00:23:10.140 William Cheng: As soon as we finished when you have the alarm clock. What we will do that. We're going to call a system called called Pause Okay, the PA system called basically it's going to suspend our threat. He will only return when a signal is delivered. 167 00:23:11.670 --> 00:23:16.860 William Cheng: Okay, so, so again the PA system called we never saw it before. So this is a system called when you call it. 168 00:23:17.490 --> 00:23:21.210 William Cheng: You know, it's a blocking call your three I will go to sleep until 169 00:23:21.600 --> 00:23:33.270 William Cheng: Signals deliver. Right. So why don't want to signals deliver when a signal is deliver you know this, you know. So in our example over here, since we specify a signal handler. The signal handler will get involved and then when the signal. 170 00:23:33.810 --> 00:23:37.710 William Cheng: The signal finished delivery your threat over here will return from past 171 00:23:38.400 --> 00:23:50.160 William Cheng: Okay, so basically, the idea was that you want to wait for any signal to get deliberate when the signals range finishing delivery, you will return from pause right so this way you wait for the signal and now the signal has the signal has been delivered. So you continue to work. 172 00:23:51.450 --> 00:23:52.530 William Cheng: Alright, so 173 00:23:53.580 --> 00:24:01.200 William Cheng: So the problem is that you know that you know this cut over here doesn't work. Okay. The reason is that what of CIG alarm occurred before pauses called 174 00:24:01.620 --> 00:24:10.440 William Cheng: Okay, you wind up the alarm clock over here. What if all of a sudden, as soon as you finish one of the alarm clock, the operating system decide to suspend you and give the CPU to somebody else. 175 00:24:12.300 --> 00:24:19.500 William Cheng: Okay, so, so again, let's assume that you have only one CPU right you wind up the alarm clock over here at the office and say hey timeout. I need to give the CPU to somebody else. 176 00:24:19.650 --> 00:24:27.000 William Cheng: So you're through. I guess the spend there. I'm going to give a CPU to another process to another thread and then now I'll give us up to another process, not a threat. I keep doing that. 177 00:24:27.240 --> 00:24:30.210 William Cheng: And then pretty soon one millisecond later the arms are going to go out 178 00:24:30.810 --> 00:24:38.880 William Cheng: Okay, when the alarm is going off. What's going to happen to your threat over here, right. So again, your threads actually between said I time right between past and now you're actually doing nothing. 179 00:24:39.480 --> 00:24:50.220 William Cheng: Right, because the operating system. Put your threat to sleep. So your threat is available to be borrow to execute a signal delivery. So what happens at your threat over here. I'll get borrow to to deliver the signal make this function call. 180 00:24:50.520 --> 00:25:01.440 William Cheng: Okay. And then when we, when you finished handling that signal when you finish the execute a signal handler. What we also do. You're gonna you're going to go to do what it was doing before, which is sleeping. 181 00:25:02.190 --> 00:25:09.480 William Cheng: Right because yes okay right before you you felt was borrow your threads actually sleeping because our businesses and suspended. 182 00:25:10.050 --> 00:25:17.940 William Cheng: Suspended your program again. So there you so therefore you're sleeping. I'll be here after your threads borrow to deliver signal is going to go back to sleeping right away. 183 00:25:18.780 --> 00:25:28.110 William Cheng: Okay, so now again the CPU is going to give the operating system is going to be at the CPU to some other thread or something like that. And eventually, when the audience is engaged the CP, CP back to you. What would you do 184 00:25:28.560 --> 00:25:34.620 William Cheng: OK. So again, when you when you go to sleep. You're in between set a timer and pause now if I resume your 185 00:25:35.760 --> 00:25:39.510 William Cheng: Resume the execution of your threat. What you do that you will call. Pause 186 00:25:41.130 --> 00:25:45.360 William Cheng: Okay. So in this case, when you call past, what's going to happen when nothing's going to happen. You will never return 187 00:25:46.080 --> 00:25:53.160 William Cheng: Okay, because the semantics of policies that will after you start causing pause if any signal is delivered then possible return 188 00:25:53.580 --> 00:26:05.370 William Cheng: Okay, but since your signals deliver a long time ago. Well, now we can pause. What you have to wait for the next signal. Okay. So, therefore, in this case, you know, we could pause there no more signal. So therefore, in this case, your program actually get stuck right right there. 189 00:26:06.390 --> 00:26:16.320 William Cheng: Okay, so again, is this a deadlock. Okay, this is not a deadlock. This is a, this is a race condition. Okay, so again, a race condition. What is the race condition rat race condition, meaning that 190 00:26:16.860 --> 00:26:28.860 William Cheng: The risk of dishy, meaning that you have some you have some kind of a race going on. If one thread wins the race, then the right behavior will happen if you if the other threads win the race that the bad behavior. Okay happen. 191 00:26:29.100 --> 00:26:38.310 William Cheng: Okay, you will see that in this case over here. There's only one thread. OK. So again, depends on exactly when the Whoa, whoa, what is the timing, you're going to end up with a bad situation. 192 00:26:39.000 --> 00:26:44.400 William Cheng: OK. So, again, in this case is not a delicate situation, especially because I only have one through. So in this case, it's not a deadlock. 193 00:26:44.820 --> 00:26:52.620 William Cheng: So it's simply a race condition. Okay. It's a timing dependent behavior that will cause error in your program. Okay, so that's called race condition. Yeah. 194 00:26:53.340 --> 00:26:55.620 William Cheng: Alright, so later on I'm going to see how to actually solve this problem. 195 00:26:56.100 --> 00:27:03.750 William Cheng: The second example is called status update there. So again, this is kind of the example that we mentioned before, if you are cooking a linked list. What if somebody press Control C. 196 00:27:04.020 --> 00:27:10.950 William Cheng: Right, so my program is going to go like this. I have a main function over here. I'm going to set the signal handler to be this function over here. 197 00:27:11.520 --> 00:27:21.570 William Cheng: So Seguin as control C, Control C is going to be handled by this threat. So inside Israel. You press Control. See, I'm going to display the state of this of this particular program. 198 00:27:22.110 --> 00:27:29.370 William Cheng: Okay. So, so what happens is that this program is going to do a competition for a very long time. So what it will do, they will call the long running a procedure over here. 199 00:27:29.550 --> 00:27:36.930 William Cheng: The Romani procedure over here, say in an infinite loop what it would do is that he would do something for a long time. And then what it will do is that it will update a state. 200 00:27:37.170 --> 00:27:41.970 William Cheng: And then what do we do, you know, do more computation and they will update the state will do more competition you keep doing this. 201 00:27:42.270 --> 00:27:55.740 William Cheng: Yeah, so this is kind of like a progress bar right what it will do is it will actually show you, you know, 5% completed a 10% completed 25% completed 99% completed or something like that. So, what we want is that every time when you press Control. See, it will tell you how 202 00:27:56.070 --> 00:27:58.290 William Cheng: How many percentage of progress have you made. 203 00:27:58.980 --> 00:28:10.410 William Cheng: You guys will get this is really, really, all in good old Unix. There's no graphics user interface, right. So one thing that you can do is that you run a program for very long time. Every time I press Control. See, it tells me it will tell me how close it is to finish it. 204 00:28:10.650 --> 00:28:16.020 William Cheng: To to to finish this program. Okay, so in this case the signal handler over here. I'm going to call it display. 205 00:28:16.590 --> 00:28:24.120 William Cheng: Followed by the argument being this global state variable we hear that sitting over here. Okay. And then whenever I finished some computation. I'm going to update the state. 206 00:28:24.750 --> 00:28:32.580 William Cheng: Okay, so you can sort of think about the state is like my for two lists. Okay, every time when I finish something, I'm going to update the list over here. So when I tried it this way. 207 00:28:32.790 --> 00:28:37.530 William Cheng: How much progress. I've made, I will use this function over here to walk down that list and try to print out something 208 00:28:38.790 --> 00:28:45.900 William Cheng: Okay, so what if you know you're unlucky when you're in the middle of updating the link lists guys over here. There's a function called update state over here. 209 00:28:45.990 --> 00:28:53.940 William Cheng: So when I update stay. I'm going to come to my phone to lift a pen. Something you know unlinked something or something like that. Okay. And in the middle of that I press Control see 210 00:28:54.630 --> 00:29:03.990 William Cheng: Okay, so in that case, I'm going to switch to the handler over here. I'm going to borrow you borrow my through my thread over here is updating the state. I'm going to borrow it to to deliver the signal. And now what I call it the 211 00:29:04.290 --> 00:29:18.840 William Cheng: Displaced a while, then the link is broken. So, therefore I'm going to end up with a segmentation, Paul. Okay. So in this case, again, this is a race condition because of timing. Timing dependent behavior is going to end up with a better result then 212 00:29:20.640 --> 00:29:27.690 William Cheng: So you can also sort of think about is that our signal handler over here is not a signal signal save right because we sort of mentioned before. 213 00:29:27.900 --> 00:29:38.940 William Cheng: All the data structure that you use, is that a signal handler. We got to make sure that they're in a good state when this case there's no way to guarantee that. So, therefore, this co over here is not as a signal say that 214 00:29:40.080 --> 00:29:42.210 William Cheng: All right. Okay. This is not a deadlock, it's really 215 00:29:44.760 --> 00:29:49.410 William Cheng: Alright, so here's a really not a race condition because it's just bad programming. Okay. 216 00:29:49.860 --> 00:29:59.700 William Cheng: So another way to sort of think about is that if you write your code. This way, you know you you allow this thing to be you allow this bug to be exposed. So this could be just, just be considered as a programming, but 217 00:30:00.810 --> 00:30:06.930 William Cheng: Then alright so so via this is not a linked list. What, then, in that case, maybe it's a race condition, you know, but but but again. 218 00:30:09.270 --> 00:30:19.110 William Cheng: Alright, so, so, so what is the solution for this problem, right, I mean you can sort of think about this one is that, you know, the state over here is the link list right over here. So they are shared by these two different pieces of code. 219 00:30:19.290 --> 00:30:24.570 William Cheng: And what I want is that maybe what I want is I want this piece of code, and this piece of code to be mutually exclusive. 220 00:30:25.920 --> 00:30:30.300 William Cheng: Okay, so therefore, in that case, I will not have any problem, right. So what I can do is that before I call status. 221 00:30:30.810 --> 00:30:36.900 William Cheng: Update state over here. I'm going to call piece me tax law. Okay. And then after updates. There'll be I'm going to call piece of music. 222 00:30:37.530 --> 00:30:45.030 William Cheng: And before I call this will be here. I'm gonna call pizza, pizza. And then after the display. I'm gonna hop era be found law. So in that case of code will look like this. 223 00:30:46.020 --> 00:30:56.790 William Cheng: Yeah. So is that a long running procedural be here before you know before I'm going to change the list. I'm gonna I'm gonna also. So, what it will do is that I will only update the linguist inside the critical section. 224 00:30:57.420 --> 00:31:03.870 William Cheng: Okay, and I will only put this way, the state inside the critical section. So, therefore, this code and this co will be mutually exclusive. 225 00:31:04.740 --> 00:31:18.000 William Cheng: Okay, so, of course, if you write your code. This way they will be mutually exclusive. But in this case, you actually get into Adela. Okay, so why would you get into Adela right so let's say that my thread over here copies of meatheads LA. And then in the middle of update. Sorry. 226 00:31:19.710 --> 00:31:20.010 William Cheng: All right. 227 00:31:22.800 --> 00:31:24.180 William Cheng: What is the example over here. 228 00:31:26.700 --> 00:31:31.770 William Cheng: Oh, OK. So my thread over here. Come call piece of meat AX log over here and clean up this picture over here. 229 00:31:32.280 --> 00:31:38.940 William Cheng: The, you know, the domain thread over here copies me tax law, and then he called update state. OK. So now when I press Control. See what's going to happen. 230 00:31:39.540 --> 00:31:46.830 William Cheng: Well, so again I set up a signal handler. This is my signal handler. So when I press Control. See, then I'm going to borrow this thread over here to execute a signal handler. 231 00:31:47.370 --> 00:31:57.090 William Cheng: Okay. So yeah, this word is available because in the middle of updates days updating the link is over here. So I'm gonna borrow. I'm going to call the signal handler over here and then the signal hello over here is going to copy semi tax law. 232 00:31:57.720 --> 00:32:07.890 William Cheng: Well, what's going to happen right now is the new tax law one this example the mute Texas locked by the main threat. OK, so now the main threat is coming over here and try to lock the new tax. 233 00:32:08.490 --> 00:32:21.000 William Cheng: To lock them. You take the second time. Okay. So, therefore, in this case, not going to work. Right, because the music is owned by the thread itself already when you try to log the music for the second time, he will not get unlocked until the main threat alchemy attacks. 234 00:32:22.410 --> 00:32:27.300 William Cheng: Okay, so therefore your thread is executing code over here. Is there any way for you to return your set back to what I was doing before. 235 00:32:27.810 --> 00:32:32.010 William Cheng: There's no way to do that because you have borrow that threat to execute a signal delivery. 236 00:32:32.670 --> 00:32:43.800 William Cheng: Okay, so therefore, in this case, when you start running this code over here, your threat cannot be returned back to what I was doing before. Okay. So this guy is going to end up with this really weird della you only have one swear by your threat dialogue with yourself. 237 00:32:45.990 --> 00:32:51.210 William Cheng: Okay. So, therefore, you know, you know, this code is not going to work. This code is it thread safe. 238 00:32:52.230 --> 00:32:58.170 William Cheng: Was according to our classes definition threat safety, meaning that when you have more than one thread. 239 00:32:58.560 --> 00:33:03.150 William Cheng: Okay, then in that case you have to worry about threat safety but if there's only one third, by definition is always safe. 240 00:33:03.690 --> 00:33:13.800 William Cheng: Okay, so this quote by our definition says there's only one third is always say, but at this call reentry. Well, this code is not re enter and because you're going to end up with your threat that luck with yourself. 241 00:33:15.090 --> 00:33:21.720 William Cheng: OK. So again, you know, I talked about that last time in this class we consider re n trend and thread. 242 00:33:22.680 --> 00:33:30.090 William Cheng: thread safe to be exactly the same thing where there's multi threading, but when there's only a single threat. And in that case, we enter in a thread safe is different. 243 00:33:30.390 --> 00:33:36.060 William Cheng: Okay, so in this case this called is not re entering. But, you know, since there's only one threat by our definition is stressing 244 00:33:37.350 --> 00:33:44.220 William Cheng: Guys will be very careful if you go on to the interview, maybe some other people have different definition of thread safe so so again you need to tell them you know when we talk about their safety. 245 00:33:44.580 --> 00:33:52.650 William Cheng: You have to give them our definition of, say, okay, and there was a, oh no, that's not what I mean. I mean, we enter it right over again that they will be talking about the same thing. Alright. 246 00:33:53.790 --> 00:34:00.360 William Cheng: So so so pleased to understand that, you know, typically happens instead of signal handler. The signal hazard get executed until completion. 247 00:34:00.780 --> 00:34:06.840 William Cheng: Guys, remember when I talked about deviation right your thread is doing something over here. Okay. And then you get borrow you get suspended to 248 00:34:07.080 --> 00:34:13.980 William Cheng: Handle signal delivery. And then the third is only returned back to what I was doing before. When you reach the end of the signal handler. 249 00:34:14.850 --> 00:34:21.720 William Cheng: Okay, so therefore once you are a threat. Excellent. Single handler, you have to finish the signal handler before you return the threat back to what it was doing before. 250 00:34:22.470 --> 00:34:31.440 William Cheng: Okay, so that's why you're going to end up with a della okay because we do it because there's no way for us to return the threat to what it was doing before. And then we can borrow later. Okay. It doesn't work that way. Yeah. 251 00:34:32.220 --> 00:34:42.420 William Cheng: Alright, so. So in general, you know, since you know since. Now that you know the signal handler has to execute your completion. So he said, a signal handler, we need to be as simple as possible. 252 00:34:42.780 --> 00:34:46.110 William Cheng: Right, because otherwise, if we do a lot of stuff, then in that case, you know, 253 00:34:46.770 --> 00:34:50.310 William Cheng: Again, the destroyer with. There's no way for us to return the threat to what I was doing before. 254 00:34:50.580 --> 00:34:57.030 William Cheng: Okay, so if you do searches on the internet, you're gonna you're gonna find some people will tell you say, hey, instead of signal. Hello, you can actually do a lot of stuff. 255 00:34:57.420 --> 00:35:03.690 William Cheng: Okay, so try not to do that. That's really a bad idea. NET. I mean we should actually a voice in your head. What altogether. So, so I'm going to cover that. 256 00:35:05.610 --> 00:35:15.750 William Cheng: Alright, so what is the actual solution, right, the actual solution over here is that you need to control the signal delivery by blocking and unblocking signals. Right. Also, there's no as masking masking the signal. 257 00:35:16.320 --> 00:35:27.450 William Cheng: Okay, so every individual signal, you can actually say that this signals blog and this thing was on blah. So if your signal is block and somebody tried to generate a signal the signal become pending until you unblocking I'm blocking the signal will get delivered 258 00:35:28.200 --> 00:35:31.290 William Cheng: Okay, it's okay. I'm gonna get we're gonna get used to the all these terminologies. Yeah. 259 00:35:32.220 --> 00:35:40.800 William Cheng: Alright, so, so, so in this case. So unlike interrupt right into rob you can enable that we can disable the interrupt when you disable interrupt you disable all interrupts 260 00:35:41.550 --> 00:35:49.680 William Cheng: Okay. And when you enable interrupt you enable or interrupt so for signal. We want to be able to control them individually so that every signal we can actually block and block them. 261 00:35:49.860 --> 00:35:57.420 William Cheng: Right, so that's why it's sort of terminology is a little different. We don't enable or disable signal we block or a block specific signals that 262 00:35:58.140 --> 00:36:04.770 William Cheng: Alright, so the way this is done is that you guys are getting just think about is that inside the control block there is something called a signal mask. 263 00:36:05.340 --> 00:36:23.220 William Cheng: Get the signal mass. It's a bunch of a bunch of bits. It's a bunch of bits every big correspond to a signal. Okay, so, so, since they're a bit. It can be 01 so so in the in the piece of our library. If a particular mass is equal to one, that means that the corresponding signal is blocked. 264 00:36:24.360 --> 00:36:31.410 William Cheng: Okay, so let's take a look at this example over here we have a single mass in the good old days, the signal mass over here is a 32 bit number, right, because they're only 32 signal. 265 00:36:31.920 --> 00:36:42.810 William Cheng: So this is going to be the most significant bit over here. So this has been 31 right and then there's this 30 and bit 29 or something like that. So since bit 31 is the one that means a signal 31 is blocked. 266 00:36:43.140 --> 00:36:55.080 William Cheng: And signal 30 is blocked signal 29 unblock signal 20 AES block, you know, etc. Okay. So, by looking at the signal mask over here, we will know which signal is blogging which know always signals out once it goes on. 267 00:36:55.980 --> 00:37:05.700 William Cheng: That all right and and now Robin is that whenever you so, so this is not a threat control, blah. So when you create a child threat right we create Charlie and the Chocolate is going to get their own signal control, blah. 268 00:37:06.210 --> 00:37:11.730 William Cheng: Sorry, that the child will get their own threat control, blah. So instead of control, but there's also a signal mask. 269 00:37:11.940 --> 00:37:23.670 William Cheng: OK. So the rule in piece of that is that when you create a child threat and then in that case this will be the parent threat and this will be the child where I'm going to copy the threat control blog over you can copy the signal mass from the parents to the child. 270 00:37:24.690 --> 00:37:29.640 William Cheng: Okay, so this way if the parents who is blocking. So again, so the child was started out blocking again. 271 00:37:30.870 --> 00:37:39.780 William Cheng: Okay, so why is it convenient way to actually block all the signal is that inside your main threat if you block one signals. So you blocks again. And now we create a bunch of swear all of them have blocks again. 272 00:37:41.190 --> 00:37:46.350 William Cheng: Okay, so that's basically how you control to make sure that every thread has begun blah. Okay. 273 00:37:49.020 --> 00:37:56.460 William Cheng: All right, so too, so. So the way to manipulate the signal mask over here. There's a system called that you can make this is called, it's called sick proc mask. 274 00:37:56.760 --> 00:38:00.840 William Cheng: Okay, so here it says you are manipulating the process mask for signal. 275 00:38:01.290 --> 00:38:11.070 William Cheng: OK. So again, again signal go with processes that with threads over here. So, again, that even though this is insider threat control block. Okay, so just tell them that your particular threat, whether they 276 00:38:11.790 --> 00:38:14.700 William Cheng: Will you know where the district has a signal block or not. 277 00:38:15.090 --> 00:38:23.880 William Cheng: Okay, so, so even though this name doesn't really seem very, very consistent because this seems that is trying to control the process signal the signal mass. But again, this is a very, very old. 278 00:38:24.150 --> 00:38:31.710 William Cheng: You know system called over here, you can actually use that to control the signal signal mass inside your threat control, blah. Okay, so in. So actually there's a 279 00:38:33.480 --> 00:38:40.980 William Cheng: Piece of signal. Sorry, a piece of function call copy threat SIG SIG mask. Okay, so that would be another function, aka us to do exactly the same thing. 280 00:38:41.490 --> 00:38:45.990 William Cheng: Or I have. But typically, you know, I was gonna ask you all the example we're going to use. It's gonna 281 00:38:46.530 --> 00:38:56.700 William Cheng: It's going to use this particular function. OK. So again, it's important to understand that this one manipulate the signal mass inside your threat control block. Okay, not the not the process control block. Yeah. 282 00:38:57.480 --> 00:39:04.140 William Cheng: Alright, this function takes three arguments. The first argument over here is going to be one of these three possibilities are going to sort of talk about what they mean. 283 00:39:04.590 --> 00:39:12.420 William Cheng: The second one over here is the address of the signal set. Okay, so what is the signal set a signal set looks exactly the same as a signal mask. 284 00:39:12.840 --> 00:39:24.090 William Cheng: Okay, so therefore, again, it's a real bits over here 11010 a bunch of bands. Right. Some of them are why some of them are zero. Okay. So, depends on the first argument we're going to interpret the second argument differently. 285 00:39:24.870 --> 00:39:30.960 William Cheng: That and the third argument over here is actually return value. So you should think about the signal mass also as a 286 00:39:31.650 --> 00:39:33.660 William Cheng: You know, also as the context of your threat. 287 00:39:34.200 --> 00:39:39.360 William Cheng: Well, so therefore you can actually change the signal mask and then you save the contacts and later on you can restore the signal mass 288 00:39:39.480 --> 00:39:50.070 William Cheng: So using the third argument over here, you can say the signal mass in another variable. And when you're done with that you want to restore it back to what it was before. In this case, you can actually, you know, put up a copy the signal bass back to your process up 289 00:39:50.670 --> 00:39:53.790 William Cheng: You can copy the signal the signal mass back to your threat control block. 290 00:39:54.870 --> 00:40:01.590 William Cheng: Guys, we're gonna see how this is done. Alright, so. So the first example here. The first argument of yours classic block. 291 00:40:02.220 --> 00:40:12.330 William Cheng: Then, then the new signal mass insider threat control blog is the union of the current signal mass and, you know, so this one is a pointer. What would the value that points to 292 00:40:13.440 --> 00:40:17.070 William Cheng: Okay, so they were where's the unit right so unit is a bit wise or operation. 293 00:40:17.520 --> 00:40:26.640 William Cheng: So in this case, what happened is I will be here. So, so that said the signal said, oh, he has the value of, you know, 0000 bunch of zero over here and only one of the bits of equal to one, everything is equal to zero. 294 00:40:27.990 --> 00:40:31.740 William Cheng: Okay. So in this case, you're going to take your signal mass and you're gonna you're not to a bit why or 295 00:40:31.920 --> 00:40:42.570 William Cheng: Why or bitwise or with the arguments over here. So, because what's going to happen, right. So if you or anything with zero, you can end up with how you had before. If you or anything with one that you don't end up with the one 296 00:40:43.170 --> 00:40:49.800 William Cheng: Okay, so therefore, whatever you have this one bit over here. When you're finished, you know that the signal mass that particular bit over here will be set to a while. 297 00:40:51.540 --> 00:40:59.520 William Cheng: Okay, so, so, so, so what is the result. Right, so the result is that this particular the signal over here that corresponding to that bear that signal will be blocked. 298 00:41:00.090 --> 00:41:03.810 William Cheng: Right. Because what because on the previous slide over here, we said in this little signal mask over 299 00:41:04.800 --> 00:41:16.410 William Cheng: The signal mouse over here if equals two to one that means that the signal is blocked. So, therefore, if the signal set over here has a single one. We know that in the end when this function is called. We know that particular signal will be blocked. 300 00:41:17.820 --> 00:41:22.200 William Cheng: Okay, so therefore, when you can see progress when the first argument over here is called SIG block. 301 00:41:22.500 --> 00:41:32.040 William Cheng: So, so in that case we're blocking a particular signal. Okay. What if the signal setup we have two beds or equal to one, all the other bits equal to zero. Well, in that case, we're going to block to signals. 302 00:41:32.880 --> 00:41:40.830 William Cheng: Okay. So in this case, you know, this is how we use a lot. Yeah. All right. The second example over here. What if the argument of year. The second argument over here is caustic and blah. 303 00:41:41.250 --> 00:41:48.510 William Cheng: Okay, so in that case the new signal mass is the intersection of the current signal mass and the core and the compliment of star sat over here. 304 00:41:48.840 --> 00:41:53.880 William Cheng: OK. So again, if the setup here is going to be equal to a bunch of zero with a single one over there, everything has to go to zero. 305 00:41:54.450 --> 00:42:06.150 William Cheng: Okay, the compliment of this value is going to be, you know, you're gonna flip all the bands one becomes 00 become why. So if you flip that you gotta get 111 when they're all what except for this video with zero everything. Everything else is going to equal to what 306 00:42:06.660 --> 00:42:12.600 William Cheng: OK, so now I'm going to perform an intersection operation where you perform intersection operation you're doing bitwise and 307 00:42:12.990 --> 00:42:18.930 William Cheng: Was in this case, will you anything with a while you're going to get what you have before. If you add anything with a zero, you're gonna end up with zero 308 00:42:19.410 --> 00:42:26.940 William Cheng: Okay. So, therefore, you know, in the end over here. One of the bit over here will be set to zero and that be a corresponding to the signal says right here. 309 00:42:27.480 --> 00:42:38.610 William Cheng: Okay, so that's what this is called sick on block, because that bit over here, whatever the value was, it was before. Now, it would be set to zero. And now you're unblocking a signal that specifies that specify in the signal set 310 00:42:40.320 --> 00:42:50.250 William Cheng: Okay, so, so that's the second block, right. The third choice over here is called except mass, if you call this function in the first argument is going to be six sigma. The new signal or be here is going to be equal to star set 311 00:42:50.760 --> 00:42:55.560 William Cheng: Okay, so therefore, in this case, this value that you have over here. He's going to get copied over to the signal mask. 312 00:42:56.010 --> 00:42:59.040 William Cheng: Okay, so one thing that you can do is that you can call sick proxy mass 313 00:42:59.250 --> 00:43:10.050 William Cheng: Using the first two ways of doing that. And then you're going to receive the previous signal mass. And then if you want to restore the signal mass. All you have to do is a caustic progress again and now the first argument we said mass and then 314 00:43:10.440 --> 00:43:17.910 William Cheng: The second argument over here will be, you know, what will be the contacts that you have saved and now we can restore the contacts. Okay. 315 00:43:20.550 --> 00:43:24.030 William Cheng: All right, so are you going to be very, very careful with the signal set over here. 316 00:43:24.270 --> 00:43:33.930 William Cheng: The signal set over here has been to one than zero. Sometime we flipped a bit something we don't put the bear. So again, you need to look at these function very, very carefully to understand how they want to use the signal set 317 00:43:34.140 --> 00:43:42.840 William Cheng: Guys are getting signals that it just a 32 bit value at the 30 to below ones and zeros. Okay, so, so, so, so we need to be able to manipulate all these bits. Okay. 318 00:43:44.730 --> 00:43:49.320 William Cheng: All right, so there are a bunch of functions that manipulated signal set over here. So again, you need to be careful. 319 00:43:49.680 --> 00:43:58.650 William Cheng: For some APIs one means to block it and then zero means on block it in some other you know APIs will be here. You know, one actually means I'm blog and then zero means block. 320 00:43:59.370 --> 00:44:04.470 William Cheng: Okay, so, yeah, you know, depends on some other argument is that a function. So you got to read the main pages, very, very carefully. Yeah. 321 00:44:05.010 --> 00:44:15.360 William Cheng: Alright, so there are a bunch of function to manipulate the signals that so one is called sick empty set. So in this case, you got to provide an sort of an address of a sad. So in this one is done. 322 00:44:15.930 --> 00:44:24.060 William Cheng: The do the signals that will be set to zero. Okay, so that's why it's called empty stale going to end up with a completely empty. That means that there's no one's it's completely zero 323 00:44:24.480 --> 00:44:29.880 William Cheng: Now if you want to add or remove a signal from the signal set over here you can cause sick asset. 324 00:44:30.060 --> 00:44:38.550 William Cheng: Right. So in this guy. I guess the first argument over here is going to be the Agile of a signal set and the second one over here will be to you'll be telling you which bit to set to a one. 325 00:44:39.300 --> 00:44:47.790 William Cheng: That. So in this guy is the signal number over here. Again, it's the numbers go from zero to 31 is one of the bids over here. You want to say that to a one. Okay, so you will call this function. 326 00:44:48.210 --> 00:44:54.270 William Cheng: That signal did a Sig SIG delete that. It's the same thing over here, except that you want to change one of the bit to zero. 327 00:44:54.810 --> 00:44:57.180 William Cheng: Yeah. So let's take an example of how this can be used. 328 00:44:57.690 --> 00:45:05.640 William Cheng: So, for example, I'm going to start with a variable over here on initialized variable. I'm going to call sick empty set to initialize it now this cell will contain everything equal to zero. 329 00:45:06.510 --> 00:45:16.290 William Cheng: Then the next thing that will do is I will cause sick asset and again the address of the set over here. And then third argument, all of us cause seek help. Seek opposite is that the hang up signal. 330 00:45:17.010 --> 00:45:21.690 William Cheng: We don't know what it means. Over here, but what I know one thing is that in the Linux system. This one is signal number one. 331 00:45:22.740 --> 00:45:32.160 William Cheng: Okay, so we're signal. Number one, is this a serial number zero the signal number one over here. So what I call seek asset for sick have over here. That means that bit one over here will be changed to a one. 332 00:45:33.600 --> 00:45:44.100 William Cheng: There. The next thing I will do is I was gonna call sick asset over here again the same set over here that has a single one in it. I'm gonna, you know, a set the bit that correspond to sink in. And see, again, I know that this one is equal to 333 00:45:44.760 --> 00:45:49.680 William Cheng: A single number two. So therefore get disability 01 bit too. So, therefore, this one will be set to one. 334 00:45:50.430 --> 00:46:01.980 William Cheng: Okay, so when I finished writing my code over here six that will be will be raised beds right 30 of them are zero and 201 and the one corresponding to signal one and signal to 335 00:46:02.880 --> 00:46:08.670 William Cheng: Guess. Okay, if you look at this number over here. They're all be zero, except the you know the I'm going to have a pattern of 110 336 00:46:09.120 --> 00:46:20.100 William Cheng: That. All right. What if you do the call on the right over here. The opposite of sit empty set is called sick Phil said so when you call six Phil set the set over here will be set to all once, right. So again, there are 32 bit of wines over here. 337 00:46:20.340 --> 00:46:31.110 William Cheng: And then when I when I call sick delete that over here, again, one of the biz over here is going to be Ted set to zero and this one is going to be a bit one over here because they copy go to one so bit one over here is going to be set to zero. 338 00:46:31.350 --> 00:46:46.170 William Cheng: Okay, and then I called silly, said the two of you will be set to zero. So when I'm done over here, this will have 30 3030 business wise and two bits of zero and the business zero correspond to seek hub. And so again, right, or, you know, serial number one. Number two. 339 00:46:47.520 --> 00:46:52.920 William Cheng: Okay, so this is how we use these functions. Okay, so we're going to see how to actually use this to solve our problems before 340 00:46:53.610 --> 00:47:00.120 William Cheng: That. So the first one. The other, the first the first solution over here is that we're going to see the correct way to wait for signal. 341 00:47:00.810 --> 00:47:04.200 William Cheng: Okay, right. So let's click right over here is our first we need to block SIG alarm. 342 00:47:04.440 --> 00:47:14.970 William Cheng: Okay, so once we block signal alarm if signal is going to get generated, you will become pending. So again what we supposed to do, is that if we want to wait for a signal. We're going to wait for it in a way so that we can never miss the signal. 343 00:47:15.360 --> 00:47:20.370 William Cheng: Okay, the way that we did in the previous quarter over here, it causes Miss, miss a signal. So in this case, again, 344 00:47:20.910 --> 00:47:27.720 William Cheng: Same thing as a piece of condition way we need some sort of atomic operation in order for us to wait for a signal delivery properly. 345 00:47:28.500 --> 00:47:31.920 William Cheng: Okay, alright. So, this is what we do over here. First we're going to block the signal. 346 00:47:32.430 --> 00:47:37.200 William Cheng: Okay, so, so we're going to plug the single before. Why not belong card because once you wind up the alarm clock is too late. 347 00:47:37.800 --> 00:47:42.450 William Cheng: Okay, so therefore we need to block the signal for us. So this way when the alarm degenerated you will be contending 348 00:47:42.810 --> 00:47:49.230 William Cheng: Yeah. Alright. So the way we do this as I we have a set over here. We're going to cause sick empty says that it alters zero over here, right. 349 00:47:49.440 --> 00:47:59.070 William Cheng: And then we're going to call sick as a single mom. So one of the bits over here that correspond a single line is going to be equal to one. I don't remember what the signal. I'm number here it is. You can actually look at the you know the Linux code over here. 350 00:47:59.520 --> 00:48:07.680 William Cheng: And then what I would do is I will cause massive blog over here with the six there so I know the set over here is going to be zero, except for one bit over here, equal to one. 351 00:48:08.370 --> 00:48:16.230 William Cheng: Guy. So when I finished doing now, I'll be here inside my throat control blah, I will have sickle unblocked then I will also take the current 352 00:48:16.710 --> 00:48:21.330 William Cheng: Signal mask over here and store in the offset the also say local variable right here. 353 00:48:21.990 --> 00:48:28.560 William Cheng: Guys again what i'm doing over here is to save the contacts and later on I can put the the the signal mass back to what it was before. 354 00:48:29.460 --> 00:48:34.380 William Cheng: Then, so once I you know I finished the system call now SIG alarm is going to be blocked. 355 00:48:35.040 --> 00:48:43.380 William Cheng: OK, so now it becomes safe for me to wind up the alarm clock, because I know that even long goes off on in this case deal others the single arm will be pending. 356 00:48:43.770 --> 00:48:51.060 William Cheng: Okay. So this guy is, again, I'm going to set up my alarm clock. I'm gonna call set I timer over here. Let's say up the alarm to go off in one millisecond. 357 00:48:51.630 --> 00:48:57.930 William Cheng: Okay, so in this case will happen if you know what will happen if I did the opposite side to give him the CBD somebody else. 358 00:48:58.200 --> 00:49:04.140 William Cheng: Well, in that case, when the signal is going to get deliver it will not get delivered because the signal is blocked so therefore the signal is going to become pending. 359 00:49:04.500 --> 00:49:09.600 William Cheng: Bigger. So, therefore I'm perfectly safe. So now I only have to do is to wait for the signal to get deliver 360 00:49:10.020 --> 00:49:14.670 William Cheng: So in this case, I'm going to call sick Phil said change all these beds equal to one over here, right. 361 00:49:14.880 --> 00:49:25.650 William Cheng: Now and then I'm going to call CIC D Lisa for single arm. So one of the bit over here that correspond to sit alarms going to set to zero. And then I'm going to call another system called that we never saw before. So this one is called six six suspend 362 00:49:26.550 --> 00:49:37.680 William Cheng: There's what is success. Ben six spin will suspend my thread. And right before to suspend my thread your map I go to sleep. What do we do, is that it will replace the color signal mess with the argument over here. 363 00:49:38.310 --> 00:49:50.730 William Cheng: Okay, so the argument over here is going to be everything is going to be zero. Everything's going to be one over here, except for one of them go to zero. So again, if you replace the signal mess with this pattern over here I'm blocking all the signal, except for signal are 364 00:49:51.960 --> 00:49:54.840 William Cheng: Okay, so right before I go six that spend signal is blah. 365 00:49:55.290 --> 00:50:08.640 William Cheng: And now what I call this signal will be unblocked and this is done in one atomic operation. Okay, so therefore we equals six has been a comically onboard the signal. So I'm only waiting for sit alone. And now I'm going to wait for the signal on one atomic operation. 366 00:50:10.050 --> 00:50:18.240 William Cheng: Okay, so this way if single arm is pending over here then then it will get deliver and then in that case I will return for six spent because now the signals they didn't deliver 367 00:50:18.480 --> 00:50:25.710 William Cheng: Okay, even the signal is not pending. So once I call six spent over here. I'm waiting for the signal and now with the alarm go off. I will never miss it. 368 00:50:26.130 --> 00:50:34.680 William Cheng: Again, so they have again. Once the alarm is deliver over here by calling that function calls doing something interesting. And then it will return us from home for six has been 369 00:50:35.640 --> 00:50:41.250 William Cheng: There. So in this case, ones that we talked about six. Why do well in this case what I would do is I will restore the signal mass 370 00:50:41.460 --> 00:50:53.940 William Cheng: By calling see process sigma success massive over here. And the argument over here is going to be all set. So in this case, I'm not interested in the, in the previous signal mass because I'm restoring the signal mass. But back to what it was before. 371 00:50:54.270 --> 00:51:01.350 William Cheng: OK, so the over the 30 minute here will be no to say that I don't want, I don't want to receive the currency currency. Don't ask. 372 00:51:01.980 --> 00:51:15.780 William Cheng: Okay, alright. So again, you know, every time in this class we see atomic operation is very important to understand exactly what's been locked together. So when you call six suspend it unblock the signal and wait for the signal in one atomic operation. Yeah. 373 00:51:16.860 --> 00:51:17.100 William Cheng: Alright. 374 00:51:18.300 --> 00:51:21.120 William Cheng: So we're going to pick totally over here. So, right, right, right. Is that is that, you know, 375 00:51:22.050 --> 00:51:26.160 William Cheng: At the time when I call six and spent over here is signal on his signal on block. 376 00:51:26.430 --> 00:51:33.030 William Cheng: Well, yeah. Because I write code over here to make sure the signal on his blog. So, therefore, if somebody tried to deliver and signal before that it will become pending. 377 00:51:33.450 --> 00:51:45.270 William Cheng: Okay, so therefore, again, you know, when I call six to spend over here, single VM is blocked before that I'm going to call salami unblock the alarm and wait for the alarming one atomic operation. So in this case, when can the signal. I'm get deliver 378 00:51:45.510 --> 00:51:51.990 William Cheng: It cannot get delivered in the middle of these two operation because it's to operation over here is atomic it cannot get delivered before because signal. 379 00:51:52.350 --> 00:52:01.080 William Cheng: Because the sick alarm is blocked so therefore the only time the signal can be delivered is afterwards. So in that case, I'm waiting for it. So, therefore, there's no way for me to miss the cigar. 380 00:52:02.070 --> 00:52:08.640 William Cheng: Then so against very, very important to understand this without the atomic operation, you know, there's no way to implement something like this. Right. 381 00:52:11.100 --> 00:52:17.490 William Cheng: Alright, so this is a very important slide. There's only one correct way to wait for asynchronous, even though you generate 382 00:52:18.660 --> 00:52:21.450 William Cheng: Okay, so, so, so over here, right. I'm the one that 383 00:52:23.040 --> 00:52:31.200 William Cheng: You know so. So over here, in this case I'm winding up the alarm clock. Right. So, therefore I'm Jen. I'm calling you know set a timer over here. So I'm generating the arm. 384 00:52:31.740 --> 00:52:35.640 William Cheng: Okay, so therefore, in this case, there's only one way for me to wait for this asynchronous ignore 385 00:52:36.360 --> 00:52:42.660 William Cheng: Guys. Okay, well, you won't have the alarm alarm clock. You say, wake up, wake me up one millisecond later when, when are you going to get get get woken up 386 00:52:42.900 --> 00:52:50.220 William Cheng: Well, you don't know right because again this is asynchronous signal. The signal is going to go off, you don't really know what co you're executing at the time this the signal is going to go off. 387 00:52:50.820 --> 00:52:59.160 William Cheng: Okay, so therefore get this is asynchronous signal. So the right way to do is, at first, you're going to block the asynchronous event right so this way when the event get generated it will become pending. 388 00:52:59.640 --> 00:53:06.840 William Cheng: Okay, and then you do something that will cause a signal is going to generate. So in this case I will wind up the alarm clock. So we can go off at any time. 389 00:53:07.080 --> 00:53:14.400 William Cheng: And then the third step of yours. Very, very important. I need, I need an atomic operation. I need to unblock the event and wait for The Event in one atomic operation. 390 00:53:15.960 --> 00:53:23.280 William Cheng: Now, so please understand that this is the only way the only correct way to wait for an asynchronous event that you generate 391 00:53:24.270 --> 00:53:31.470 William Cheng: Okay, if you see any other way to do it. They're probably wrong. Okay. Alright. So again, this is the only way to do it. Okay. All right. 392 00:53:32.430 --> 00:53:38.130 William Cheng: And also there is the one we talked about before, a piece a piece of conditional access piece of 393 00:53:38.790 --> 00:53:49.440 William Cheng: A piece of initial way over here. There's only one correct way to wait for a scene where does the bear that somebody else generate it right so somebody else generally will call it the event will call it a notification. We call it a condition 394 00:53:49.860 --> 00:54:00.930 William Cheng: That. So again, what, what is the proper way to do it better. And this guy's you yeah the example of yours that you wait for a garden become true in a garden command and the guard become true when another threat you know 395 00:54:02.940 --> 00:54:13.110 William Cheng: One another, to change the bearable. Okay. So, therefore, in this case the correct way to do it is to unblock the asynchronous event. Right. So why is your market. You can check in the event has has been generated 396 00:54:13.530 --> 00:54:24.450 William Cheng: By by evaluating the guard that if it turns out the guard is false. It's really the goddess far as in that case Youngblood the event and you wait for The Event in one atomic operation. 397 00:54:26.490 --> 00:54:33.690 William Cheng: OK. So, again, very important to even memorize the slide over here when you try to you know wait for asynchronous event that you generate 398 00:54:33.900 --> 00:54:43.260 William Cheng: This is the only way when you try to wait for a segment as the event or notification coming from another threat, then you know this is the only way. Okay. There's no other way to do it. Okay. 399 00:54:44.850 --> 00:54:53.190 William Cheng: All right, so I guess this is a good time to break. So this ends the first part of the lecture six video and then we're going to go to the second part. Okay.