WEBVTT 1 00:00:01.439 --> 00:00:13.469 William Cheng: Okay, this is the third video for Lecture five, so. So let's take a look at the solution for the readers and the writers problem. So over here says, you know, what is wrong. What is wrong with this code. Okay. 2 00:00:14.130 --> 00:00:24.600 William Cheng: So, so, so what happened over here is that, so let's say that we have a very, very popular, you know, database. Everybody wants to read it. Okay. And then some people want to modify the database in this case. 3 00:00:25.200 --> 00:00:34.710 William Cheng: So it is possible that you can write your code like this, it is possible that the writers would never get a chance to write, right, because whenever you have a writers over here. So let's say you have writers over here. 4 00:00:35.370 --> 00:00:41.070 William Cheng: You know, calling the writer function over here and and in this case, what if the number of readers never goes to zero. 5 00:00:41.520 --> 00:00:46.260 William Cheng: Okay, so for example we have 100 readers over here. And then when some leave the new threads going to come in. 6 00:00:46.710 --> 00:00:54.270 William Cheng: So when a new thread come in, since there's somebody reading already all the new reader threat they simply they come and join all the other readers. Right. They will read in parallel. 7 00:00:55.050 --> 00:01:02.100 William Cheng: Yeah. So if you think about the system as a dynamic system all the readers keep coming and coming in. So in this case, the writer would never get to right 8 00:01:02.610 --> 00:01:10.230 William Cheng: Okay, so this is known as a starvation problem I starvation, meaning that you have a thread that wants to sort of to to to to to get access to data. 9 00:01:10.650 --> 00:01:25.080 William Cheng: So in this case, you never get to access the data. So therefore, you're so you know where your threat will starve to death. Okay. So clearly, if you write your code that there's a chance that the threat will never get the CPU, then you know the people who 10 00:01:26.250 --> 00:01:31.740 William Cheng: Are those people who want to modify the database, they're going to get very upset. Okay, so how do you really solve this problem. 11 00:01:32.550 --> 00:01:42.420 William Cheng: So so clearly in this case if you you know if your system is very, very popular. Lots of people want to read a database, all the time, then the writers never get to write to write to the database. Okay. 12 00:01:43.230 --> 00:01:48.990 William Cheng: So the solution over here is that, you know, so what we can do is I'll be here is that we can sort of change the definition of some of these variables over here. 13 00:01:49.200 --> 00:01:53.370 William Cheng: Whereas, for example, the number of the writers over here. He used to be the number of writers that are writing 14 00:01:53.730 --> 00:02:01.230 William Cheng: Okay, what if we change the definition over here to be writers for the writers over here to be the number of writers who wants to write to the database. 15 00:02:01.770 --> 00:02:11.910 William Cheng: Okay, so this way. As soon as any writer thread. Want to any writer threat comes to the system account called this code over here, then in that case it will block off all the future readers. 16 00:02:12.630 --> 00:02:15.630 William Cheng: Okay, so if you want to do that. Is that really a good idea. 17 00:02:16.590 --> 00:02:25.170 William Cheng: Okay, so let's say you can do that right there. The writers over here is going to be the number of writers who wants to rise, and it can go from 0123 to a very large number. So as long as there are writers who wants to write 18 00:02:25.350 --> 00:02:27.060 William Cheng: All the future readers that will get block. 19 00:02:27.360 --> 00:02:34.050 William Cheng: Okay, so you know where to kind of make sense. And this kind of application, right, because if you have a writers who wants to read that means that the writer want to modify the database. 20 00:02:34.230 --> 00:02:39.090 William Cheng: If you keep letting the readers keep reading the database. Well, then in that case the reader will be reading old data. 21 00:02:39.810 --> 00:02:43.170 William Cheng: Right, because the new writer who wants to modify the database, they will change the data. 22 00:02:43.440 --> 00:02:51.480 William Cheng: But if you block them out and if you let the other readers were continue to read and you let the new new readers. Let's continue to read the data. So in a way, they're reading all data. 23 00:02:52.110 --> 00:02:58.050 William Cheng: Okay, so therefore it will actually make make perfect sense. In this case, to define the writers as a number writer who wants to right 24 00:02:58.890 --> 00:03:04.740 William Cheng: Okay, so now we're going to end up with three different variable in the guard. They have number of writers over here, a number of readers and a number writer who wants to write 25 00:03:04.890 --> 00:03:10.020 William Cheng: We also need to make sure that there's only one writer thread that's writing at a time. So we need to introduce another variable. 26 00:03:10.560 --> 00:03:19.200 William Cheng: Okay, so to to count the number of writer who is currently writing into the database. So this way, we're not going to end up with multiple writers threat right into the database at the same time. Yeah. 27 00:03:19.950 --> 00:03:26.070 William Cheng: Alright, so the solution over here is that we're simply going to use exactly the same code. Okay. For the readers might be exactly the same. 28 00:03:26.550 --> 00:03:36.960 William Cheng: Except we're going to interpret this data a little differently. So for the reader, when the number of writers who wants to write is equal to zero, that means nobody wants to right. So in that case, all the readers over here. A common reading parallel 29 00:03:37.290 --> 00:03:47.880 William Cheng: As soon as a writer you appear over here, what he would do is anyone incremental number of writers who wants to right so this way you will block all the future reader and all the future either. They will get stuck over here in the first quarter command. 30 00:03:48.450 --> 00:03:58.530 William Cheng: Okay, so therefore the solution will look like this. Okay, the reader code over here exactly the same but the number, the writers that we give me the number of writers who want to write. So now we need to examine the code for the writers 31 00:03:58.800 --> 00:04:05.100 William Cheng: When the writer threat comes in. The first thing that we're gonna do is we're gonna we're going to increment atomic Lee, the number of writers who wants to right 32 00:04:05.610 --> 00:04:15.090 William Cheng: Okay. And then, as long as the number of readers, you go to zero. So, so, so in this case whenever a new writer threats appear all these reader through eventually they will have to finish. Right. 33 00:04:15.690 --> 00:04:22.770 William Cheng: Okay, the reader through either reading the database, sooner or later, they have to finish, but the new reader thread. They're all block, you know, in the first quarter command. 34 00:04:23.130 --> 00:04:32.940 William Cheng: Okay, so therefore, eventually the number readers of the year ago. Go to zero. So when the number of readers, go to zero. And then the other variable we hear is known as the activator. So the activator is the one that act like a 35 00:04:33.720 --> 00:04:39.540 William Cheng: Binary sevenfold go from zero to 110 while the number of writers who wants to write that will behave like a counties that before. 36 00:04:40.200 --> 00:04:44.010 William Cheng: Okay, so it again, a number of activities over here. Additionally, equal to zero. So, now, 37 00:04:44.310 --> 00:04:54.720 William Cheng: As soon as the number which is zero. Now you're going to come in over here and now the number of active right it's going to become one. And then you're done with the garden command and now you come over here and you take your time to modify the database. 38 00:04:55.260 --> 00:05:03.660 William Cheng: Okay, so when you're done writing to the database over here, what you will do is that you would recommend a number of writers who wants to write, you will also a detriment. A number of active writers 39 00:05:04.080 --> 00:05:16.980 William Cheng: So, not this. In this case, the double the number of active writers over here. Well, go to zero. Okay, while the number of writers who wants to read, maybe still bigger than why. So in this case, as long as your writers through to who wants to write, they will block out all the future readers. 40 00:05:18.390 --> 00:05:27.510 William Cheng: Okay, so this will solve our original problem. But in this case, is it possible that that the reader third one will will actually get blocked out in definitely 41 00:05:27.930 --> 00:05:36.630 William Cheng: Okay. So in this case, they will be popular. It's possible. If you have a system where the writers keep coming in Russia. This guy is going to have the reverse problem, the reasonable get blocked out 42 00:05:37.470 --> 00:05:43.800 William Cheng: Okay. So is that a problem. Whereas, if you think about if you have a system that everybody's want to modify the data, but nobody wants to read there. 43 00:05:44.130 --> 00:05:50.670 William Cheng: And pretty soon you're going to go out of business. Anyways, because nobody's interested in your data. Okay, so, so, so by that kind of logic, you know, 44 00:05:51.240 --> 00:05:54.150 William Cheng: Writing code. This way to actually make sense. OK. So again, you 45 00:05:54.810 --> 00:06:01.350 William Cheng: Know this, this call was sort of sorry example, the two version of the CO. One is to give the reader threat higher priority to read the database. 46 00:06:01.530 --> 00:06:07.470 William Cheng: And now, if you write your code like this. Now you're giving the writers were higher priority to to read to read the database. 47 00:06:07.650 --> 00:06:20.790 William Cheng: Okay, we just accomplish this without using something called PCI party. Right. So again, I mentioned before in the Peter library, there's a way to actually set the priority for your threat. Don't do that. Okay. If you want to get gives you a priority right it this way. 48 00:06:22.080 --> 00:06:31.560 William Cheng: Okay, so this is the better way. So, so in this case again. All this will have exactly the same priority, but because of the way you organize CO, you can actually says the reader through more important or the writer three are more important. 49 00:06:32.100 --> 00:06:37.560 William Cheng: Okay, so that will be a better way to go. Yeah, right. So. So in the end, you know, if it turns out that reader and 50 00:06:37.980 --> 00:06:46.620 William Cheng: Writer thread. They are just as important to each other. So, you know, maybe you need to write the code to say the reader will read and the writer will ride the reader Maria, you're going to sort of give them alternating 51 00:06:46.830 --> 00:06:56.400 William Cheng: Right. So this way, they all get a chance to access the database. Okay. So, yeah, we don't have co for that. But you can use that as an exercise. Try to think about how would you write a record like that. Okay. 52 00:06:57.780 --> 00:07:12.330 William Cheng: All right, so the reader rider problem are very, very popular. So P3 also input as well. Sorry, I jumped ahead myself. So if you take the right it's cold over here again you can directly translate into sicko right by converting, you know, using the formula we saw before. Now, 53 00:07:13.410 --> 00:07:19.260 William Cheng: Alright, so the reader coast, they exactly the same. The writers over here again codes going to be a be a little longer. Again, I'm not going to go over at 54 00:07:19.740 --> 00:07:25.830 William Cheng: The recipes exactly the same. Alright, so, politics, you know, because return writers are very, very popular positive actually 55 00:07:26.070 --> 00:07:38.160 William Cheng: implementing them from scratch. You can see that there's a lot of function over here support reader and writer. So again, we are not going to talk about them because we know how to implement them simply using a new tax and a condition variable and to condition variables. 56 00:07:38.490 --> 00:07:40.320 William Cheng: Okay. And that will be the right way to do it. Yeah. 57 00:07:42.240 --> 00:07:46.620 William Cheng: Alright, the last synchronization. We're going to look at. It's called barrier synchronization. 58 00:07:46.920 --> 00:07:56.070 William Cheng: This is also known as a fork join network, right. So the idea here is that if a bunch of threats, they want to come over here and they they they want to come through something called a barrier. 59 00:07:56.670 --> 00:08:02.340 William Cheng: So for example, let's go to a barrier looking like this. Okay. So the idea here is that this barrier has a height. 60 00:08:02.850 --> 00:08:09.720 William Cheng: For example, if this barrier has a height of five. That means that in order for me to cross the barrier. I need five threads to come to the barrier. 61 00:08:09.960 --> 00:08:17.820 William Cheng: Okay if I only have four threads. They all have the way that the barrier when the fifth threat comes along the barrier is going to collapse and all these five threat they will all go across. 62 00:08:18.900 --> 00:08:21.750 William Cheng: Okay, so. So what kind of a, you know, CO will require that 63 00:08:22.290 --> 00:08:32.100 William Cheng: So if you are a W person, you probably do some signal processing the signal processing, you know the the way the single positive works is that sometimes there's there's parallelism. And sometimes there's no parallelism. 64 00:08:32.310 --> 00:08:37.620 William Cheng: And when there's parallelism. You gotta sort of figure out what is the number of parallelism, you have and you can actually create a bunch of threat to do that. 65 00:08:37.950 --> 00:08:45.180 William Cheng: Yeah. So what I can do over here is that, you know, so, so my competition against you know so if you read computation that way is known as the computation graph. 66 00:08:45.570 --> 00:08:53.880 William Cheng: Guy. So, I will write my competition, grab like this. I have a serial stage number one over here. So in the serial stage. There's no parallelism. So I only need one threat. Okay. 67 00:08:54.090 --> 00:09:00.720 William Cheng: And then I come to a parallel stage. So here's the palace station number one over here, I can actually have a parallel ism or four 68 00:09:00.870 --> 00:09:08.910 William Cheng: So I can create four threads over here and they were running parallel when they're all done, they need to join together over here. And then I can go to zero stage number two. 69 00:09:09.870 --> 00:09:13.920 William Cheng: That. So the way that you can do this as I can use bear synchronization over here as I 70 00:09:14.580 --> 00:09:22.080 William Cheng: Let me go back a little bit. Okay, so what I can do is that right before zero stage number to over here. I'm going to create a barrier synchronization, you know, a five threads. 71 00:09:22.680 --> 00:09:25.350 William Cheng: Okay, so in this case what I can do is that when I finish. 72 00:09:25.590 --> 00:09:37.620 William Cheng: You know, a serial number one over here, what I would do is I will create for how threat, Chapter Number 1234 over here. And then my main side over here will come to the barrier. See when ization over here to wait for all the other four threads to finish. 73 00:09:38.040 --> 00:09:49.140 William Cheng: Okay, so this way when all the other four threads finish that there are five threads that the barrier, the bear is going to collapse. And then what I would do is I you know I will move across the barrier, and then we'll go serious stage number to 74 00:09:49.410 --> 00:09:55.650 William Cheng: Get when I finished hero say number two, maybe I have a parallel ism of nine rise over here again I can actually create nice there's over here. 75 00:09:55.800 --> 00:10:04.230 William Cheng: And then when I'm done with join with them. And now I end up with the barrier barrier of 1010 threads. So when tensor. It comes over here, the variable collapse and I can all go across. 76 00:10:05.130 --> 00:10:12.750 William Cheng: OK, so the implementation over here. I mean, if you see the implementation right away. Right. It's very, very simple. All I have to do is I call P3 create four times. Great for childcare. 77 00:10:12.960 --> 00:10:25.320 William Cheng: Let them do these things in parallel, I come over here I call pizza joint four times right so and so, in this case, when all the for child threat they are done, then I will cross the barrier of your I go to the next stage. Okay, so what is wrong with that implementation. 78 00:10:26.460 --> 00:10:30.240 William Cheng: As it turns out, P3 Korean pizza joint. They are very, very expensive. 79 00:10:30.780 --> 00:10:35.940 William Cheng: Okay, because a lot of some of the criticism over there, they will actually make them into system call so will you. 80 00:10:36.180 --> 00:10:41.640 William Cheng: Know when you're doing signal processing, you're doing things that are very, very high speed. You don't really want to call he said Korean PHP thread joy. 81 00:10:42.570 --> 00:10:49.260 William Cheng: Okay. So the typical solution. What you would do is that you will use something called a thread pool approach right thread pool here is a pool of threats. 82 00:10:49.470 --> 00:10:54.270 William Cheng: So it's like a swimming pool over here. There's a bunch of threats swimming in those pool over there because what you would do is that, will you 83 00:10:54.480 --> 00:10:59.160 William Cheng: Will you know we'll probably start to run, you're going to create a bunch of threat and you have that swimming in a swimming pool. 84 00:10:59.490 --> 00:11:11.400 William Cheng: Okay, so it's called a thread pool over here. So when you come to this parallel the palace station number one over here. What you would do is that you will go to the thread pool and borrow for threats to do these for operations over here in parallel. 85 00:11:11.760 --> 00:11:21.000 William Cheng: Okay, so you borrow this 1234 here. You tell them come here to some work over here and then you go to a barrel synchronization and then you wait for these fights were to get to the barrier. 86 00:11:21.750 --> 00:11:24.360 William Cheng: Because again, you're going to do this without calling peace or joy. 87 00:11:24.750 --> 00:11:33.690 William Cheng: Okay. And when all fighters come to the barrier over here. What you want to do is that your main so I will continue forward what all these forth to add, they will go back to the flagpole. Again, they're going to continue this way. 88 00:11:34.170 --> 00:11:38.880 William Cheng: Okay, so when you finish your station number to over here again, he will borrow nine threads on the thread pool. 89 00:11:39.060 --> 00:11:47.820 William Cheng: And then would you do that you'll create a barrier synchronization for 10 threads when all these tend to has come to the barrier nine threads going to go back to the flagpole while you continue to see your code over here. 90 00:11:48.450 --> 00:11:56.880 William Cheng: Okay, so this way you copies are great at the beginning. Well you know and and then from that point on, you don't call piece or create you don't look happy to join anymore. 91 00:11:57.540 --> 00:12:04.260 William Cheng: Okay, so you know for you to implement since signal processing application, you got to make sure that you don't you don't keep doing a piece of Korean people enjoy 92 00:12:05.100 --> 00:12:14.640 William Cheng: That alright so the question over here is that now we have all these together, always running. How do we make sure that you know the to make sure that we can implement the barrier synchronization correctly there. 93 00:12:15.330 --> 00:12:17.430 William Cheng: So let's take a look at a solution over here. 94 00:12:17.970 --> 00:12:28.770 William Cheng: Okay, so here's the solution over here, right. So, so what you can do is that you can have to. So here's I would try to synchronize and thread over here. So my example over here and is equal to five and the beginning over here. 95 00:12:29.190 --> 00:12:38.820 William Cheng: Okay, so the first four threads. What they will do is that we're going to have that sleep in a condition variable. Q What the fifth threat when it comes into the piracy issue what he would do, is there a wake up all the other four threads. 96 00:12:39.690 --> 00:12:44.280 William Cheng: OK. So, the solution is very simple. Right. First of all three I call Peter condition ways right so the CO look like this. 97 00:12:44.910 --> 00:12:50.040 William Cheng: So, so all they will do that when they try to synchronize they will call berra synchronization over here. Okay, there's a barrier. 98 00:12:50.910 --> 00:12:54.780 William Cheng: There's a condition verbal cue called the barrier q, where the, where the second sleep in. 99 00:12:55.140 --> 00:13:03.270 William Cheng: So what the all the third will do is that they will copies of new tax law. And then what it will do is that it will increment of the town to find out whether they are three number 1234 or five 100 00:13:03.570 --> 00:13:09.210 William Cheng: Again, if they are not the last year. What they will do is that it will copy. So a condition where and they will wait inside the bears cute. 101 00:13:09.900 --> 00:13:13.440 William Cheng: Okay, so, so pretty soon so 123 and four. 102 00:13:13.650 --> 00:13:19.650 William Cheng: They will all be sleeping in the barrel. Q And the fifth when it comes along, it will increment count over here and then cans going to be equal to five. 103 00:13:19.800 --> 00:13:26.790 William Cheng: So therefore, the last day will reset County, go to zero so that this barrier synchronization code can be used for the next time you want to use bear synchronization. 104 00:13:27.180 --> 00:13:31.530 William Cheng: Yeah. And then what do we do that, it will copy search engine broadcast to wake up all these for threats. 105 00:13:32.250 --> 00:13:35.010 William Cheng: There. So this way you know the fifth threat well 106 00:13:35.430 --> 00:13:42.090 William Cheng: After a finished piece of engine broadcast, it's the one that's inside the critical section right so therefore it. We'll get it. We'll get two copies of meters unlock 107 00:13:42.300 --> 00:13:49.650 William Cheng: The fifth one lever, we hear all the other four threads, they're going to wake up one at a time when they you know because because they are they are inside the critical section co 108 00:13:49.830 --> 00:13:56.790 William Cheng: Right. So when you return from Peter condition where you still have the new tax law. So they're gonna wake up one at a time and then they will all copies me that a lot 109 00:13:57.480 --> 00:14:09.450 William Cheng: Okay, so this way, once you, you know what, once you have five to arrive at the barrier. Pretty soon they are going to leave the barrier. Okay. They don't really leave very exactly the same time, but pretty soon, they're going to need that. 110 00:14:10.110 --> 00:14:14.790 William Cheng: So what is wrong with this code that, as it turns out, this code doesn't work because 111 00:14:15.270 --> 00:14:19.620 William Cheng: As it turns out, you know, there's something I didn't talk about here. The piece or a condition way. 112 00:14:19.860 --> 00:14:26.730 William Cheng: The way the square piece or condition way is that when you throw a core piece or condition way and then if somebody wake you up then piece of content will return 113 00:14:27.420 --> 00:14:39.210 William Cheng: Okay. As it turns out, people are conditioned way can actually return for no good reason. Okay you three copies or condition way you unlock the meat as you go, you go sleeping so the condition verbal cue and Now somebody can wake you. 114 00:14:40.470 --> 00:14:44.520 William Cheng: As it turns out, this function can get, we can return even nobody wakes you up. 115 00:14:45.120 --> 00:14:52.290 William Cheng: Okay, when nobody a core piece of condition signal. Nobody's copy the country broadcast, all of a sudden you will return from Peter condition way with me timeslot 116 00:14:53.250 --> 00:15:00.510 William Cheng: Okay, so in this case why it's not going to work because all you know the other forthrightly manage leave early before the fifth come to the counter barrier. 117 00:15:01.110 --> 00:15:04.500 William Cheng: There, there's a link over here are the talks about, you know, 118 00:15:04.860 --> 00:15:12.810 William Cheng: What to do with this particular situation right because some people you know if the code starting to happen this way, they're going to complain to the P3 library implemented, say, Hey, you guys have a bug. 119 00:15:13.110 --> 00:15:23.220 William Cheng: Okay, because you promised me that you know when Peter condition where it over here when it returns. It means that somebody has signal broadcast the condition. Okay. The Peter library implement this is a no no no that's not what we said. 120 00:15:24.330 --> 00:15:37.080 William Cheng: There are little tricky. They say that, well, what we actually told you that if you call Peter condition way you have to put it in a while not guard yeah but putting an infinite loop, where is your internal. Well, there's no influence over here. Well, that's why it doesn't work. 121 00:15:38.370 --> 00:15:44.580 William Cheng: Okay. So, therefore, you have to call it while not guard piece or condition way. Otherwise, there's no guarantee that Peter condition will work that 122 00:15:45.240 --> 00:15:51.210 William Cheng: All right. So in the end, the pizza library people won the argument. So, therefore, you know, the, you know, you can complain about this implementation. 123 00:15:51.600 --> 00:15:59.160 William Cheng: Be incorrect. So now you know that piece or a condition where you can return for no good reason. That's why, as soon as piece of condition will return, you have to check the guard. 124 00:15:59.670 --> 00:16:03.630 William Cheng: Yeah, so now we're going to put it inside a guard over here while now guard. So what should be the guard. 125 00:16:04.080 --> 00:16:10.500 William Cheng: OK, so I'll be here. So, well, we can actually check it the county's lesson and if the cameras lesson. And that means that the Pfister hasn't arrived yet. 126 00:16:10.860 --> 00:16:18.510 William Cheng: Okay, so therefore we should all go back to sleep right so so 1234 now they will continue to go to sleep over here. As it turns out, this call also doesn't work. Why doesn't it work. 127 00:16:18.930 --> 00:16:22.800 William Cheng: Right, because when the fifth threat comes along the fifth or what you would do is that it will 128 00:16:23.160 --> 00:16:34.680 William Cheng: It will wake up all these threat and now the mutates is still lots of therefore no other thread all these for thread, they will not be able to execute because you know the fifth is the only new tax and now at some point he needs to set count equal to zero. 129 00:16:36.000 --> 00:16:42.300 William Cheng: Okay. The fifth or say the county go to zero. So, therefore, when all the other for third when they start running, they all see county go zero, they all go back go back to sleep. 130 00:16:43.860 --> 00:16:53.070 William Cheng: Okay, so. So what should you do right so maybe you can move the count outside, but he could move outside, you're going to end up with race condition. So no matter what you do, there's no guarantee that this code will work. 131 00:16:54.240 --> 00:17:00.300 William Cheng: Then, alright. So what is the solution over here, right, the solution over here is that you need to step back and say, well, maybe I'm using the wrong guard. 132 00:17:00.720 --> 00:17:07.170 William Cheng: Okay, maybe I should use a different guard right so you sort of think about is that, you know, these four threads over here 1234. Why should they leave. 133 00:17:08.250 --> 00:17:14.520 William Cheng: Okay, so if you think about the Baron synchronization embarrassing version like this right the first time. There are five threads. The second time there's 10 threads are 134 00:17:14.760 --> 00:17:21.030 William Cheng: Using over here. And the third time, maybe they are. You know, I don't know, again, five threads or something like that. Okay. Well, I sort of think about 135 00:17:21.480 --> 00:17:29.310 William Cheng: That you know the the guard he check is that is my current synchronization done okay my current civilization is down. What, then, in this case, everybody should leave. 136 00:17:30.030 --> 00:17:38.460 William Cheng: Okay, so what we should do is that we can actually look at this barrel synchronization. I'm going to call them different generation or barrel synchronization in the, in the beginning, you're in generation one. 137 00:17:39.510 --> 00:17:43.650 William Cheng: Okay. So, therefore, you know, in Generation one you're synchronizing five threads. 138 00:17:43.890 --> 00:17:51.720 William Cheng: That in the second generation over here. You're going to synchronize it tends to add the third generation is going to synchronize five threads over here. So in this case, you know, what should I do 139 00:17:52.320 --> 00:18:00.270 William Cheng: Okay, when every thread coming to the berry over here, they're going to find out which generation they belong to. When their generation is over the nation all leave the barrier. 140 00:18:01.560 --> 00:18:06.330 William Cheng: Okay, so therefore, in that case, the co will look like this there. So over here. 141 00:18:08.010 --> 00:18:15.060 William Cheng: Okay. So over here, this, this is actually this is the best solution over here. Again, we just going to skip this. I'm going to look at the actual solution. Yeah, the actual solution over here. 142 00:18:15.390 --> 00:18:20.160 William Cheng: To say that, you know, there is a global variable here called generation right generation in the beginning of you is equal to one. 143 00:18:20.280 --> 00:18:28.050 William Cheng: Right. So today I'm going to see the picture over here, we start with five and then we're going to go to 10 that will go to go to five. And here's Generation Y generation to generation three 144 00:18:28.350 --> 00:18:38.190 William Cheng: There. So this generation of years of global variable. So when you know when the first for threat corporate synchronization over here, they will use a local variable to remember which generation they belong to. 145 00:18:38.520 --> 00:18:43.530 William Cheng: Okay, so for three or 123 and four, they all belong to generation one. 146 00:18:44.280 --> 00:18:54.510 William Cheng: Okay, so this way when Peter condition would return for no good reason, they will compare to see if my generation equal the current generation. If there is no equal to one, that means that I'm not done with the current generation. 147 00:18:55.290 --> 00:19:04.620 William Cheng: Okay, when the fifth through a common loans over here right over here, you know, you'd be equal to five. So in that case, what you would do is that you will reset the counter back to zero, so you can use it for the next synchronization. 148 00:19:05.400 --> 00:19:10.380 William Cheng: And what it will also do is that it will increment the generation over here to say that generation one is done. 149 00:19:10.590 --> 00:19:17.220 William Cheng: Everybody needs a barrier. And now I will increment the generation of your ego to to I get ready for the next time I want us to bear synchronization. 150 00:19:17.670 --> 00:19:21.600 William Cheng: Okay, so this way. The fifth fret over here is going to leave right it's gonna call piece of content broadcast 151 00:19:21.810 --> 00:19:27.450 William Cheng: Wake up to 1234 and now the fifth is going to lead when all the stress when they wake up, they will check that the 152 00:19:27.690 --> 00:19:39.060 William Cheng: Their generation is equal to one. And now the current generation is equal to two. So, therefore, they will all your jump out of the infinite loop over here and they will then they will copies of meter and law. So therefore, they will leave the barrier one at a time. 153 00:19:40.320 --> 00:19:50.370 William Cheng: Okay, so here again the message over here is that what your code doesn't work. Maybe it's the problems with your design. Maybe if you use a different different guard and all of a sudden, things are things going to see things going to work. Okay. 154 00:19:51.780 --> 00:20:00.090 William Cheng: All right, so that's basically what it is also very, very popular because you know if you want to do signal processing or any kind of a, you know, application like that. 155 00:20:00.540 --> 00:20:14.430 William Cheng: So you want to use a thread pool and the year so you also want to have a barrier synchronization. Right. So again, we're not going to do that because we know how to do it by using just a mutation and condition variable. Okay. So, therefore, again, don't use this in warm up to. Okay. 156 00:20:16.470 --> 00:20:23.700 William Cheng: Alright, so we are done with synchronization or the next thing we're going to look at. It's called threats safety. So what is the safety there. 157 00:20:24.330 --> 00:20:35.520 William Cheng: So welcome, is a Unix was developed, long time ago before threading before multi threading was invented. OK, so the UNIX library code that were implemented, they are actually not safe when you run multi threaded code. 158 00:20:36.000 --> 00:20:41.400 William Cheng: OK. So the idea of a safety is that you want to make the UNIX library safe under multi threading. 159 00:20:42.450 --> 00:20:52.110 William Cheng: OK. So again, the definition of safety is that you want the UNIX library to be saved under multithreading so actually want any kind of a library that you want to build, you want to make sure that it's safe. 160 00:20:52.410 --> 00:20:56.880 William Cheng: Okay, so will you go work for a company for summer intern or for, you know, for your permanent job. 161 00:20:57.120 --> 00:21:08.700 William Cheng: One thing that they will do, they will ask you to implement libraries. So whenever you try to implement functions that are library. You got to make sure that your function is always safe okay when there are multi threading. Okay. They need to be safe, right. 162 00:21:10.020 --> 00:21:19.290 William Cheng: So if you look at if you look up the word thread safe, you know, Wikipedia, you're gonna you're gonna see somebody says, you know, through a safety and we entered, they are the same. Some people say they're not the same. 163 00:21:19.710 --> 00:21:30.360 William Cheng: So what is the difference between a safety net retention. Okay, so in this class we define threats at as you know library function being safe under multithreading 164 00:21:30.840 --> 00:21:37.650 William Cheng: Okay, what if you don't have multithreading. What if you only have one threat. Okay. So, in that case, according to our definition, if you have a single thread. It's always safe. 165 00:21:39.360 --> 00:21:53.550 William Cheng: Right, so, so that will be our definition of safety when you have a single so it's always say that what is the definition of a coping retention. Okay. The definition of retention is that when you have multi threading re entering equals two, three safety. 166 00:21:54.750 --> 00:22:00.390 William Cheng: Okay, so, so will your library code is safe under multithreading you can call it re enter or you can also call it 167 00:22:00.840 --> 00:22:12.930 William Cheng: A Coca Cola thread safe. But when there's only a single thread, you can also write code as it turns out not to be safe, right. So, in that case, if your code is re enter that means that your code is also saved under a single threaded 168 00:22:14.550 --> 00:22:18.570 William Cheng: Or so under multithreading. So again, let me say this again under multithreading 169 00:22:18.780 --> 00:22:28.260 William Cheng: Safety means exactly the same thing as we enter. But when there's only a single thread, it is possible to write code that's not safe. So in that case, the Yoko will not be a brewery retention. 170 00:22:28.590 --> 00:22:32.940 William Cheng: But your code will be thread safe because according to our definition if there's only one thread. It's always say 171 00:22:34.680 --> 00:22:42.750 William Cheng: Alright, so again, this is the definition for our class. If you get a exam question like that, you're going to answer according to the definition of this class. And now that definition out there in 172 00:22:43.920 --> 00:22:50.880 William Cheng: There in the internet that. All right. Okay. So let's take a look at an example how to make the code thread safe. Safe 173 00:22:51.360 --> 00:22:54.420 William Cheng: The first one over here is that we use the global variable, you're going to be very, very careful. 174 00:22:54.690 --> 00:23:01.740 William Cheng: Okay. So, for example, I'm going to make the racism call okay when the right system called fail, it will set the air number right here number is a global variable. 175 00:23:01.920 --> 00:23:12.540 William Cheng: So if I have one function over here is calling the racism call another function make another racism call over here. If both CPU are executing simultaneously both of them call right and both of them failed. 176 00:23:12.870 --> 00:23:15.930 William Cheng: One. In that case, when you try to get the air number, what's your number are you going to get 177 00:23:16.200 --> 00:23:28.110 William Cheng: Are you going to get your air number that corresponding to your failure or, you know, and now you have multithreading, it is possible. The air number that you see actually belong to the other thread, because when the other side fail. It will also set the air number 178 00:23:29.040 --> 00:23:35.640 William Cheng: Okay. So in this case, you know, all the library code over here for the system call if you want to set the ERA number there on that thread safe. 179 00:23:36.840 --> 00:23:46.200 William Cheng: Okay. So in this case, in order to convert the UNIX library to be thread, say, what do we have to do. Yeah. So the solution over here is that basically every threat has to have their own air number 180 00:23:47.430 --> 00:23:56.400 William Cheng: Okay, so if I once they actually over here, the air number that I use over here should be the air number for this to me. So this way when another thread over here, execute another system call 181 00:23:56.640 --> 00:24:00.120 William Cheng: Okay, when they try to exit the number that will be the arena before that threat. 182 00:24:00.960 --> 00:24:08.310 William Cheng: That's how to actually make sure the air number actually belong to different threats. Right. So in this case, again, every three have their own air number. So where would you put the number 183 00:24:08.580 --> 00:24:11.700 William Cheng: But one of the places you can put it is that you can put it inside the threat control, blah. 184 00:24:12.570 --> 00:24:15.150 William Cheng: Okay, so. So our solution over here is actually in two parts. 185 00:24:15.360 --> 00:24:24.600 William Cheng: The number one is that we're going to redefine air number to be nama global variable. And we're going to use the arrow number to indicate that we're referring to the error number inside the threat control, blah. 186 00:24:24.870 --> 00:24:37.080 William Cheng: OK, so I'll be here. Here's a central blah right TCP central threat control blah PCB stands for process control blah, so he's not a threat control block. We're going to do it in a more general way there's going to be a, an area called threat specific data. 187 00:24:37.770 --> 00:24:45.390 William Cheng: Okay, so they're going to be other global variable like the air number we're all going to put them in the same place over here, right, one of them over here. It's going to be the threat specific Error number 188 00:24:45.570 --> 00:24:50.850 William Cheng: So this way when we tried to access Eric number using the global variable, he will be converted into a function call. 189 00:24:51.270 --> 00:24:57.720 William Cheng: Okay, so therefore we're going to use quantify so pounded by Aaron number over here to be underscore, underscore error and Aaron number and this is the function 190 00:24:57.960 --> 00:25:05.940 William Cheng: And we're going to use the identifier as an argument. So this way when he's a three identifier to find the threat control blah and this way we can access this era number 191 00:25:07.380 --> 00:25:17.640 William Cheng: Or is that by combining these two solution. One is to redefine air number to be a function call and also insert everywhere control blog. We have their own we have we have a specific number. So this way. 192 00:25:18.090 --> 00:25:21.360 William Cheng: You know, if we're going to go back to our code over here, this code will actually work. 193 00:25:21.810 --> 00:25:27.360 William Cheng: All we have to do is to re compile this call when we re compile this code this error number over here will become a function call. 194 00:25:27.510 --> 00:25:33.720 William Cheng: And then when we perform our system call when we're done with the system called going to change the error number to be the one that didn't belong to this. 195 00:25:34.200 --> 00:25:38.550 William Cheng: Again, you know, inside the system called implementation over here. They also need to be re compile 196 00:25:38.820 --> 00:25:45.180 William Cheng: It after we can follow up here that also become a function call. And then in that case this particular right right system called when you fail. 197 00:25:45.390 --> 00:25:52.110 William Cheng: It will set the air number instead of three control block for this threat and this way we can pick it up the air number we can pick up the air number this way. 198 00:25:53.040 --> 00:26:03.090 William Cheng: Okay, so by combining these two of you know approaches over here. This solution is actually very, very elegant. Right. It's very elegant because the application programmer don't have to change a single line of code. 199 00:26:04.470 --> 00:26:08.250 William Cheng: That the application programmer. The co work. All they have to do is to be compatible. 200 00:26:08.430 --> 00:26:13.680 William Cheng: If they don't recall how their Co. What, then, in this case, the air number to become a global variable, then in that case won't work. Right. 201 00:26:13.860 --> 00:26:19.500 William Cheng: But as soon as they take the new header file inside the head of our we're going to pound defined Aaron number over here to be a function call. 202 00:26:19.710 --> 00:26:25.170 William Cheng: And then the third control blinds going to have the strip the specific Error number and now everything, all of a sudden, everything's going to work. 203 00:26:25.650 --> 00:26:29.190 William Cheng: Okay, so this is going to work for every UNIX system called that we use the number 204 00:26:29.610 --> 00:26:41.670 William Cheng: Okay, and the application programmer doesn't have to change a single line of code. Okay, so this is the best way to go. Okay. And, and turns out Unix actually did that to fix all their, you know, to fix all their era number problem. 205 00:26:42.900 --> 00:26:47.430 William Cheng: The second example for code that's not you know that that's our first save 206 00:26:48.060 --> 00:26:57.510 William Cheng: So there's a function called get hosed by name. It's a system call what he tried to do is that he tried to get you know the the the IP address, you know, for a particular host name. 207 00:26:57.870 --> 00:27:06.270 William Cheng: Okay, so if you don't know what IP address. It doesn't really matter. It's just a bunch of numbers over here. So for example, if the name that I want to provide over here is CNN com 208 00:27:07.050 --> 00:27:09.840 William Cheng: Okay, CNN com or google.com, whatever it is. 209 00:27:10.530 --> 00:27:15.540 William Cheng: So, so that's called a hostname okay by calling get host by name. You can convert a hostname 210 00:27:15.780 --> 00:27:21.660 William Cheng: To be an IP address. Okay, why do you need the IP address or because you want to use networking, you have to use IP addresses. You cannot use the host name. 211 00:27:21.900 --> 00:27:30.630 William Cheng: Yeah, so therefore the Unix operating system and a Linux operating system, give you a system called over here to to to to return an IP address, you know, for for pretty good hostname 212 00:27:30.960 --> 00:27:39.540 William Cheng: Okay. The problem with IP addresses that every host and can actually have multiple IP addresses. So this guy is when you return the data to you is going to be using to you as a data structure. 213 00:27:40.230 --> 00:27:49.590 William Cheng: Okay, so now if I have to threats one called a host by name for CNN com the other one called hosted by name and then what it will do is that I will try to ask for Google com 214 00:27:50.460 --> 00:27:56.730 William Cheng: Okay, so you can see that this function returns the return of pointer to a data structure. And now what is this data structure. 215 00:27:57.540 --> 00:28:03.750 William Cheng: There so chances are this data structure is going to be inside the data segment or inside the BSS admin. So it's going to be a global variable. 216 00:28:04.590 --> 00:28:12.420 William Cheng: Okay. So, therefore, if you have to threats. Both of them are calling co host by name it exactly the same time. In this case what it would do is they will cooperate each other's data. 217 00:28:13.350 --> 00:28:20.850 William Cheng: Okay. So, therefore, this particular function can host by name. It's also your this function is also not safe in order to make a threat, say, what do you have to do. 218 00:28:21.990 --> 00:28:29.130 William Cheng: That. So this one is different from Aaron number right it was the more complicated. The is going to return you a data structure and this data structure is complicated. 219 00:28:29.850 --> 00:28:35.460 William Cheng: So what it did over here is that, you know, in the unit system is that they come up with a re entry and VERSION OF GET HOST by name. 220 00:28:35.880 --> 00:28:40.290 William Cheng: Well, so again, they call this the re entry. And so again under multithreading we enter equal to thread safe. 221 00:28:40.890 --> 00:28:50.220 William Cheng: So, this function is also safe. You know, if you only have a single thread. Okay, so, but it's not really clear how can they be unsafe and when you only have a single thread. We're going to look at that a little later. 222 00:28:50.700 --> 00:28:55.320 William Cheng: Okay, so right now under multithreading re entering mean exactly same thing addresses that. 223 00:28:55.620 --> 00:29:05.190 William Cheng: Are so in this case we're going to have a new function. So, this is called get hosed by name underscore are to say that this function is retention. Okay. You can see that this is a much more complicated function. 224 00:29:05.910 --> 00:29:13.470 William Cheng: Okay, so the idea here is that before you can call get host by name minus, are you have to allocate all the buffers that will be used by this function. 225 00:29:14.070 --> 00:29:21.270 William Cheng: Okay. So, therefore, what do you application program has to do what they have to read the man pages to understand exactly how to create the data structure for this function. 226 00:29:21.480 --> 00:29:26.520 William Cheng: And then once they create all this data structure, they will call this function and this function was a returner them a bunch of data structure. 227 00:29:27.240 --> 00:29:32.700 William Cheng: Okay, so why did this code so sad right because we have two threads that are calling this function simultaneously. 228 00:29:32.970 --> 00:29:43.110 William Cheng: Both of them will allocate a data structure on their own and then they will call this function at the same time, but each one of them will provide their own buffer. So this way you know they're not going to clobber each other's data. 229 00:29:43.950 --> 00:29:52.650 William Cheng: Okay, so this is actually a really, really good technique. So whenever again you work on, you know, you get to work on a summer job you work on work for a company, whenever you need to write code inside the library. 230 00:29:52.950 --> 00:29:58.470 William Cheng: Always ask your application to create their, their own buffer they pass the buffer to you and then you fill the buffer. 231 00:29:59.370 --> 00:30:06.870 William Cheng: OK. So again, this is the same thing as we call the racism call right we call the recent system call you need to provide your own buffer. And then you ask the opposition to fill it. 232 00:30:07.200 --> 00:30:14.820 William Cheng: Okay. Similarly, you know, in any kind of a library function. The right thing to do is to ask the application to create a buffer for you pass the buffer, you can actually fill the buffer. 233 00:30:15.750 --> 00:30:22.680 William Cheng: Yeah. Alright. So. So again, the application program is not gonna be very happy for this because they actually have to really learn 234 00:30:23.280 --> 00:30:28.800 William Cheng: How to actually call this function by creating all these buffer, just the right number just the right size buffer. 235 00:30:29.130 --> 00:30:33.750 William Cheng: So when they call this function, they will get all the IP addresses. Because again, we're not going to get into detail for this function. 236 00:30:34.200 --> 00:30:37.470 William Cheng: If you take a networking class, maybe you will learn how to use this function. Okay. 237 00:30:38.400 --> 00:30:44.190 William Cheng: All right. Another example with first ad. If I was to ask our printer and the other so called printer of in the same time. 238 00:30:45.030 --> 00:30:54.240 William Cheng: So in this case, what kind of problem. Can I get into, okay. So in this case, through one and two, they are sharing file descriptor number zero right standard outside the 239 00:30:54.900 --> 00:30:59.160 William Cheng: state of affairs agreement, number one, so they're sharing file descriptor number one over here. 240 00:30:59.640 --> 00:31:07.170 William Cheng: The printer function is a wrapper functions around the right system call. Okay, so we don't really know exactly how print f is implemented. So when eventually call right 241 00:31:07.950 --> 00:31:14.640 William Cheng: And then you know you have one third call right the other calls right they come into the kernel can they share the standard output in a nice way. 242 00:31:15.360 --> 00:31:20.970 William Cheng: Okay, so it turns out. Now, they might not be able to do that. So in this case, maybe some of the character over here is going to appear first 243 00:31:21.150 --> 00:31:28.410 William Cheng: And then you switch to another, you know. So again, we are running that Colonel when you make when you make a system called it is possible, the opportunities that will switch to the other thread. 244 00:31:29.280 --> 00:31:36.630 William Cheng: Okay, so, so it is possible. You will print the first four characters over here. And all of a sudden the athletes advocated the organism is 245 00:31:38.610 --> 00:31:42.900 William Cheng: This example over here. What it will do is that it will print the first five characters over here. 246 00:31:43.320 --> 00:31:55.980 William Cheng: On the first of that and then it will switch to the second you know the the second to read over here for the next few character and then go to the first way and go to the next. So you actually will in the print out over here, all the characters over here. We're interleaved 247 00:31:57.450 --> 00:32:00.750 William Cheng: Okay, if you don't do your coke or carefully your warm up to 248 00:32:01.530 --> 00:32:08.700 William Cheng: GET YOU WANT TO YO, you have multiple threads. They all tried to print a standard out at the same time. So in that case, you will see that all the characters actually gotta get jumbled together. 249 00:32:09.360 --> 00:32:12.990 William Cheng: Okay, so what do you have to do over here, but one thing you can do is that before you call printer. 250 00:32:13.170 --> 00:32:19.320 William Cheng: You need to understand that the standard out is also a shared resource right so they will you will do is that you will call piece of meat AX lot 251 00:32:19.470 --> 00:32:30.390 William Cheng: And then you call printer, but you copied me on law. So basically, every time when you call print. If you need to call piece of meat AX luck and then you call the PDF printer and you copy me that unlocked. So this way, the Abu Dhabi jumbled together. 252 00:32:31.470 --> 00:32:33.900 William Cheng: Okay, so you should do that one off to that. 253 00:32:34.830 --> 00:32:43.830 William Cheng: Unix tried to do this in a more generic way. So, what it will do is that for every file pointer over here. So again, the Fall point of data search. It's a rapper data structure around the file descriptor. 254 00:32:44.220 --> 00:32:55.800 William Cheng: Okay, so in that case for every file descriptor, we can actually use the new tax. So in this case, you can call the function f log file over here for file pointer. So this way, it will call the new tax law for the corresponding file descriptor. 255 00:32:56.490 --> 00:33:04.710 William Cheng: Okay. And then what you can do is you can actually call you know print a function over here. And when you're done, you can you can call me unlocked file. So this way you unlock the metrics. 256 00:33:05.250 --> 00:33:14.940 William Cheng: Okay, so this way for every, you know, again, for every file descriptor. There's going to be associated new tax so we call Africa. Africa, it will lock and unlock the techs right 257 00:33:16.440 --> 00:33:24.900 William Cheng: All right, one more example here. So this is called killing time over here. So let's say that you want to go to sleep. Will you have only one we 258 00:33:25.470 --> 00:33:31.650 William Cheng: Have only one threat and now we call this function called nano sleep right so now sleep over here, you're going to see for certain number nanosecond. 259 00:33:31.950 --> 00:33:37.290 William Cheng: You're going to use a data structure to say how much you want to sleep. Okay. So data structure is known as a time spec. 260 00:33:37.650 --> 00:33:48.570 William Cheng: Okay timestamps back has to feel one is a number of seconds and the other ones, the number of nanosecond. Okay. So, therefore, the second number of here is going to go from zero all the way to 1 billion minus one. 261 00:33:49.020 --> 00:33:57.390 William Cheng: Okay, because there's 1 billion, you know, nanosecond in a second. Okay, so in this example here for somebody that I want to sleep for three seconds, plus 262 00:33:57.840 --> 00:34:06.480 William Cheng: You know, one microsecond okay or 1000 nanosecond. So, therefore I'm going to set up my data start over here. I'm gonna call nano sleep over here. I'm going to see for almost three seconds. 263 00:34:07.500 --> 00:34:15.030 William Cheng: Okay. So in this case, when there's only one. Well, I don't have one thread. It's very clear when I want to wake up. Okay, but what if I have three threads. 264 00:34:15.240 --> 00:34:22.830 William Cheng: Okay, each one of them. So it's good to have multiple threads over here. How should I sleep so that the wake up you know in so they wake up in the 265 00:34:23.640 --> 00:34:24.630 William Cheng: In the way that I want it. 266 00:34:25.440 --> 00:34:34.740 William Cheng: Okay. So as it turns out, when you have multiple threads, it's not really clear when you have three starts at asleep. So should I, should I give every threat obviously for one second. So in the end, when all three thirds finish sleeping. 267 00:34:35.220 --> 00:34:42.180 William Cheng: They sleep a total, total three seconds. Okay. So, therefore, in that case it might be a little ambiguous. When you have multiple threats. 268 00:34:42.570 --> 00:34:47.070 William Cheng: OK, so the right way to actually to to to call a nano sleep is that instead of saying 269 00:34:47.340 --> 00:34:55.860 William Cheng: You know how much you want to sleep under multithreading maybe we should do that. You tell the piece or library or you tell the opportunities that tell tell me this is a this is a time you should wake me up. 270 00:34:57.030 --> 00:35:01.170 William Cheng: Okay, so if you can tell the audience is and this is the time to wake me up. Well, then there will be no ambiguity. 271 00:35:01.920 --> 00:35:07.380 William Cheng: Okay, so therefore, when you have multi threading and you want your code always work. You should avoid calling function like Dental Sleep. 272 00:35:07.620 --> 00:35:17.220 William Cheng: Or a warm up to you have to call the function call you sleep right so you sleep over here USL EP over here. So this one sleep or integer and the image over here is a number of microseconds. 273 00:35:18.360 --> 00:35:28.500 William Cheng: Okay, so, so since your, your programming assignments only has to run on a 32 bit Ubuntu 16.41. This is the sofa 30 to be 160 104 we can 274 00:35:28.860 --> 00:35:41.250 William Cheng: We understand that very well. So when you call you sleep, you know, three seconds over here, even when you have multiple threads everyone call you sleep through second simultaneously all three threads will wake up at about the same time three seconds later. 275 00:35:42.780 --> 00:35:51.210 William Cheng: Okay, so therefore, for Ubuntu 16.04, you don't have to worry about using you sleep. Okay. But in general, you still have to worry about it. So in that case, you need to find out what the current time is 276 00:35:51.420 --> 00:35:56.460 William Cheng: And then you add three seconds to it. And then you say, hey, operating system. Could you wake me up at that time. 277 00:35:57.510 --> 00:36:08.760 William Cheng: Then alright so so one of the function look like that is something called a piece or a condition time. Wait, okay, so this one is kind of like pizza condition way except that you're impatient. You said I want. If I don't get the, you know, 278 00:36:10.050 --> 00:36:17.370 William Cheng: If nobody wakes me up before the timeout. So in this case we return for this function. Anyway, and tell me that there's there's a timeout. 279 00:36:17.790 --> 00:36:28.920 William Cheng: Okay. So in this case, this piece of furniture way. It takes the regular to argument. The condition variable and the new tasks. There's also the third argument over here, which is the time spec. Right. And this one tell you when you should be woken up 280 00:36:29.790 --> 00:36:35.550 William Cheng: Okay, so therefore you can actually tell the piece that we just say, Hey, if nobody wakes me up wake me up at this absolute time 281 00:36:36.420 --> 00:36:44.130 William Cheng: Right. So in this case, how do you calculate the absolute time. Right. So again, you need to read the current time at the amount of time you need to sleep in order for you to figure out when you want to get woken up 282 00:36:44.940 --> 00:36:56.670 William Cheng: There so therefore you will look like this. Okay, so when you try to find out what the current time is you need to call get time with a good time of day that data structure that he uses called a time Val time Dell has to, you know, 283 00:36:57.210 --> 00:37:02.730 William Cheng: Actually, I should has two parts. One is the number of seconds. And the other one is the number of microseconds. 284 00:37:03.240 --> 00:37:08.490 William Cheng: OK. So again, it's different on high spec time. So I guess nanosecond and kinda has has this microsecond 285 00:37:08.850 --> 00:37:15.810 William Cheng: So for warm up to. You're supposed to use to get time with a function over here. So your clock resolution is going to be on the order of microsecond 286 00:37:16.290 --> 00:37:24.330 William Cheng: There. So in this case, what we need to do is that we need to set up a data structure to tell us when to wake up. So the time you know the time we need to wake up. It's going to be 287 00:37:24.600 --> 00:37:30.600 William Cheng: You know the current time which you get from the time of day over here and then you need to add the amount of time that you want to sleep. 288 00:37:31.110 --> 00:37:40.800 William Cheng: Okay, so what you need to do is that you need to edit the second field over here and also you need to add up the nanosecond field over here. Now, second view over here is going to be the the the microsecond multiply 1000 289 00:37:41.130 --> 00:37:52.890 William Cheng: Okay, so therefore the absolute time you want to wake up over here for a nanosecond feel you're going to take 1000 multiply by the microsecond feel of the get time of day return value and then you add it to the amount of time that you want to sleep. 290 00:37:53.580 --> 00:37:59.670 William Cheng: Okay, but in this case where you try to add a nanosecond a field over here, you might get numerical overflow, you might get bigger than 1 billion. 291 00:37:59.850 --> 00:38:07.350 William Cheng: So you to check whether it's bigger than 1 billion or not. If it's bigger than 1 billion. What do you have to do, right, you need to detriment. The number of nanosecond by 1 billion. 292 00:38:07.530 --> 00:38:15.420 William Cheng: And you need to also increase the amount of second by one second. Okay, so this is how we actually have, you have to perform the carry you know to to another data structure. 293 00:38:15.840 --> 00:38:23.220 William Cheng: Explicitly by doing co like this. Okay. If it turns out that the total number of nano. Nano second that you want to sleep over here. 294 00:38:23.820 --> 00:38:28.020 William Cheng: The total number of NASA novia is less than 1 billion, while in that case, you don't have to make any adjustment. 295 00:38:28.920 --> 00:38:36.780 William Cheng: Okay, so this way you can you can actually figure out what is going to be the time that you need to be woken up and now you can copy the condition way with this one as last argument. 296 00:38:38.070 --> 00:38:47.580 William Cheng: OK. So again, we saw a bunch of example to try to make sure that you throw your code is always work perfectly. No matter, no matter what kind of, you know, sort of a 297 00:38:48.330 --> 00:38:54.390 William Cheng: Kind of a piece or elaborate implementation that you have to deal with and whether we have multiple threads, they will always work correctly. Yeah. 298 00:38:56.280 --> 00:39:00.720 William Cheng: All right. Ah, so that's three safety. So the next thing we're going to talk about is deviation 299 00:39:01.020 --> 00:39:07.200 William Cheng: Okay, so what does deviation, right, the idea of deviation over here is that you are so, so let's say that you know we're going to sort of draw through like this. 300 00:39:07.440 --> 00:39:11.340 William Cheng: Okay, here is the beginning of your first procedure and then at the end over here. 301 00:39:11.700 --> 00:39:17.850 William Cheng: When your thread return on your first procedure. Your story is going to be the he's gonna be dead. Okay, so the normal execution or my third looks like this. 302 00:39:18.180 --> 00:39:25.920 William Cheng: Okay deviation, meaning that we want this thread would want to borrow distracted do something else. So what is this something else that we want to do, or maybe what to do a signal deliberate 303 00:39:26.550 --> 00:39:34.890 William Cheng: Okay. So, therefore, what we can do is I'll be here is that your threat is doing something over here. We're going to deviate your thread execution to do something else. We're going to suspend your thread right here. 304 00:39:35.160 --> 00:39:40.110 William Cheng: borrow your thread over here to do something else. And we finished our something else. We're gonna go back and return your 305 00:39:40.410 --> 00:39:49.380 William Cheng: Back to the previous day. So again, I'm going to say the contacts and never got to restore the contacts will restore the contacts over here. It's going to continue on the can continue the regular execution path. 306 00:39:51.060 --> 00:39:58.410 William Cheng: Okay, so this is no as borrowing your threat to deliver a signal right over here. Here's a signal handler. The secret handler, we saw before. It's called the f function over here. 307 00:39:58.620 --> 00:40:03.660 William Cheng: Right, so we're going to borrow this thread to execute signal delivery when it's done, we're going to return your threat back to what it was doing before. 308 00:40:04.110 --> 00:40:11.820 William Cheng: Okay, there's another kind of deviation that we're going to do is that it's called Bracewell termination guy what so I actually this kind of 309 00:40:12.930 --> 00:40:18.150 William Cheng: A termination over here. So your thread over here is doing something over here. And all of a sudden we decided to kill your threat. 310 00:40:18.780 --> 00:40:27.840 William Cheng: Okay. So in this case, what do we do that we need to deviate your threads, actually, you know, be here to have a do something else. When it finished doing something else over here. What it will do is that the watch yourself terminate. 311 00:40:29.370 --> 00:40:41.460 William Cheng: Okay, so you know what what we want to do is I'll be here is that your threaded executing as normal Co. We're going to deviate execution code over here to execute P3 exit. Okay, so this way, it will copy that and then eventually, that's where we're self terminate. 312 00:40:42.630 --> 00:40:49.470 William Cheng: Alright, so this is called deviation, right. So we're going to sort of talk about how to actually deal with, you know, deal with these two different kinds of deviation. Yeah. 313 00:40:50.190 --> 00:40:52.860 William Cheng: Alright, so the US okay though here. These are sort of 314 00:40:53.640 --> 00:40:56.730 William Cheng: Tells you what are the two different scenario or that you want deviation 315 00:40:56.910 --> 00:41:07.320 William Cheng: Number one is to implement the UNIX signal mechanism. So again, the signal mechanism is that something happened inside the Colonel. What it will do is that it will make an appt call into the user test program. 316 00:41:07.590 --> 00:41:13.830 William Cheng: borrow one of your threat to execute a signal handler when they finished doing that he will return your threat back to what it was doing before. 317 00:41:14.610 --> 00:41:22.380 William Cheng: Okay, so this is the first thing we're going to look at and the second, the second one is called cancellation. Well, one thing I was going to ask another thread to die. 318 00:41:23.190 --> 00:41:34.830 William Cheng: They were going to call it in peace, read is called school cancellation. Okay, so you have threat. A over here running code over here and then threat be over here is going to execute code so straight one one or the Astra be to die. 319 00:41:35.430 --> 00:41:40.710 William Cheng: Okay so therapy over here, what it would do is, I will get deviated to copy. So access and then in the end it was off terminate. 320 00:41:41.430 --> 00:41:47.340 William Cheng: Okay, so we're going to take, take a look at it through a cancellation. In the next lecture, okay. So right now we're going to sort of start the introduction. 321 00:41:47.790 --> 00:41:49.980 William Cheng: To the unit. The other to the unit signal mechanism. 322 00:41:50.940 --> 00:42:03.480 William Cheng: Okay, the original idea here for the signal mechanism is to have graceful termination of your program. Right. So, for example, your program over here is running for a very long time. And then for some reason you run into a bank and they're going to divide by zero. 323 00:42:04.200 --> 00:42:11.430 William Cheng: Okay, so we do divide by zero, we are not allowed to execute any more code over here. So in this case, what happens is that the system is going to kill your program. 324 00:42:11.910 --> 00:42:21.030 William Cheng: Okay, but what if you run this club this poem. Here is the largest simulation, you run it for seven days. And all of a sudden you have a divide by zero, and your program is going to crash, you're going to lose all your work. 325 00:42:22.080 --> 00:42:31.020 William Cheng: Okay, so, so in the good old days where units was embedded over here. People say, that's really terrible. We need a way to save our program. So we're gonna do is we're going to use this mechanism call signaling. 326 00:42:31.410 --> 00:42:34.020 William Cheng: Okay, we're going to create a signal Heather over here called f 327 00:42:34.440 --> 00:42:36.210 William Cheng: Okay, and what we're going to do is that when Kelly. I'll bring 328 00:42:36.390 --> 00:42:47.820 William Cheng: Them to say when we divide by zero. Don't kill our program. And then what happened is that you need to call this function on our behalf and then we're going to execute this code and an idiot over here. We're going to call exit ourselves. 329 00:42:48.720 --> 00:42:56.100 William Cheng: Okay, so in the interview, we're going to call exit over here, because in this function over here. We're going to save all the computation that we have done in the past seven days when I write it to a file. 330 00:42:56.340 --> 00:43:04.080 William Cheng: And then we're going to call exit over here. We're going to self terminate. Why do I have to call exit, right, because I'm not allowed to go back to what I was doing before, because I had a divide by zero. 331 00:43:05.340 --> 00:43:13.800 William Cheng: Okay, so there will get in this particular example. Right. I will deviate what I was doing over here, I will go do a graceful shutdown and then I will call exit at the end. 332 00:43:14.130 --> 00:43:16.440 William Cheng: Okay, for our warm up to, don't do that. 333 00:43:16.860 --> 00:43:25.200 William Cheng: Okay, I will not do has a different definition of grace. What termination. And I so again read the warm up to spec and make sure you understand how we're going to implement a grateful grateful termination. 334 00:43:25.530 --> 00:43:32.190 William Cheng: Okay. But again, this is the original units intent we do something like that, then, then we can actually, you know, a color have the organism. 335 00:43:32.520 --> 00:43:39.390 William Cheng: A call a function on your behalf. And that will be the signal handler and at the end of the signal handler, you will exit to terminate your program. Yeah. 336 00:43:40.050 --> 00:43:46.740 William Cheng: All right. Another thing is that, you know, if I have a program that most of the time when it was they were looking around perfectly, and I know that when my program run perfectly 337 00:43:47.190 --> 00:43:48.600 William Cheng: It will finish running in one second. 338 00:43:49.290 --> 00:44:00.390 William Cheng: Okay, but once in a while when it goes beyond one second. And in that case, I know my program is going to get into trouble, they might get into an infinite loop, may they may get into a Della. Well, in that case, I want my program to to to terminate gracefully. 339 00:44:00.930 --> 00:44:05.190 William Cheng: Okay. So, therefore, what I can do over here is I can actually wind up an alarm clock guide I can 340 00:44:05.490 --> 00:44:14.280 William Cheng: Open. This is the hey could you wind up alarm clock for me and said, For 10 seconds. Okay. It might program finished normally within one set one second, that everything is fine. I'm going 341 00:44:14.640 --> 00:44:21.510 William Cheng: To turn. My turn into my program. So, so therefore the alarm will never go off, but in case I went into a deadlock, or I went into some kind of infinite loop. 342 00:44:21.780 --> 00:44:30.840 William Cheng: At 10 seconds again call my function f over here. Don't kill my program. So typically what happens that we allow it. When we went up your love when the alarm goes off. 343 00:44:31.350 --> 00:44:41.580 William Cheng: You're going to generate interrupt inside the operating system kernel, right, because you know the alarm is actually computer hardware. So you're going to get a highway interrupt and inside the the the interrupt service routine. What it will do is it will kill your program. 344 00:44:42.570 --> 00:44:50.460 William Cheng: Okay, so if I provide a signal handler. In this case what the audience's that one now carrier program. It will make an appt call call this function. So in this case, again, 345 00:44:51.030 --> 00:45:03.390 William Cheng: In this case, this will be a signal handler for the for the alarm signal. So you know that your program has been running for more than a 1010 seconds, so therefore it's bad news. You should save all your computation and India, you cannot call exit. 346 00:45:04.200 --> 00:45:15.150 William Cheng: OK. So, again, as far as our warm up to is concerned, we should not call x that we should handle our graceful shutdown in a different way. Okay. Alright. So again, these are the original intent of a unit signals. Yeah. 347 00:45:16.650 --> 00:45:23.580 William Cheng: Alright, so again the signals over here. Somebody who some people call signal a software interrupt, but they are not. They are an upcoming mechanism. Right. So, so 348 00:45:23.880 --> 00:45:31.800 William Cheng: So in a way, in the general sense, it's called a callback mechanism you specify a callback function just say when something happened over here called this function. 349 00:45:32.130 --> 00:45:36.210 William Cheng: Right, so people have implemented, you know Java code or something like that you're familiar with the callback function. 350 00:45:36.720 --> 00:45:39.480 William Cheng: So again, the signal is actually a callback mechanism. 351 00:45:39.990 --> 00:45:52.980 William Cheng: For units. The signal is, you know, it's a special. It's a special kind of call that mechanism when something happens inside the Colonel, they're going to come, they're going to make an app called into the user's Facebook when borrow one of your threat to execute a signal delivery. 352 00:45:54.420 --> 00:46:04.350 William Cheng: So so so again this is done using up call. Okay. They are generated by the ordinances and, you know, in response to the following event inside the operating system. For example, 353 00:46:05.460 --> 00:46:14.580 William Cheng: Exception when you divide by zero, we take the square root of a negative number. When you take the logarithm or negative number. So these are exceptions. Right. So in this case, a signal will get generated 354 00:46:14.970 --> 00:46:21.900 William Cheng: That also that can be external event for example at the timer expiration. Right. That's the external interrupt over here, a time expression. 355 00:46:22.110 --> 00:46:32.760 William Cheng: Certain keystroke when you press Control. See, you're going to kill your program. Right. So again, you will generate a signal will you press Control Z. You will suspend your program. So in that case, again, certain keystroke what generally signal. 356 00:46:33.300 --> 00:46:36.870 William Cheng: Action of other processes such as to terminate or pause the process. 357 00:46:37.500 --> 00:46:43.950 William Cheng: So I don't know if you actually try that you want to kill the program can say kill minus, you know, something like that to try to kill the program. Okay, so in that case. 358 00:46:44.190 --> 00:46:48.660 William Cheng: Another program will send you a signal. So in this case, you have to, you have to respond to the signal. 359 00:46:49.080 --> 00:46:55.710 William Cheng: That they can also be user defined events. So, typically in this class. We don't care about that. Right. But you need to know that they're actually use to define the bets. 360 00:46:56.400 --> 00:47:02.760 William Cheng: That. So, these, these, you know, you know, so these events are generated by the operating system so signals are generated by the opportunities and 361 00:47:03.300 --> 00:47:12.060 William Cheng: The effect on the process over here when the signal is get deliver these are the five possible for responses. Okay, number one is termination. 362 00:47:13.050 --> 00:47:17.610 William Cheng: Okay, so when the signal get generated. If you don't specify a signal handler, what happened is that is 363 00:47:18.540 --> 00:47:24.630 William Cheng: That the reaction of the operating system is that it's going to go to the default action. The default action typically is to kill your program. 364 00:47:25.500 --> 00:47:31.080 William Cheng: Okay. It will also said that you know what let's try to cure program. It will possibly also produce something called a core dump. 365 00:47:31.650 --> 00:47:42.450 William Cheng: Okay, I don't know if you want one or two, you see a file called core is the file. You're working directory called core, guys. In that case, your program diet. A will generate an image of your address space. 366 00:47:42.990 --> 00:47:54.000 William Cheng: Okay, so the quarter via the I mean in the good old days CORE MEANS memory. So this has the core DOM is a memory dump. So basically what you will do is that will your program does. It will take a snapshot of your address space and a story into a file. 367 00:47:54.390 --> 00:48:05.400 William Cheng: Okay, so this way after your program is dead. We can actually run GDP with your core DOM. And this way we can actually find out exactly what your program died. Okay, so this is actually very useful if you can create the core file. Yeah. 368 00:48:06.570 --> 00:48:12.630 William Cheng: The second possibility over here is that if you have specify a signal handler. In this case what he would do is that it will make an appt call 369 00:48:12.840 --> 00:48:15.810 William Cheng: Call your function on your behalf I born in one of your threat. 370 00:48:16.050 --> 00:48:27.090 William Cheng: Is it's going to be as if one of your user threat was borrowed to execute that particular function. So the function f that I mentioned before, it will be executed in you know you'll be executed using one of your threats. 371 00:48:27.660 --> 00:48:30.780 William Cheng: Okay. So in this case, he will call this function A will not kill your program. 372 00:48:31.230 --> 00:48:36.720 William Cheng: Okay. And when this function is done, it will return back to the offices that and the ordinances that will continue as it nothing has happened. 373 00:48:37.230 --> 00:48:46.890 William Cheng: Okay, so you will not will not create a program. Okay. The other thing that can happen over here is going to is going to be suspend your program. It's going to put your program to sleep. And later on, you need to send in another signal. 374 00:48:47.280 --> 00:48:53.580 William Cheng: To to to resume the execution of your program. So in this case, the orem involved saving contacts and restoring contacts. 375 00:48:53.940 --> 00:49:01.710 William Cheng: OK. So again, when you get one of these albinism events. I'll be here one of these four things can happen. Okay, so how do you know which one is which. Yeah. 376 00:49:02.220 --> 00:49:13.080 William Cheng: We're going to show you a table that will actually show you where all the possibilities there. But before we see that table we're going to introduce the important terminology. So this terminology. Why should we see them before when we talk about how interrupt. 377 00:49:13.590 --> 00:49:19.680 William Cheng: Okay, so, so this is why people confuse signal with interrupt because the terminology is very, very, very, very similar. 378 00:49:20.460 --> 00:49:29.190 William Cheng: That the horizontal axis over here is time. Right. So at some point going to generate a signal. So again, what a signal generation right signal generation is the stuff that we just mentioned before, over here. 379 00:49:29.370 --> 00:49:40.500 William Cheng: Something happening. So the opportunity is me going to Jeremy the signal at some point the signal is going to get delivered. So again, what a signal delivery signal delivery over here is one of these for action will be taken, that's when the signal get deliver 380 00:49:40.920 --> 00:49:48.330 William Cheng: That. So, therefore, you know, as it turns out that the signal, you can actually block them. You can also unblock them right so the terminology over here is a little different. 381 00:49:48.780 --> 00:49:55.410 William Cheng: From interrupt which we talked about interrupting enable and disable when you talk a lot of signals over here. You're going to change the wording over here. 382 00:49:55.740 --> 00:50:03.930 William Cheng: To talk about with the signal is blocked around blah, okay, if the signal is not blocked. So in this case, when you generate a signal, pretty soon the senior will get deliver 383 00:50:04.290 --> 00:50:14.130 William Cheng: Okay, why is it pretty soon, why can I get delivered right away. Well, because when you're operating system execute. I'll be here, the obvious isn't going to detect that the condition exists over here to generate signal and then 384 00:50:14.490 --> 00:50:18.570 William Cheng: The opposite. I'm going to ask you some more code in order for it to eventually deliver the signal. 385 00:50:19.080 --> 00:50:23.880 William Cheng: Guys over here it's going to show you that it's going to take a little bit of time before the signal. It's actually delivered 386 00:50:24.390 --> 00:50:32.670 William Cheng: Okay, so this will be the case if the signal is not bought, then what did the signal is blocked. OK. So again, the terminology over here will be very, very similar. 387 00:50:33.060 --> 00:50:39.480 William Cheng: To a higher Rainer up when the signal is blocked over here and then you generally the signal. Now the signal is going to become pending. 388 00:50:40.530 --> 00:50:47.730 William Cheng: OK, so the signals going to be compounding until, at some point you onboard the signal. So over here, you're going to write some code over here to unblock the signal. 389 00:50:47.910 --> 00:51:01.020 William Cheng: When the signal is unblock then your signal will get deliver again when you deliver one of those for action is going to happen. Okay. So if you look at this picture if you replace the word signal Buhari interrupt and you replace the word block. 390 00:51:02.040 --> 00:51:10.470 William Cheng: Okay, you were blocked by disable and then you do on blog with enable this picture will look exactly the same as a highway interrupt. 391 00:51:10.980 --> 00:51:18.540 William Cheng: OK. So again, when the hallway interrupt get generated if the if the inner Rob is disable then the hallway interrupt become pending. 392 00:51:18.780 --> 00:51:29.790 William Cheng: Okay, so later on when they interrupt is is enabled, then the hallway normal get deliver when the hallway and get interrupted deliver weapon. Well, when they get delivered you will execute interrupt service routine. 393 00:51:31.110 --> 00:51:35.400 William Cheng: Okay, so, you know, a Unix, you know, so I was trying to sort of use the same terminology over here. 394 00:51:35.730 --> 00:51:43.650 William Cheng: But again, one is used on the signal and the other one is used on her interrupt, even though they are not related but people will confuse them and think that signal is the same thing as a software interrupt. 395 00:51:44.250 --> 00:51:50.790 William Cheng: OK. So again, you're in autism class, you should understand that they are completely different animals right even though we use the same terminology. Yeah. 396 00:51:53.070 --> 00:51:58.980 William Cheng: Alright so here I'm going to show you all the signals. Well, I think this is a subset of all the signal that we need to deal with. 397 00:51:59.370 --> 00:52:06.450 William Cheng: So in the good old Unix over here. There are 32 different signals. So the signal over here at number 0123 all the way to 31 398 00:52:06.930 --> 00:52:18.090 William Cheng: Okay, so here is a subset of the signals. Okay. So, for example, one of them is sick alarm over here. I don't. So over here on the left hand side over here says these are the name of the signal. They all correspond to a single integer between 399 00:52:18.450 --> 00:52:23.880 William Cheng: Having a number between zero and 31 okay so different companies and they will actually use a different signal. 400 00:52:24.300 --> 00:52:30.660 William Cheng: But the symbol name that you use, they will remain the same. Okay, so this way you know different operating system could actually have more than 32 signals. 401 00:52:30.990 --> 00:52:35.460 William Cheng: They will have 64 or they'll have even 128 whatever you know the operating system. So, yeah. 402 00:52:35.820 --> 00:52:44.910 William Cheng: So what are they missing a lot. So in the middle column over here. I'm going to sort of describe what it does, right. So this one is the the alarm clock when the alarm clock clock go off, he will deliver the signal on 403 00:52:45.330 --> 00:52:51.600 William Cheng: Okay. On the right hand side, he would tell you the default action. So, therefore, if you don't provide a signal handler. This is what's going to happen. 404 00:52:52.530 --> 00:52:59.160 William Cheng: Okay, so when the signal alarm getting delivered over here. What it will do is that it will kill your programs or term over here is going to be terminate your program. Okay. 405 00:52:59.670 --> 00:53:08.310 William Cheng: What about sick child over here, when a child process is dead, it will actually deliver a signal over here. So the death of a child process over here, but by default, it will get ignored. 406 00:53:09.150 --> 00:53:21.240 William Cheng: Okay. So, therefore, if you're interested in knowing that your child processes there, you can actually provide a signal handler for the sick child signal. So this way when your child processes dead your function will get caught. You can actually know that a child processes that 407 00:53:22.530 --> 00:53:37.560 William Cheng: Yeah. All right. Then there are six epi over here. So this is floating point exception epi so divide by zero. Take the square square with a negative number over here. So by default, what it will do is that it will terminate your program. It will also generate a core dump. 408 00:53:38.280 --> 00:53:43.590 William Cheng: Okay, so this way when you die you can actually debug your code or w or your. So this is a 409 00:53:43.860 --> 00:53:50.910 William Cheng: This is called course post mortem analysis and after your program is dead. You can actually look at the core file and try to figure out exactly what your program is dead. 410 00:53:51.480 --> 00:53:56.190 William Cheng: Okay, so you can read GDP on it and you do where it will tell you exactly what your program diagram. So this is really neat. 411 00:53:56.910 --> 00:54:01.230 William Cheng: There's also sick illegal instruction, the one of the instruction, it's actually illegal. 412 00:54:01.860 --> 00:54:06.210 William Cheng: Okay, so for example, if you're executing code in the wrong place. You're going to actually end up with the legal instruction. 413 00:54:06.450 --> 00:54:12.840 William Cheng: So in this case, by default, it will terminate your program and they will generate a core DOM. So this way again can find out where your program die. 414 00:54:13.440 --> 00:54:24.210 William Cheng: sich in over here is enough on the keyboard. So this is where Control C is getting press ok for all the Unix, Linux operating system that I know of sick in IS ALWAYS SIGNAL number two. 415 00:54:25.890 --> 00:54:36.570 William Cheng: Okay. The other thing you know i don't know that for sure but Seguin is always number to over here. So, so again, when you see again, or the serial number to over here, they mean exactly the same thing, right. So by default, over here, it will terminate your program. 416 00:54:37.110 --> 00:54:45.750 William Cheng: Why it doesn't generate core down well because if the user tried to kill your program. Well, that means that use it didn't really care about. We pulled on dice. Okay, so therefore there's no need generate corner that 417 00:54:46.440 --> 00:54:52.470 William Cheng: There's another signal vehicle seeks to kill we here. So this one. I also know the serial number. This one is always signal. Number nine. 418 00:54:53.430 --> 00:55:00.480 William Cheng: That. So, this one will force determination or your program. Well, guess what it means is that even if you set up a signal handler, it will get ignored. 419 00:55:01.320 --> 00:55:08.070 William Cheng: Okay so UNIX system. There's always a sure way to kill your program. If you send the signal number not over here. The program will always die. 420 00:55:09.720 --> 00:55:19.560 William Cheng: Okay, so I don't know if you if you are a user a window something windows refused to die. And it's really annoying so UNIX system. There's a guarantee way to kill a process by sending a signal. Number nine. Yeah. 421 00:55:20.400 --> 00:55:32.220 William Cheng: All right, what else is there a six, like the right that's our favorite, you know, a signal here segmentation fought over here, again, in this case it will terminate your program with generally a core DOM six stop over here is stopping the process. 422 00:55:33.780 --> 00:55:42.360 William Cheng: So, so I guess the against one of the signal over here, sick term, I think. Typically, this one is signal number 15 over here for all the Unix and Linux system that I know over here. 423 00:55:42.780 --> 00:55:51.120 William Cheng: So this way. It's a soft software termination signal. So you can actually have your program deliver a signal to another program using this particular serial number. 424 00:55:51.450 --> 00:55:55.440 William Cheng: Okay, by default, it will just kill the program you will not generate it will not generate code up 425 00:55:56.280 --> 00:56:03.810 William Cheng: There. And then there's a signal, this one call, you know, a signal stuff from the keyboard. So when you press Control Z. Over here we generate the signal. 426 00:56:04.020 --> 00:56:09.210 William Cheng: Okay, I don't really know what the serial number is. So by default, it will suspend your program you will save the contacts. 427 00:56:09.420 --> 00:56:18.960 William Cheng: And later on when you send your program. The sea continue signal over here that escaped before we here. So in this case, your program will continue will resume the execution by restoring the contacts. 428 00:56:20.130 --> 00:56:27.750 William Cheng: Name. Alright. The last two over here are the application device signal over here. So we don't know what it does. Over here, we don't know what their meaning the application need to define them. 429 00:56:28.140 --> 00:56:34.950 William Cheng: So in this case, the default action over here will suspend your program. So if you don't provide a signal harder for it. What it will do is that it will put your program to sleep. 430 00:56:35.460 --> 00:56:43.620 William Cheng: Okay, so therefore, in this case, you might have another program sending a sick continue signal over here. So this will, it will get woken up it will continue to do what I was doing before. 431 00:56:44.700 --> 00:56:52.080 William Cheng: Okay. All right. So this is all I want to talk about in lecture five over here. So I will see you in lecture six