WEBVTT 1 00:00:02.040 --> 00:00:18.660 William Cheng: Okay, here is the second part of lecture three. So we finished introduction on processes and a little bit of threads. And now we're going to talk about the file systems. Okay, so that's where in Section 1.3 of the textbook. 2 00:00:22.110 --> 00:00:28.530 William Cheng: Okay, so our primes program wasn't very interesting, it computes the 300 primes 100 prime numbers. 3 00:00:28.830 --> 00:00:36.450 William Cheng: And then he just quit the program just quits. Okay, so you have no idea that whether the values are correct or not because it doesn't print anything out. 4 00:00:37.170 --> 00:00:44.700 William Cheng: Okay, so. And the reason is that the pipes program. The only thing that it does is that it access the address space and it didn't do anything else. 5 00:00:45.120 --> 00:00:50.100 William Cheng: So if you only if the only thing that you do in your program is to change data is your address space, and will you 6 00:00:50.580 --> 00:01:00.240 William Cheng: Know when you call exit or your address space get destroyed you actually, you didn't leave any information to the user to tell them what do you have computer. Okay, so it's really not very interesting. 7 00:01:00.810 --> 00:01:06.390 William Cheng: In order for this to happen, you need to communicate information with the user, you have to use the file systems. Yeah. 8 00:01:07.050 --> 00:01:12.690 William Cheng: So the files file system over here has to purpose and number one is that it's an abstraction of persistent data storage. 9 00:01:13.110 --> 00:01:22.440 William Cheng: If you want to, you know, if you want to produce some data and then you want to, you want to turn off machines or tomorrow when you open the machine that data is still there while you have to write the data to a file. 10 00:01:22.980 --> 00:01:33.240 William Cheng: OK, so the reason one reason you might want to use the files for persistent storage. And the other reason for using found is that it's it's the means for fetching is storing data outside of a process. 11 00:01:34.140 --> 00:01:41.940 William Cheng: Okay, because your process all you have so far is the address space if you modify your address space, then you know you didn't really communicate your data with anybody else. 12 00:01:42.120 --> 00:01:47.970 William Cheng: So in order for you to communicate your data with anybody else, you have to use the file system, you have to use the files. 13 00:01:48.600 --> 00:01:52.260 William Cheng: Okay, so this is how it works with the Unix and Linux operating system. Okay. 14 00:01:52.740 --> 00:02:03.660 William Cheng: So, so, so what are the things that you know what are the things that are also your address space. Well, pretty much everything is also your address space all the devices that you need to talk to. They're all outside of your address space. Yeah. 15 00:02:04.110 --> 00:02:14.490 William Cheng: So for example, if you want to put data on a desk. You want to put data in another process. You want to put date you want to read data from the keyboard. You want to put data on the display. You have to use the file systems, man. 16 00:02:14.940 --> 00:02:24.000 William Cheng: So for all these different places on the disk, the keyboard. This way, we need to have a way to name all these different places. The way they Unix does and Linux does 17 00:02:24.240 --> 00:02:32.730 William Cheng: Is it uses a hierarchical naming convention, right. So, so you have a root directory, followed by, you know, slash home or something like that and then 18 00:02:33.240 --> 00:02:37.320 William Cheng: And then come your home directory and then there's user been there's been all these kind of directory 19 00:02:37.530 --> 00:02:46.350 William Cheng: We use a hierarchical naming structure to name all these places where you can access all these devices you can access the desk and also asset your keyboard. You can also access your display. 20 00:02:47.130 --> 00:02:55.890 William Cheng: Yeah, so, so, so, what sort of look a little more detail, you know, pretty soon. Yeah, so the file system is also part of the process extended address space. 21 00:02:56.490 --> 00:03:02.970 William Cheng: Right, because we mentioned before, right, the address space will know what they are the three of the four gigabytes of memory location, not your address space. 22 00:03:03.330 --> 00:03:16.920 William Cheng: Anything else is outside it's outside the address space and we call them the extended address base. Okay. As it turns out that you know for the Unix and Linux system. If you want to go outside your address space. The only place you can go is to go to the File systems. 23 00:03:18.060 --> 00:03:22.350 William Cheng: Okay, so in a way we use the file system to implement the concept, but we extended address space. 24 00:03:23.430 --> 00:03:29.310 William Cheng: Over here, there's a weird comment to say that the files cursor position is part of the execution context, what is the cursor position. 25 00:03:30.090 --> 00:03:34.980 William Cheng: So in warm up one when you try to read from the input file, you try to read one line at a time. 26 00:03:35.580 --> 00:03:40.740 William Cheng: Okay, so you've caught F gates will read the first line and then the second time when you call f. Get it will go get the next slide. 27 00:03:41.100 --> 00:03:45.060 William Cheng: Okay, how does it. Remember that you have finished reading the first line, you need to go to the second line. 28 00:03:45.990 --> 00:03:51.540 William Cheng: Okay, so that information is stored somewhere so that information is stored inside the cursor position for the file that you have open 29 00:03:52.140 --> 00:03:57.180 William Cheng: Okay, so for example if I ever found I looking like this right here is by number 01 to all devices sitting over here. 30 00:03:57.390 --> 00:04:05.280 William Cheng: When I try to read from the file. I need to put a cursor position. And so as a personal decision, right. So it's like when you're typing on the screen you see the cursor, that's where you're going to type 31 00:04:05.610 --> 00:04:11.130 William Cheng: Okay. Same thing for the files that there's something called a person position when you try to read data from the file that's what you're going to start reading 32 00:04:11.640 --> 00:04:18.090 William Cheng: Okay, so when you first open the file the cursor is put a character zero once you finish reading a line around when you finish reading a line. 33 00:04:18.450 --> 00:04:24.810 William Cheng: When you finish reading a line over here the last character of US backslash. And so in that case the cursor position has moved to pass that backslash yet. 34 00:04:25.530 --> 00:04:29.970 William Cheng: So next time when you try to read another line, it will pick up where you left off and then continue to refund there. 35 00:04:30.450 --> 00:04:37.830 William Cheng: Okay, so. So in this case, is you sort of think about this cursor position is part of the state of your program, so therefore it's part of your execution context. 36 00:04:38.130 --> 00:04:49.410 William Cheng: Okay, so get execution contact is the state of your program you can save the content you can restore the contact. So it sort of tells you where you are. So if you're reading from a file, then the cursor position for that file is also part of your execution context. 37 00:04:50.400 --> 00:05:00.000 William Cheng: Alright, so, so again, sorry. So the first thing is very, very important right as long as you need to go out to your outside of your outer space, you have to rely on your file system to do all these kind of things now. 38 00:05:01.230 --> 00:05:08.550 William Cheng: The notion of the notion of a file is our Unix and Linux system. So abstraction for the concept of somewhere outside the process. 39 00:05:08.940 --> 00:05:15.570 William Cheng: Okay, so if you want to go outside. We address space if you go on. You guys had a process. Well, in that case, you have to go through the file system. 40 00:05:16.230 --> 00:05:20.940 William Cheng: Yeah. And the last comment over here says modern UNIX is that have additional abstraction. 41 00:05:21.120 --> 00:05:27.270 William Cheng: Right. Some people sort of talk about, you know, share memory and things like that how the process communicate with each other. So there are other abstraction. 42 00:05:27.510 --> 00:05:32.520 William Cheng: As far as sixth edition units is concerned, if you want to go outside your address space, you have to use the file systems. 43 00:05:32.880 --> 00:05:41.460 William Cheng: Yeah, alright. So, again, right now we're at the introductory you know stage, we're in chapter one. So we're going to give you the sort of a high level, you know, sort of an introduction to what a file this, how do you 44 00:05:41.850 --> 00:05:45.900 William Cheng: You know what, what is the meaning of the file. How do you access data or this kind of stuff. Yeah. 45 00:05:47.670 --> 00:05:54.900 William Cheng: Alright, so the way we sort of name value that we have this hierarchical naming convention, right, we can go to slash home and go somewhere, we have 46 00:05:55.290 --> 00:06:03.930 William Cheng: Been slash user slash been there's a hierarchical structure. Okay. So, this way we can actually name every location that's outside of address space. Okay. 47 00:06:04.350 --> 00:06:07.680 William Cheng: So the way we we chose our access part of our sites, I will 48 00:06:08.070 --> 00:06:16.560 William Cheng: Try to access any place outside of our space and we have to use this hierarchical naming convention. So that's where we can store data somewhere and then retrieve them at a later time. Yeah. 49 00:06:16.980 --> 00:06:27.810 William Cheng: So we try to access some data. What we need to do is that we need to provide the you know the operating system was a string and the string describe what we need to go right. We saw the example before this homes BC. 50 00:06:28.350 --> 00:06:37.200 William Cheng: Slash been slashed, you know, you know, slash after been is a prime or something like that, right. So that's my premise world where I need to provide the string. 51 00:06:37.680 --> 00:06:41.850 William Cheng: So the street name of place inside you know inside the file system. 52 00:06:42.480 --> 00:06:49.410 William Cheng: Okay, so sort of the technical term computer sound will say that, well, you know, you sort of need to provide the name, you need to provide a name instead of namespace. 53 00:06:49.950 --> 00:06:59.730 William Cheng: Exactly what is the namespace right so namespace, just like address space right address space it's it's adjustable memory location that you're allowed to use to access memory locations. 54 00:07:00.150 --> 00:07:05.550 William Cheng: Okay. So, similarly namespace are the name that you're allowed to use in order for you to access files inside of our system. 55 00:07:06.000 --> 00:07:10.470 William Cheng: Okay so units. What we need to do is that we need to provide a string and then we need to pass a string to the 56 00:07:11.160 --> 00:07:17.130 William Cheng: To the operating system. And we asked the operating system to interpret that string in that string is interpret. You know, so the 57 00:07:17.340 --> 00:07:25.050 William Cheng: System sort of know what the name names to a supposed to look like. So what it will do is it will take that string and try to see if that string matches something. Is that a namespace. 58 00:07:25.650 --> 00:07:30.930 William Cheng: Okay, if it's inside the name says what it will do is that it will find that file, it will open it for you and return to you for for for you to access 59 00:07:31.230 --> 00:07:38.610 William Cheng: If it turns out that that you know that the stream doesn't really matter anything's that namespace. Well, in that case, the organism will return with an error. 60 00:07:39.600 --> 00:07:45.750 William Cheng: Okay. All right, so, so, so, so what happened is that, you know, when you ask somebody to open a file, like in one mouth wide. 61 00:07:46.290 --> 00:07:51.450 William Cheng: Open right. You can also call the if you want to do things in a more raw form, you will you will make the open system call 62 00:07:51.810 --> 00:08:00.930 William Cheng: F open is a wrapper functions around the open system call is open so open is the system call f open is sort of a more convenience function that people like to use to make a wrapper around 63 00:08:01.560 --> 00:08:10.440 William Cheng: Around the open system called. So either way, it doesn't really matter. So often is that when you open a file, this file, it's going to be outside of your address space whereas in this case, how can you refer to that particular file. 64 00:08:11.430 --> 00:08:20.940 William Cheng: Okay, so the file is outside of your address space. Once you open it. How do you say I want to work on that file. Okay, so you need to have some way to to for the opposite Terry's that hey I have open 65 00:08:21.780 --> 00:08:28.950 William Cheng: Open the file to do for you. And now if you want to do something to this file, you need to return to me something that signifies that this is the father have open 66 00:08:29.610 --> 00:08:39.570 William Cheng: Okay. In as the general opportunism terminologies that this is called so this is no as a handle. Okay, so, so, so what is the handle. Right. So if you had a handle for frying pan. 67 00:08:40.050 --> 00:08:50.910 William Cheng: Then that's how you reach the frying pan. If you haven't handled for a knife. That's how you reach the night so hand or something that you hold on to. So this way you can use the handle to refer to something that's inside the extended address space. 68 00:08:51.450 --> 00:08:59.610 William Cheng: Okay, so what you would do is that you want to open a file, you are, you will be part of the system was shrink the organism will try to open the file, try to interpret that string in the namespace. 69 00:08:59.820 --> 00:09:06.000 William Cheng: If you were able to do that what he will do is that inside the operating system inside your extended address space, it will create an object to represent that. 70 00:09:06.720 --> 00:09:13.590 William Cheng: Okay, but this object is inside the kernel. So therefore you don't have any access to it. So what it will do is it will create a handle for you to refer to that particular object. 71 00:09:14.250 --> 00:09:24.750 William Cheng: There. So when you when you make an open system call what it will do is it will return a handle. Back to you from this point now you need to hold on to the handle, because if you want to do anything to that file, you need to return a handle back to the operating system. 72 00:09:25.410 --> 00:09:32.520 William Cheng: Okay, so. So what kind of stuff need to do you want to do to that file. Well, you can read for me can write to a when you're done done with it. You need to close the file. 73 00:09:33.360 --> 00:09:42.810 William Cheng: Okay, so all these operation is to involve a handle guys over here says using a handle to refer to an object managed by the colonel is an important concept. 74 00:09:43.500 --> 00:09:51.180 William Cheng: Okay so anytime the operating system want to create something on your behalf, what he will do is it'll create an object or the kernel. And then he will return a handle, back to you. 75 00:09:51.330 --> 00:09:56.160 William Cheng: And now with the handle. You can continue to refer to the object that are inside your extended address face. 76 00:09:57.060 --> 00:10:04.440 William Cheng: OK. So, again, that objects not inside your address space, right, because it doesn't have a memory location for you to use. Okay, if you try to use the kernel memory location while you're 77 00:10:05.040 --> 00:10:10.830 William Cheng: There, then the system is going to kill your program because you do not allow us to to use a colonel virtual dress. 78 00:10:11.400 --> 00:10:16.350 William Cheng: Okay, so therefore, again, we saw in the last time you address space you're only allowed to use the first three gigabytes. 79 00:10:16.830 --> 00:10:26.820 William Cheng: Okay, the audience's that might create some object in the last gigabyte. But you don't have access to it. So what the system will do is I will return a handle to you for you to refer to the object that has been created inside the Colonel. 80 00:10:27.900 --> 00:10:41.220 William Cheng: Okay, so here it says the handles are essentially an extension to the processes address space. Okay, so, so, so, so if you think about your process your process has an address space if you want to go beyond the address space, you have to use the handle. 81 00:10:42.990 --> 00:10:48.240 William Cheng: Then, and over here is also not common to say that what the handle over here can survive exacts 82 00:10:48.780 --> 00:10:54.660 William Cheng: Okay, so. So again, what would last time we talked about you like it is the beginning of today's lecture we talked about the exact system call 83 00:10:54.840 --> 00:11:02.910 William Cheng: What does the exhaust system called do. Right. So would you use that we make one of the exact system called you go inside the colonel inside the Colonel. We're going to wipe out your address space. 84 00:11:03.450 --> 00:11:09.750 William Cheng: Okay. Actually, I was being very, very careful when you come into the kernel you wipe out your address space. What about the extended address space. 85 00:11:10.500 --> 00:11:13.590 William Cheng: Okay. As it turns out, the extended address space is untouched. 86 00:11:14.340 --> 00:11:18.480 William Cheng: Okay. So, therefore, if you create any kind of a state information inside your extended address space. 87 00:11:18.690 --> 00:11:28.170 William Cheng: And we made the exact system call those things are not tied. So they so so in this case over here, we said they survived the exact okay after you make an exact system call you start creating a new address space. 88 00:11:28.590 --> 00:11:32.100 William Cheng: Okay, but the extended Outer Space still exists. So stay exactly the same. 89 00:11:33.060 --> 00:11:44.190 William Cheng: Okay, so today we're going to sort of talk about, you know, why do we need to do that. What kind of, you know, how we actually use that that that information in order for us to do interesting stuff inside the Colonel, right. All right. 90 00:11:45.480 --> 00:11:56.100 William Cheng: Okay, so let's talk a little bit about about about exactly what the fall is OK. So again, this is the UNIX fault abstraction, they're so used to obstruction a file is simply an array of bytes. 91 00:11:57.210 --> 00:12:00.870 William Cheng: Okay, so here again you know so again you read these words very, very carefully. 92 00:12:01.200 --> 00:12:08.340 William Cheng: Fall is an array of bytes. It doesn't say that's a real integers. It doesn't say it's a it's a real floating point number. It doesn't say the real object. 93 00:12:08.520 --> 00:12:15.660 William Cheng: It just, it's an array of bytes. Okay. So as far as unique system is concerned with Linux system is concerned, a file is simply not array of bytes. Okay. 94 00:12:16.050 --> 00:12:19.500 William Cheng: So are they signed or unsigned. It doesn't matter. They just a bunch of bytes. 95 00:12:19.980 --> 00:12:29.490 William Cheng: Okay, so if you think about you have a file that's 100 bytes long right over here is the 100 bytes law. Here's by zero by one by two, all the way to by 99 and that will be my file. 96 00:12:30.000 --> 00:12:40.170 William Cheng: Okay, my fault is simply an array of bytes. It's not a linked list advice. But as you know, simply are real bites there. Alright, so, so again, in this case, is there something called a text file. 97 00:12:41.130 --> 00:12:52.920 William Cheng: There's no such thing as a text file or files are binary files. Okay, so what's this is what what what what is the text file a text file, it's a binary file that just happened every character is a text character. 98 00:12:53.640 --> 00:13:01.560 William Cheng: Okay, so that's what the tax dollars right so so so those are typically we so we talked about it like it's a text file. But in reality, every file is a binary file. 99 00:13:02.490 --> 00:13:14.160 William Cheng: Right again very important to remember that. Yeah. And also, I guess what we're doing, warm up while we mentioned before, you know, like the file name the founding can find them extensions Unix and Linux system and the founding an extension means nothing. 100 00:13:14.970 --> 00:13:23.580 William Cheng: Okay. Because what will happen is that what you try to specify a file. All you need is a string you pass a stream to the operating system. The opposite will try to interpret that stream in the namespace. 101 00:13:23.790 --> 00:13:30.660 William Cheng: And if it turns out it is it you know that that particular strain match the sort of map to a particular file while then you open up our 102 00:13:31.140 --> 00:13:39.810 William Cheng: Okay, it doesn't really matter what the string look like okay alright so here's a weird thing the files or may larger by writing beyond the current no 103 00:13:40.440 --> 00:13:56.070 William Cheng: Okay, so if I fall that's 100 bytes long. How do I make this far. By far bigger. So what I can do is I can actually move the cursor to the end of the file right here to be pointing to buy offset 100 and if I put a character in here and now, this part is going to become 101 bites long 104 00:13:57.150 --> 00:14:01.950 William Cheng: Okay, so all I have to do is to to make the font bigger is to, you know, to write beyond the current end 105 00:14:02.490 --> 00:14:12.360 William Cheng: Okay, I can actually, if I can do this. I can actually make my cursor position, all the way down to 1 million buys later. And then I put one buys there and now the file is going to become 1 million or one baseball 106 00:14:13.530 --> 00:14:21.330 William Cheng: Okay so units is really easy to make a far bigger. All you have to do is to go to, you know, WHEREVER YOU WANT TO GO RIGHT ONE buy into it and now you're far is going to become much bigger. 107 00:14:21.900 --> 00:14:32.310 William Cheng: Okay, although you cannot replace the current net okay if I tried to read by one hundreds or here's by 0123 all the way to 99 if I try to read by 100 I'm going to get an error. 108 00:14:33.300 --> 00:14:38.220 William Cheng: Okay, so you can write beyond the current and but you cannot be beyond the current net okay that just how Unix 109 00:14:38.610 --> 00:14:50.130 William Cheng: You know the Unix operating system is okay. There are some others is operating system. They will do things in a different way. Rapid in this class. Again we mainly focus as a song sixth edition UNIX and this is basically how sixth edition units works now. 110 00:14:50.760 --> 00:14:59.970 William Cheng: All the files are name you know about by a path in the naming treat right so we care slash home slash BCS bins like whatever. So again, that's a path. 111 00:15:00.450 --> 00:15:02.520 William Cheng: So in this case, you know, the slash over here. 112 00:15:02.760 --> 00:15:10.530 William Cheng: Our directory. The limiters, right. So if you look at the slides over here. They need to be interpreted as a directory. So at the top of. Yeah, we have a root directory 113 00:15:10.710 --> 00:15:22.800 William Cheng: Inside the root directory. We have a subdirectory call home inside a subdirectory home. Over here we have another directory called bc inside BC. There's another directory called been inside been over here. There's a file called warm up one 114 00:15:23.430 --> 00:15:29.250 William Cheng: Guys, but again if you pass all this information to the fastest the fastest them actually figure out you know where these things are directories. 115 00:15:29.430 --> 00:15:35.400 William Cheng: And the last part over here is a file and everything is okay when then what it will do is it will open the file for you. And now you can start accessing the data. 116 00:15:35.880 --> 00:15:42.690 William Cheng: Okay, if there's anything that's wrong. We saw we saw last time. If there's anything wrong, the you know the the the the open 117 00:15:43.290 --> 00:15:48.660 William Cheng: I guess that the system was going to fail. And you need to check the air number to find out why did it fail right 118 00:15:49.380 --> 00:16:01.260 William Cheng: Right. The file system API we saw one of them already. Right. So there's for them. One is called open so open the first argument over here will be a fastest and pad is going to be a string and then the criticism, we interpreted 119 00:16:01.560 --> 00:16:06.840 William Cheng: You know, when the file, you know, we can actually open the file what he will do is that they will return a handle, back to you. 120 00:16:07.050 --> 00:16:16.020 William Cheng: Okay, so you're supposed to store the handle in a variable global variable local variable anywhere you want to store it and now all the other system calls over here. The first argument needs to be the handle. 121 00:16:16.410 --> 00:16:23.610 William Cheng: Right. So this way, once you open, if I can say I want to refund this file, I want to write to this file and eventually when you're done, you will close this particular file. 122 00:16:24.630 --> 00:16:31.500 William Cheng: Okay. So pretty much, you know, most of the operating system are like that when you try doing you know when you open a file you return a handle. And from this point on, you're going to keep passing the handle. 123 00:16:31.710 --> 00:16:35.520 William Cheng: Back to the ordinances and if you want to read or write that particular file, then 124 00:16:36.960 --> 00:16:41.610 William Cheng: Alright, the next one will be here says the system calls on files are synchronous 125 00:16:42.240 --> 00:16:48.360 William Cheng: So what does that mean, right. So we thought we would talk about synchronous, asynchronous. Unfortunately, computer science people like to reuse these terms. 126 00:16:48.780 --> 00:16:58.260 William Cheng: So in this case actually means something else. Okay system calls are faster synchro. So this means that these, you know, these functions will be here. They will not return until the operation is done. 127 00:16:59.520 --> 00:17:07.650 William Cheng: Okay, so these are called asynchronous system calls or a simple dysfunction call where you make this function called the function called the political is going to get stuck inside the cardinal for a while. 128 00:17:07.860 --> 00:17:15.540 William Cheng: And eventually, when the operation is done and then this case, you know, the, the, the, you know, the functional return. Okay, so for example when you try to write to a file. 129 00:17:16.290 --> 00:17:23.460 William Cheng: Okay, we want to write to a file this you know when you try to write to a file, this file I guess if this file is referred to some location on the desk. 130 00:17:23.910 --> 00:17:38.730 William Cheng: The rice system called will not return until the data get onto the desk. Okay. There are other other operating system or the other. They provide a synchronous system call. So in that case, will you make the racism call the racism of actual return right away. 131 00:17:40.200 --> 00:17:47.580 William Cheng: Okay so return right away so has the data gone to the days where you don't really know yet. So you need to have other mechanism to find out whether the data has gone to the desk already 132 00:17:48.150 --> 00:17:55.050 William Cheng: Okay, so that might be a little annoying units is pretty straightforward. You know what you call right you know when the function returning you know that that has gone to the desk. Yeah. 133 00:17:55.350 --> 00:18:05.760 William Cheng: And what about the racism called right well the racism called takes three arguments. The first argument is going to be the handle. Right. The second argument is going to be a buffer. Right. The third argument is going to be the size of the buffer. 134 00:18:06.090 --> 00:18:16.620 William Cheng: So if the size of buffer is at that means that you want to read at characters. Right. So in the Unix and Linux. When you make the recent call when re system called return to return to you with the success code. 135 00:18:17.100 --> 00:18:19.830 William Cheng: Guys are in that case, you know that data is in the buffer already 136 00:18:20.700 --> 00:18:29.310 William Cheng: OK. So again, there are other opportunities and where they provide an asynchronous racism call so often. Is that what you call, read, read, actually returns right away. But there's no data in the buffer. 137 00:18:30.000 --> 00:18:37.770 William Cheng: Okay, so when is the buffer getting filled well. So again, you need to sort of learn how to use those operating system. So those systems alone is going to be a little harder to you to us. 138 00:18:38.250 --> 00:18:46.920 William Cheng: Because at the time when he returns. There's no data. So you need to have other mechanisms to say hey you know when the data is ready, let me know. So this way we can actually go reach the goal. Go get go get the data. 139 00:18:47.850 --> 00:18:58.170 William Cheng: OK, so again for Unix and Linux open read right close all these, you know, all these file system related you know system call, they are all synchronous. Yeah. When the function returns you know the operations. 140 00:18:59.010 --> 00:19:12.270 William Cheng: Guys, okay, we're talking about this. So the autism have been synchronous is for these for, you know, for, for, for, for these for file system related system calls. Okay, there might be other system call that that might be asynchronous right 141 00:19:15.480 --> 00:19:23.400 William Cheng: So let's take a look at example how to use the you know handles and unit four units. They actually have a special name for handles. There are called file descriptors. 142 00:19:23.850 --> 00:19:33.750 William Cheng: Okay so far the script over here. There are small integers. Typically, they go 0123 and, you know, the typical is a small number of guys a little I'm going to sort of see exactly what this will do is 143 00:19:34.260 --> 00:19:39.510 William Cheng: You try to open a funny windows. The windows when you open a file, what it will return to you is actually something called a handle. 144 00:19:40.080 --> 00:19:50.700 William Cheng: Okay, so in Windows, they call the handle in Munich system and they give a special name to the file system handle they call a file descriptor. And then the file descriptor are small managers. Yeah. So, here 145 00:19:51.030 --> 00:19:56.220 William Cheng: Typically the variable that we use is called F D amp D stands for file descriptor. So here's an integer at D. 146 00:19:56.640 --> 00:20:00.870 William Cheng: And this way when we make an open system called we can store the return value into FD there. 147 00:20:01.230 --> 00:20:06.030 William Cheng: Alright so what we'll do is we're going to make an open system call right open. The first argument is going to be a file system path. 148 00:20:06.210 --> 00:20:14.280 William Cheng: Again, it's just a C Street with passing the operating system. So the appearances and then try to locate this file or try to interpret the stream inside a namespace. Yeah. 149 00:20:14.580 --> 00:20:18.540 William Cheng: Alright, so obviously the second argument over here is to say, how do we want to open this file. 150 00:20:18.780 --> 00:20:26.610 William Cheng: Okay, you can open up a read only if you just want to read from it. You can open a file for right only if you just want to write to it. You can also open a file for read and write. 151 00:20:27.060 --> 00:20:29.670 William Cheng: So in this example, we open a pop up both reading and writing 152 00:20:30.180 --> 00:20:34.020 William Cheng: Okay, so at this point, you've got, you know, open is the system call you type into the opportunities and 153 00:20:34.230 --> 00:20:43.140 William Cheng: The opportunities that will try to figure out whether this fall exist or not. If this file, it doesn't exist, it will return to you he will return a minus one, right. So this way, there's an error. 154 00:20:43.740 --> 00:20:55.830 William Cheng: If this file system that actually exists, but if it turns out that you don't have the right kind of privilege that you are not allowed to open this file for reading and writing, why not here's again. IT WILL RETURN RETURN RETURN Erica. 155 00:20:56.310 --> 00:21:03.330 William Cheng: Okay, so not example over here, what we're gonna do is, I'm going to call the open system call. When we return, we're going to store the return value over here into this 156 00:21:03.690 --> 00:21:12.780 William Cheng: This file descriptor variable. And we're going to check it is equal to minus y is equal to minus one, that means that they fail. So in this case, again, what do you do when open system called fail. 157 00:21:13.350 --> 00:21:19.680 William Cheng: Okay, so just like warm up one and later only going to see what I'm up to whenever you encounter an error you print an error message. You could your program. 158 00:21:20.490 --> 00:21:30.720 William Cheng: Okay, so we're here, we're gonna do is, I'm going to use this P error PR. I think the C library calls not really a system called so what it will do is that it will print an error numbers specific error message. 159 00:21:31.380 --> 00:21:39.390 William Cheng: Okay, so in this case if open system called fail. All I need to do is that when I call P error. I need to supply the name of the file system path that failed. 160 00:21:39.840 --> 00:21:49.830 William Cheng: OK. So again, this will be the first argument to obey. So I'm going to call P error, you know, slash home species large file was in this case is going to print an error message that are specific to the air number 161 00:21:50.220 --> 00:21:58.470 William Cheng: Okay. So in this case, you know, if the file is the pattern executives, they found, I found if it turns out I don't have the right privilege, then it will say, you know, that 162 00:21:59.340 --> 00:22:07.770 William Cheng: That the access denied or something like that. So again, what's important. Over here is if you write your code like this, it will always print a very, very specific error message. 163 00:22:08.130 --> 00:22:18.870 William Cheng: Okay, and that's exactly what you want. Right. You don't really want us to have a higher high level kind of a you know a confusing error message you want it to be a specific a specific as possible. So in our case the right thing to do, or we here's us Pierre 164 00:22:19.350 --> 00:22:25.350 William Cheng: Okay, so I seen in the previous semester, what people will do is that when a when a system called succeed, they still use Pierre 165 00:22:25.710 --> 00:22:31.170 William Cheng: That doesn't work that way. Okay, so I've been is that when the system called fail, you have to use Pierre immediately. 166 00:22:31.680 --> 00:22:37.230 William Cheng: Okay. Otherwise, if you make another system called the other system called fail. What is going to is going to mess up the air number 167 00:22:37.710 --> 00:22:46.350 William Cheng: Okay, so at the time when you fail, you need to print an error message and then immediately you need to quit your program. So in this case, the creature program you make the exorcism call 168 00:22:46.860 --> 00:22:56.700 William Cheng: Okay, so by Unix convention x is zero, meaning that everything's fine access anything other than zero that means something has gone wrong. Okay. So in this case, I'm going to exit one just did something has gone wrong. 169 00:22:57.330 --> 00:23:09.630 William Cheng: Okay. All right. If it turns out open city or was successful. So in this case, the FD value of here is going to be the file descriptor. It's going to be a small integer 012345 or something like that. And we're going to store them into this variable. 170 00:23:09.870 --> 00:23:15.330 William Cheng: OK, so now when we try to perform the read operation. The first argument is going to be this handle is going to be the file descriptor. 171 00:23:15.840 --> 00:23:21.030 William Cheng: Okay, so, so, so over here. What we do that we're going to perform the racism called we said we're going to read from this file. 172 00:23:21.480 --> 00:23:33.600 William Cheng: In this example, it might be equal to three, maybe four to five or some small number. So we're gonna say re from found number. In this case, it's three. So we're going to say read from phone number three and three has to be a return value from open 173 00:23:34.080 --> 00:23:43.320 William Cheng: There and then we specify a buffer this buffer sizes. So the last argument should be the size of the buffer. Because if it's bigger than the size of the buffer that you can end up with buffer overflow. 174 00:23:43.920 --> 00:23:52.500 William Cheng: Okay, so you got to be very careful. So typically, the last number over here we're going to use size of right we saw this before size of buffer right so in this case to be exactly 1044 175 00:23:53.580 --> 00:24:00.270 William Cheng: All right, so the resistance. Go over here when it was when it's successful is going to return the number of bytes that has been put into this buffer. 176 00:24:00.780 --> 00:24:05.100 William Cheng: So again, where you make the racism call you traveling to the ordinances there and the 177 00:24:05.280 --> 00:24:13.140 William Cheng: System will say that, oh, here's a buffer. You want the data to be fill. So what it will do is that I will go to the file and re some data for you. And then he will put into this buffer. 178 00:24:13.320 --> 00:24:19.950 William Cheng: And then when they return back into your user space program what he will do is that I will set the return value to be the number of bytes it put inside a buffer. 179 00:24:20.850 --> 00:24:27.870 William Cheng: Okay, so this buffer is 1024 bytes long, you know, if the criticism only put at bites into the buffer. So in this case, return value over here will be at 180 00:24:28.290 --> 00:24:35.730 William Cheng: Okay, we're going to store the return value in the local variable. We are just took the call count so count. Tell us how many bytes of data that was put inside the buffer. 181 00:24:36.360 --> 00:24:46.500 William Cheng: Okay, so, so, so, so, so, so, so in this case, you got to be very, very careful in the return value is at the buffer is 1024 bytes law. We should only look at the first at bites. 182 00:24:46.860 --> 00:24:59.910 William Cheng: Okay, why is that, well, because the other the other data over here the evidences of didn't put any data in there. So if you want to, you know, if you want to access the data young, the first at bites you might be. You might be, you know, sort of, you might be reading garbage. 183 00:25:01.350 --> 00:25:07.530 William Cheng: OK. So again, it's very, very important to look at the return value of read it will tell you how many bytes in the buffer has been modified 184 00:25:07.770 --> 00:25:18.360 William Cheng: Okay, and therefore you should only look at that many bytes, where if it turns are returned minus one. So again, you can use P errors and this guy is Pierre, the argument over here is going to be the name of the system call 185 00:25:19.650 --> 00:25:26.820 William Cheng: OK. So again, depends on which system call you know when when it fails. Again, you need to play around the PHP era to sort of know you know what value, you need to put in 186 00:25:27.120 --> 00:25:34.530 William Cheng: There are so this guy's you put the name the system call it was a system called failed and then again you equal access right away because that's where we're supposed to do. 187 00:25:35.310 --> 00:25:44.220 William Cheng: That otherwise in this case. So what happened is that if the real function returns at and I mentioned about the cursor position right instead of fall over here. So let's say you have really, really long file. 188 00:25:44.430 --> 00:25:56.340 William Cheng: For some reason the opportunities that only return the first ad bites right so 012 all the way to 70 died over here. So what do we do that about copy all this data into the buffer. Right. And then he will leave the cursor position fit. 189 00:25:57.420 --> 00:26:06.450 William Cheng: Okay, so next time when you call read again, it's going to start from offset at over here and get some more data and then ago. Move the cursor position. So they will keep remembering where you are, you know, 190 00:26:06.870 --> 00:26:10.170 William Cheng: Inside you extended address space. I see in the past. 191 00:26:10.830 --> 00:26:19.470 William Cheng: Semester. Some people are they returning some really weird Co. So when they try to read a particular file. So what they will do is that it will read a line and that's like a warm up one, they will rely on the input. 192 00:26:19.680 --> 00:26:30.480 William Cheng: And then what it will do is that it will try to try to make another system call find out where the cursor position is. And then they will move the cursor position to where the submission is already, and then it will call call call read the next slide. 193 00:26:31.350 --> 00:26:43.710 William Cheng: Okay, so now you know you don't have to do that right because inside the operating system. You will remember for you where the current position is. And if you continue to call REE REE REE REE REE it will keep reading by picking up data from where you left off. 194 00:26:44.370 --> 00:26:54.420 William Cheng: Okay. And the reason, again, is that is that because your execution context part of your execution, how that is stored inside the Colonel. The Colonel knows you know where you are, you know, in this particular file right 195 00:26:56.400 --> 00:27:02.370 William Cheng: OK. So again, here's the example you can keep reading and reading and eventually when you're done, you need to make the closest them call 196 00:27:02.550 --> 00:27:13.680 William Cheng: Will you said closest them called the close to them, call only take one argument, and that will be the handle. So again you fight you pass the file descriptor say I'm done with this file I'm closing this file. So for this one out. If I try to read the same 197 00:27:14.070 --> 00:27:19.800 William Cheng: Using the same file descriptor. Then I then then then I shouldn't I you know I i should be expecting you know air. 198 00:27:21.030 --> 00:27:23.010 William Cheng: Should be expecting air conditions. Yeah. 199 00:27:25.740 --> 00:27:30.390 William Cheng: Alright, so the the file descriptor. As it turns out, the three of them are special. 200 00:27:30.780 --> 00:27:39.840 William Cheng: Okay. So as it turns out, you know, get file descriptors that are small integers 01 and two they have special names for them. Okay. Zero is Stan is also know a standard in 201 00:27:40.290 --> 00:27:44.280 William Cheng: One is called is also known as standard. Oh, and two is also know a standard error. 202 00:27:44.490 --> 00:27:54.270 William Cheng: Okay, so I've been using that term you warm up wine that's a you will try to read it from standard and well now you know exactly what's to enter in this okay standard. It just, if I the spouses, go to zero. 203 00:27:54.780 --> 00:27:59.370 William Cheng: Okay. So over here, it says, by default, that means that if you don't do anything funny to your program. 204 00:27:59.970 --> 00:28:11.790 William Cheng: You know when you progress starts. If you try to read from, you know, if you try to reframe file descriptor zero, or will you try to read from standard in then in this case file descriptor zero is map and connected to the keyboard. 205 00:28:13.260 --> 00:28:21.000 William Cheng: Okay, I'm putting the word map and connect to the keyboard, the map and connect over here in double quotes. Right. So, so, because I'm not going to explain exactly what that means. 206 00:28:21.540 --> 00:28:27.390 William Cheng: Okay, because mapping and connecting. This is a very, very complicated process when we go to chapter six. We're going to see exactly how this is done. 207 00:28:27.720 --> 00:28:32.880 William Cheng: Okay, when you're doing your kernel assignment number two, you're going to actually make that connection, you're going to make that mapping 208 00:28:33.180 --> 00:28:38.250 William Cheng: Okay. So until we get to them. We don't know exactly what it is. So right now we're sort of using this this loose term. 209 00:28:38.520 --> 00:28:47.310 William Cheng: Followed the script to zero is connected to the keyboard. So if you think about your keyboard is right here. Here's a keyboard. I would love keys on that that file descriptor zero or standard in 210 00:28:47.610 --> 00:28:53.670 William Cheng: You know it's gonna, it's going to map to the keyboard. So there's some kind of a connection, all the way all the way to the bottom to connect to the keyboard. 211 00:28:54.600 --> 00:29:03.990 William Cheng: Okay, this particular path is going through the operating system is going through the fastest there you might go through many, many data structure inside the kernel and eventually you will lead to the keyboard. 212 00:29:05.250 --> 00:29:15.030 William Cheng: OK. So again, how do you get there. Well, we don't you know at this point we don't really know. Okay, but when we get to Chapter six, you're going to see the connection when you're doing your current assignment, you're going to see exactly how this one eventually go to the keyboard. 213 00:29:15.750 --> 00:29:23.490 William Cheng: Okay, so right now we just said I, you know, again, when we say mapping. Well, what does a map right so again map is like a sort of a map, when you try to find the address. 214 00:29:23.730 --> 00:29:28.860 William Cheng: We will try to find the address you look at a map and then you try to sort of figure out how to go somewhere. OK. So again, 215 00:29:29.430 --> 00:29:35.820 William Cheng: Here's a way. So here's a map for you to actually go somewhere. We don't know exactly how it's done. At this time, but trust me, you know, later on we're 216 00:29:36.090 --> 00:29:48.150 William Cheng: Going to learn how this is, is that right, okay. So, so far it's good to zero or standard in map to the keyboard. They also connect to the keyboard. How does it connect to the people. Right. Here's a really, really long connection. Eventually, we'll get to get to the keyboard. 217 00:29:49.440 --> 00:29:55.230 William Cheng: guys again for the scuba zero standard in by default map connected keyboard their 218 00:29:55.650 --> 00:30:02.820 William Cheng: First discovered. Number one is, no, no. A standard out. So by default, again, if you don't do anything to it. It's going to map and connect to the display. 219 00:30:03.210 --> 00:30:11.970 William Cheng: Whereas, which this way. Right. So if you're on a console window where you try to run one offline that console window is your display as over here. I'm going to see a monitor over here, you know, with 220 00:30:13.230 --> 00:30:22.020 William Cheng: With text getting displayed over here. So by default file descriptor one if you write about this good news because there's a description descriptive why data is going to appear on the screen. 221 00:30:23.370 --> 00:30:30.690 William Cheng: Okay, so again if you read from file descriptor zero, you're going to be reading data from the keyboard. If you write at the file descriptor while you're going to see data show up on the screen. 222 00:30:31.470 --> 00:30:38.400 William Cheng: Okay, so again we're going to use the word map and connect. We don't know how you get there, you know, multiple levels of indirection. You know, eventually going to get there, they're 223 00:30:39.180 --> 00:30:50.070 William Cheng: Far just kind of a number two is no standard error by default there map connected to display. So if he tried to, you know, right data to standard error. The error message will also will also show up on the screen. 224 00:30:51.240 --> 00:30:58.140 William Cheng: Okay, so again, standard out of here is your standard output. So each other. I'll put something he's ready to standard out if you try to write error messages you should write to stand air. 225 00:30:58.740 --> 00:31:01.980 William Cheng: Okay, so I think you have seen these kind of wording, you know, in the 226 00:31:02.760 --> 00:31:13.590 William Cheng: In the warm one speck ok so now you sort of. No, no, no, no. Exactly what they mean right standard, it means file descriptor zero standard army file descriptor. One is tender era means file descriptor. Number two, okay. 227 00:31:14.280 --> 00:31:17.520 William Cheng: Alright, so here's the program as. Let's see what this program does. 228 00:31:17.760 --> 00:31:29.550 William Cheng: This program will be here at the main function over here. There's an infinite loop. So what I would do is I will read from file descriptor zero. I don't have to open file descriptor zero because by default. They are open already right 012 they're always open. 229 00:31:29.940 --> 00:31:41.250 William Cheng: Okay, unless you close it otherwise they're open to zero. So, if you have to read from zero you're reading from the keyboard or you can read so so so I guess people reading for standard in we saw in one month. Why, you can also make that brief on the file. 230 00:31:41.880 --> 00:31:49.800 William Cheng: Okay, so in this case we're going to reverse data input and then we're going to put into the buffer over here and then the number of bytes that we're going to use this is going to be the size of the buffer right 231 00:31:50.310 --> 00:31:59.520 William Cheng: So the size of Barbara. We have bigger. So we don't really know. It doesn't really matter. This might be at this might be you know 1024 or whatever. So we try to read a buffer full data. 232 00:32:00.240 --> 00:32:05.460 William Cheng: Okay, so the first line over here, which I did read a buffet full of data and then the return value over his going to tell it. 233 00:32:05.880 --> 00:32:09.510 William Cheng: Tell us how many bytes, the operating system had put it put data into the buffer. 234 00:32:10.020 --> 00:32:19.950 William Cheng: Okay, so let's say that the buffer size of your could be at. And the reason this case the return value over it could be 10 I only put 10 bites into into the buffer. So therefore, I'm only allowed to touch the first 10 bites. 235 00:32:20.370 --> 00:32:30.900 William Cheng: Okay, so in this case if the number of here is bigger than zero that means the opposite has put some data into the buffer. So in this case, I'm going to write to standard out right be this file descriptor one over here standard out 236 00:32:31.140 --> 00:32:40.110 William Cheng: I'm going to write to standard out and then again the data over here is going to stop on the buffer, but in this case I don't write the entire size of the buffer out right. Why is that well because you know 237 00:32:40.710 --> 00:32:44.790 William Cheng: If anything, go to 10 over here, only the 10 the first 10 bites was valid data. 238 00:32:45.600 --> 00:32:54.060 William Cheng: Okay. So, therefore, what I'm supposed to do over here is to write the number of data. I just referenced entry and I'm going to read them all out and then I will compare the return value against an 239 00:32:54.750 --> 00:33:02.100 William Cheng: OK, so the racism call when the function returns successfully, it will tell you how many bytes has been written out to, you know, in this case, standard out 240 00:33:03.600 --> 00:33:09.570 William Cheng: Okay, so if I'm trying to write 10 bites out. Well, I should expect that the return value should be equal to 10 right otherwise something's wrong. 241 00:33:10.770 --> 00:33:16.260 William Cheng: OK. So again, I tried to write 10 bites out the return value better be 10 if the return value is not equal to 10. What do I do 242 00:33:16.470 --> 00:33:23.280 William Cheng: I write two standard error with no note over here says right failed. And again, I print out error message over here. And then I quit my program. 243 00:33:23.790 --> 00:33:29.910 William Cheng: Okay, so over here. Exit failure over here. Maybe one maybe minus one doesn't really matter what it would matter what it is, as long as it's not equal to zero. 244 00:33:30.630 --> 00:33:40.560 William Cheng: Okay. Otherwise, you know, the otherwise if I was successful. And then the number of bias that the road out over here is equal to the number of bytes i right 245 00:33:40.950 --> 00:33:55.830 William Cheng: Okay, so then in that case, I'm going to go to the standard input read some more and then write some more recent Marissa Marissa, Ma, Ma, eventually the racism was going to return a value equal or less than zero. Okay. So, in that case, what it means is that I have reached the end of the file. 246 00:33:57.570 --> 00:34:09.810 William Cheng: Okay, so when the racism called return a zero or negative number. So they will never take it typically means there's an error. So when you return to zero or on an error, one in that case it means that I have no more data is there. Don't know what data for me to read 247 00:34:11.100 --> 00:34:17.400 William Cheng: Okay, so remember read over here. It's a blogging system call right so what it will do is that we make the racism call you're going out organiser the organiser 248 00:34:17.670 --> 00:34:24.360 William Cheng: Is going to go to the File and then get data for you if it turns out there's no more data. Well, then, what it will do is that a return value less than or equal to zero. 249 00:34:24.990 --> 00:34:29.850 William Cheng: Okay, so in that case you jump out of the infinite loop and then you return access access and this one will be equal to zero. 250 00:34:31.830 --> 00:34:38.370 William Cheng: Okay, so what does this program do right, it just refreshed standard in and then right at the center out. Okay. The name of this program is called cat. 251 00:34:39.060 --> 00:34:45.030 William Cheng: Okay. So, therefore, you know, when you try to write you seen calorie in warm up one, it does something slightly different. 252 00:34:45.480 --> 00:34:57.600 William Cheng: Okay, so, so, how so so we use a cat. You know, I guess, you know, F0 or something like that. What do you do is it will open this particular file and instead of reading from file descriptor zero it will read from that file descriptor. 253 00:34:58.140 --> 00:35:04.260 William Cheng: OK, so the rest of the code over here is going to be the same thing as what what's going cat. Okay so cat is very, very simple program. 254 00:35:04.740 --> 00:35:12.840 William Cheng: So then the meaning of this program is concatenation, because what it will do is that I will actually concatenate data from from the input and the printer to the output. 255 00:35:14.370 --> 00:35:17.370 William Cheng: OK. So again, it's a very, very simple program you saw the code already 256 00:35:17.640 --> 00:35:27.630 William Cheng: In the future, if you ever. If you ever found him argument over here, then all we need to do is add another line of code to open a file and then we're going to take the other the return file descriptor, and then pass it to the racism called 257 00:35:28.650 --> 00:35:34.410 William Cheng: Alright, so that's the person is purpose of cap. OK. So now you know what what it's doing. And one more. One. Okay. 258 00:35:36.990 --> 00:35:44.460 William Cheng: Oh, I better our price program our program, you know, computer, you know, a bunch of prime numbers over here and now you need to have them print the values out 259 00:35:45.000 --> 00:35:49.140 William Cheng: So at the end over here, we can do that will be here. There's a roof to compute all the prime numbers. 260 00:35:49.350 --> 00:35:59.400 William Cheng: India, we can use the racism call racism, we're going to write your standard out and then the problems over here has a bunch of numbers over here. So what we can do is, I'm going to make the racism call and write all those number out to standard out 261 00:36:00.390 --> 00:36:04.020 William Cheng: OK. So again, I'm going to check the error code to make sure that it's, there's no error. 262 00:36:04.470 --> 00:36:11.790 William Cheng: So in this case, as it turns out, if you do something like that. You can try it and you will find out that you know what's printed onto the screen is actually unreadable. 263 00:36:12.690 --> 00:36:17.760 William Cheng: Okay. The reason for that is that the prime numbers over here. They're integers. They are not string representation 264 00:36:18.390 --> 00:36:23.100 William Cheng: Okay, so when you need to print strings onto the screen. What do you have to do, right, what you have to do that, you have to call print if 265 00:36:23.400 --> 00:36:29.310 William Cheng: Okay, if you look at the main page of printer, the memory page apprentice as the purpose of printer is to format the output. 266 00:36:29.850 --> 00:36:34.170 William Cheng: Okay. So, so what does it mean to format the other is format the output for human reading 267 00:36:34.650 --> 00:36:43.260 William Cheng: Okay, if you use the racism call you don't forget the outcome you just print it, but the value directly, you're gonna look like garbage. But if you format and carefully. What, then, you know, people can read it. 268 00:36:43.770 --> 00:36:49.440 William Cheng: Okay. So, therefore, if you just call right the result might not be readable by humans were you supposed to do is the following. 269 00:36:49.650 --> 00:37:00.000 William Cheng: Four i equal to zero, i less than prime over here and we're going to use the printer function. I'm going to, I'm going to go to every integer over here prime is going to be going from zero all the way to 300 all the way to 199 270 00:37:00.270 --> 00:37:13.590 William Cheng: And then when you use a credit function just a format this integer as a ASCII string and then I followed by backslash. And so this way you're going to see the output 2357 that are all the way to the 300 points. 271 00:37:15.840 --> 00:37:18.060 William Cheng: Okay, so, so again, in this case over here. 272 00:37:18.480 --> 00:37:19.740 William Cheng: You know, if you're doing a printer. 273 00:37:19.980 --> 00:37:29.880 William Cheng: To print out over here, they're actually using something called the file pointer. So, so you are not using the racism call because the racism call is not very user friendly, right, because we saw before. If you use racism called over here. 274 00:37:30.120 --> 00:37:36.840 William Cheng: While you won't be able to tell what's going on. OK. So the idea that we would like to function to use printer right because printer is 275 00:37:37.320 --> 00:37:40.470 William Cheng: Is very convenient if you want to print something for human three 276 00:37:41.070 --> 00:37:47.700 William Cheng: Okay, so in that case the printer is going to use some kind of a, you know, data structure that we're gonna we're gonna call these data sources default pointer. 277 00:37:48.090 --> 00:37:56.460 William Cheng: Okay, so if you read a man page against you probably use F gets or something like that. So, F, F gets at get string or something like that. 278 00:37:56.970 --> 00:38:01.320 William Cheng: So what are the arguments over here. It's going to be file F i l e capital F g star. 279 00:38:02.070 --> 00:38:09.390 William Cheng: Okay, so that's called a file pointer. So what's the file pointer. Right, so follow pointer is a rapper data structure around the file descriptor. 280 00:38:10.320 --> 00:38:11.310 William Cheng: Okay, so when you call 281 00:38:11.670 --> 00:38:24.030 William Cheng: If what you're doing is that you're using a file pointer data structure. And that's a rapper data structure around file descriptor. Number one, right, because number one over here standard out so we could print out your writing center. So therefore, in this case, there's a 282 00:38:25.230 --> 00:38:37.140 William Cheng: There's a 4.8 you can use is called STD out there if you use STD, while the SCD is a rapper data structure around the file descriptor. Number one, so will you call if you will be writing to standard out 283 00:38:38.070 --> 00:38:45.480 William Cheng: Yeah. Alright, so please read the programming FAQ to sort of tell you the difference between the file descriptor. And if I'll pointer. Again, the bottom line is that 284 00:38:45.660 --> 00:39:00.510 William Cheng: The file pointer data structure. It's a rapper data structure around a file descriptor. Okay, so this way you can use these nicer function to read to read a line for the keyboard or rely on a file and then pulling the data out on the screen. So the human can read all these data. Okay. 285 00:39:03.510 --> 00:39:14.250 William Cheng: All right, so, so, so, so what did this the audio file descriptor come from. Alright. So as it turns out, for Unix and Linux. For every process. The Colonel maintains what's called a file descriptor table. 286 00:39:14.580 --> 00:39:23.250 William Cheng: Okay, so there's a colonel in. So there's a Danish are things that are Colonel called the file descriptor table. I'm going to abbreviate that to be file descriptor table at dT over here. 287 00:39:23.580 --> 00:39:25.650 William Cheng: Okay, the file descriptor table over here. 288 00:39:25.950 --> 00:39:37.200 William Cheng: They are array of pointers get which is an array of pointers to fall objects. So, this one will point to a file objects over here is a file object right here. And what's a file object to fall off days a kernel data structure that represents five 289 00:39:37.770 --> 00:39:44.310 William Cheng: Okay so far. The script is zero over here, this will be appointed that point to the file object that represent the keyboard. 290 00:39:45.120 --> 00:39:54.660 William Cheng: Well guess what, again, what does that mean to represent a table right so if you if you follow the file object over here the theological viewpoint, or something else and it will point to something else. We wanted something out and eventually it will point to the keyboard. 291 00:39:55.320 --> 00:40:01.350 William Cheng: Okay, so that's what I mean by mapping to the keyboard or connect to the keyboard. Right. So the first point over here. We know what it points to 292 00:40:01.500 --> 00:40:08.910 William Cheng: It points to follow directions that Colonel and then from that point on, we don't know where points to anymore. It will be will follow. Many, many pointers and eventually points of keyboard. 293 00:40:09.360 --> 00:40:15.120 William Cheng: Yeah. Similarly file descriptor number one over here is going to be, you know, again, for the school and I'm why it's an array index. 294 00:40:15.450 --> 00:40:28.440 William Cheng: OK, so again you know 012 there are a index. So, so instead of the scooter table. The second pointer over here index number one will point to another file object right this file object. If you follow the pointer, eventually it will point to the display. 295 00:40:29.310 --> 00:40:38.640 William Cheng: Okay re index number to over here. There's another point is that point to another file object and this file object. If you again if you follow the pointer, eventually it will point to also appointed it this way. 296 00:40:40.650 --> 00:40:53.520 William Cheng: Alright, so every fall out there will be here. So what does it fall object represented it represented a file that has been open. Okay, so if you see a file object that means that this particular file is open. So if you want to close the file. Once you call it the fall, the fall. I will disappear. 297 00:40:54.780 --> 00:41:01.710 William Cheng: Okay, so every fall object represent open file and the file object is eternal data structure that represent a file that has been opened for you. 298 00:41:02.220 --> 00:41:05.820 William Cheng: There and an file descriptor is simply an array index. 299 00:41:06.480 --> 00:41:12.150 William Cheng: That. So when you try to tell the ordinances and say, Hey, you know, could you read some data for me from file number five. 300 00:41:12.330 --> 00:41:21.750 William Cheng: What does that mean, right. You go to the father's company instead of current oh 012345 and five over here will be appointed appoint yourself out loud. Yeah, you've got to go to that file object and get the data for me. 301 00:41:22.350 --> 00:41:27.780 William Cheng: Okay, or if you try to perform the right operation, you want to write data, you know, into that into that into phone number five. 302 00:41:29.070 --> 00:41:32.670 William Cheng: Alright, so this is the sort of this video. This is the meaning of a file descriptor. 303 00:41:33.000 --> 00:41:40.050 William Cheng: OK, so again for Unix and Linux system. The file descriptor has very, very simple definition to because the abstraction is that inside the Colonel. 304 00:41:40.320 --> 00:41:47.160 William Cheng: Every process has a file descriptor table and the file descriptor is simply an array index into that interval discovery table. 305 00:41:47.700 --> 00:41:59.460 William Cheng: Okay, how big is the file descriptor table while in the good old days in sixth edition Unix. I think the file descriptor table has 32 entries or to go from zero all the way to 31 and that's why they're always a small integer. Okay. 306 00:41:59.850 --> 00:42:09.630 William Cheng: Today's Linux system. I think they have 1024 entries over here. So the file descriptor, you know, the file descriptor can have value 0123 all the way to 1023 yeah 307 00:42:10.470 --> 00:42:20.010 William Cheng: All right, whenever across as a request a new file descriptor. So what does that mean require a new file descriptor at one, this guy, he, he called open, you try to create a new file. 308 00:42:20.490 --> 00:42:29.910 William Cheng: Okay. For Unix and Linux operating system inside the operating system when when they try to allocate an entry instead of file descriptor table. They have to follow this rule that I'm going to talk about 309 00:42:30.360 --> 00:42:32.190 William Cheng: Okay, so again this is a very, very important rule. 310 00:42:32.670 --> 00:42:41.160 William Cheng: If you don't, you know if if I, if this isn't doesn't follow this rule, then it's not Linux and Unix okay if there are links when they're Unix, they have to follow this particular rule. 311 00:42:41.430 --> 00:42:56.070 William Cheng: Okay, so the rule over here says the following day. So whenever a process request a new file descriptor. You can open, which are created to, to, to try to create a new file object, the lowest number file descriptor not already associated with the open file must be selected. 312 00:42:57.270 --> 00:43:03.780 William Cheng: Okay. So in this case, how do you implement that. Right. So again, instead of file descriptor table over here. I got here is the file descriptor table over here, there are 313 00:43:04.110 --> 00:43:11.850 William Cheng: A bunch of pointers over here right so far discourage 012 they all point to something. So let's say that three over here. It doesn't point anything it pointed a null pointer. 314 00:43:12.030 --> 00:43:23.070 William Cheng: And for over here point or something and follow your point is something okay according this rule, if you call if you can open it open was successful, you're guaranteed to reach to return file descriptor number three. 315 00:43:24.210 --> 00:43:31.500 William Cheng: Okay, because Unix and Linux. They will always follow this rule, they will find the lowest number file descriptor that's currently available and they have to allocate that 316 00:43:31.830 --> 00:43:36.900 William Cheng: You guys are getting consumer see the algorithm that you have to implement over here. So in Colonel to you actually have to implement this rule. 317 00:43:37.410 --> 00:43:40.710 William Cheng: Okay, so what you need to do is I need to go down the file descriptor table over here. 318 00:43:40.890 --> 00:43:52.140 William Cheng: Check if every entry is equal to zero. The first entry that you found is not equal to zero. So the first entry that you found is equal to zero or equal to, now that will be that will be that will have to be the one that you would do. Okay. 319 00:43:53.100 --> 00:43:59.940 William Cheng: Okay, because you have to follow the rule. If you're Unix or Linux there. Alright, so let's take a look at this code and see what what is called you 320 00:44:00.540 --> 00:44:03.510 William Cheng: Okay, so this code over here, I will close file descriptor zero 321 00:44:04.050 --> 00:44:14.880 William Cheng: Right. So again, when I close for the scripture zero here zero here used to point to a file object and this file, I'll get will point, all the way to the keyboard. So when I close file descriptor zero, what's going to happen. 322 00:44:15.180 --> 00:44:22.800 William Cheng: Right over here, this connection was going to get separate over here and then this fall RJ will disappear. Okay, so now for the group 00 point now. 323 00:44:24.090 --> 00:44:29.610 William Cheng: Okay, so when I made the closest them call over here. Father groups are zero now will point to will become a null pointer. 324 00:44:30.660 --> 00:44:39.180 William Cheng: There. And then if I make the offices of call and I want to open a system work for read only if this open system called successful, what will be the return file descriptor. 325 00:44:39.660 --> 00:44:50.670 William Cheng: Well, according to the rule right here. It has to return to zero, right, because I just talked about. This has to be the rule. So if you just close to zero. And I hope and right away you guarantee that this world will return a file descriptor zero 326 00:44:52.320 --> 00:45:01.620 William Cheng: Okay. So in this case, you know, why is this value return zero over here. Now you zero. What were the point to right it's going to point to a file object that are associated with this particular file. 327 00:45:03.000 --> 00:45:11.400 William Cheng: Okay, so this is how you disconnect file descriptor zero from the keyboard and now you reconnect them you reconnect file descriptor zero to this file. 328 00:45:13.560 --> 00:45:24.360 William Cheng: OK. So again, what I mentioned before is that by default file descriptor zero. They are mapper connect to the keyboard. But if you close that one in this case you could actually open it again. When you open it again and now it will connect to something else. 329 00:45:24.810 --> 00:45:38.280 William Cheng: Okay, so in this case it will connect to a different file. Okay so Unix and Linux. You can actually do something like that. Yeah. Alright. So. So the code above over here will always associated file without descriptor zero because of the rule that I just talked about. Okay. 330 00:45:40.440 --> 00:45:43.530 William Cheng: Alright, so let's take a look at this this code example to see what what happened. 331 00:45:43.830 --> 00:45:49.200 William Cheng: There. So now again I'm going to sort of combine the process you know system column here with the fastest and call 332 00:45:49.320 --> 00:45:59.490 William Cheng: So what I would do is I will make the fourth system call here. Check it equal to zero is equal to zero this code over here will be executed by the child process. And then again, again, what would a parent process do well again and this 333 00:46:00.060 --> 00:46:04.380 William Cheng: This code over here will do exactly the same thing as before the parent process will wait for the child process to die. 334 00:46:05.610 --> 00:46:13.950 William Cheng: So now instead of child process again when you make the fork system code over here, you make a copy of the address space walk into the extended address space. 335 00:46:14.430 --> 00:46:27.600 William Cheng: The extended address space stays exactly the same. Okay, so this is just like the exact system called over here. You know when you make a forecast, you know, will will win the parent process. Make the fourth system call the child process will actually shared the same thing as the parent 336 00:46:28.860 --> 00:46:38.430 William Cheng: Okay, because there is an extended address space. So what it will do is that they will actually be able to share it between the parent and child. So this way, the parent of the child, they can actually communicate with each other using the extended address space. 337 00:46:38.850 --> 00:46:42.300 William Cheng: Okay, so they're, they're all going to sort of talk about what exactly what kind of mechanism, they will use 338 00:46:42.720 --> 00:46:47.820 William Cheng: to to to to to talk to each other. At this point, we just say that they can share the extended address face. 339 00:46:48.090 --> 00:46:56.640 William Cheng: That. So at this point we make the forces them call both attended the child. They all have file descriptor one for this Scrivener one by default, they're connected with terminal. 340 00:46:57.360 --> 00:47:03.780 William Cheng: OK, so now the child processes, what it would do that. It will close number one over here. So again, just think about the file descriptor table file descriptor table over here. 341 00:47:04.050 --> 00:47:07.770 William Cheng: Zero still connect to the keyboard. Right. So both apparently apparently 342 00:47:08.190 --> 00:47:13.110 William Cheng: They all connect to exactly the same keyboard one over here. Again, they're connected the same display. 343 00:47:13.380 --> 00:47:22.680 William Cheng: Now, inside the child process over here. You're going to call the closest them call you going to close. Number one, so therefore you're going to disassociate file descriptor number one over here with the display and that will point zero 344 00:47:23.700 --> 00:47:26.670 William Cheng: Sorry done that it will point to now okay become a null pointer. 345 00:47:26.880 --> 00:47:33.960 William Cheng: Alright, the next thing that you do that, you're going to perform the open system call. I'm going to call open slash home science bc output output over here is the file. 346 00:47:34.170 --> 00:47:41.190 William Cheng: And now, what happened is that if this system color successful, what happens. I'm going to enter opening a file so thought the script or number one over here is 347 00:47:41.400 --> 00:47:49.650 William Cheng: Going to be the first available file descriptor. So when this function returns successfully thought descriptor. Well, what were the point to it will apply to this file right 348 00:47:51.510 --> 00:47:59.040 William Cheng: Okay, so over here again because father's group number one is closed. So when I made the open system call if it's successful. I know for sure that it will allocate file descriptor number one. 349 00:47:59.820 --> 00:48:07.860 William Cheng: Okay, so therefore this point or if I write too far this quarter. Number one, what would it go, it will no longer go to the display in but instead he will go to this particular file. 350 00:48:08.940 --> 00:48:16.620 William Cheng: Okay, so what I would do is I will set all these things up inside my extended address space is the file descriptor table inside your address, as well as how you address space. 351 00:48:16.920 --> 00:48:20.700 William Cheng: Well, I mean, clearly the file descriptor table is inside the current also, it's also the IP address space. 352 00:48:21.270 --> 00:48:25.770 William Cheng: Okay, so therefore, so therefore the father's go to the table is part of the extended address space. Okay. 353 00:48:25.980 --> 00:48:31.800 William Cheng: The extended address space will survive. Exactly. So therefore, the next thing that you do over here is to copy the exact system call 354 00:48:31.950 --> 00:48:44.550 William Cheng: We made the existence. I'm calling this example, we're gonna we're gonna run the program prime 300 to computer 300 primes. So in this case, when you provide start running over here at the end he will print out 300% to standard out. Where would it go 355 00:48:45.600 --> 00:48:52.470 William Cheng: There. So remember this file descriptor table over here belong to the child process file descriptor number one over here. Connect to the file. And now, will you run 356 00:48:53.700 --> 00:49:04.770 William Cheng: When they try to write to standard out there all the data will appear inside this file, they will no longer go to the display because we have reconnected, the standard output for this particular for the child process, you know, to this particular file. 357 00:49:06.060 --> 00:49:16.110 William Cheng: Okay so units. This is called IO redirection we can actually redirect the IO, whereas in this case we're redirecting the output from the, you know, from the display to a particular file. 358 00:49:16.830 --> 00:49:27.270 William Cheng: Okay, so when we run co like this. We run Prime's 300 right arrow right, which means that we're going to redirect the, the, the output to go to a file slash home size bc slash output. 359 00:49:27.600 --> 00:49:43.290 William Cheng: This will be exactly the code that gets executed inside your login show, okay, or inside your command command show your commercial over here. Again, it's your passion UTC shop where you type this command in your terminal. This will be exactly the code that will get executed. 360 00:49:44.460 --> 00:49:53.370 William Cheng: Okay, so therefore your command show is actually very, very simple program. What it does is that it read the command line over here one line at a time, just on your warm up wine. Why you read it one at a time. 361 00:49:53.580 --> 00:49:59.820 William Cheng: And then what it will do is they'll look for character likes like right arrow over here. If it sees a greater than sign over here. 362 00:50:00.000 --> 00:50:08.730 William Cheng: What he knows is that on the left hand side is going to be a problem that it will run that will need to run it on the right hand side over here that you need to redirect standard out into this file. 363 00:50:09.420 --> 00:50:15.180 William Cheng: Okay. So, therefore, to actually exactly this code over here, it will close. Number one, he will open this fall over here. 364 00:50:15.390 --> 00:50:23.790 William Cheng: As the standard output right and then on the left hand side over here, it will for kopitar process and run exact out over here using the command line arguments right here. 365 00:50:24.240 --> 00:50:33.420 William Cheng: You know, for, for, for the command that you type. Okay, so this way when our program run when I tried to print the standard out it will go into the file. Instead, because we have redirected the I O inside our 366 00:50:34.530 --> 00:50:35.340 William Cheng: Our logging show 367 00:50:36.750 --> 00:50:44.280 William Cheng: Then, alright. So, again, are logging sounds very simple program, you know, this is all it does. Okay. You can also redirect standard input. 368 00:50:44.520 --> 00:50:51.690 William Cheng: For example, you can actually say cat less than starch homes I species such output. So in this case, what what is, what is my program will do 369 00:50:52.020 --> 00:50:55.290 William Cheng: Okay, so instead of closing file descriptor one it will close by the script zero 370 00:50:55.770 --> 00:51:07.200 William Cheng: Guys, okay you log is going to look at this character, it's less than so therefore he will close by this group to zero and then open this file and this file will get out to be zero. So now instead of reading from the keyboard. It will refund this file. 371 00:51:08.460 --> 00:51:14.520 William Cheng: OK. So again, the result of cat less than a file. What it will do is it will refund this file and then print all the data, our standard output. 372 00:51:15.810 --> 00:51:21.120 William Cheng: That alright so now you sort of know what login shoulders. Again, it's pretty pretty is pretty straightforward. Yeah. 373 00:51:23.430 --> 00:51:32.580 William Cheng: Alright, so the file descriptor we mentioned before, right. So there's a file descriptor table over here 01 to every one of them point to a file object, right, so what's inside the file object. 374 00:51:33.180 --> 00:51:38.760 William Cheng: Okay, so what's inside the fall out there is the context for your process, right, because you have open this file already 375 00:51:38.880 --> 00:51:47.070 William Cheng: And now you want to, you know, sort of read and read the file. So whenever you try to read. And by default, we're going to move the cursor position around and the cursor position is part of the context for your program. 376 00:51:47.460 --> 00:51:49.740 William Cheng: Okay, so therefore they are stored inside of objects. 377 00:51:50.280 --> 00:51:58.710 William Cheng: Yeah, so the cursor position over here. So I guess sometimes they are called noise file position. Sometimes there are known as cursor position because people will when I think about cursor position. 378 00:51:58.890 --> 00:52:03.780 William Cheng: They're thinking about your editing a file on the screen or something like that. So you can also call it as default position. 379 00:52:04.410 --> 00:52:12.540 William Cheng: Okay. So inside the colonel inside the file object over here. We're going to keep track of how many buys you have rough on this file. So next time when you try to read a file again, we're gonna 380 00:52:13.230 --> 00:52:17.970 William Cheng: We will next time when you try to read the file again we're going to start from your current cursor position. 381 00:52:18.540 --> 00:52:32.250 William Cheng: Okay. And if it turns out that we re ad buys from the file, we're going to return to you at, we need to increment the cursor position by at okay so this way, next time when you read when you call read again we're going to pick up at characters later start reading the file again. 382 00:52:33.540 --> 00:52:33.840 William Cheng: Okay. 383 00:52:35.460 --> 00:52:44.190 William Cheng: All right, whenever you open the file the cursor position will be initially zero. So that's why when you first open the file when you try to read a refund the beginning, it will start reading from Paul file offset zero 384 00:52:44.760 --> 00:52:51.090 William Cheng: Okay, so the Christmas, you know, via it's a bite offset. It's a biopsy into the file, it tells you where you start reading the file right 385 00:52:52.980 --> 00:53:00.900 William Cheng: One of the other information that are stored inside of objects, right. So the other thing that is other than cursor position also when you open the file you need to remember how you open the file. 386 00:53:01.290 --> 00:53:07.980 William Cheng: Okay, because if you don't remember how you open the file we mentioned before you can open up a read only you can open the file for right only you can open a powerful rewrite 387 00:53:08.640 --> 00:53:15.990 William Cheng: If you don't remember how you read the file. There's one trick that you can play is that you open the file for read only. Okay. And then you will write to the file. 388 00:53:16.800 --> 00:53:20.130 William Cheng: Okay, if you open up a read only if you have read access to the file, you'll be able to refund if 389 00:53:20.340 --> 00:53:28.110 William Cheng: You want me to open the file And now, if you write to the file, you're going to end up modifying the file right so that in a way that's how you actually can circumvent security. 390 00:53:28.620 --> 00:53:35.280 William Cheng: Okay, if you have a file will get this kind of security. This is file access permission. If you have a file, you only have read only access to it. 391 00:53:35.490 --> 00:53:46.890 William Cheng: If we don't remember how you open the file you can open up a read only. And since you have read only access to this file, you have the accent will be granted, but then if you pull a racism call you can enter modify modify the file and that should not be allowed. 392 00:53:47.580 --> 00:53:56.280 William Cheng: Okay, so therefore, instead of file object we need to remember. You know what kind of access was granted. So when you open up a read only. We need to remember that you open this file for read only. 393 00:53:56.940 --> 00:54:03.240 William Cheng: Okay, next time. Will you try to write to the file where you made the racism call the first thing we have to do is to go to the file object. 394 00:54:03.420 --> 00:54:12.660 William Cheng: And try to sort of figure out say hey, you know, how did you open this file. If you open the file for read only. And now you tried to write to the file. What, then, we're not allowed to play at the to let you right to the file. 395 00:54:13.170 --> 00:54:16.350 William Cheng: Okay, so in this case wouldn't give you an error message to say the access has been denied. 396 00:54:17.820 --> 00:54:21.090 William Cheng: Okay, so instead of our logic over here, we also need to remember 397 00:54:21.360 --> 00:54:32.550 William Cheng: You know how you open the file. So this way every time we try to access the file. We need to check if the access is compatible with the way the file is open if they are not compatible that we're going to reject your request. Right. 398 00:54:34.290 --> 00:54:42.420 William Cheng: So here's a picture of what a file descriptor look like. So this so I sort of turn the picture around over here on the left hand side is going to be the user space right here's application. 399 00:54:42.690 --> 00:54:45.540 William Cheng: On the right hand side over here is going to be the colonel is going to be the operating system. 400 00:54:45.900 --> 00:54:52.770 William Cheng: Okay. So inside the kernel for every process. There's a file discovery table. So the file descriptor table over here is process. 401 00:54:53.070 --> 00:55:04.380 William Cheng: Rather, again, we'll go from 0123 every one of them is going to point to a file object. So we here inside the cardinal, we're going to have another global data structure that's a collection of file objects. Okay, so this one we're going to call it the system. 402 00:55:05.580 --> 00:55:16.800 William Cheng: We're gonna call it a system file table. It's just as simple as just a collection of objects. Okay. Every object over here is going to be a horizontal roll over here. So in this example over here every object has four fields. 403 00:55:17.670 --> 00:55:25.140 William Cheng: Yeah, the first field over here is no as a reference count. Okay, so what's the reference count the reference count count the number of pointers that are pointing to this object. 404 00:55:25.440 --> 00:55:31.980 William Cheng: Okay, so in this example over here. So let's say that this is 012345 the file descriptor that we have over here is equal to five. 405 00:55:32.310 --> 00:55:37.380 William Cheng: Okay, so that means that this pointer over here pointed this file object. So therefore, the reference count over here is going to be equal to one. 406 00:55:37.980 --> 00:55:39.840 William Cheng: Okay, every time when you open the file. 407 00:55:40.170 --> 00:55:49.470 William Cheng: Okay, we're going to create a file object. So therefore, the reference count over here for the fall RJ is going to equal to one and then we're going to use the file descriptor. The table entry over here to point to this particular file object. 408 00:55:49.980 --> 00:55:56.280 William Cheng: Okay, so in this case reference got over here to be one because we're saying that there's one of those is going to be one pointer pointing to the subject. 409 00:55:56.880 --> 00:56:04.470 William Cheng: Okay, when we create a child process again will create chocolate says we are sharing the extended address space. So we could I child prices over here we're going to create a 410 00:56:04.950 --> 00:56:12.570 William Cheng: We're going to play in a file descriptor table and then we're going to copy from the parent of the child. So therefore, the child will point to exactly the same, exactly the same followed yet. 411 00:56:13.110 --> 00:56:19.440 William Cheng: Okay, so in this case this project will have two pointers point two. So, therefore, in this case, we're going to increment this by one and now it's going to be go to two 412 00:56:20.340 --> 00:56:26.910 William Cheng: Guys, but again the reference call what it does is that it count the number of pointer punted this object, were you when this program quit. 413 00:56:27.240 --> 00:56:35.700 William Cheng: I've got went and spoke on quit over here. You gotta, you gotta go to every file descriptor table if they point to a file object. So what you need to do is that you need to remove this pointer from from the project. 414 00:56:36.000 --> 00:56:43.230 William Cheng: When you remove the point of on the file object, you got a detriment the reference count if after your document the reference count if they represent go to zero, what does that mean 415 00:56:43.680 --> 00:56:47.730 William Cheng: That means that nobody else is pointing to the spot. Yeah, so therefore this file object can be freed up 416 00:56:48.420 --> 00:56:53.730 William Cheng: Okay, if it turns out that this biologic over here the reference gotta go to when you document it and ask and go, go to what 417 00:56:53.910 --> 00:57:01.170 William Cheng: That means that somebody else is still using this file object. So therefore you are not allowed to free up this file object for it to somebody else. Is that a kernel is actually using this one. 418 00:57:01.770 --> 00:57:11.910 William Cheng: Okay. So in this case, when you close this file, you would just point to know over here is the file the reference kind of your go down to one, you still need to keep this object around because another process might be actually using this object. 419 00:57:12.720 --> 00:57:15.480 William Cheng: Yeah, so that's the first field that's a reference count. 420 00:57:15.750 --> 00:57:17.160 William Cheng: Let me clean this up a little bit. 421 00:57:17.370 --> 00:57:27.030 William Cheng: The second one over here is accessible. When you open the file for read only. You need to remember it's read only. So next time. Will you try to write to the file. You're going to follow this pointer over here, verify that you access this 422 00:57:27.330 --> 00:57:32.610 William Cheng: is compatible with the ways that it's open. If they're incompatible. In this case, you will return an error message. 423 00:57:32.940 --> 00:57:40.620 William Cheng: There. The next one over here. It's called the location of yours again the cursor position in the beginning is equal to zero, every time. Will you try to read and write on the 424 00:57:41.250 --> 00:57:48.180 William Cheng: Roof on the file right to the file you need to change the cursor position out of it turns out successfully return data from it or you write it into the file. 425 00:57:48.720 --> 00:57:54.060 William Cheng: Okay. And the last field over here. So this is going to be the other point that I talked about. Right. So I say, you know, 426 00:57:54.540 --> 00:58:00.090 William Cheng: Starting from the file descriptor. You go to point to the file object and the follow up there is going to point to other stuff inside the Colonel. 427 00:58:00.510 --> 00:58:09.420 William Cheng: Okay, so now we have a name for this one. This is called I know pointer because it wasn't. I know okay in Unix. I know it's a data structure that represent a file. 428 00:58:10.320 --> 00:58:14.100 William Cheng: Okay, so this is the file object over here the file object doesn't really represent a file. 429 00:58:14.610 --> 00:58:26.370 William Cheng: Okay, so this biological via keep track of some of the, you know, so, so some of your execution context for your file and then the I know pointer is going to be the one appointed the I know the I know is the UNIX name for something that represented file. 430 00:58:27.360 --> 00:58:33.690 William Cheng: Well, what does that look like, well, we don't know. So at this point we don't know in chapter six. We're going to show you an example. What I know look like 431 00:58:35.460 --> 00:58:39.630 William Cheng: Okay, so again, you know, this part over here, depends on what kind of up in the system you have, what kind of file system you have 432 00:58:39.870 --> 00:58:51.390 William Cheng: Every file system will have a different I know representation. So therefore, all we introducing right now is that the concept. There's no point in appointed a part of the I know what does it look like we don't know. You need to tell us what kind of file system you have 433 00:58:52.230 --> 00:58:58.080 William Cheng: Okay, if you have one kind of forces them to either we're looking at one way, if you have a different kind of our system, it will look at a different way. Okay. 434 00:58:59.700 --> 00:59:07.800 William Cheng: All right, so here again we see a lot of pointers right the file descriptor over here is appointed the point to a file object, the file object over here has appointed the point that I know. 435 00:59:07.980 --> 00:59:14.160 William Cheng: Maybe inside. I know there's not a pointer and then eventually, there's not a partner in that upon to eventually it will point to the keyboard. 436 00:59:15.270 --> 00:59:23.760 William Cheng: Okay, so if this one is associated with a keyboard, you eventually will point or keyboard, if this one you know if this pretty good size associate with the desk. What eventually ended appointed a desk. 437 00:59:24.030 --> 00:59:34.710 William Cheng: Okay, this one is associated with your terminal display eventually appointed display. So again, this is a very, very complicated data structure. So again, when we talk about chapter six. We're going to sort of take a look at more details. 438 00:59:35.160 --> 00:59:40.440 William Cheng: Okay, so now I'm going to leave it at that. We're going to, you know, because we're in Chapter while we sort of give you this high level, you know, sort of, 439 00:59:40.680 --> 00:59:48.300 William Cheng: The high level picture of what the file system will guy. So again, this will be the last picture that we show in chapter one and now we're going to go into chapter two. 440 00:59:49.620 --> 01:00:00.210 William Cheng: Okay, so, so we're done with chapter one for now. And if you look at the lecture slides over here. There are three sets of lecture slides we finished the first set and the second set. And now we're going to skip the first day. 441 01:00:00.450 --> 01:00:09.000 William Cheng: And if you read the the the lectures web page, it will say, I'm going to come back to the third set of lecture slides after we're done with chapter five. 442 01:00:09.810 --> 01:00:24.210 William Cheng: Okay, so now we're going to skip into, you know, we'll start talking about Chapter two, because we're willing to sort of give you a sort of servers will give an introduction to a threat implementation. Okay, there's going to be a threat implementation at a very, very high level. Okay. 443 01:00:26.250 --> 01:00:36.870 William Cheng: All right, so we'll go into chapter to chapter two is called multi threaded programming. So you have never write a robot that sort of program. Don't worry, we're going to tell you everything that you need to know about multi threaded programs. 444 01:00:37.230 --> 01:00:45.990 William Cheng: That right, some people ask the question before, you know, we're going to talk about tags and we're going to talk about, you know, threads and stuff like that. The answer is yes to all those kind of stuff. Yeah. 445 01:00:48.030 --> 01:00:51.570 William Cheng: All right, so, so we can talk about threat, so 446 01:00:52.080 --> 01:01:02.520 William Cheng: You know, so, so why do you need threats. So we talked about that before right threats on abstraction of the CPU. So if you have more than one CPU. So let's say you have two CPUs, you want to use them simultaneously. 447 01:01:02.820 --> 01:01:10.440 William Cheng: How can you use them simultaneously. Right. So. So in this case you need to create two threads, then it's possible for you to run on to CPU simultaneously. 448 01:01:11.790 --> 01:01:21.870 William Cheng: Okay, if you only have one thread, you can only use one CPU at a time there's a new case you will not be able to perform anything in parallel. So the idea here is that if you want concurrency. You got to have multi threading. 449 01:01:22.890 --> 01:01:35.880 William Cheng: Okay, so, so, so if you have multiple CPUs or multiple processors. So in that case, you have to use concurrency, you have to use parallelism that even if you're only one one CPU way sir, if I only have one CPU. Why do I have to use multiple threads. 450 01:01:36.540 --> 01:01:45.690 William Cheng: Okay. As it turns out, multi threading is a very, very powerful paradigm. It makes your code actually looks cleaner. Okay, so, so this is one of the reason why you go to interview. 451 01:01:46.950 --> 01:01:54.240 William Cheng: You can reach out to get an interview on industry and when they see that, oh, you have taken an operating system class, they're going to ask you a lot of questions about multi threading. 452 01:01:54.750 --> 01:02:01.140 William Cheng: Okay, because if you know how to write motors to add a co your co will be cleaner and then we'll your code is cleaner, you're going to end up with less bugs. 453 01:02:02.100 --> 01:02:08.910 William Cheng: Okay, so therefore, even though we only have one CPU is still a very good practice to use motor to use more that's already there. 454 01:02:09.570 --> 01:02:16.020 William Cheng: Right, so, so it's a good idea to learn so that therefore will learn all about multithreading 455 01:02:16.590 --> 01:02:22.920 William Cheng: The downside with multi threading over here is that even if you want concurrency. Then you also have to be able to control the concurrency. 456 01:02:23.100 --> 01:02:30.510 William Cheng: Because if you don't have concurrence if you don't control the concurrency. Why is that you're going to have your threat writing to the same memory location, you're going to mess up your program. 457 01:02:31.650 --> 01:02:34.950 William Cheng: Okay, so therefore we have to use that we have to learn concurrency control. 458 01:02:35.280 --> 01:02:41.970 William Cheng: Okay, so in chapter two, we're going to spend. We're going to spend a little bit of time talking about how to have parallelism and we're going to spend most of our time. 459 01:02:42.180 --> 01:02:51.750 William Cheng: Talking about concurrency control right it is concurrency control is much harder than running things in parallel, they're going to make sure that our thread doesn't step on each other's toes that 460 01:02:56.820 --> 01:03:04.500 William Cheng: Alright, so these are my logos for this amount of icons for threat. Every time you see one of these things. There's a swear. So in this case, there are four threads that are running 461 01:03:04.710 --> 01:03:12.300 William Cheng: These four threads. They may belong to two different processes or they may belong to one process. Okay. Each one of them represent a threat, and then a thread is and 462 01:03:12.660 --> 01:03:18.750 William Cheng: We talked, we talked about in the early part of chapter one. These are virtual CPUs again. So, so they are really not real. 463 01:03:19.350 --> 01:03:28.620 William Cheng: Okay. They represent a CPU and we're going to sort of multiplies the real CPU among all these virtual CPUs, we're going to multiply real CPU among all these threats. Right. 464 01:03:29.400 --> 01:03:38.160 William Cheng: Alright so many things are easier to do with threats right multi threading is a very, very powerful, powerful paradigm. It makes your design much much cleaner. And so, therefore, that 465 01:03:38.430 --> 01:03:44.730 William Cheng: You have less bugs and that's the main reason why everybody wants you to be able to do to write a multi threaded program that 466 01:03:45.360 --> 01:03:53.070 William Cheng: There are also things that are run faster with with the with whatever thread, even if you're one CPU, you might be able to run faster with with multiple threats. 467 01:03:53.280 --> 01:03:59.910 William Cheng: Right. Why is that right so so for example if you have two things that can be running parallel, even if you are one CPU, but you are 468 01:04:00.630 --> 01:04:08.460 William Cheng: But since all the other hardware that you are using their, their different hardware. So all the other hardware that can run in parallel, together with the CPU. 469 01:04:09.360 --> 01:04:12.270 William Cheng: Okay, so for example if you try to, you know, sort of get data from the desk. 470 01:04:12.630 --> 01:04:25.230 William Cheng: Okay, if you're trying to get data from the desk and the disk is a different piece of hardware, it can run in parallel with the CPU. So, therefore, if you are waiting data for the, you know, if you're waiting for waiting for data on this, you can actually use another threat to use the CPU. 471 01:04:26.610 --> 01:04:34.770 William Cheng: Okay, so therefore multi threading, even if you have one CPU is going to be worthwhile because you have one, you know, one thread waiting for this to finish, while the other thread will using CPU. 472 01:04:36.030 --> 01:04:43.890 William Cheng: Okay, so even if he had one CPU multi threading is still very, very worthwhile, right, because you'll be able to use multiple pieces of hardware in parallel. Yeah. 473 01:04:44.520 --> 01:04:51.240 William Cheng: Alright, so the other important thing is that, again, is that, you know, the colonel thread and the user. So, as we mentioned before, there's really no difference. Okay. 474 01:04:51.690 --> 01:04:57.900 William Cheng: So as far as this causes concern when you make a system call your user throughout Cummings that Colonel become a colonel threat. 475 01:04:58.290 --> 01:05:03.180 William Cheng: Okay, and then it's going to do something for you when it's done, it's going to return back into the user space IQ, but how many users that again. 476 01:05:03.750 --> 01:05:17.220 William Cheng: OK. So again, conceptually, they are exactly the same thing. The only difference is that inside the CPU, you know, the CPU register. What did they point to and and also whether the kernel of the CPU mode is in the privilege mall or in or in the user mode. 477 01:05:18.030 --> 01:05:28.800 William Cheng: Okay. Otherwise, you know, Colonel throughout users to add, there's really not much difference. OK. So again, inside the colonel your users. If you are a user threat you run in the user space, then you're not allowed to touch hardware. 478 01:05:29.400 --> 01:05:36.300 William Cheng: Okay, when you become a colonel threat, you're inside the cardinal and now. Now, you become you're running the privilege Mo and now all of a sudden, you can actually use hardware. 479 01:05:37.470 --> 01:05:45.510 William Cheng: OK, so again. Yeah, so, so, so, so again, depends on whether you know where you are, you know, there are different kinds of capability that you have, again, that all depends on the CPU mode. 480 01:05:46.140 --> 01:05:51.480 William Cheng: Okay. But basically, if you look at the behavior of users to and Carol threat. Again, they're very, very similar. Right. 481 01:05:53.670 --> 01:06:02.310 William Cheng: Okay, the textbook. Give them an example, what's called an otter login D. So I don't know if you have actually remote log into some something a USC. There are 482 01:06:02.940 --> 01:06:11.970 William Cheng: There is a machine that you can use. I don't know. Maybe if you take some other classes, you are required to use that machine. It's called B2B dash SCF one that usc.edu there's another one is called 483 01:06:15.030 --> 01:06:20.190 William Cheng: If you're logging on your laptop, your desktop, you can be, you can secure a shout to that machine. Okay, we used to use 484 01:06:20.520 --> 01:06:29.610 William Cheng: Machine. What do you need us. They need to provide your credential you provide your user ID and password. So on the server machine over here. What it will do is that it will run a program called our login D. 485 01:06:30.360 --> 01:06:38.640 William Cheng: Okay, our login D is a remote login demon allows you to remotely login from one machine into the machine. Okay. So, therefore, what will you try to secure 486 01:06:38.910 --> 01:06:47.970 William Cheng: That machine, you need to provide your user ID and password. Those information will be received by the otter login D and what it will do is that he will check your credentials to see if they match. Good. 487 01:06:48.180 --> 01:06:55.680 William Cheng: Once they figure out that you know your user ID and password matches what it will do is that it will allow to allow you to have access to that machine. 488 01:06:56.610 --> 01:07:03.330 William Cheng: Okay, so in that case the are logging the over here. They basically doesn't have any us right because they need in the beginning, it's used to to verify your credential. 489 01:07:03.570 --> 01:07:15.120 William Cheng: Once you pass beyond that point. What it does is that it actually send data as around it will read data from the one file descriptor, and then send it to the remote server, and then when the remote server over here to print something onto the screen. 490 01:07:15.540 --> 01:07:23.400 William Cheng: You will receive from another file descriptor. And then you will send it out to the network connection. So again, in this case the network connection over here, the way you represent a network connection is why 491 01:07:24.300 --> 01:07:34.080 William Cheng: OK. So again, the only way to represent anything that's outside of your address space is going is going to be a file descriptor. So in this case, the otter login D over here. We'll be talking, you know, to these four or five descriptor. 492 01:07:34.470 --> 01:07:41.130 William Cheng: Okay, to have the file descriptor over here is connect to the Internet and the other two are the file descriptor over here it's connected what's going on inside your server. 493 01:07:42.240 --> 01:07:47.670 William Cheng: Okay, so what the otter login D do over here is, is basically shuffle data in this direction also shuffle data in that direction. 494 01:07:48.480 --> 01:07:57.600 William Cheng: Yeah, so if we, if we take a look at the old way to implement our logging d, right. So this is the good old days, how you implement a login d and this was the day before multithreading 495 01:07:58.260 --> 01:08:05.910 William Cheng: OK, so the way you're gonna write this code over here is gonna stay inside this infinite loop. Once you verify the credential over here. All you're doing is to shuffle data this way I shuffle data that way. 496 01:08:06.720 --> 01:08:10.830 William Cheng: Okay. So in this case, what do you have to do, right, you have to, you have to manage these for file descriptor. 497 01:08:11.010 --> 01:08:16.860 William Cheng: He to see if any of the file descriptor has any data up here. So the code over here is going to make a select system call we here. 498 01:08:17.010 --> 01:08:24.900 William Cheng: I'm not going to go into detail the select system call. So the structures and call as a blocking system call it will return when something happened with any of these four five descriptors. 499 01:08:25.680 --> 01:08:30.540 William Cheng: Okay, so what do you have to do over here is that when this function return you need to say hey you know what you know what 500 01:08:30.720 --> 01:08:37.260 William Cheng: What what's going on with these for file descriptor is their data coming from here is where did I go into there is a data coming from here is the data, go go in there. 501 01:08:37.590 --> 01:08:41.700 William Cheng: You can look at this code over here, the code is kind of messy, even though it's nicely structure. 502 01:08:42.420 --> 01:08:46.530 William Cheng: Okay, but if you look at this call if I tell you that there's a code. And there's a bug in this code worth about 503 01:08:47.400 --> 01:08:51.420 William Cheng: That, as it turns out, if you write your code. This way, it's going to be very, very difficult for you to debug. 504 01:08:52.260 --> 01:08:57.180 William Cheng: Okay, so. So what we're going to show you is I want to show you a multi threaded version of this political call 505 01:08:57.600 --> 01:09:07.530 William Cheng: This code. I just shared it over here. It's a single threaded program. Okay. So, therefore, we're going to make the sexism call and we're going to check all these file descriptors, see if there's anything going on over here. And then we're going to perform some action. 506 01:09:08.880 --> 01:09:12.030 William Cheng: Alright, if you are allowed to do multi threading. What you can do is that 507 01:09:12.720 --> 01:09:16.290 William Cheng: They. So here are our login do up here, we can create two threads. 508 01:09:16.500 --> 01:09:26.340 William Cheng: One one square over here, what he would do is that it will you will read data from the file descriptor on the left top corner over here and then you will take the data and send it out to the right, top corner over here. 509 01:09:26.910 --> 01:09:35.640 William Cheng: Okay, the second, third over here. I'm going to call the incoming threat. He will read data from this, you know, perform on this file descriptor, and he will send data out to the other file descriptor. 510 01:09:36.480 --> 01:09:42.870 William Cheng: Okay. So, therefore, what is that called look like yeah for the incoming thread over here, he will refund Alfonso scripture and write it out to the 511 01:09:43.260 --> 01:09:47.820 William Cheng: To the alpha square descriptor. We saw the code already that program is no as cat. 512 01:09:48.420 --> 01:09:52.920 William Cheng: Okay. So over here, we're gonna do exactly the same thing, right, they have to file descriptor are in and out over here. 513 01:09:53.100 --> 01:10:03.450 William Cheng: Both of them are small integer because their file descriptor. So all we do is that we stay in an infinite loop. We're going to read from our end, I'm going to write a letter to the Allah. I'm going to say in the inner loop over here until we get to the end of our 514 01:10:05.220 --> 01:10:09.900 William Cheng: Okay, so that's how we have to do. So if you look at this fall over. If you look at this code over here. Is there a bug. 515 01:10:10.920 --> 01:10:17.670 William Cheng: Okay, you can actually look at this code line by line you compare that with the cat program, you can see that exactly that this for this go has no bucks. 516 01:10:18.330 --> 01:10:25.710 William Cheng: Okay, what about the ongoing threat over here the outgoing thread this call look exactly the same as this goes, I actually can you actually use exactly the same function. 517 01:10:25.950 --> 01:10:37.890 William Cheng: All you need to do is and you provide different function argument. So this one you have L in over here and over here and then this code will look exactly the same thing as that code. Okay. So, by using multi threaded program our program become much, much simpler. 518 01:10:39.390 --> 01:10:46.860 William Cheng: Okay, so you can also by looking at this code over here, this this code is much much cleaner and you can actually look at anything you will be able to tell that there's no bug in the scope 519 01:10:47.070 --> 01:10:51.180 William Cheng: Okay, compared to the other always over here doing something like that. This is much more complicated. 520 01:10:51.660 --> 01:11:02.910 William Cheng: Yeah. The second example given a textbook is a multi it's a database server there in the good old way in the good old days when there is no multithreading when you try to implement a database server. What do you have to do over here. 521 01:11:03.660 --> 01:11:14.520 William Cheng: Now, so we here. So to show you that we have a we have a database and up here, lots of requests coming to the database over here so happens that if you have a single thread this single thread need to need to juggle among all these requests. 522 01:11:15.120 --> 01:11:21.900 William Cheng: Okay, so there are lots of requests coming over here. So maybe these requests are coming from the internet. Maybe there's 1000 requests that are actually making requests to the database. 523 01:11:22.050 --> 01:11:30.930 William Cheng: So in this case, this thread need to juggle all these are these connections over here. And for every one of these requests over here. He needs to find out what you know what it needs a database. 524 01:11:31.140 --> 01:11:39.360 William Cheng: It will send a request into the back end database and then what it will do is that it might have to simultaneously wait for 1000 requests come from the database. 525 01:11:39.930 --> 01:11:45.090 William Cheng: Okay, so you can actually imagine that the code for this pretty girl, so it's gonna be really, really complicated. 526 01:11:45.810 --> 01:11:50.490 William Cheng: That if we are allowed to have multi threading. Well, if we're allowed to have multi threading. What will we do 527 01:11:50.850 --> 01:11:59.910 William Cheng: Okay, so it turns out it's gonna be very simple. All we have to do is that whenever you get a connection from the, you know, from from our side. What we can do is, I will contend that dynamically create a threat. 528 01:12:00.360 --> 01:12:05.790 William Cheng: Okay, so we're going to create the stress and this thread the job is simply to to handle this connection. 529 01:12:06.330 --> 01:12:09.930 William Cheng: Okay, so in this case we're going to read from this connection and find out what the request is 530 01:12:10.110 --> 01:12:17.490 William Cheng: And then we're going to write to the database by making a request to the database. And now we're going to the what our third is going to do is that we're going to read the result back from the database. 531 01:12:17.730 --> 01:12:25.530 William Cheng: If the database now ready. Well, in this case, our Colby a blocking call because we try to refund the database. We will block and then our third. Oh, he'll go to sleep. 532 01:12:26.310 --> 01:12:29.520 William Cheng: Okay, eventually, when a database is ready will send data back to you, then you 533 01:12:29.730 --> 01:12:39.120 William Cheng: Will get woken up when you finish woken up you're going to get the data from the database you right to the connection over here, right, whoever and these data and then you close the connection and then you're threatened self terminate. 534 01:12:39.900 --> 01:12:42.060 William Cheng: That. So again, if you write your code this way. 535 01:12:42.300 --> 01:12:53.130 William Cheng: Every thread that you have. They're all exactly the same code over here. So they will all be nice and simple you refund the connection you send a request to the database you read a response from the database and you send a response back to the request. 536 01:12:54.450 --> 01:12:58.890 William Cheng: Okay, so again, by using multi threaded program your program will be super simple. Yeah. 537 01:13:00.750 --> 01:13:08.040 William Cheng: Alright, so, so hopefully you know you're convinced over here to say that multithreading actually is going to make us you know co simpler and 538 01:13:08.490 --> 01:13:17.130 William Cheng: Less less back and now the next thing we're going to do is I'm going to talk about how to create a thread and I'm going to sort of talk about how to how to thread terminates all these kind of stuff that 539 01:13:18.000 --> 01:13:26.280 William Cheng: Alright, so we're going to take a look at one function over here. So, this function is called P3 create. So we're going to focus on a threat standards know as politics thread. 540 01:13:26.580 --> 01:13:39.090 William Cheng: Okay. Politics thread over here, again, a positive P threads another term for is sp threat because P stands for politics. Okay, every function in the P3 library, they all start with P3 I followed by underscore and then followed by something else. 541 01:13:39.630 --> 01:13:45.990 William Cheng: Okay, so if you want to create a threat, you call piece or create if want to look at the man pages where Peter create you say man follow a piece or create 542 01:13:46.500 --> 01:13:52.980 William Cheng: That so the the main pages. The main pages can be very, very long. So what you need to do is they need to learn how to read the main page. 543 01:13:53.250 --> 01:14:00.930 William Cheng: That at the beginning, the first part of the map page is the most important part. There's a section called synopsis. It shows you how to actually use this function. 544 01:14:01.320 --> 01:14:13.350 William Cheng: Okay, so they will tell you how to pound include so if you want to use people create your insight, your code, you have to say pal include left the angle bracket piece, right. Ah, OK. So this way you can 545 01:14:14.790 --> 01:14:26.370 William Cheng: If you don't include this line, then you're going to get a compiler one to say, I have no idea what what what Peter creators. Okay, so therefore you you have to create it, you have to include this line and then he will give you the function prototype. 546 01:14:27.570 --> 01:14:32.430 William Cheng: So we can actually see that in the Peter query function over here, this is the function that returns an integer. 547 01:14:32.970 --> 01:14:41.850 William Cheng: OK. So again, if you continue to reuse things I see that they will return zero. And it was successful otherwise going to return minus one and then you should look look at air number to find out, you know, what is the error. 548 01:14:42.180 --> 01:14:52.350 William Cheng: Okay, it wasn't showing you that this function takes For argument's. Okay, the first argument is going to be a pointer to throw it. So in this case, this is very similar to the waste ism call 549 01:14:52.590 --> 01:14:58.200 William Cheng: Piece where we actually return to values, the first value over here is going to be an integer telling you whether telling you whether 550 01:14:58.590 --> 01:15:03.540 William Cheng: The threat creation was successful or not. And then the first argument is going to be a return value. 551 01:15:04.350 --> 01:15:10.080 William Cheng: OK. So again, what you're supposed to do is to use a local variable at the local variable is going to be of high P threat underscore te 552 01:15:10.290 --> 01:15:18.030 William Cheng: Again underscore te over here, and it means that it's a data structures that data type. So, so you're going to create a local grab a piece of it and then you pass the address as the first argument. 553 01:15:18.360 --> 01:15:24.600 William Cheng: And then this arguments over here is really known as the threat identifier. Okay. We talked about the process and then the fire the 554 01:15:25.440 --> 01:15:28.650 William Cheng: Fire is just an integer a threat identifier. It's actually a data structure. 555 01:15:29.430 --> 01:15:37.050 William Cheng: Okay. So over here, you know, when the, the threat was successfully created the threat identifiers going to get returned back to you in the first argument. 556 01:15:37.740 --> 01:15:45.750 William Cheng: That the second argument is called a threat creation attribute. So if you want to use Chris to either create make sure that the second argument is always no 557 01:15:46.200 --> 01:15:50.640 William Cheng: Okay, so later on we're going to talk a little bit about it. But most of the time, you know, you should ignore it. It's just that it's, you know, 558 01:15:51.210 --> 01:16:03.420 William Cheng: The third arguments over here. It looked very, very aware and so this one, you actually, you saw in my for to list out. Ah, right. It's a function pointer. Every time you see an asterix surrounded by a pair of parenthesis. That's a function pointer. 559 01:16:04.560 --> 01:16:11.790 William Cheng: Okay, so, so, so, you know, to the left with this that this notation over here it tells you what kind of function it is to the love it tells you what kind of 560 01:16:12.990 --> 01:16:17.640 William Cheng: Data Type for this function return on the right hand side, it tells you what the function arguments us 561 01:16:18.210 --> 01:16:22.020 William Cheng: Okay, so if you read the third arguments over here, over here, it says that, you know, this one is 562 01:16:22.590 --> 01:16:33.810 William Cheng: The practices with with Asterix in that. So this one is a function pointer. What kind of function it is. This is the function that return a voice star and it only takes one argument and that argument is also a voice start 563 01:16:35.610 --> 01:16:45.480 William Cheng: That. So, and this one, you know, the argument over here is known as a star routine. Right, so I'll be here. So for for, you know, for a particular threat the threat, you need to specify a star routine because of what's a star routine. 564 01:16:45.870 --> 01:16:57.780 William Cheng: When we saw that, you know, when you get started, your program has a star routine. That's called the main function. Right. So there's a startup function of the cell phones color is going to call the main function. So the main function is the starter routine for your main threat. 565 01:16:58.800 --> 01:17:06.480 William Cheng: Okay, so when you start running, you have one third run. And that's what we're gonna call the main threat and now we try to create other child threat, you're going to make a piece of create call 566 01:17:07.020 --> 01:17:16.200 William Cheng: Okay. So whenever you create. So, so what do you call a piece or create you need to specify your starter routine. And that started a routine will be the lifetime for your child threat. 567 01:17:17.130 --> 01:17:29.820 William Cheng: Okay, so for example if you have a function call f. We're going to put f as a third argument over here. So here's the code for F that, so your third when they start executing it will start executing at the beginning of that when you return from F your child threat is dead. 568 01:17:31.290 --> 01:17:35.880 William Cheng: Okay, so, so this startup routine over here. It's also, I guess the textbook call it the first procedure. 569 01:17:36.090 --> 01:17:43.500 William Cheng: The first procedure. It's just like the main function for the main threat. So then you know for the child read over here at the third argument specify the first procedure. 570 01:17:43.950 --> 01:17:58.080 William Cheng: Okay, the first procedure also will be no as a threat procedure because that's the function that sort of tell you what is the lifetime for that through your third star star executing to be here. The function where your threat SQ. The at the end of the function, your thread with self terminate. 571 01:17:59.490 --> 01:18:05.520 William Cheng: That, again, this function over your take one argument. The argument. Time is why star this function also return value of oyster. 572 01:18:06.240 --> 01:18:11.040 William Cheng: Okay. The fourth argument over here for Peter create that is going to be the argument. 573 01:18:11.460 --> 01:18:18.600 William Cheng: You know that when you threats are executing so that when you start a thread. So I feel it, there's an argument over here on high voice Doris. What does the argument come from. 574 01:18:18.960 --> 01:18:21.720 William Cheng: The argument come from the fourth argument apiece recreate 575 01:18:22.650 --> 01:18:33.780 William Cheng: OK, so the fourth argument piece. Okay. Over here is a voice star and we saw in your warm up one boy stars compatible with integer. So therefore, the fourth argument, we can actually pass the number of voice star over here 95 576 01:18:34.710 --> 01:18:42.990 William Cheng: Okay, so in that case. Well, your threats are running. If you look at the function argument over here that will be equal to 95 okay so therefore you're threatened. We'll start with the with the function argument with 95 577 01:18:43.380 --> 01:18:55.740 William Cheng: Okay, so the fourth argument over here was simply get copy into the first procedure of this particular function. So this way, every so you can actually use the same code to run different threads every third can potentially have a different argument. 578 01:18:56.490 --> 01:19:01.680 William Cheng: Okay, because what you called Peter create the last argument will be the argument that you use to start up that function. 579 01:19:02.760 --> 01:19:03.060 William Cheng: Yeah. 580 01:19:04.650 --> 01:19:12.300 William Cheng: All right, I have a string comment over here to say, you know, what is the threat identifier over here the thread. And if I will be here. Some people will call it a threat control, blah. 581 01:19:12.930 --> 01:19:19.200 William Cheng: Okay, but in a way that's really not the right terminology, because we talk about process control blah, the process control blah is a colonel data structure. 582 01:19:19.500 --> 01:19:27.300 William Cheng: Okay, so if you call it a three identifier over here, a threat as sort of a threat control blah, you're sort of implying that this is going to be a kernel data structure. 583 01:19:27.840 --> 01:19:32.430 William Cheng: Okay. Is it possible the ordinances that will return to you and address of a colonel data structure. 584 01:19:33.390 --> 01:19:37.410 William Cheng: That seems to sound wrong. Okay. So, therefore, typically what happens is that that doesn't really happen right 585 01:19:37.830 --> 01:19:44.190 William Cheng: You know, so over here, the threat identifier over here. So, we should also think about, is it just like a process identifier, except it's a data structure. 586 01:19:44.550 --> 01:19:47.730 William Cheng: Okay, so this data structure will represent or that particular threat. 587 01:19:48.120 --> 01:19:53.610 William Cheng: Okay. Is it possible that this one turns out to be an integer. What's also possible that turn out to be an integer. 588 01:19:53.790 --> 01:20:02.550 William Cheng: But again, the way we sort of think about it, it's a data structure that represents sweat. It doesn't have to be a threat control blog because I start control blah implies that it's a kernel data structure. 589 01:20:03.390 --> 01:20:13.200 William Cheng: Okay, as it turns out that they don't in chapter five. We're going to learn that you can actually implement threat in the user space or you can implement thread inside, Colonel. So if you implement threads in the user space. 590 01:20:13.500 --> 01:20:16.470 William Cheng: Why not case, the threat control block will be a user space data structure. 591 01:20:17.160 --> 01:20:24.000 William Cheng: Okay, so again you need to be very careful with the terminology of control blah versus three identifier. So three identifies the terminal, you should always use 592 01:20:24.360 --> 01:20:29.280 William Cheng: Because this one is used to identify identify a particular threat. Okay. Is it possible that it's a threat. 593 01:20:29.910 --> 01:20:36.480 William Cheng: Threat control blah or yes it's possible, but we don't really know what it is. Okay. All we know that it's something that identify a particular threat. 594 01:20:37.140 --> 01:20:51.720 William Cheng: OK. So, again, in this case, when you call Peter create the return value over here. It's going to be a handle for you to refer to this particular threat if later on. YOU WANT TO DO SOMETHING TO THE THREAT HE THE PASTOR identifier back to the library to say, hey, do something to the threat. 595 01:20:52.800 --> 01:20:53.130 William Cheng: Then 596 01:20:54.600 --> 01:21:01.860 William Cheng: Alright, the next slide over here will be the last slide for today is sort of shows you an example, how to copy through create 597 01:21:02.040 --> 01:21:11.580 William Cheng: That. So in this case, we're going to have a function and over here called Star server. So the main functions that are called x x is going to call why and why is going to cause star server over here to start a potential threat. 598 01:21:11.970 --> 01:21:19.140 William Cheng: Okay, so in this example in 100 healthcare. So when this function is called Who is calling this function what the main threat is calling this function. 599 01:21:19.410 --> 01:21:30.360 William Cheng: Okay. The main thing is understanding the main function. So what it would do is a call x will call why why is gonna call star star server over here. So now our main photo of you will be actually let's go and what he will do is that it will create 100 child threat. 600 01:21:30.690 --> 01:21:36.180 William Cheng: Okay, so when you're done, you should have 101 third running in parallel, if you have enough CPUs. 601 01:21:37.020 --> 01:21:42.870 William Cheng: That alright so let's take a look at what this would call will do over here. I'm gonna say, for I go to zero, i less than 100 plus plus. 602 01:21:43.050 --> 01:21:54.060 William Cheng: So the first iteration i will be equal to zero, we're gonna call Peter create and then the, you know, the argument over here. Again, it's going to be address have a local variable, the local variable over here. It's going to be of the data type P through it. 603 01:21:54.630 --> 01:22:01.950 William Cheng: Okay, so again, when this function returns successfully this local variable is going to contain the threat identifier for the threat, you just created. 604 01:22:02.580 --> 01:22:12.210 William Cheng: That the argument over here. This one is a null pointer over here. So the second argument is always know the third argument over here is a function point or a function point is simply. So what is the function pointer. 605 01:22:12.750 --> 01:22:17.490 William Cheng: Okay function partners is a pointer that point to a function again. What is the function 606 01:22:18.330 --> 01:22:25.680 William Cheng: Okay, we mentioned before, if this is your address space. There's a tech segment, the function is inside your tech stack, man. So, the function is simply 607 01:22:26.400 --> 01:22:33.000 William Cheng: Simply an address. Okay so function pointer is something that refer to one memory location inside your tech segment. 608 01:22:33.840 --> 01:22:40.170 William Cheng: Okay, so again, a function pointer is simply just a pointer is a 32 bit value that tells you where this function is 609 01:22:40.710 --> 01:22:44.670 William Cheng: OK. So in this case, I have a function of your core server. Here's my function server. 610 01:22:44.850 --> 01:22:57.450 William Cheng: The server is of the right type a return voice star and takes one argument over here, which is also a high voice Dar, and this co when you're running your program. This goes inside tech segment right so he said the examiner over here. This is my function server, right here. 611 01:22:58.710 --> 01:23:03.870 William Cheng: There. So again, the third argument over here is simply an address that point to your tech segment. 612 01:23:04.890 --> 01:23:10.830 William Cheng: Okay, the last argument. Over here, over here, I sort of say last. That's what I call an argument. So we call piece or create 613 01:23:11.040 --> 01:23:20.520 William Cheng: As soon as you got Peter create your child processes will start aside. But as soon as you called Peter create a piece of create successful, you're going to have one child thread over here. So running the server over here. 614 01:23:21.240 --> 01:23:30.330 William Cheng: Okay. So in this case, your you know your your chapter start Ronnie. What's going to be the ARG over here the ARG over here will be copied from the fourth argument or Peter create 615 01:23:31.650 --> 01:23:37.410 William Cheng: Okay, so what you can do is, I will be here, the argument of in actually pass the argument of AI and you typecast the voice star. 616 01:23:37.620 --> 01:23:46.470 William Cheng: Okay. So in this case, the first time when you call Peter create I will be equal to zero. So, therefore, when your child to start running over here. Well, then this case they are will be go to zero. 617 01:23:47.190 --> 01:23:52.560 William Cheng: Okay, will you create shelter number one over here is going to be go to y. So if you pass. Again, pass it over here. 618 01:23:52.860 --> 01:23:55.950 William Cheng: Type customer voice star and now the argument over here will equal to one. 619 01:23:56.190 --> 01:24:06.360 William Cheng: So, so if you're if you're chocolate over here, we'll look at the argument over here, your chapter will be able to say, oh, I'm Chelsea number zero. I'm totally, number one. Number two, all the way to say I'm Chelsea number 99 620 01:24:07.410 --> 01:24:14.730 William Cheng: Okay, so this way, every child right over here. What we don't have a way to identify themselves. Okay. You can also pass some other arguments, whatever you want. 621 01:24:14.970 --> 01:24:21.630 William Cheng: This could be also a pointer. It could be a data type of your pointer to a point or whatever you want. Anything that's compatible with a boy star, you can actually pass it. 622 01:24:23.160 --> 01:24:27.780 William Cheng: Okay. So, therefore, what happened is that this function over here. Alright, so, so you create a charter 623 01:24:27.990 --> 01:24:39.090 William Cheng: Charter is no sir running over here. And again, this function is going to be the lifetime of your child threat. So we're going to sort of do some function over here, in the end, when this function is there is when these functions, functions, about to return. 624 01:24:39.300 --> 01:24:44.790 William Cheng: We will return zero again zero is compatible with voice star. And now when you return the chocolate is dead. 625 01:24:45.960 --> 01:24:50.130 William Cheng: Okay, so the idea here is that the other main thoroughfare is going to create 100 Charles where 626 01:24:50.310 --> 01:25:03.480 William Cheng: All the one chapter is going to run the same code over here, maybe using a different argument when they are finished, they will return zero. And now all the Charter would die. Okay, so at some point, it is possible and I will have 101 thread, they're all running parallel 627 01:25:04.230 --> 01:25:12.270 William Cheng: There so so so some of the stuff will die at different times. I don't know who's going to die for us, will not have that dynamic this example over here, just show you how to start up all these threats. 628 01:25:13.770 --> 01:25:27.750 William Cheng: Then alright so so with the end lecture three right now and electric for we're going to sort of talk a little bit more example what happens over here, will you create multiple threats there. What do you address space and we create all these chocolates. Okay. Alright, see you next time.