WEBVTT 1 00:00:01.800 --> 00:00:12.389 William Cheng: This is the third part of lecture 21. So that's the end of chapter five. Guess we're going to briefly take a look at case studies to look at Linux and Windows 2 00:00:13.110 --> 00:00:19.710 William Cheng: The Linux operating system is positive compliant operating system so positive is Unix standard 3 00:00:20.340 --> 00:00:29.010 William Cheng: So Linux is also part of it. And so the way that the pacific standard specify about a scheduler is that there has to be three different priority level. 4 00:00:29.460 --> 00:00:45.960 William Cheng: One is the highest party level. So that's the one that's used for scheduling real real time job. And this one is using the fivefold the procedure for so scheduling discipline and also in this case, you know what they're doing strict first in first out. So, therefore, if the 5 00:00:47.100 --> 00:01:02.550 William Cheng: If the job or refuse to give up the CPU the job has been run inside of CPU forever. So over here, it says, you know, basically it's the same, same thing as having an infinite time quantum the middle one, the middle priority, you know, so, so sort of a 6 00:01:03.690 --> 00:01:10.200 William Cheng: priority queue is using the round Robin scheduling policy. And again, this is also for, you know, for real time job. 7 00:01:10.710 --> 00:01:21.210 William Cheng: So typically will happen is that the top one typically they use for things like audio kind of kind of got kind of a real time workload. And then for something that has a little bit higher. 8 00:01:21.990 --> 00:01:31.410 William Cheng: Your sort of a higher bandwidth, they will use the medium priority over here. So, again, both of them for real time. But again, since since this is a, you know, soft real time systems. 9 00:01:32.250 --> 00:01:37.320 William Cheng: So typically we can use a general purpose operating system to to to implement all these 10 00:01:38.010 --> 00:01:44.520 William Cheng: Yeah, and you know, since. And also, since the first one over here, you know, has infinite time content. 11 00:01:44.940 --> 00:01:51.240 William Cheng: If you want to use this one to implement a real time scheduler in that case also possible. But in that case you have to introduce more 12 00:01:51.690 --> 00:01:55.860 William Cheng: Priority level or if you want to use a monotonic scheduling. Yeah. 13 00:01:56.430 --> 00:02:09.480 William Cheng: And the you know the middle one over here as the positive standard, it doesn't really tell you how to set the time slice. And as I mentioned before, the time size is very difficult as a very tricky to to to say you don't want it to be too sure. 14 00:02:10.050 --> 00:02:16.740 William Cheng: If you too short. Then it transitions, because I bought it doesn't work very well. It was too long, that it basically is the same thing as first come first serve. 15 00:02:17.160 --> 00:02:23.040 William Cheng: So the way that this is specify in positive is is that you can do it any way you want. They, you know, so the event. 16 00:02:23.550 --> 00:02:34.050 William Cheng: You're allowed to use adjustable time content basically as your service scheduler start running or you can actually change the change the time slice depends on you know whatever heuristic than the US. 17 00:02:34.740 --> 00:02:37.530 William Cheng: Yeah. So the first one here is basically is to 18 00:02:38.040 --> 00:02:50.280 William Cheng: Is to run your real time jobs or they can use to run audio and video job. Everything else go to the third scheduler. So third schedulers sort of the general purpose scheduler. They call it the normal scheduler. 19 00:02:50.880 --> 00:02:57.420 William Cheng: So we mentioned before in Linux. They use dry scheduling, you know, but it can also use other other kind of schedulers 20 00:02:58.260 --> 00:03:07.500 William Cheng: Yeah, so the textbook. I just spent quite a bit of time talking about the different kinds of scheduler for the normal scheduler. The went back to some of the, sort of, you know, 21 00:03:08.070 --> 00:03:16.020 William Cheng: Sort of historical background of what kind of a Linux scheduler we're using a one on one of them to call the old scheduler. That was the original scheduler. 22 00:03:16.500 --> 00:03:24.630 William Cheng: So that was clearly not very, very inefficient later on a Linux went to schedule a call to order one scheduler. So I'll go, or do I sounds really really good. 23 00:03:25.080 --> 00:03:35.310 William Cheng: Bevel, we know that later on. They were replaced by the scheduler. And as I mentioned before, you know, if you're using a heat data structure to sort the the past value. 24 00:03:35.850 --> 00:03:42.000 William Cheng: Then in that case the performance going to be order login. Okay, so why would they actually switch from, you know, order one to order login 25 00:03:42.540 --> 00:03:48.750 William Cheng: So, so you need to remember right order. So what does that mean, that's the order one scheduler is when you think about order one typically using the hash table. 26 00:03:49.140 --> 00:03:57.180 William Cheng: We use the highest table. And then, you know, the problem with how stable is that once your hash into a bucket inside the bucket, there is a 27 00:03:58.500 --> 00:04:02.430 William Cheng: There's a collision resolution Chang. What is the collision resolution Chen is really law. 28 00:04:03.420 --> 00:04:07.650 William Cheng: Okay, if the collision resolutions is really long. One thing that case or even though it's order one. 29 00:04:08.160 --> 00:04:19.530 William Cheng: But in the end it's proportional to the collision resolution chair. So if the collusion resin resolution chain is, you know, longer than the height of a binary tree. One is, you know, order login is actually better than one 30 00:04:20.430 --> 00:04:27.240 William Cheng: Now, so, so, so the bottom line over here is that, you know, Linux did a lot of experiment. Ever since the strike schedule come out to them 12 years 31 00:04:27.630 --> 00:04:40.770 William Cheng: Eventually, they're convinced that, you know, the otter login schedule is actually order has been an older one. So, eventually they switch right so I guess we mentioned that in 2007 they switch to the, you know, completely fair scheduling based on the strict schedule. 32 00:04:41.790 --> 00:04:48.480 William Cheng: So I'm not going to sort of spend too much time talking about the old scheduler, and also the order one scheduler. So if you're interested, you should read the 33 00:04:49.590 --> 00:04:58.890 William Cheng: Textbook now windows. Again, we don't have that much information on Windows, other than the papers they publish. So I guess it was sort of briefly going to talk about kind of stuff. They use 34 00:04:59.730 --> 00:05:07.650 William Cheng: So so window again windows very general purpose they can actually be used to run all kinds of stuff you can use them to to to to to to 35 00:05:08.280 --> 00:05:17.010 William Cheng: Tu, Tu do real time to implement a lot of real time system or you can use that as a general purpose system. So the windows, you know, as usual, is very complicated. 36 00:05:17.760 --> 00:05:21.840 William Cheng: So this picture sort of show you the window party classes and levels over here. 37 00:05:22.230 --> 00:05:29.340 William Cheng: So some people, you know, look at this. Was it oh they're 32 level. These are the IP obviously the interrupt previous level. This has nothing to do with interrupt service level. 38 00:05:29.670 --> 00:05:38.880 William Cheng: Okay, so these are the you know the the windows job. Our priority level of the third party level. Yeah. So they go from zero to 31 so you can divide them into two parts. 39 00:05:39.330 --> 00:05:53.310 William Cheng: The one from 16 to 31, those are, you know, the, the, you know, those are the party level, they are reserved for real time jobs. Okay, so again if you have 16 level. And then if you are, if you're implementing a 40 00:05:55.410 --> 00:06:02.280 William Cheng: Raid monotonic scheduler, you can actually runs on you know 16 different your Periodic task. So in that case, you know, you can actually assigned to 41 00:06:02.790 --> 00:06:10.380 William Cheng: The party level over here simply based on the period right we thought we just saw that, so they can actually work in the windows. So in that case, 42 00:06:10.680 --> 00:06:16.230 William Cheng: What would the window and they try to schedule these job is going to be presented and then they can schedule them based on the priority. 43 00:06:16.710 --> 00:06:21.630 William Cheng: Yeah, the other ones over here for non real time job over here and this is what I mentioned that, you know, 44 00:06:21.870 --> 00:06:28.500 William Cheng: The Microsoft programmers menu this he said that you know what you should write a program, you should pick your own, you know, the poverty level. 45 00:06:28.890 --> 00:06:37.140 William Cheng: So what they do is that they divide this one into five different category, the one with the highest poverty level, they call it the high priority category and then followed by the 46 00:06:37.740 --> 00:06:42.660 William Cheng: above normal the one in the middle. It's called the normal category and then they're below normal and there's a low priority. 47 00:06:43.170 --> 00:06:51.090 William Cheng: Okay, it's the windows programmers menu as you to actually select, you know. So what we try to program, you always choose, you choose to select as low as possible. 48 00:06:51.510 --> 00:06:56.310 William Cheng: But in the end, everybody wants to be on to high group. So this way, they will run as early as possible. 49 00:06:56.850 --> 00:07:04.410 William Cheng: Okay, so what window does is over here is that, you know, they actually took all the jobs together. Look at all the party, the party. And now, will they will try to do 50 00:07:05.400 --> 00:07:07.320 William Cheng: Is that they'll try to serve their job. 51 00:07:07.680 --> 00:07:17.730 William Cheng: You know, based on strict party starting from, you know, the one that has highest poverty level and the only one a higher price level accuser completely empty, then he will actually serve the coupon. The next party level. 52 00:07:18.060 --> 00:07:27.510 William Cheng: Now, and also, you know, for the non real time job over here, you know, for every category, they are doing the you know the, I guess they have their own version of the 53 00:07:28.020 --> 00:07:42.390 William Cheng: The, the, the, the feedback you right. The one we sort of talked about before. I was a Windows. Windows, you know, they, they basically use the same idea. They use the multi level P back Pew queue, but they continue to optimize this you know as 54 00:07:43.710 --> 00:07:55.170 William Cheng: You know, sort of all over different generations of with the Windows operating system now alright so again you know typically this is very complicated but but I shouldn't be surprised. Surprised windows. 55 00:07:56.550 --> 00:08:01.110 William Cheng: Alright, so we are done with chapter five and now we're going to go all the way back. 56 00:08:01.650 --> 00:08:12.990 William Cheng: To chapter three. There's one papi that we skip. So that's the one that talked about dynamic Lincoln and loading why we mentioned before that we have a static Lincoln Elodie so now we're going to sort of talk about dynamic mentioned 57 00:08:14.100 --> 00:08:22.410 William Cheng: That so so before we finished adding Lincoln loading, other than another term for that. I mean, we can I know these know assure libraries are going to see what they are. 58 00:08:22.740 --> 00:08:28.800 William Cheng: But before we get there, we're going to sort of finish up the stuff that we didn't really finish our static thinking and loading now. 59 00:08:29.100 --> 00:08:34.860 William Cheng: I guess some of the stuff that we really haven't finished. How do you actually build a static library. What does it look like, you know, all that kind of stuff. 60 00:08:35.730 --> 00:08:42.360 William Cheng: OK. So the little background over here, again, a library is simply a collection of data file. So you can think about a bunch of 61 00:08:42.660 --> 00:08:51.480 William Cheng: Files. If you can cover them all together, it becomes a library. Okay, you've seen a library already right in the Phoenix programming assignments. There's Lyft drivers dot a 62 00:08:52.140 --> 00:09:03.630 William Cheng: There's live si, si de de de files so that if I mean that they are studied libraries okay for the, you know, for, for the dynamic library. They actually different than an extension which we're going to see pretty soon. Yeah. 63 00:09:04.200 --> 00:09:15.030 William Cheng: Alright, so there are two types of library ones, the one that we saw is a static Library and the other one is that the one we're going to see pretty soon. It's a dynamic library also done in libraries also noise to share the same thing. Okay. 64 00:09:16.770 --> 00:09:23.490 William Cheng: So here's an example how to create a static library. So we're going to sort of take a bunch of files concatenate them together color library. 65 00:09:24.000 --> 00:09:33.540 William Cheng: So here is a simple example we're going to have three different files. So one Darcy Darcy Darcy. They contain a bunch of sub routines. Right. So in. So when I see it contains the function for sub why 66 00:09:34.050 --> 00:09:46.170 William Cheng: So this function. All it does is to a print something to stand out so they call this C library function called pushing, pushing, will take the argument over here is going to be a C Street not terminated and it will print it to standard out 67 00:09:46.770 --> 00:09:55.980 William Cheng: OK. So, again, very simple function is up to see there's a bunch of course up to all it does is we call push drinks up to and then sub three dot c has a function to sell because of three. 68 00:09:56.190 --> 00:10:04.560 William Cheng: And then what do we do that, it will, you know, print sub three to standard out there. So, so what we try to compile them or you can actually do separate compilation 69 00:10:04.890 --> 00:10:13.380 William Cheng: By using this command GCC minus see you are very familiar with my to see because, you know, one on one and two. You know what you said to compile a sci fi data file, you always just trying to see 70 00:10:13.560 --> 00:10:20.700 William Cheng: So now we can say Jesus t minus c and all of our CFO, what it will do that for every SEPA it will compile the data into the corresponding 71 00:10:21.540 --> 00:10:26.730 William Cheng: That's what we're done, we would do an ls. We're going to see that we have all of our CFO. We all the files over here. 72 00:10:27.060 --> 00:10:32.970 William Cheng: And then we can we can take all of us together we can you know can kind of them together at this way to create library. 73 00:10:33.480 --> 00:10:40.200 William Cheng: Now, so the command that we use is known as the archive command. Right. So this AR over here. I mean you actually you have seen a 74 00:10:40.500 --> 00:10:47.370 William Cheng: Sort of a different version of the archive command and that's called the car command a TA RT right the target minutes. That's what tape archive. 75 00:10:48.120 --> 00:10:57.300 William Cheng: Archive a bunch of, you know, file into a tape drive so again paper is a very, very old storage device. We don't use that anymore. But again, to come in space. 76 00:10:58.050 --> 00:11:04.440 William Cheng: Well, so I are over here, the argument over his car. So we need to create a file. The next argument specify the output file. 77 00:11:04.800 --> 00:11:14.430 William Cheng: So in this case, the output file. So for Unix and Linux system, the naming convention, you sort of have to stick to it for a library of the first three character has to be led 78 00:11:14.850 --> 00:11:22.110 William Cheng: The last two character has to be a for a static library. Okay. And the middle part over here is going to be the name of the library. 79 00:11:22.440 --> 00:11:30.420 William Cheng: Okay, so in this case we'll have live prayer, one day, so one is the name of the library. So in this case, we're creating private library number one. 80 00:11:31.170 --> 00:11:36.690 William Cheng: There and this guy is what we do that we're going to take all the thought of us together combine them together and this will be 81 00:11:37.080 --> 00:11:49.110 William Cheng: Provided a there. So if this is live prayer one a is a fall over here. It has, you know, sub one dot o sub 203 dot. Oh, okay. Just simply just can kind of come together. 82 00:11:49.650 --> 00:11:56.850 William Cheng: Now, so that's what a library. If I look like. So later on I can actually use it to create my executable files will remember where you try to create your executable file. 83 00:11:57.060 --> 00:12:04.200 William Cheng: You're going to import the linker. The linger perform we relocation. He also performed symbol resolutions promotions symbol resolution. 84 00:12:04.440 --> 00:12:09.450 William Cheng: What he will do is it will go through a library fought over here, find a function and that is looking for 85 00:12:09.630 --> 00:12:18.000 William Cheng: And then what we'll do is I insert tab over here, right. So again, what's inside of that over right we mentioned before in chapter three data file contains tech segment data set up as a segment. 86 00:12:18.240 --> 00:12:25.920 William Cheng: Of these data structure that that describes the segment and also there's a bunch of information that goes along with it. You know, like, you know, 87 00:12:26.430 --> 00:12:36.660 William Cheng: The relocation instruction and all that kind of stuff. Guys, what do you do that it will search through this that a file over here, try to look for the right punchy and then copy you know all that information from the 88 00:12:37.740 --> 00:12:39.150 William Cheng: The copy that into the executable file. 89 00:12:40.050 --> 00:12:51.960 William Cheng: Okay, so you can imagine that if this file is a really, really big file, like your C library. You'll see largely is a concatenation maybe 100 maybe $1,000 offer. So in that case, when he started performing linear search over here is going to be really, really slow. 90 00:12:52.620 --> 00:13:03.630 William Cheng: Okay, so one more thing that you should do for life before is that you need to create a table. Table contents for this particular file. So, so at the head of this library about over here, you need to create a table contents and then 91 00:13:04.230 --> 00:13:05.640 William Cheng: So what it will do is it'll actually, you know, 92 00:13:06.300 --> 00:13:16.020 William Cheng: For every one of the functions over here that there is that is that ova it will say where those functions are so this way you can use an LLC function and he can go to the File quickly and then you'll be able to 93 00:13:16.740 --> 00:13:17.970 William Cheng: Quickly copy the data out. 94 00:13:18.510 --> 00:13:29.880 William Cheng: There. So yeah, to increase the performance of surgeons to a library file, you need to create a table contents. Yeah. So the way we do this, I'm going to run ASR again at this one we're going to use the other T command to create a table contest. 95 00:13:30.150 --> 00:13:35.940 William Cheng: For this library over here. So, what it will do is it will show you that it actually goes through all these down. Oh wow, you know, parse every one of these 96 00:13:36.210 --> 00:13:44.820 William Cheng: Power, find the function name find information about her and then create a table content at the beginning over here. So from this point now where you try to use this library, it will be reasonably efficient. 97 00:13:45.570 --> 00:13:52.530 William Cheng: Right. Alright, so this guy's will create the lip proven a over here. So since this is the library file, you can run the 98 00:13:53.250 --> 00:14:03.510 William Cheng: Command right we saw and and command is the symbol table of the file you can use and and command on a data file, you can use the LM command on the executable bar. You can also use the LM command on the library file. 99 00:14:03.780 --> 00:14:10.350 William Cheng: And you will see that if you are an emo look provide a, you will see that pushing over here is unresolved inside of the provider a 100 00:14:11.460 --> 00:14:21.450 William Cheng: guy or gal who is going to perform the, the, the, the symbol resolution or the link or will have to do it right. Because right now, you're not generating the executable file, so therefore it's okay to eat things are resolved. 101 00:14:21.720 --> 00:14:27.060 William Cheng: Then later on when you sort of create an executable file that's when you have to resolve all the undefined symbols. 102 00:14:27.390 --> 00:14:36.540 William Cheng: Okay, so at that time, pushing eventually can be found inside the C library. So therefore, the link will go to see library and copy the code. And now you know this kind of code will run. Yeah. 103 00:14:38.520 --> 00:14:47.460 William Cheng: Alright, so let's create a program that will use it right so let's have a program called of proxies are inside the main function over here pretty boring. I caught a sub one. Step two. And step three. 104 00:14:48.180 --> 00:14:54.960 William Cheng: So then I can compile my code GCC minus oh just said I here's the output executable file name and then the source code over here. 105 00:14:55.290 --> 00:15:03.030 William Cheng: Is proud that sees that will compile this one. And also, in order for it to create executable. It needs to perform you know relocation also symbol resolution. 106 00:15:03.360 --> 00:15:09.270 William Cheng: There so he proposed him over as Lucy need to find all these library files. So you need to specify what the C library files are 107 00:15:09.480 --> 00:15:17.580 William Cheng: And also the sub one and step two. And step three. They are not in the CI Rita is actually in the private library. Number one, so you need to tell the linker to say hey you know this is 108 00:15:17.880 --> 00:15:20.430 William Cheng: This is what you need to go to find all these functions. 109 00:15:20.850 --> 00:15:29.820 William Cheng: OK. So the way you do this is that in the command line for you run a GCC command. Use a minus capital L, followed by a directory name to say, where do you find this private library. 110 00:15:30.210 --> 00:15:37.530 William Cheng: There and the second argument over here is going to be the name of the library. So you say minus l one, right. So in this case, what you would do that. I will look for live 111 00:15:37.800 --> 00:15:44.310 William Cheng: Prayer one day and then he will look into the current working directory. Okay, well, if you can find it. Then in that case it will 112 00:15:44.610 --> 00:15:53.880 William Cheng: You know, you'll go to this library. So in this case, it will look for step one, step two. And step three. And sure enough, they will be able to find that and then copy that into the executable, and then whatever it is that in the end. 113 00:15:54.270 --> 00:16:03.510 William Cheng: They also need to find the C library because they will also realize that the clustering function is is not available inside the private ivory. It's also not available inside the program. 114 00:16:03.840 --> 00:16:12.960 William Cheng: So therefore, in this case, it has to go to the C library. Okay, so you know when when you try to run this command the command line at the end, there's an implied minus capital else 115 00:16:13.320 --> 00:16:23.340 William Cheng: That followed by minus LLC. That means that you should go to the slash lip directory. Look for a file called lipsey a guy and this is the one that has a C library and 116 00:16:24.780 --> 00:16:29.100 William Cheng: Then this guy. You can find all the function that you need printer pull string or that kind of functions. 117 00:16:29.880 --> 00:16:37.920 William Cheng: Right. So again, you know, the last part over here are hidden from you. So when you run this command over here, you know, you're not going to see that, you know, it's magical to see library. 118 00:16:38.280 --> 00:16:47.970 William Cheng: But clearly that's been done, because otherwise you know your your your your your your link or is going to print an error message to say you know unresolved symbol on the symbol. Yeah. 119 00:16:49.980 --> 00:16:57.630 William Cheng: Alright, so, so over here. There's a comment is that the order of the library matters over here. So, so, you know, when I tried to, you know, 120 00:16:58.230 --> 00:17:09.840 William Cheng: When the linker tried to perform. So, by the way, where's the linker guys remember the linker is LD right so you know the GCC command over here. When you say I want to generate the executable file. So at the end of compilation 121 00:17:10.380 --> 00:17:15.510 William Cheng: Book linker again the link or is a wonderful point symbol resolution and also as a side. 122 00:17:16.770 --> 00:17:22.890 William Cheng: Profile relocation also symbol resolution that so what I propose symbol resolution. What you would do is that you actually look at this command line. 123 00:17:23.130 --> 00:17:28.320 William Cheng: And when you try to find a particular symbol is going to look downstream and not gonna, it's not gonna look upstream 124 00:17:28.920 --> 00:17:39.000 William Cheng: Okay. So for example, when I tried to find the function or that's needed for privacy. Privacy requires step one, step two. And step three or so we here. What it will do is it will find it at the end of the libraries over here. 125 00:17:39.420 --> 00:17:48.870 William Cheng: Okay. So, so what it is, every will look for step one, step two, step three in the private library first and then we'll look for the function is that is the library. Okay. It doesn't go the other way around. 126 00:17:49.230 --> 00:17:54.600 William Cheng: And also we try to, you know, in a sub one. Step two. And step three functions they call the function push drink right 127 00:17:54.750 --> 00:18:01.650 William Cheng: So they are found in this library. So when they try to look for the Christian functions, they will only look towards the right over here to look for other library. 128 00:18:02.010 --> 00:18:10.740 William Cheng: So in this example, they will find the pusher in Boston. He said to see library where if you reverse the order of one and the C library and I in our case. 129 00:18:11.160 --> 00:18:23.670 William Cheng: So what you will see that when you try to execute this particular command, you will see that some of the symbol cannot be okay. And that's because the compiler, the linker they follow this rule or the link of all this rule to perform simple resolution. Yeah. 130 00:18:25.050 --> 00:18:31.770 William Cheng: So after you've done this over here. You can run your program. Right. It's a dot slash Prague over here and you will see that, since I'm calling step one, step two and. Step three. 131 00:18:31.950 --> 00:18:38.100 William Cheng: The printer will be step one, step two. And step three. There's no backslash end because I simply call in the streets, step one, step two and. Step three. 132 00:18:38.430 --> 00:18:48.810 William Cheng: Now, and also there's no Patterson at the end. So, therefore, you know, when I finished running my program. The program or the command prompt is going to be. It's going to be printed at the end of the current line. 133 00:18:49.710 --> 00:18:54.600 William Cheng: Okay, so this is clearly doesn't look very good. Okay. So, in that case, what should we do 134 00:18:54.960 --> 00:19:01.800 William Cheng: You guys want to do is that we can actually go fix the party library over here and then re compile the private library right using the previous command over here. 135 00:19:02.190 --> 00:19:06.780 William Cheng: So in this case, what we can do that again for every one of these lines, we're going to add batch size and over here. 136 00:19:06.960 --> 00:19:13.170 William Cheng: And then when we run a less the subway and followed by sub two. In the next line suffering, the next time. And then the prompt will appear on the next slide. 137 00:19:13.680 --> 00:19:18.540 William Cheng: Okay, what if you don't have the source code for for, you know, for you know for for the private library number one. 138 00:19:19.320 --> 00:19:28.500 William Cheng: Okay, so in that case we need to have another way to override the behavior is that a library. Yeah. So in this case, we're going to actually, you know, have our own version of the pushing function. 139 00:19:29.520 --> 00:19:36.660 William Cheng: Then, so again, as you create another library that will override the standard behavior of the pushing function inside of C library. 140 00:19:37.110 --> 00:19:44.970 William Cheng: Okay, so let's see how this can be done. So this is, this is no substitution, we're going to write our own clustering function that will replace the pushing function in this 141 00:19:45.570 --> 00:19:51.240 William Cheng: Is that a C library. Right. But of course, you know, the C library is installed in the system via. So therefore, we can't really modify that file. 142 00:19:51.540 --> 00:19:55.710 William Cheng: But in this case for our program. We just need to have a different version of the pushing budget right 143 00:19:56.130 --> 00:20:05.280 William Cheng: Alright so here. Here's my pushing that see over here, right. So inside this function over here. I'm going to have a function that's exactly the same function prototype as the one in the C library. 144 00:20:05.640 --> 00:20:17.130 William Cheng: Okay, so this way my you know my pushing function. If it turns out that my pushing a bunch of get picked out by my program. Well, then in that case I end up the overriding the behavior of pushing function is that to see 145 00:20:17.820 --> 00:20:25.350 William Cheng: Okay, alright. So that's why, you know, pushing budgeted returns an integer. It takes an argument, you know, this is what I want, want to print. So, and this is what I want to print this i want i 146 00:20:25.890 --> 00:20:39.660 William Cheng: Want to add a backslash and so that every, every time he called pedestrian function, you will you will show up on a separate line also want to demonstrate that I have taken over the function of the C library. So I will also write my history. My posting colon followed by space over here. 147 00:20:40.710 --> 00:20:50.970 William Cheng: Okay, so this way, you know, be clear that I've taken over. So in this case, you know, I cannot call the pushing function because if I if I use a pushing function inside this function, it will make a look like a recursive function. 148 00:20:52.020 --> 00:21:01.740 William Cheng: Right, because if I if I if I call pull string, then you know the compiler. There's really no better, the compiler will say, Oh, you're calling yourself. So, therefore, this guy's up a recursive call. And that's not what you want. 149 00:21:02.220 --> 00:21:07.170 William Cheng: Okay, so therefore inside this function, I have to re implement pushing without using the string function. 150 00:21:07.740 --> 00:21:13.920 William Cheng: Right. So in this case we know since I know the pushing function is going to write the stream to stare out so I can simply use the right system called 151 00:21:14.340 --> 00:21:18.840 William Cheng: Guys, and this guy's this example over here I'm using the racism call writings to standard out over here. 152 00:21:19.080 --> 00:21:28.860 William Cheng: I'm going to write pushing a colon followed by space and then I'm going to put this up on the screen itself. And then I'm going to print it followed by backslash so again called I'm going to call ride with the screen. 153 00:21:29.280 --> 00:21:39.720 William Cheng: Printing only one character. Okay. So in this case, I'm the return one. I'm not move the clustering function. But if you should look at the man page to see what actually returns. Okay. 154 00:21:43.500 --> 00:21:50.400 William Cheng: Alright so this function. I'm going to create a library out of it. Right. So unless he CC minus c, am I pushing that seed will get combined to my screen. 155 00:21:50.610 --> 00:21:59.040 William Cheng: Oh, I'm going to use the AR command over here are to generate the library. So in this case, it will be live my put strain over here. So that will be named the library, followed by 156 00:21:59.400 --> 00:22:11.730 William Cheng: a.ai over here and this guy's only consider one file. So in that case, maybe I don't. And this not only has one function. So in that case, I don't really have to add a table content. Maybe this is only on the one function or you want file. 157 00:22:12.390 --> 00:22:14.250 William Cheng: And then finally, when I tried to, you know, 158 00:22:14.580 --> 00:22:19.800 William Cheng: A compartment program. So again, this is very similar to before Jesus C minus oh prod prod dot c 159 00:22:19.980 --> 00:22:29.310 William Cheng: And now inside the current working directory. I have to first mentioned my program library and then followed by my stream library because I know that in the end over here is going to be my C library. 160 00:22:29.940 --> 00:22:35.550 William Cheng: Okay, so this way when it's hard to perform symbol resolution in the pro one library is going to try to locate 161 00:22:35.760 --> 00:22:47.070 William Cheng: The pusher in function. So what we'll do is we'll look downstream over here and then the first library that you will see is in the pipe. My push your budget. So therefore it will pick up this implementation instead of the one inside the C library. 162 00:22:48.090 --> 00:22:55.890 William Cheng: Okay, so this is why this is called substitution. Because, you know, so now my you're pushing policy has been substituted for the pushing function inside the library. 163 00:22:56.340 --> 00:23:02.940 William Cheng: Okay, so once I do that when I start running my program you will see that I put out over here is gonna say my posturing, followed by colon followed by space. 164 00:23:03.150 --> 00:23:12.810 William Cheng: And then the original stream, followed by backslash and over here. So this way you will look fine. Okay, and also the command prompt is going to show up on the at the beginning of next slide and that will also look correct 165 00:23:14.010 --> 00:23:25.590 William Cheng: Then alright so this is this is why I've been, you know, when you're using static library. The next thing we're going to look at is the dynamic library or the shared library in this case. So what is wrong with static Lincoln unloaded 166 00:23:26.610 --> 00:23:37.290 William Cheng: By so so so so if you think about the function that are using and pushing, pushing, over here, push it is actually going to exist. It is exist. 167 00:23:38.130 --> 00:23:43.350 William Cheng: Inside multiple program over here because the problem in order for the program to run it needs to contain all the functions. 168 00:23:44.040 --> 00:23:49.620 William Cheng: Okay, so if you think about your warm up one of my one of them up to what is the function that you called the most well maybe the printer function. 169 00:23:50.130 --> 00:24:01.260 William Cheng: Okay so printer, we saw before. It's a huge function. So for our class we have you know 5070 students. So in a way, everybody has a copy of the printer bungee inside your warm up one also use a warm up to 170 00:24:01.680 --> 00:24:07.200 William Cheng: Okay, they're exactly the same function. I mean, since the you know the executable code. They are read only. We should be able to share it. 171 00:24:08.010 --> 00:24:13.680 William Cheng: Okay, if we don't share it, then every program is going to end up with a copy of it. And if it turns out that we're actually running on a shared server. 172 00:24:13.830 --> 00:24:19.590 William Cheng: If everybody's running the same program. Everybody's running their own warm up our warm up to, we're going to end up with print function. 173 00:24:19.920 --> 00:24:28.200 William Cheng: So, so, so, so again we're going to create our address space instead of address space. There's a prenup I'm sure we're going to have 50 copies of the printer function. They're all taking up precious memory. 174 00:24:29.220 --> 00:24:35.880 William Cheng: Okay, so in that case. Why don't they share right because you know all these programs you run them individually. They're not parent child processes. So therefore, the shared library. 175 00:24:36.660 --> 00:24:42.960 William Cheng: Then, so that's the whole idea over here for the shared library. We want them not to waste all the space, right. So when we perform 176 00:24:43.320 --> 00:24:50.940 William Cheng: Well we perform compilation. We don't want to copy the executable code for the printer budget into our executable. We want to keep it inside the C library. 177 00:24:51.330 --> 00:25:00.420 William Cheng: Okay, so on the desk, we need to actually have one copy of the printer function. So, this will can say this space and also we are running inside. Inside our program. We also need 178 00:25:00.660 --> 00:25:09.720 William Cheng: To use the printer budget. So in that case, the printer forms you need to be assured cleaner version. So there, so therefore there's only one copy of the brain function in all of memory across our entire machine. 179 00:25:10.380 --> 00:25:17.670 William Cheng: Okay, so if you're running 1000 process, there should be only one copier printer. Right. So in this case, we can also save precious memory. Yeah. 180 00:25:18.600 --> 00:25:23.070 William Cheng: So, so, so, so the question is, how do you actually perform to to to to share something like that. 181 00:25:23.370 --> 00:25:30.360 William Cheng: So one thing is that we're going to sort of create a dynamic library over here. It's called a C library. So in this case, when we try to create a query or executable. 182 00:25:30.720 --> 00:25:35.700 William Cheng: You know, in this case, we're not going to copy the code from a dynamic library into our executable. We don't leave a hole there. 183 00:25:35.910 --> 00:25:46.950 William Cheng: So at the time when we can run it. Well, I will, when we start running our program at that time, but I should perform dynamic link and then loading. So we're going to actually call the function inside the shared library. And there's only only only going to be one copy of it in memory. 184 00:25:48.060 --> 00:25:50.460 William Cheng: Okay, so now we're going to sort of describe how can they actually achieve this. 185 00:25:50.970 --> 00:25:59.370 William Cheng: This can be done there. But the basic idea here is that the C library is going to be is going to be entity of itself, it will it will be the only 186 00:25:59.700 --> 00:26:07.110 William Cheng: You only be the only you know you'll be only the it will be the only library or the executable that keeps a copy of the print function. 187 00:26:07.350 --> 00:26:15.060 William Cheng: It will also be the online when you load into memory. So, so the C library will get loaded into memory and then that will be the only copier printer. 188 00:26:15.480 --> 00:26:27.210 William Cheng: Exists in memory for this particular machine. Okay, so. So in this case, all these programs we hear when they try to make function call, they will use exactly the same coast is this code is read only is perfectly good for them to share it now. 189 00:26:28.920 --> 00:26:30.450 William Cheng: So, so what's gonna be the problem. 190 00:26:31.110 --> 00:26:37.080 William Cheng: OK. So again, you know, the problem is that when I try to compile process and process be over here I'm dumb component using compiler. 191 00:26:37.260 --> 00:26:43.470 William Cheng: And the compiler, you know, Indians when they invoke the linker there's going to be another linker and the linker is the one that perform relocation. 192 00:26:44.070 --> 00:26:49.980 William Cheng: Again, when they perform the location. They all have to independently decide what virtual address to allocate will print out 193 00:26:50.370 --> 00:26:53.730 William Cheng: There. I don't know if you remember in chapter three, we sort of talked about the algorithm is that 194 00:26:53.940 --> 00:27:04.350 William Cheng: They are, you know, the linker is the one that plan out the address space rather take the outer space over here, put all the function here pack them together, one after another. So this way, they will actually figure out, oh printer is that this word to address. 195 00:27:04.950 --> 00:27:11.400 William Cheng: painful process be process is completely different program. So maybe you know process over here is your warm up one process be over here is a woman to 196 00:27:11.610 --> 00:27:17.220 William Cheng: You'll want to is going to be bigger than warm up one and then the printer function is going to get the sitting at a different virtual address 197 00:27:18.390 --> 00:27:28.440 William Cheng: Okay. So is it okay that printer for process. A. Oh no, this will be a worldwide for one on one printer sitting at one virtual address and and then improper process P which is a warm up to 198 00:27:28.740 --> 00:27:33.690 William Cheng: Print ever sitting in a different virtual address there while the sheer library is going to load only one copier printer. 199 00:27:34.620 --> 00:27:39.420 William Cheng: Okay. I mean, theoretically, they should be able to work right. So what we've tried to load this vertical function, you know, inside this 200 00:27:40.080 --> 00:27:45.810 William Cheng: Is that the C library or we're going to allocate physical pages over here and we're going to copy this function into the physical page right 201 00:27:46.020 --> 00:27:57.120 William Cheng: And now have a process AMB. All they have to do is just set up memory map so that they will point to the same function. So in this case process. A can call print up at one virtual address what process beacon call printer in a different virtual address 202 00:27:57.660 --> 00:28:01.890 William Cheng: Well guys, but again, if you look at if you do if you do nm, you know, on your 203 00:28:02.400 --> 00:28:10.230 William Cheng: One on one a one off to you will see that for both of them. The printer might be sitting at a different different virtual address and that's perfectly okay right because you use the memory map. 204 00:28:10.830 --> 00:28:18.780 William Cheng: You know, so eventually they will end up calling the same code. Okay, so it looks like this is going to work there. So in this example over here for 205 00:28:19.500 --> 00:28:28.170 William Cheng: Process, they will be one on one process POV is warm up to one on one wants to call you know print out a virtual address 10,000 warm up to over here want to call 206 00:28:28.560 --> 00:28:32.010 William Cheng: They'll print out a virtual address you know 20,000 over year. So in this case, 207 00:28:32.310 --> 00:28:40.830 William Cheng: You know, the, the sheer live way over here will know the printer function into maybe a you know one current we are one virtual address and then they can be shared by all these other processes over here. 208 00:28:41.190 --> 00:28:47.010 William Cheng: So in this case, using address translation. We couldn't get a different virtual dress but in reality of course we know that since we're doing address translation. 209 00:28:47.160 --> 00:28:57.690 William Cheng: We're going to refer to the physical address. So instead of physical address we saw before the printer is a big function is 12,000 buys law. So once we figure out the first physical address over here. There will be three different pages. 210 00:28:58.110 --> 00:29:04.830 William Cheng: So again, using either transition, they can they can book called same function. Okay, so it looks like this is gonna work. So what's going to be the problem. 211 00:29:05.670 --> 00:29:12.270 William Cheng: Okay, so, so in this case again for process at over here. We're going to compile them into a machine structuring the machines. God says, Call 10,000 212 00:29:12.540 --> 00:29:21.150 William Cheng: For process be willing to the call printer. The machine. So I can go to generate is called 20,000 over here. Okay. And 10,000 20,000 through the magic of, you know, 213 00:29:21.660 --> 00:29:32.340 William Cheng: Building a memory map and they all point to point of exactly the same physical address yeah alright so the problem over here is that what if the print function over here is going to call another function inside to see library. 214 00:29:33.600 --> 00:29:40.770 William Cheng: Okay, so, so this print up is actually sitting at two different virtual addresses you know 10,000 over here for for law of wine and 20,000 people long to 215 00:29:41.010 --> 00:29:45.180 William Cheng: One of this function is going to call another function right because print up is really, really big chances are, 216 00:29:45.510 --> 00:29:51.810 William Cheng: You know, the programmer pop up by them into multiple functions over here. So let's say that instead of printing machine. I'm going to call another function called to print 217 00:29:52.080 --> 00:29:59.160 William Cheng: They do print over here and look like this then. So, this one says to print and to print is another function inside the seed library over here. 218 00:29:59.370 --> 00:30:10.980 William Cheng: So the question is that you can compound, you know, in the application program to call followed by a virtual address. What about inside the seed library over here. When the print function, you need to call do print. What kind of kosher degenerate. 219 00:30:12.030 --> 00:30:24.690 William Cheng: Okay, so let's say that the do print function over here is 1000 bytes below the printer function over here. So in this case, you know, for process a, you know, when I try to call do print issue generate a cold call 11000 process it. 220 00:30:25.050 --> 00:30:35.070 William Cheng: There. But what about for process be well process be over here is also 1000 bikes below so proud process be over here, it needs to call this the do prevention and virtual address or 21000 221 00:30:36.150 --> 00:30:42.030 William Cheng: Then, so instead of the C library is over here for for printer when you try to compile to print over here into a machine. 222 00:30:43.140 --> 00:30:50.340 William Cheng: You know into assembly code into machine code what kosher regenerate regenerate call 11,000 or call 21,000 223 00:30:51.120 --> 00:30:57.360 William Cheng: Well, there's no way for you to solve this problem. Here we go. Yeah, I need to actually generate call followed by a number and this number cannot be a physical address 224 00:30:58.350 --> 00:31:07.140 William Cheng: Right, because this code execute inside your address space over here. So this has to be a virtual address. And then, you know, in this case, we can't have one virtual address work for two different different programs. 225 00:31:08.070 --> 00:31:14.340 William Cheng: Okay, so, so everything sort of falls apart. Well, when inside of C library, you have to call another function inside the library. Right. 226 00:31:15.090 --> 00:31:20.820 William Cheng: Right. So what are the solutions over here, right. So there are two or three different solutions over here. Number one is to give up. 227 00:31:21.210 --> 00:31:26.250 William Cheng: Okay, to just say that, well, you know, we can actually share the same you know print out function, you know, on the desk. 228 00:31:26.700 --> 00:31:31.290 William Cheng: But we start running our program every process gets a separate copy while certainly that will work. 229 00:31:31.680 --> 00:31:38.100 William Cheng: But again, that's not what we're going after we want to want to make sure there's only one copier printer in memory in the entire system then 230 00:31:38.730 --> 00:31:49.410 William Cheng: The other way. Some people actually use some Capri relocation. So we're going to sort of have all the compilers under the sun all agree that the printer function. They all have, they have to use exactly the same virtual dress. 231 00:31:50.100 --> 00:31:57.960 William Cheng: Okay, so this way you know everybody when they call a printer, they will all use the same virtual address. So in this case, you know, things that printer. If you want to call another function again the other function. 232 00:31:58.260 --> 00:32:03.210 William Cheng: The virtual does also predetermine so this way, all these, you know, all these function call, it's gonna work. 233 00:32:03.690 --> 00:32:14.460 William Cheng: I mean, clearly, this will, you know, this particular approach is not very, very flexible. What do you feel program really want to call printer and one virtual dress and another program on the call at different virtual dress. Right. So, for example, small programs will be here. 234 00:32:14.880 --> 00:32:20.100 William Cheng: You know, you don't really want to use a large virtual dress up or large program over here. They have a lot of functions. They were trying to call 235 00:32:20.430 --> 00:32:26.370 William Cheng: So in that case, they will want print have to be at a different virtual dress. Okay. So even though pre rotation is a possibility. 236 00:32:26.790 --> 00:32:32.700 William Cheng: But again every time somebody who wants to write a compiler. You got to make sure they generally the right code. So again, it can get very, very complicated. 237 00:32:33.540 --> 00:32:48.030 William Cheng: Now, so typically, you know, all the system today. They all implement the third approach over here which is using position independent code. Yeah. So what we're going to do is that when we, when we try to call a function, we need to generate code that doesn't depend on the memory location. 238 00:32:49.560 --> 00:32:59.250 William Cheng: Okay, so we're going to make a function call. Without knowing what the virtual address is for that particular function that's not gonna work right again solution over here is going to use one level of indirection that 239 00:33:00.450 --> 00:33:09.900 William Cheng: Solution over here is that for process and for process be we're going to build a table of virtual addresses. So this table over here is right here. And this pointer to buy the R1 register. Right, so the 240 00:33:10.530 --> 00:33:17.100 William Cheng: Register is one registered inside of CPU, they will be used as a base address for an array. And this one will be an array of function pointers. 241 00:33:17.520 --> 00:33:28.320 William Cheng: Right. So in this case, print out over here will be sitting at a rate index zero and do print over here. We'll be sending an array index one and then the corresponding value will be the virtual address or that's needed, you know, for this process. 242 00:33:28.770 --> 00:33:37.290 William Cheng: OK. So the example that I use over here for one of one you want to call prenup of virtual address 10,000 and you want to call to play in a virtual address 11,004 process. 243 00:33:37.740 --> 00:33:45.060 William Cheng: For for warm up to over here. You want to call printer a virtual 20,000 and you want to call printer. You want to call to print a virtual just one 1000 244 00:33:45.990 --> 00:33:53.010 William Cheng: Right. So, therefore, you're going to build this table over here. And then you're going to use the information inside this table indirectly when you make function call. 245 00:33:53.460 --> 00:34:00.030 William Cheng: Okay, so therefore the function call cannot be called followed by a virtual address because that will be a direct call tonight going, they're going to make an indirect call 246 00:34:00.270 --> 00:34:07.290 William Cheng: So we're going to use two pairs of register one point database address of this table and the other one is used for you to make a register indirect call 247 00:34:07.950 --> 00:34:19.620 William Cheng: So therefore, the colon look like this. Instead of saying call 10,000 there. I'm going to make. I'm going to generate position independent call over here. So I'm going to load a registered are two over here with our one as the 248 00:34:20.220 --> 00:34:32.160 William Cheng: Array as the base address when array and then print out over here as an array index, right. So this is like an array notation over here. So this is a rare function pointer and this will be the array index right so printer will be a function number zero 249 00:34:32.580 --> 00:34:46.530 William Cheng: Guys 00 here is going to be equal to 10,000 so you know 10,000 over here into the order to register, so he's not a CPU. There's our to register right you can load it with 10,000 over here and then you gotta call our to using a register indirect call 250 00:34:47.040 --> 00:34:53.250 William Cheng: Okay. So when you say call followed by a registered name, you're going to take the register, look at the function inside and they're going to make a registry indirect call 251 00:34:54.000 --> 00:35:06.000 William Cheng: Okay, so this way you will call the function print up at virtual address, you know, the virtual drive 10,000. Okay, what am I inside to see library over here. So you need to compile the C library over here also you know 252 00:35:07.080 --> 00:35:13.920 William Cheng: Sort of through us using a special compiler. So they will generate position independent call if you need to make function call inside to see library. 253 00:35:14.400 --> 00:35:18.660 William Cheng: OK. So the code over here look exactly the same as the other one. So when the printer function need to call printer. 254 00:35:19.560 --> 00:35:27.960 William Cheng: To print, what would it do, right. So that would generate exactly the same code over here. I'm going to low register our to using our one as a base, the base of the of 255 00:35:28.470 --> 00:35:32.850 William Cheng: The of the array. And in this case, we're going to call a function print out over here is going to be 256 00:35:33.330 --> 00:35:44.670 William Cheng: Zero and supernovae is going to be arraigned next one. So because this will be our one with a rate. That's why. So therefore you will pick up the value 11,000 you will copy them into the are two registers and our two is going to have 11,000 257 00:35:45.450 --> 00:35:52.620 William Cheng: There and then you're going to make a registered in direct call you gonna say call or two, you're going to end up calling the do print function of virtual address 11,000 258 00:35:54.210 --> 00:36:05.370 William Cheng: Alright, so this is how you make registering direct call you're going to make registering direct call over here or using position independent call inside your insight, your program. We also need to compile your C library using positioning the brand out 259 00:36:05.790 --> 00:36:12.270 William Cheng: There. What about process ME OVER HERE, THIS IS A warm up to warm up to over here, you know, they want to call these functions, a different virtual addresses. 260 00:36:12.540 --> 00:36:21.690 William Cheng: But the code over here generated exactly the same write the code over here when you try to call print up the call. We here at exactly the same as your one on one coach that's gonna call for an F. 261 00:36:22.260 --> 00:36:28.530 William Cheng: Okay. So, therefore I'm going to lower register or two with our one but now for process number to over here alone will be plenty at this table. 262 00:36:28.740 --> 00:36:38.580 William Cheng: Or a index over here for printer busy and I'll print able to zero and to print over here. Right, next one. So if you go to our wine with a range like zero, you're going to end up loading the are to register with 20,000 263 00:36:39.450 --> 00:36:47.220 William Cheng: Yeah, so therefore you going to call the to the print function over 20,000 so we used to execute this code and you need to call do print. You're going to know our to 264 00:36:47.520 --> 00:36:58.920 William Cheng: You know, with our one with the array index. What you're gonna copy 21,000 over here into our to so you don't end up with 21,000 over here and then you call our tool with the registry and direct call. So now I'm going to end up calling this function. 265 00:36:59.370 --> 00:37:01.620 William Cheng: You know, did this function it with a virtual address 266 00:37:03.420 --> 00:37:14.880 William Cheng: Okay, so you can see that, you know, in both program also the C library I execute exactly the same code. But in this case, you know, the print function in a different function. I'm using a different virtual address to call these functions. 267 00:37:16.170 --> 00:37:25.920 William Cheng: Right. So in this case over here. You know, you know, full, full, full process. A and B. They don't need a copy of putting up anymore because the, the only, you know, they're the only Cody. The only library code. 268 00:37:26.460 --> 00:37:31.830 William Cheng: That the other, that's the that's going to have the function is going to be the one inside the seed library. 269 00:37:32.550 --> 00:37:43.770 William Cheng: Okay. And also, you know, the pseudo code over here, don't need to have a copy of the printer and print QR code inside the executable because the only place that you keep a copy of the printer code is going to be used to see library. So in the end, by using 270 00:37:44.010 --> 00:37:54.180 William Cheng: You know, positioning pentacle you only have one copier printer on desk and also one copy of a happy memory. Okay. And that will be the that is the goal, the way I should try to achieve then 271 00:37:55.320 --> 00:38:02.220 William Cheng: Oh so oh yeah I'm sort of using my own notation as an array index. Right. So if we go back to chapter three and look at the notation. That was us over here. 272 00:38:02.670 --> 00:38:07.530 William Cheng: So you try to load it registered over here with the different you know notation over here. I don't know if you remember 273 00:38:08.040 --> 00:38:12.660 William Cheng: The regular the vision that we use is that will put a practices with a registered name in there and then we 274 00:38:13.380 --> 00:38:21.000 William Cheng: Know, we'd read the offset over here in front of it. So this one over here will be used all my register, we can put parentheses in front of that us as a base address 275 00:38:21.450 --> 00:38:28.260 William Cheng: Typically, we're going to see this notation using EBS and I remember you know you say percent you know he X over here and then we can say plus 276 00:38:28.650 --> 00:38:41.220 William Cheng: Or minus 16 or something like that. So in this case over here printer will be plus zero. Okay. And then the duping over here will be plus for over here. So again, using our one over here, you ask for to the memory location over here. They are you going to get an eccentric 277 00:38:42.480 --> 00:38:50.910 William Cheng: Alright, so you can actually see that, you know, by using the same syntax that we saw before, in Chapter three of this coke come work. Okay. But in reality, there's a lot more detail because 278 00:38:51.630 --> 00:38:53.790 William Cheng: The basic question over here as a hobby actually fill this table. 279 00:38:54.750 --> 00:38:57.510 William Cheng: Okay, so, you know, when do you build this table right at the time when you 280 00:38:57.720 --> 00:39:04.710 William Cheng: Run your program you put on your doesn't have this table. So we're going to sort of build this table at runtime. And that's what's called dynamic library. Right. 281 00:39:04.860 --> 00:39:11.940 William Cheng: You know, the share lab results with noise dynamic library because we're actually going to build some part of our program over here at the time we run our program. Right. 282 00:39:13.860 --> 00:39:16.890 William Cheng: I just wanted to mention that the technology has a lot more detail. 283 00:39:17.280 --> 00:39:24.300 William Cheng: You know, you know, for elf, you know, 32 they request three different data structures. So again, if you're interested, you should look at the textbook over here, they were 284 00:39:24.570 --> 00:39:33.690 William Cheng: Getting going to see a lot more assembly code, they will tell you exactly how to build that particular table there. So over here, I'm gonna sort of just, you know, so go over some of the high level idea how to do that table. Yeah. 285 00:39:35.190 --> 00:39:41.280 William Cheng: Alright, so I guess we'll talk about a producer. So, so we can sort of briefly introduce you know sort of shared library. 286 00:39:41.670 --> 00:39:48.180 William Cheng: The library today. They're extensively, the US in all kinds of operating system windows, they are using it for a long time and also in the 287 00:39:48.630 --> 00:39:56.700 William Cheng: Linux and Unix them. They also have been using them for a long time. Okay, so if you're familiar with Windows a window has something called a dynamic link library or DLL 288 00:39:57.600 --> 00:40:06.840 William Cheng: Damn. If you search, you know, some of the windows literature, they can call the allows over here. So the do we, here are the dynamic at at the dynamic link library over here. So basically it's the same thing as our 289 00:40:07.620 --> 00:40:23.220 William Cheng: As our shared library Unix. So, you know, the old days, the dynamic library on here. They have the phone then extension DL out there today. Pretty much everything that you running Windows that are DLL okay they have all kinds of different extension over here. So they're all dynamically 290 00:40:24.750 --> 00:40:30.780 William Cheng: Library. So if you have a program, you actually can can link to this library a runtime and then you'll be able to actually use a function there. 291 00:40:31.170 --> 00:40:35.490 William Cheng: Is a Windows use the dynamic library extensively inside the architecture now. 292 00:40:36.150 --> 00:40:48.570 William Cheng: For Unix. They are no as a shirt object file. So they therefore they have the filename extension of that. So, okay. So, for example, the, the sheer C library is called Live si.so 293 00:40:49.020 --> 00:40:58.530 William Cheng: Okay, so again, you know, the first three characters be on live the last three it's if it's not a to study vibrate with that. So it's a shared library and then the middle part over here is named the library. 294 00:40:58.980 --> 00:41:04.080 William Cheng: Okay. So, therefore, this is the UNIX version of the C library at the procedure library. Yeah. 295 00:41:06.060 --> 00:41:15.000 William Cheng: Alright, so, so, so, so, so in this case, you know, these kind of shared library. They are, you know, the patient is this they will only be loaded into memory on demand, when you need them. 296 00:41:15.330 --> 00:41:22.050 William Cheng: There's typically they all sit inside a slash live directory slash lip directory. And there's also another directory called slash user live 297 00:41:22.560 --> 00:41:31.230 William Cheng: Okay, that's where a lot of these these your library was said. So this will typically sit on the library and then will you start running your program. If you require one of the function inside the shared library. 298 00:41:31.650 --> 00:41:34.800 William Cheng: So, at that time, you need to invoke something called the runtime loader. 299 00:41:35.340 --> 00:41:46.620 William Cheng: Will get or sorry, the runtime linker the renter linker which had to find all the symbol that you cannot find. So in that case, they watch you know all these functional view into library. So you only have one copies of that. And if you can actually find these function in the 300 00:41:47.520 --> 00:41:54.360 William Cheng: You can actually find all the songs in memory already or not. It doesn't have to, you know, that again guys. So this way, we'll only have one copy is 301 00:41:55.110 --> 00:42:07.440 William Cheng: Inside you know inside in some memory there. So, so it's also, you know, one of the advantages of doing this is that when you start running your program when you start running your program you can run with no library at all until your 302 00:42:08.070 --> 00:42:09.480 William Cheng: Until you start making those function call. 303 00:42:09.930 --> 00:42:19.860 William Cheng: Okay. In the previous example before you call printer, even the C library doesn't have to be loaded. Right. Unless you call some other function and C library so only the first time equals C library over here you can replace far 304 00:42:20.220 --> 00:42:28.080 William Cheng: So he's our Patreon, we're going to handle paid for by loading are the dynamic library into memory over here. And then we're going to perform dynamic Lincoln Elodie and then 305 00:42:28.560 --> 00:42:41.250 William Cheng: They're going to return back to your user space program and your program continue to run. Okay, so we're going to, we're going to actually be able to fix up all these pay as they are these tables over here using the pace for mechanism that we're very familiar with. Okay. 306 00:42:43.440 --> 00:42:50.100 William Cheng: So, so, so, so, so, so, you know, we do it this way. Also, your program startup time is cut down significantly. 307 00:42:50.430 --> 00:42:57.750 William Cheng: Because when you start running your program. You don't need to have any of the library. Right. So you actually can be actually very, very small. It can be as small as your main function. 308 00:42:58.680 --> 00:43:06.150 William Cheng: Okay, because your main function before calling because because if you are called anything else you know that that will be the only function that has to be loaded 309 00:43:06.450 --> 00:43:14.520 William Cheng: Right. I mean, you're, you're, you're a startup function into call me right so those will be the only to function that will need to exist. Everything else that can be done using dynamic library. 310 00:43:15.090 --> 00:43:28.830 William Cheng: Okay. So actually, for your winnings code. If you said dynamic equal to one and a m i c equals two, one in config. I am k. And then if you can compile your winnings Colonel as a sorry if you compile your 311 00:43:29.580 --> 00:43:39.330 William Cheng: executable file, like the whole program. Okay, you're going to see that the only function that you see is going to be the the the lipsey the startup function and also the main function. 312 00:43:39.780 --> 00:43:45.390 William Cheng: Okay, everything else is dynamically loading right so this way you can actually start really quickly. Right. 313 00:43:46.200 --> 00:43:51.300 William Cheng: Right. So there seems to be a lot of advantages of the dynamic library. So what is going to be the downside. 314 00:43:51.630 --> 00:43:59.040 William Cheng: Okay. There are two major drawback of that image library and number one is that they may they may have dependencies. Okay, you have one library defense or another. 315 00:43:59.550 --> 00:44:06.270 William Cheng: Another library. What if you know what if, when it's a contender on your program you found one of the library and you cannot find another library. 316 00:44:06.630 --> 00:44:12.120 William Cheng: Well, in that case, you'll probably will not be able to route or you start running in all of a sudden function, you can apply and you invoke 317 00:44:12.360 --> 00:44:17.520 William Cheng: You know, sort of dynamic link and then load the mechanism. And they said, we cannot find it either way. Decades of programs gonna crash. 318 00:44:18.390 --> 00:44:28.950 William Cheng: Okay, so, so, you know, this is not gonna happen with studied studied library because when you study Lincoln Elodie you copy all those functions inside you executable. So when you run your executable all the code. 319 00:44:29.460 --> 00:44:35.850 William Cheng: All the functions are there. So, therefore, in that case, you know, you're not be able to, you know, not find the library. 320 00:44:36.450 --> 00:44:42.420 William Cheng: And also in the good old days, Robin is that, well, when there's only studying Lincoln and Lori. I mean, you can actually combine your program is instigating Elodie 321 00:44:42.840 --> 00:44:50.280 William Cheng: So if you compile your code, you know, inside Linux operating system, you can actually send your executable file to your friend, as long as your friend has the same 322 00:44:51.510 --> 00:44:58.170 William Cheng: The same you know Linux Linux machine, you know, where they did. They have libraries or not doesn't really matter as long as they have the executable code. 323 00:44:59.190 --> 00:45:07.710 William Cheng: That they will be able to run your program. Okay, but now with dynamic loading. If you use the library. The only your machine has when you send it to your friend, your friend will not be able to run it. 324 00:45:08.370 --> 00:45:11.160 William Cheng: Okay, so that would be a problem. So they will say, oh, I will not be a runner. 325 00:45:11.400 --> 00:45:19.770 William Cheng: Could you send me this library and then you send them another library and then they start running this old another libraries missing, we're gonna keep going back and forth, back and forth, eventually going to send them all the library files. 326 00:45:20.280 --> 00:45:37.050 William Cheng: Okay, so yeah that's pretty annoying also different library files has different versions, you know, so, you know, so for example, if you look at the the Linux library. I guess you know 160 point oh four. I think the C library is version two point 23 327 00:45:38.070 --> 00:45:44.910 William Cheng: Okay, so when you try to send you know you'll see library, you know, to your friend. What if your friends are actually has one point 37 version. 328 00:45:45.390 --> 00:45:51.330 William Cheng: There. So in that case that library will not be compatible. So Nick is going to will refuse to run. So typically, again, when you type your name. 329 00:45:51.630 --> 00:45:59.190 William Cheng: In all these version number. So just like a device driver. There's a major version number. There's a minor version number. So the main major version numbers over here will tell 330 00:46:00.000 --> 00:46:08.850 William Cheng: Will basically tell you that the header file between these two libraries, even though they're all they're all the both the same kind of library. Both of them are lot let's see that. Oh. 331 00:46:09.240 --> 00:46:15.120 William Cheng: We have a one of them is version two point 23. The other one is version 2.1 point 37. That means that they're header files are different. 332 00:46:15.870 --> 00:46:19.710 William Cheng: Okay, so what if there's heard of other different. That means the data structure might be different sizes. 333 00:46:19.890 --> 00:46:28.920 William Cheng: So therefore, if you are expecting library of version to version two. And if it turns out that you're using the Copa version one. In that case, you know, all the data structure is going to be on the wrong the wrong size. 334 00:46:29.340 --> 00:46:30.900 William Cheng: So therefore your program is going to blow up. 335 00:46:31.740 --> 00:46:40.770 William Cheng: Guys, so therefore, again, you got to make sure that the major version. They are compatible then Nick has the minor version number will basically be different, different implementation of the same kind of function. 336 00:46:41.100 --> 00:46:49.260 William Cheng: Okay, so yeah, not only the header files over here, the sort of the data show you how to be on the same size. And also, you got to have a 337 00:46:50.100 --> 00:46:56.040 William Cheng: Certain collection of function inside your library. If you start deleting some of the function over here again you need to pump the major version number 338 00:46:56.490 --> 00:47:03.390 William Cheng: So this way you don't accidentally, you know, start to use the wrong the wrong library that doesn't have a function that you actually using in that case it will also cause problems. 339 00:48:07.920 --> 00:48:12.930 William Cheng: Okay, I think I lost internet connection, you know, for a while. So I'm going to continue here. 340 00:48:14.280 --> 00:48:21.450 William Cheng: Alright, so for the the UNIX system. The runtime linker is called El de de so it's a different program. 341 00:48:21.960 --> 00:48:29.610 William Cheng: So obviously, when you start running your program, you need to build this table over here the table that we saw before, over here, this is a table that's bill at the wrong time. 342 00:48:30.240 --> 00:48:36.720 William Cheng: Okay, so when you start running your program before your programs are running. What it will do is that it will invoke the runtime linker and the runtime Lincoln will feel 343 00:48:37.110 --> 00:48:41.280 William Cheng: Real good about this particular table by scanning for all these functions inside the 344 00:48:41.790 --> 00:48:49.650 William Cheng: Inside the sheer C library and then you can actually call the function as opposed to the table and figure out and perform additional a relocation. 345 00:48:50.010 --> 00:48:52.230 William Cheng: So this way, it will figure out where all these different addresses. 346 00:48:52.740 --> 00:49:01.710 William Cheng: That because what happened is that if some of the preferred location for this function. They are inside your address space and those memory location has already me us now you will you perform the dynamic 347 00:49:02.190 --> 00:49:06.840 William Cheng: Dynamic Link A you're going to find new addresses for them. And then you're going to set up your memory map to map to 348 00:49:07.410 --> 00:49:12.960 William Cheng: These functions, the right places. And then finally, when you're done you fill this table with the virtual address all these functions. 349 00:49:13.200 --> 00:49:26.220 William Cheng: OK, so now when you start your trial run your program or you can actually use all these virtual dresses. Okay. So all these things are done by the LDS that so for the Linux system is called LD dash Linux, you know, followed by the version number, you know, die so 350 00:49:28.590 --> 00:49:31.590 William Cheng: Okay, so, so, so their main purpose is to perform 351 00:49:32.940 --> 00:49:34.200 William Cheng: Lincoln and loading around town.