WEBVTT 1 00:00:01.890 --> 00:00:09.960 William Cheng: Okay, this is the third part of lecture 11 I just remember that I forgot to mention one little thing in the second part. 2 00:00:10.500 --> 00:00:17.850 William Cheng: Whenever you get a page for you get paid for at a single virtual address right the example that I gave is if you access. One of the memory location over here. 3 00:00:18.510 --> 00:00:23.400 William Cheng: For example over here. If it turns out that the access over here is is no access 4 00:00:23.910 --> 00:00:29.820 William Cheng: You're gonna you're gonna cause a trap you trapping the operating system. Okay. So you started out with a single virtual address 5 00:00:30.210 --> 00:00:40.620 William Cheng: So then what do you do, right. So the picture that we saw you know later is that there's an address space implementation that described all your memory segments. They have started dress. They have a length. 6 00:00:41.130 --> 00:00:47.250 William Cheng: So what you will do is that you will use that virtual address when it comes to Colonel, you know, which virtual just give you a page fault. 7 00:00:47.550 --> 00:00:55.920 William Cheng: Then, so now what you have to do is say, you simply do a linear search over here. Well done, this data structure and find out that virtual address which memory segment it belongs 8 00:00:56.460 --> 00:01:03.840 William Cheng: Okay, so for example you know if the address is in this range, whether they belong to the first memory segment. If the address belongs to the second range over here, then it belong to the second 9 00:01:04.170 --> 00:01:11.310 William Cheng: The second every segment. So all you do is to take that virtual address and you do a linear search over here and the as the as region over here. 10 00:01:11.760 --> 00:01:22.950 William Cheng: They are sorted by virtual address okay this example, the smallest ones on the left, the largest one is on the rise. So, therefore, you do a linear search, pretty soon you're going to find out you know which which memory segment. 11 00:01:23.370 --> 00:01:30.420 William Cheng: Which memory seven mini belongs. And then what you do is that you use the information is that as region and try to figure it out if we can resolve that. 12 00:01:32.100 --> 00:01:40.560 William Cheng: You can resolve that page fault, okay. If you cannot resolve that pays for all then you kill the program in the kernel. So, therefore, you know, the user process is killed. 13 00:01:40.800 --> 00:01:45.540 William Cheng: Unless there's a you know signal handler that in that case, you go, you know, go to the user space able to settle handler. 14 00:01:45.930 --> 00:01:50.580 William Cheng: If it turns out that there is a way for you to resolve that problem or. For example, if the data is not, you know, 15 00:01:51.450 --> 00:01:57.030 William Cheng: It's not a physical memory that in that case you will go to the desk right you will ask these objects over here to transfer data. 16 00:01:57.390 --> 00:02:06.030 William Cheng: From the right place into memory and then you change your virtual I just met you go back into the user space program and the user spiritual and we'll try that. Try that member reference. One more time. 17 00:02:06.510 --> 00:02:16.080 William Cheng: That. So again, you know, this list over here is sorted according to the virtual dress and of course you know these memory segment. They are not allowed to overlap. 18 00:02:16.500 --> 00:02:23.730 William Cheng: Right, because, you know, here's, here's a tech segment. Here's the data side, where are you allowed to have them overlap and some virtual dresses. Of course, you know, they're not allowed. 19 00:02:24.150 --> 00:02:37.530 William Cheng: So they have in this case, they don't have any overlap. So, this this particular search. It's pretty straightforward. Okay, so that's how you. That's how you start resolving, you know, a baseball later on in chapter seven, we're going to see more of 20 00:02:39.690 --> 00:02:47.700 William Cheng: Going to see more detail so so that so that you know exactly what to do and also in Colonel three. That's one of them. The main thing that you have to learn how to implement. Yeah. 21 00:02:48.480 --> 00:02:53.820 William Cheng: Alright, so, so we're done with this part. And now we're going to go back to chapter one, as we mentioned before. 22 00:02:54.180 --> 00:03:00.870 William Cheng: Now we know the difference between virtual file system and the actual file system. And now you realize that all the stuff that we talked about a chapter one. 23 00:03:01.410 --> 00:03:07.350 William Cheng: So they are you know part of the file abstraction. So, therefore, you know, we're talking about the the virtual file system. 24 00:03:07.650 --> 00:03:13.950 William Cheng: Okay, in chapter six. We're going to see how the data can be laid out on the disk. And then we're gonna talk about the actual file system. Right. 25 00:03:14.730 --> 00:03:21.720 William Cheng: Okay, so this is the last set of slides in Chapter ones over here we're going to start talking about files. The first 13 slides. 26 00:03:22.230 --> 00:03:29.250 William Cheng: overlap with the the earlier time when we talk about chapter one. So I'm just going to sort of flip through the slides over here. 27 00:03:29.730 --> 00:03:38.250 William Cheng: We talked about what the thought abstraction is what the file system API is what are the special file descriptor the file descriptor table. We've gone through some examples over here. 28 00:03:38.520 --> 00:03:49.410 William Cheng: And we also show you how you can redirect the IO. We also show you the rules of allocating fault table and verify the script to entry. Will you try to create a new file, you know, etc. 29 00:03:50.250 --> 00:03:59.880 William Cheng: And then we come to the last slide over here and this is the you know what will. What will the last time when we come to chapter one. This was the last slide. And now we know that there's more detail right because because 30 00:04:01.260 --> 00:04:11.790 William Cheng: This picture over here. We're actually we're talking about the other virtual classes, then and now we know that on the right hand side, there's more stuff right there's the either on the right hand side over here, there is the actual process and all the right stuff. 31 00:04:12.360 --> 00:04:18.750 William Cheng: Okay, so, so again, now we're going to sort of still focus on you know the the virtual POS system because we're still in chapter one. 32 00:04:19.740 --> 00:04:21.900 William Cheng: Okay. So in this case, again let that so sort of 33 00:04:22.440 --> 00:04:31.410 William Cheng: remind ourselves, you know, what is actually inside the virtual POS system. And there's a file abstraction. Right. And then for the fall. What can you do to the file. Well, you can without being in the previous example in the fall, I 34 00:04:32.280 --> 00:04:36.540 William Cheng: Know that the other sort of the I know points that points to. You need to be able to create a file. 35 00:04:36.810 --> 00:04:43.560 William Cheng: So how do you create a file, right. So if you're inside the virtual file system, you can start to create a file the virtual file system doesn't know how to create a file. 36 00:04:44.340 --> 00:04:53.970 William Cheng: Right. Why doesn't know how to do that, right, because the act in order for you to create a file, you need to know what is the data structure on desk so therefore you need the actual of us to do that. 37 00:04:54.660 --> 00:05:01.350 William Cheng: Okay, so therefore on the verge of a system. There's nothing you can do other than call a function is that the actual process that and the actual file system will create a powerful you 38 00:05:01.680 --> 00:05:06.510 William Cheng: And then once you create a file. If you want to write to the file. Does the virtual file system know how to write to the file. 39 00:05:06.750 --> 00:05:16.890 William Cheng: I mean, clearly the verge of buses and has no clue how to write to the file. So in that case, again, what it needs to do is the is to find the function pointer and then call out the actual file system, the actual fosters that will help you to write it down to the bottom. 40 00:05:17.700 --> 00:05:27.150 William Cheng: Okay, so. So anyway, the virtual file system. It's pretty boring. Okay. Because in the end, it can't really do anything real because anything real you need to be needs to be done inside the, you know, inside actual file system. 41 00:05:28.170 --> 00:05:34.680 William Cheng: So, so, so, since we're doing chapter one. We're going to sort of see the rest of what the virtual file system is supposed to do. Yeah. Alright. 42 00:05:35.940 --> 00:05:38.190 William Cheng: So, so sort of give a quick review of 43 00:05:38.520 --> 00:05:43.860 William Cheng: You know why, but will you make an open system called when you make the racism called will you make the racism, because it made made the closest them call 44 00:05:43.980 --> 00:05:49.890 William Cheng: So again on top over here where the user space and the bottom over here with kernel space. We're not joining any of the hardware here. 45 00:05:50.310 --> 00:05:57.030 William Cheng: Before we saw a bunch of stuff happening, the processes subsystem and now things gonna happen inside the cell, the file subsystem. Okay. 46 00:05:57.360 --> 00:06:03.300 William Cheng: Alright, so let's start with the open system called right or we hear open system called the first argument over here is going to be a file system path. 47 00:06:03.690 --> 00:06:05.820 William Cheng: You need to try to find a particular file over here. 48 00:06:06.480 --> 00:06:15.360 William Cheng: So in this goes, what we'll do is is is a system called going to tap into the operating system. Right. So we start with open and as soon as you execute that we try and do the ordinances there and what the organism. 49 00:06:15.690 --> 00:06:20.910 William Cheng: Has to do right now is to make sure that this file exists. Okay, if you try to open a file right so 50 00:06:21.390 --> 00:06:26.790 William Cheng: Depending how you want to open it. If you want to open this file to create a file. And in that case, you have to make sure that it doesn't exist. 51 00:06:27.180 --> 00:06:32.220 William Cheng: But if you want to open the file for reading. I mean, our example, have you tried to read a file, then we're going to make sure that this exists. 52 00:06:33.000 --> 00:06:40.080 William Cheng: That so so so this pretty operation over you're going to try to the file system. And now the file system actually has gone through a pretty long process. 53 00:06:40.290 --> 00:06:44.670 William Cheng: Tried to find out whether this file exists or not. Right, so they don't. I'm going to sort of see exactly what what 54 00:06:44.850 --> 00:06:53.880 William Cheng: Has to be done right, but the basic idea over here is that, you know, at some point over year, you know, the we're going to make sure that you have access right to this file, this file exists. 55 00:06:54.360 --> 00:06:58.590 William Cheng: So again you warm up one and two, you know the difference between a file exists or not or whether you have access to 56 00:06:59.460 --> 00:07:07.590 William Cheng: That. So if you have access to the file, this file exists. Well, in the hope is that we're going to sort of build this path, all the way to this particular file. So, so where's this part. 57 00:07:07.770 --> 00:07:11.700 William Cheng: This part is sitting on this somewhere. So there's food that takes over here sitting on this. 58 00:07:12.450 --> 00:07:14.100 William Cheng: Okay, so when he said, open the file the of the 59 00:07:14.340 --> 00:07:21.060 William Cheng: The other opportunities and actually is going to build this long list of data structure started out with the fallout jazz or so we need that there's a file descriptor table. 60 00:07:21.240 --> 00:07:25.080 William Cheng: And there's the file object over here the file object points to the I know table, the 61 00:07:25.320 --> 00:07:34.080 William Cheng: Table is going to point to something and eventually is also going to point to it. As I mentioned, it's going to point to the device driver, the device driver know how to get data from the desk right so so we're sort of 62 00:07:34.620 --> 00:07:40.140 William Cheng: You know, build a really long list of objects over here that eventually pointed this data on that they are on the desk. 63 00:07:40.890 --> 00:07:48.030 William Cheng: Okay, I'm clearly at the level of the virtual file system, the virtual bosses that has no idea how this is done. Right. So all the nose is how to make a function call. 64 00:07:48.420 --> 00:07:51.690 William Cheng: Into the actual process me the actual file system will make sure that everything is set up. 65 00:07:52.410 --> 00:08:02.130 William Cheng: Okay, so in the end if you are capable will open this far, lots of data structure inside a kernel is being is going to be set up and then in the end, you know, we're going to allocate an entry instead of file descriptor table. 66 00:08:02.370 --> 00:08:07.440 William Cheng: Yeah, I'm going to return the array index into the file descriptor table and that will be the return value over here. 67 00:08:08.280 --> 00:08:14.700 William Cheng: That. So when what's up. If you can actually open this file. We're going to sort of check access right you know make sure that the Father exists all that's kind of saw 68 00:08:14.970 --> 00:08:18.750 William Cheng: Where we're done over here, right. We know that eventually we're going to put into this date on the file. 69 00:08:19.050 --> 00:08:22.290 William Cheng: Okay, we also want the beginning right the beginnings can be a file descriptor table. 70 00:08:22.530 --> 00:08:31.890 William Cheng: An entry is that if I just go to the table. Is there a point in the fall object, the object is going to point to, you know, the I know Tabor, all that kind of stuff there many, many leveling direction, but eventually will point to the desk. 71 00:08:32.580 --> 00:08:38.580 William Cheng: Okay, so the open system is actually very complicated. It's only involved many, many things inside of our system, but eventually will set up this path. 72 00:08:38.760 --> 00:08:45.300 William Cheng: And if everything is OK. You will allocate a new entry. Again, the way you allocate this new entry instead of a descriptor table in chapter one. We talked about the rule, right. 73 00:08:45.450 --> 00:08:50.430 William Cheng: You go down this list, you find the first spot that's open or you have to add okay that that about descriptor table entry. 74 00:08:51.510 --> 00:08:54.780 William Cheng: And then eventually when everything is fine. Over here you return to follow the script over here. 75 00:08:54.960 --> 00:09:03.540 William Cheng: Into the user space. And now you treat the father script, but as a handle into the ordinances there. So next time we need to call read or write a closed, you need to pass the head back to the office up 76 00:09:03.870 --> 00:09:15.420 William Cheng: There. So in our example here. I'm going to call the racism call. So the first argument here is that would be the handle. And so in this case we for the Unix and Linux system. You notice it's an array index into the file descriptor table. 77 00:09:16.230 --> 00:09:22.740 William Cheng: And then the second argument over here is going to be the data that you need to count on me to copy data into. And then the third argument is that how many 78 00:09:22.950 --> 00:09:29.550 William Cheng: How many bytes of data you want to, you want to get copies of this buffer. OK. So the third argument here is going to be the maximum number of bytes. 79 00:09:29.760 --> 00:09:39.150 William Cheng: Typically is going to get the size of this buffer. So this one is a little weird over here. I say, I only want to read up to 100 bytes for whatever reason, right, it doesn't really matter what the reason is. Just as an example. 80 00:09:39.900 --> 00:09:43.500 William Cheng: Right, so we are going to make the system called right the reader, we're gonna try this out of Colonel 81 00:09:43.680 --> 00:09:49.350 William Cheng: And then in this case the file descriptor over here. I'm going to make sure that this entry over here is done now, right, because it is possible 82 00:09:49.500 --> 00:09:55.830 William Cheng: You know, in between you close the file or something like that. So, so, so why is it found that the other file descriptor entry just using an array index. 83 00:09:56.040 --> 00:10:00.210 William Cheng: Make sure this one doesn't point to a null pointer, then I will follow the logic over here. 84 00:10:00.480 --> 00:10:09.390 William Cheng: Why does that get into the fall object. How do I read data from the desk. So again, the virtual person doesn't know what to do. So all it will need to do is to use the file object. There's a real function pointer. 85 00:10:09.570 --> 00:10:13.710 William Cheng: You go through the reef function over here and then the actual file system will do the reading for you. 86 00:10:13.980 --> 00:10:21.360 William Cheng: Right. So what would the actual file system do it right the actual busting them over here will take them off on this and then copy into this buffer in the user space. 87 00:10:21.870 --> 00:10:28.620 William Cheng: Okay. How does it do that why it's a lot of work to get that done. So while you're working on your kernel three you can actually sort of figure out exactly how this is done. Okay. 88 00:10:29.280 --> 00:10:32.580 William Cheng: Alright, so now I'm just gonna assume that some magic is going to happen eventually data. 89 00:10:32.850 --> 00:10:41.610 William Cheng: Transfer over here and the return value is going to be the number of bytes. I get copied into this this buffer. So in this case, the number of bytes over here has to be less than or equal to 100 bytes. 90 00:10:42.060 --> 00:10:46.080 William Cheng: Okay, if it goes over here, we only asked it to read the maximum 100 bytes there. 91 00:10:46.740 --> 00:10:51.720 William Cheng: So, so in this case it will return to you. And then, then what you can do is that, and also read before we turn over here. 92 00:10:51.840 --> 00:10:59.910 William Cheng: Inside of our agenda, we need to update the cursor position right so so let's say you return 51 or something like that. So in this case, of course, the position is going to be 51 93 00:11:00.090 --> 00:11:06.690 William Cheng: So next time. Will you try to read it again. Now we're going to start reading from fall offset 51 and then start reading the next so many number of bytes. 94 00:11:07.470 --> 00:11:12.360 William Cheng: OK. So again, this example, we're, we're only read was The next time over here. We're going to call close 95 00:11:12.570 --> 00:11:21.030 William Cheng: When we close close over, you're going to get troubles that option. Is this a use the star descriptor as an array index over here. And then what we need to do is that we need to change this into a null pointer. 96 00:11:21.900 --> 00:11:27.090 William Cheng: Okay, so remember the fall logic has a reference count. So when you remove a pointer to the fallout. Do you have a detriment the reference 97 00:11:27.570 --> 00:11:37.800 William Cheng: In the reference on go to zero, you can actually destroy all these things right. If the reference count doesn't go to zero, that means that there's something else inside the operating system still using this fall. Why, in that case, you leave it alone. 98 00:11:38.550 --> 00:11:44.760 William Cheng: OK. So again, we're going to use reference counting mechanism inside the colonel to determine whether an object can be free or not. Yeah. 99 00:11:46.230 --> 00:11:54.510 William Cheng: Alright, so we're here you know here says, you know, so one thing that has happened is, is that when you close the file descriptor this pretty good answers over here is going to be zero and that is going to point to know 100 00:11:54.660 --> 00:12:02.640 William Cheng: And now you can reuse it. And in this example over here the file object, the reference Congress still bigger than zero, so therefore going to keep it around inside the cardinal alright 101 00:12:07.590 --> 00:12:11.310 William Cheng: Alright, so the bomber jacket and not not the advocate a good reference cards because zero 102 00:12:13.410 --> 00:12:20.700 William Cheng: So, so that's sort of a quick review of what happened inside the file system, will you make open system called followed by read and then followed by close. Yeah. 103 00:12:21.390 --> 00:12:29.490 William Cheng: Alright so what. So what's important to remember is that, you know, when over here. So, so as I mentioned this, you know, every call to open 104 00:12:30.210 --> 00:12:43.200 William Cheng: Yeah, whenever you call open it will create a new entry in the file in this is about about table. Another way to say is that every time we open a return successfully he will create a new file object. 105 00:12:44.250 --> 00:12:49.920 William Cheng: Okay, so that's just the way Unix and Linux implement this, you know. So whenever you call open. They never share an existing 106 00:12:50.610 --> 00:12:57.510 William Cheng: Is in the file object, it always create a new one. Right. So when he creates a new I the reference code is going to equal to one, two Christmases you're going to be able to zero. 107 00:12:57.720 --> 00:13:03.930 William Cheng: And then, you know, however you open the file that goes into file access and then the, the, I know pointers or the point you know i put upon the rest of the way. 108 00:13:04.620 --> 00:13:08.400 William Cheng: OK. So again, these are the things that happened at the virtual file system level right 109 00:13:08.760 --> 00:13:18.330 William Cheng: Alright, so let's take a look at these example to see what the, what would the Fallen code do I'll be here. So what I will do is I will, I will, for coffee shop process. Right. So the code over here is going to be executed a child process. 110 00:13:18.510 --> 00:13:23.190 William Cheng: The parent over here. Continue right here. So the parent typically what we'll do is that a wave, which are process to die. 111 00:13:23.520 --> 00:13:31.260 William Cheng: Yeah. So in this case, what would a child processes do right the child classes will close by the script or one and close out this script or two. So now the trial process will 112 00:13:31.860 --> 00:13:37.260 William Cheng: Only zero entry zero be he'll be back all the other one, the one and two over here. They're all pointed out. 113 00:13:38.010 --> 00:13:43.410 William Cheng: There. And then what it will do that will make the open system calls. So this one I'm going to call it you know home slash species. 114 00:13:43.710 --> 00:13:51.720 William Cheng: Output. I'm going to open this for right only because I know that, you know, according to the rule of allocating file descriptor. When I call open successfully. 115 00:13:52.260 --> 00:13:58.950 William Cheng: The return value over here has to be able to walk right because that will be the first entry over there that's available and next time when I call open the second time over here the return 116 00:13:59.580 --> 00:14:12.300 William Cheng: Again to to be to that. So in this case, the first argument over here. They are both the same file. So what I'm doing over here is I'm redirecting both the standard standard output and standard error to the same file. 117 00:14:13.440 --> 00:14:16.110 William Cheng: Yeah. So in this case, you know, from from this point on, 118 00:14:16.320 --> 00:14:24.750 William Cheng: If I, if I secure program over here for the program needs to write to standard out. Where would it go right it will go to file descriptor one would just a home site. He says, I'll put 119 00:14:24.930 --> 00:14:30.660 William Cheng: When he tried to print an error message board ago why it's called to standard error over here, they naturally will go to exactly the same file. 120 00:14:31.260 --> 00:14:40.020 William Cheng: Okay, so, so if that's what you want that this is the kind of code that you will. Right. Right. But we need to understand exactly what you're getting. Okay, so let's sort of all, you know, draw a picture like this. 121 00:14:40.890 --> 00:14:42.840 William Cheng: So after we execute closer and closer one 122 00:14:43.050 --> 00:14:53.100 William Cheng: This one become a no entry and this will be kind of nice entry. And now what we call open over here. I went to their new file object and then going to allocate the first available entry will be here to pointed out why. So when we're done. It will look like this. 123 00:14:53.880 --> 00:14:57.780 William Cheng: Okay, so in this case the file descriptor number one over here will point to this fall. I just 124 00:14:58.380 --> 00:15:05.100 William Cheng: Pointed this file descriptor will be an array index into this hard scripted table. And then over here we have a file object pointers all point to the object. 125 00:15:05.310 --> 00:15:13.830 William Cheng: reference counting go to zero the world we open the file. We're going to copy this information over here into into the demo right here. So, this one will remember how this was open. 126 00:15:14.040 --> 00:15:19.260 William Cheng: Right, and then the cursor position is reset to zero. And then the I know point is over here. And where does the point to 127 00:15:19.530 --> 00:15:29.220 William Cheng: Where is it again. It points to the actual process there. But we know that, you know, if you follow the pointer. You have to keep following the partner. Eventually this I know partner will point to slash home site species are y'all put 128 00:15:29.730 --> 00:15:37.140 William Cheng: Okay, however, that's do that at the virtual classes that we don't know how that's done right. We just trust that the actual file system will build that path to point to that file. 129 00:15:38.250 --> 00:15:46.410 William Cheng: So continue to execute a second function call over here. So I'm going to call open exactly the same arguments over here. Every time we call open, we're going to end up with a new file object over here. 130 00:15:46.680 --> 00:15:56.250 William Cheng: Okay, so again the file object reference call equals zero, the mode over here will be the same thing location will be zero. And now the return of the script over here. Be equal to two. And then this point, at what point right here. 131 00:15:57.330 --> 00:16:05.340 William Cheng: That so they will look like this. Okay, so this object. And these are these are two different far larger. What about the I know pointer to day point to the same thing. 132 00:16:06.600 --> 00:16:12.210 William Cheng: Okay, well, we know that this one points into the actual process. And when it points to the actual Wasilla in today's show the same pointer. 133 00:16:12.690 --> 00:16:24.150 William Cheng: What we don't know that depends on the implementation of the actual process them right because at the virtual bosses and I'm over here, we have no idea how the actual file system works. We just know that if we if we have a real function pointer. We call the function, everything will 134 00:16:25.200 --> 00:16:31.470 William Cheng: Everything will work just fine. Okay, so we're going to trust that those kind of function. The function pointer works. So, India, we know that, you know, 135 00:16:32.160 --> 00:16:43.020 William Cheng: Maybe this one will eventually wanted this file this pointer over here, eventually they will also point to the same file. So at the beginning they started at different point at some point they're gonna start doing to merge and then in the end it will point to the same thing. 136 00:16:43.590 --> 00:16:48.090 William Cheng: Okay, exactly where they merged. We have no idea because we don't know how the actual file system is doing this. 137 00:16:48.660 --> 00:16:57.300 William Cheng: OK. So again, don't assume anything about the actual file system. At this time, but this part over here since is the virtue of us that we know we're learning how the virtual POS system work. Okay. 138 00:16:57.540 --> 00:17:06.000 William Cheng: Every time he kept hoping you get a brand new file object that's just the rule that right. So this guy's gonna start writing your program. Right. So, for example, 139 00:17:07.230 --> 00:17:14.760 William Cheng: The child classes over here at a program where this whole process over here right 100 points to standard out. Okay, so what you wrote was a bystander. I 140 00:17:15.090 --> 00:17:19.980 William Cheng: Guess standard always file descriptor one over here. So now this picture over here against the Charles 141 00:17:20.520 --> 00:17:32.340 William Cheng: Space over here so far descriptor number one over here, you're going to write this one. So the way that you perform the you know the the right function, I will you pass that information to the actual process that you need to pass the current, current position. 142 00:17:32.820 --> 00:17:37.020 William Cheng: Right, because what you do is that you, you know, writing the data to the, you know, to, to that particular file. 143 00:17:37.590 --> 00:17:42.660 William Cheng: You know, so what do we need to do is that you need to tell the actual of us is that, where do you start where do you start writing 144 00:17:43.320 --> 00:17:52.350 William Cheng: Okay, so, so, so, you know, the, the, the file can be a really, really big file. Right. Do you want to start from the beginning, do you want to start from here we're going to start writing data. So that's called the cursor position. 145 00:17:52.830 --> 00:18:02.490 William Cheng: Okay, so what do we do is that you will Kurt you'll take the fall location over here also the curses you pass it into the actual file system. So the actual file system over here well right 100 bytes over here into this file. 146 00:18:04.020 --> 00:18:08.940 William Cheng: Okay, so now what do we do, is that whenever he turns over here it's going to return the number of bytes address. And so let's assume that 147 00:18:09.060 --> 00:18:18.420 William Cheng: 100 bytes has been written over here. So when this one is done this location is going to get modified to become 100 so next time, will you call it the racism call again it will start writing at location 100 148 00:18:19.770 --> 00:18:26.340 William Cheng: Okay, so that's why we need to remember the cursor position because when you try to write you continue from where you left off, right, because that's part of the context. 149 00:18:26.580 --> 00:18:35.340 William Cheng: Of your program you know where you are writing you know inside a particular file. Okay. All right. But what if the next time over here. You don't want you to, you know, right, to say, what do you think righteous standard error. 150 00:18:36.480 --> 00:18:39.600 William Cheng: Okay, if you're ready to stand there again, you know, the racism cause pretty dumb. 151 00:18:39.840 --> 00:18:48.360 William Cheng: Right you spot the script or two over here. We're gonna follow the ponder over here. We're going to take the offset zero here, pass it to the actual processor. So we're, we're going to end up writing the error message. 152 00:18:48.750 --> 00:18:56.520 William Cheng: Well, the error message is going to hear a star ready and memory. Look, the signing a fall off zero. So we're going to end our wiping out some of the data we have written out 153 00:18:57.870 --> 00:19:07.770 William Cheng: To the file that. So, therefore, when you call. So, so why is the location will be become 100 but you know the file the cursor position for for the other file object is still seeing a zero. 154 00:19:08.580 --> 00:19:16.710 William Cheng: Okay, so therefore, when you write to file descriptor to over here we're going to end up wifey me out that wiping out the data that you have written previously, is that OK. 155 00:19:17.730 --> 00:19:23.070 William Cheng: OK, so if that's what you want. And that's okay. But if that's not what you want. The ratio code this way. Okay. So as it turns out that 156 00:19:23.370 --> 00:19:26.340 William Cheng: The reason this behave this way is because of the way we roll our code. 157 00:19:26.610 --> 00:19:31.710 William Cheng: That the organism, just follow your instructions. If you say you know when you tried to write your standard standard error. 158 00:19:31.920 --> 00:19:39.540 William Cheng: You want to wipe out that the data instead of out. What know you write the code you as the auspices of to execute call like that. What they will do that for you. 159 00:19:40.350 --> 00:19:47.700 William Cheng: Know that so so if you don't want this to happen. What should you do right what you should do is I, you should you should be what you want is that you want to be able to share the same file object. 160 00:19:48.000 --> 00:19:58.650 William Cheng: You understand that are in the sense in the air to share the same file object. So in this case, once you finish writing 100 bytes of data into the file and next time. If you read the standard error, you want to start fo offset 100 161 00:19:59.490 --> 00:20:08.280 William Cheng: Again, so the only way to do it is for them to share the same project, right. So in this case you should not call open twice because every time you call obey you get a brand new file object. 162 00:20:09.600 --> 00:20:10.560 William Cheng: So, therefore, what should you do 163 00:20:10.830 --> 00:20:17.910 William Cheng: We go. We're going to learn a new this new system call. So we're gonna still do the same thing. I'm going to close one close to over here. Then we're going to do open wise, we're going to get a new file object. 164 00:20:18.090 --> 00:20:21.480 William Cheng: And then the next system call over here to tell us to share a file object. 165 00:20:21.960 --> 00:20:28.980 William Cheng: Okay, the name of the system called is called dupe. Okay, so do what we're here to say that I want to share file descriptor. Number one, and give me a 166 00:20:29.310 --> 00:20:34.080 William Cheng: Give me another new file descriptor again. How do you allocate a new file descriptor. I will follow exactly the same rule. 167 00:20:34.350 --> 00:20:37.200 William Cheng: We go to the file descriptor type of find the first entry is going to be 168 00:20:37.500 --> 00:20:43.860 William Cheng: That's available. So in this case we are we're using while we're using zero. So, therefore, the next available entry over here is equal to two. 169 00:20:44.070 --> 00:20:50.520 William Cheng: And then we're going to share a file descriptor one and two over here and they will point to the St. Paul object. Yeah. So the picture look like this. 170 00:20:51.210 --> 00:20:59.430 William Cheng: Because, again, we closed one and close to these tools to become null pointer. So when we call open, we're going to create a new file. I've never saw that before. And that's exactly what happened is before 171 00:20:59.610 --> 00:21:05.250 William Cheng: So now we call dupe right so. So again, what we do is that we're going to make a duplicate of our, you know, 172 00:21:05.850 --> 00:21:12.780 William Cheng: We're gonna make a duplicate of file descriptor one into the new file descriptors. Okay, I'm going to come down this file descriptor tab over here. 173 00:21:12.990 --> 00:21:22.860 William Cheng: Find the first entry that's available that will be file descriptor to right here and then this one will be sharing the same a follow up debt as file descriptor one right so they will going to point to that one like this, right. 174 00:21:23.070 --> 00:21:28.590 William Cheng: And now we need to change the reference gotta go to to because now we have two pointer pointing to exactly the same object. 175 00:21:28.980 --> 00:21:39.060 William Cheng: That so we're done. It will look like this. They thought the script for one and two, both of them share the same file object over here. So when you try to perform I write operation for 100 bytes. So I using standard 176 00:21:39.780 --> 00:21:42.990 William Cheng: When you finished writing it. The follow up cursor position will be equal to 100 177 00:21:43.170 --> 00:21:53.010 William Cheng: And next time when you try to write your standard error when this guy is, again, we're going to pass the cursor position of 100 into the actual process. Now, so therefore the error message will appear after the first 100 178 00:21:54.090 --> 00:22:02.670 William Cheng: OK. So again, if that's what you want. You were like, whoa, like this, but if you prefer the previous behavior that you will open the file twice, you get the ZTE how you want to write your code. 179 00:22:03.090 --> 00:22:14.130 William Cheng: Okay. Both ways are correct. Okay. But again, your coach should reflect your intention. So you need to express your intention, you know, clearly, your code, right. Otherwise, you know, bad things gonna happen. Yeah. 180 00:22:18.480 --> 00:22:25.110 William Cheng: All right. There are also other you know tube system call. I think there's a dupe too. I also think there's a dupe three still got a few 181 00:22:25.560 --> 00:22:30.420 William Cheng: So when you're doing your kernel to assignment. If you see a function that you have never seen before, that you have to implement 182 00:22:30.600 --> 00:22:43.410 William Cheng: All you need to do is look at the man pages on the next right. So in this case, if you don't know what to to as you say man do to right and then read the man is very carefully to understand, you know what, what should be the behavior or that you have to implement 183 00:22:44.400 --> 00:22:49.620 William Cheng: OK. So again, you know, if it's Colonel to you should read the comment blog and all that kind of stuff. Yeah. All right. 184 00:22:51.480 --> 00:22:59.460 William Cheng: Okay, so, so, so over here says you know what, if you actually, you know, allocate a file descriptor entropy for the fork. 185 00:23:00.000 --> 00:23:04.290 William Cheng: Okay. So in this example, we call for quite hear it but right before we actually will open a file. 186 00:23:05.280 --> 00:23:14.400 William Cheng: Okay, so again, you know, the fallout data is inside. Inside the current also therefore they're sort of considered to be part of your extended address pay they keep track up the you know the 187 00:23:14.730 --> 00:23:18.030 William Cheng: The open for state for you, right, what is the current position, all that kind of stuff. 188 00:23:18.450 --> 00:23:24.330 William Cheng: Okay. So in this case, once you you know so so well. Well, once you open the file And now we mentioned before. 189 00:23:24.600 --> 00:23:30.450 William Cheng: The extent that address space survive the exact the exact address space also survived fork mess. Oh yeah, well, you 190 00:23:30.690 --> 00:23:40.560 William Cheng: When you make the fourth system call you make a copy. You are you taking a parent process and you you create a child process and the child process gets a copy of the address space. 191 00:23:41.010 --> 00:23:50.460 William Cheng: We're guys. But again, I'd be very, very careful to say that the child get a copy of the address space. I didn't really say exactly what happened to the extended address me as it turns out, you're sharing the extended address space. 192 00:23:51.150 --> 00:24:03.030 William Cheng: Yeah, so therefore once we open a file, you know, and they said, we remember we remember that instead extended I guess phase. Now, when we perform a phone system call. Well, then in that case the child. The child will share the parents extended outer space. Yeah. 193 00:24:03.630 --> 00:24:10.260 William Cheng: So the pictures don't look like this is over here again the code over here that is I'm going to open up all over here. So at the beginning. We all have the parents address space over here. 194 00:24:10.440 --> 00:24:14.340 William Cheng: The parents executing this code, we try to open the fall. We're going to quit and you'll follow me here. 195 00:24:14.730 --> 00:24:28.110 William Cheng: Despise going to call log. It's right only. So we're going to allocate this entry over here 012 345-501-2345. This is number five over here. So in this case, log file over here will be equal to five. 196 00:24:28.440 --> 00:24:34.110 William Cheng: Okay, so we're going to allocate a new file object over here. I'm going to point to it. So this will be gone. So we're done. It will look like this. 197 00:24:35.250 --> 00:24:39.210 William Cheng: So long time over here will be equal to five. Okay, so now we're going to go 198 00:24:39.720 --> 00:24:43.200 William Cheng: We're going to fork over child process when we for carpet tile process. We're gonna say 199 00:24:43.410 --> 00:24:54.390 William Cheng: When I create an address space for the child again by copy the parent address space. So when you copy the paragraph spaces. The child is a child log file is also global and local variable and this will be equal to five. 200 00:24:54.780 --> 00:25:03.150 William Cheng: Okay, and then every process has their own file descriptor table, right. So, therefore, you know, the child processes over here also has a file descriptor tables. In this case, what we'll do is 201 00:25:03.450 --> 00:25:07.470 William Cheng: We'll take the parents file descriptor table will make a copy into the child file descriptor table. 202 00:25:07.650 --> 00:25:15.360 William Cheng: When you copy pointer and they point to the same place. So in the fifth entries over here. We also got appointed this object. And now the reference count over here. It's going to be equal to two. 203 00:25:16.440 --> 00:25:21.150 William Cheng: Okay, so what this operation is done right, it will look like this. Okay, so, so, so 204 00:25:22.080 --> 00:25:28.140 William Cheng: So in this case, once we're finished the fork over here, then you know he's not a colonel, it will it will look like this. 205 00:25:28.350 --> 00:25:36.990 William Cheng: And now we see two different ways for the reference, how to go to to write. One way is for you to make a dupe system call and the other way is that when you forgot the trial process. 206 00:25:37.200 --> 00:25:46.500 William Cheng: So when we create the file before you do the fork and at the time when you do a fork and now you know the small object that will have a reference column bigger they will be there equal a bigger than one. 207 00:25:47.580 --> 00:25:54.150 William Cheng: That is an example over here at the parent over here is going to write to the log file, right. So again, how does the parent right to the log file or unlawful. 208 00:25:54.360 --> 00:26:04.320 William Cheng: equals to five you'll follow this pointer over here and then you will pass the cursor position over here into the actual file system. And so that's it over here. You, you, you write 100 bytes right when this 209 00:26:04.830 --> 00:26:09.780 William Cheng: Is done successfully, you know, the cursor location over here is going to be equal to 100 210 00:26:10.350 --> 00:26:21.210 William Cheng: Okay, so what if the child also want to run right to the same file the channel also tried to write to file descriptor number five over here. Why, in that case, again, it will start writing and cursor position 100. Okay. Is that what you want. 211 00:26:22.290 --> 00:26:25.410 William Cheng: Okay, so if that's what you want. They'll be fine for these two processes. 212 00:26:26.790 --> 00:26:35.430 William Cheng: To share this file object. So sharing. So every dangerous, right, it is possible that a parent and a child they try to write it as far as exactly the same time on in that case. 213 00:26:35.610 --> 00:26:41.160 William Cheng: That behavior is going to be unpredictable. Right. Or if the parent or the child tried to one tried to read on one side, right. 214 00:26:41.340 --> 00:26:48.420 William Cheng: Oh, when they try to read at the same time you one of the finishes early over here, then he will update the cursor position. One of the other processes will read from a different place. 215 00:26:49.320 --> 00:26:58.230 William Cheng: Okay, so this kind of sharing over here. Again, it's very, very dangerous. So this is, this is kind of behavior that you want. That's perfect. Right. Yeah. But otherwise, if it's not, then you shouldn't read your code this way. 216 00:26:59.070 --> 00:27:04.830 William Cheng: Okay, so, so, so one. So what, why would somebody want to do that. Right. So maybe the long side over here. Maybe the parent and a child wants 217 00:27:05.220 --> 00:27:12.960 William Cheng: To share file. So let's say here's a follow on the desk over here called log file. So that's it. The parent always right to the first part of the file and the chart over here, right. The second part of our 218 00:27:13.110 --> 00:27:18.210 William Cheng: So in that case, maybe there's a way for the parent and child to communicate by writing data to a shared file. 219 00:27:18.900 --> 00:27:25.560 William Cheng: Guys, because right now when you do things like this. The log file here, become a shared file. Okay, so the question is that, you know, is this a really good idea. 220 00:27:25.950 --> 00:27:35.940 William Cheng: Okay, we haven't really talked about to process it. How do they actually you know coordinate with each other so they can share a file so far in fifth edition Unix. There's no way for them to actually share a file share file nicely. 221 00:27:36.810 --> 00:27:42.330 William Cheng: Okay, so in that case, when both of them try to you know update a cursor position at the same time, bad things might happen. 222 00:27:43.140 --> 00:27:50.280 William Cheng: Okay, so what will be the right way for the parent of a child they tried to sort of communicate with each other. Okay, so there's one way to do it. And that's what we're going to look at next. 223 00:27:51.090 --> 00:27:58.380 William Cheng: Is over here. So, so that the parent child process can communicate using such as share file descriptor, although it's difficult to synchronize that 224 00:27:58.950 --> 00:28:07.620 William Cheng: Okay, so they're going to end up sharing the cursor position. And once you, you know, read or write from a file. The person has us going to change. So the other process is going to pick up the current 225 00:28:07.860 --> 00:28:16.620 William Cheng: Value of the cursor position. So in that case, it can be really bad. Okay. All right. So one way to do it for the to process to communicate is to use a Unix pipe. 226 00:28:17.070 --> 00:28:27.810 William Cheng: Okay, so a pipe is a means for one process to send data to another process directly as we are right into a file. So how do you do as it were writing to a bar or use a file descriptor. 227 00:28:28.080 --> 00:28:31.590 William Cheng: You could use a file descriptor right we use the file descriptor, you're going to write to the file. 228 00:28:32.010 --> 00:28:36.390 William Cheng: So here's an example or whatever. You know what a pipeline guy so so again it's like a water pipe. 229 00:28:36.810 --> 00:28:44.850 William Cheng: Okay, you have one process over here. And so it's a standard process, it will, right, to the right end of the pipe another process over here will read from the region at the pipe. 230 00:28:45.060 --> 00:28:49.320 William Cheng: Right, so this end of the part over here is write only and this end of the pioneers read only. 231 00:28:49.800 --> 00:28:54.690 William Cheng: Okay, so you have two processes they attached to a different end of the pipe and now they can communicate with each other. 232 00:28:55.110 --> 00:29:03.270 William Cheng: Okay, the pipe is a uni directional object over here. So one process over here. We're right, to the right and and the other one will refund the reader. So how do you implement a pipe. 233 00:29:04.320 --> 00:29:08.520 William Cheng: While you actually already know that how to do that you use a producer consumer problem. 234 00:29:09.960 --> 00:29:12.330 William Cheng: Right, so, you know, this will be the producer right and this will be the come 235 00:29:13.350 --> 00:29:18.780 William Cheng: Over to this is the producer. This will be the consumer. So you already see the CO how to implement a Unix pipe. 236 00:29:19.350 --> 00:29:26.580 William Cheng: Okay, so, yeah, we can use a finite buffer right and then we'll use a conveyor belt or something like that. And this way you know one process could right and then the other. 237 00:29:26.730 --> 00:29:33.840 William Cheng: One can read. You can read it in parallel. And right in parallel, right, or if they're writing and reading on the same spot. Well, in that case, one process has to wait, yeah. 238 00:29:35.730 --> 00:29:42.600 William Cheng: So the way you create a pipe pipe is an object inside a corner and now we notice a producing consumer is the instance of producing consumer problem. 239 00:29:42.990 --> 00:29:55.050 William Cheng: So how do you create a pipe things out of Colonel, there's a system called for you to create a pipe and you know the the system called, it's called pipe. What do you will do is that this function will return to you to file descriptor one for writing to the pipe at the animal for reading 240 00:29:56.430 --> 00:29:59.250 William Cheng: Reading from the type of so therefore the code will look like this. 241 00:30:00.060 --> 00:30:10.650 William Cheng: That. So what we'll do is that you first you need to, you know, allocate to your two integers ones to receive the file descriptor for the reason that the pipe and the other ones for receiving the 242 00:30:11.130 --> 00:30:20.820 William Cheng: file descriptor for the right of the pie. Okay. So this guy is going to declare two integers over here, right. So the these two into one will be referred to SP zero the edible will be referred to the PS1. 243 00:30:21.180 --> 00:30:27.240 William Cheng: Po boy. So that's why it's a real to part two integers over here and I'm going to pass this array to the pipes are some call 244 00:30:27.540 --> 00:30:38.640 William Cheng: Okay, when this function returns success will be against you read them and paste. By now, how to determine whether it's successful or not what this function returns successfully PR zero will refer to the end of the pipe. 245 00:30:39.480 --> 00:30:47.280 William Cheng: Guys, again, I'm Pilate one over here, referred to the right end of the pipe. OK. So the way I remember is that, you know, again, if you remember the standard file descriptor. 246 00:30:47.640 --> 00:30:54.360 William Cheng: file descriptor zero is standard in for the script or one and two are for output as a zero is for reading and one and two is for writing 247 00:30:54.750 --> 00:31:04.980 William Cheng: Okay, so therefore PR zero is were reading and Peter, what is for writing. Okay, so that's how I can memorize it. So can you try to do things consistently. So this way, it makes it easier to remember 248 00:31:05.820 --> 00:31:12.630 William Cheng: Right. So what we do is, I'm going to create a pipe have things that are Colonel, and then I'm going to create a child process. And now we're going to have the parent 249 00:31:13.020 --> 00:31:16.440 William Cheng: Connect to the right end of the pie, while the child connect to the other to read and 250 00:31:16.920 --> 00:31:21.690 William Cheng: Write how to actually set up this connection right this cut over here was to show you how to do that. So over here. 251 00:31:21.990 --> 00:31:26.310 William Cheng: You know, we're going to call forth. So this is the code for the child. And then this is the Copa the parent 252 00:31:26.550 --> 00:31:33.270 William Cheng: OK, so the chart over here, we're gonna we're going to read from the pipe over here. So what we need to do is that we need to actually close the right end of the pipe. 253 00:31:33.810 --> 00:31:40.530 William Cheng: Very good child only want to read from it. If you don't close this, the run of the pipe, you know, as it turns out that you're going to get weird things happening is that a carnal. 254 00:31:40.980 --> 00:31:43.920 William Cheng: Says every this case you're required to close it right into the pipe. 255 00:31:44.160 --> 00:31:49.050 William Cheng: Yeah, so you close rather the parable here. And then what you would do is that you will read from the region of the pie. 256 00:31:49.170 --> 00:32:00.090 William Cheng: So we here. There's an infinitely what we hear a while read from the buffer over here and the size of Buffalo be good at. So he can keep writing commands on the parent one command at a time. So we're gonna assume that every command is less than 80 characters. 257 00:32:00.780 --> 00:32:09.180 William Cheng: So guys, are we here, we can actually lead to come out on the parent over here. So at the beginning, the parent hasn't, you know, written any commands through to the chakras. So in this case, the racism whole block. 258 00:32:09.540 --> 00:32:13.440 William Cheng: Right, so again it blocked because we have a producer consumer problem. Yeah. 259 00:32:14.100 --> 00:32:21.420 William Cheng: Right, what would the parent, the parent over here. What are those are the parent on these, right, to the right of the pie. So therefore, the parent needs to close the beat of the pie. 260 00:32:21.660 --> 00:32:30.540 William Cheng: At the bottom of the pile here is going to be p zero. So there was one called P zero right here. And then what we'll do is we'll go through an infinite loop over here, it would generate data to 261 00:32:30.900 --> 00:32:39.930 William Cheng: Generate commands with a child process. And then he will perform the right operation to right into the right end of the pipe and then this way, you know, the child can receive data for reading from the readout pipe. 262 00:32:41.040 --> 00:32:45.840 William Cheng: Or as this case. Now the parent and a child can communicate with each other, you know, through this way. But, you know, 263 00:32:46.110 --> 00:32:52.830 William Cheng: Most of communication, only the parent can send a command to the child and there's no way for the child to extend a result back to the parent 264 00:32:53.070 --> 00:32:59.370 William Cheng: So how do you say resolve on the phone for the job back to the parent, you need to create a second pipe. Okay. So, therefore, you know, again, the pipe is 265 00:32:59.970 --> 00:33:07.800 William Cheng: Uni directional. So only goes one ways and this example it go from the parent to child if you want the child to send data back to the parent. All you have to do is create another pipe. 266 00:33:08.190 --> 00:33:19.470 William Cheng: Yeah. Alright. So let's see, you know, at this example. What happened is that the current all that. So again, you make the pipe system call we finished making a pipe system call over here PR zero over here, it's gonna 267 00:33:21.510 --> 00:33:25.650 William Cheng: It's gonna correspond to the reading of the pipe and Peele one over here is going to correspond to the right. 268 00:33:26.250 --> 00:33:33.720 William Cheng: Now, so this guy's, what would this look like, right. So this is the example that we see over here. This is 0123 and four. 269 00:33:34.320 --> 00:33:39.000 William Cheng: So in this case, again, when we tried to allocate a file descriptor table over here. We're going to allocate using the same rule. 270 00:33:39.210 --> 00:33:42.180 William Cheng: Go down there at the file descriptor table, find the first entry that 271 00:33:42.390 --> 00:33:52.140 William Cheng: It's available at this time, we only have the parent process because we have called forget. So inside a parent address space over here P of zero with this function return is going to be equal to three. 272 00:33:52.350 --> 00:34:04.620 William Cheng: And then p one over here is going to be reject equals two, four. Okay, and then again three is going to be the the the return of the pipe because file descriptor zero is for reading. So, so if this is a weed out of the park over here. Then we're going to read from here. 273 00:34:05.370 --> 00:34:08.340 William Cheng: If this is the right of the park. Then we're going to sort of right like like this. 274 00:34:08.730 --> 00:34:17.850 William Cheng: Okay, I can't remember which way that I saw the picture of your eyes. Okay, is the p zero P is going to vote for reading over here. So, it will be attached to this end of the pipe and PR one over the orientation of the sentence. 275 00:34:20.100 --> 00:34:22.830 William Cheng: Okay. So at this point I can you actually talk to yourself. 276 00:34:23.100 --> 00:34:30.840 William Cheng: You can actually talk to yourself. Right. So if you if you right to the relevant to the pipe over here, you can actually read the real answer this case, you can actually read the data, you said to yourself that 277 00:34:31.230 --> 00:34:37.470 William Cheng: But our goal here is for the parent to child so to communicate. So at this point, we're going to for coffee child processor. So if you're going to call forth 278 00:34:37.710 --> 00:34:44.910 William Cheng: When we finish, you know, the Chopra says over here. I said address space is that child will get a copy of the variable we here is a p zero is going to appeal one 279 00:34:45.120 --> 00:34:52.380 William Cheng: What is P zero P AMP z is going to be a silly quarter three MP or well here, equal to four. Right. So if your child process over here. Again, it says on 280 00:34:53.820 --> 00:35:01.890 William Cheng: The top lessons over here get its own file descriptor table. We're going to copy over here where we copy the pointer. They point to the same place. So therefore, the 281 00:35:02.130 --> 00:35:08.790 William Cheng: Return of the pipe over here also go to file descriptor three right here and the writer of the pipe over here also go to file descriptor for 282 00:35:10.080 --> 00:35:21.660 William Cheng: Okay, so when we finished making the fourth system card, it will look like this in the kernel guys okay the char address space over here is the copy of the pair, we get a copy of the the the the 283 00:35:22.470 --> 00:35:35.070 William Cheng: The, the file descriptor table. So therefore, we are sharing, you know, the return of the pilot will be here. Both of them goes to, you know, a file descriptor entry number three. And we also sharing the right into the pipe and they will go to file descriptor. Number four. 284 00:35:36.150 --> 00:35:44.520 William Cheng: That's what again when you when you copy you know will will copy point and they point to the same place. Okay. Over here, there's a file object over here, there's 285 00:35:44.790 --> 00:35:51.900 William Cheng: Another file object, the file descriptor the table of your points with objects and these fell off jack represent different ends of the different ends of the pipe. 286 00:35:52.860 --> 00:36:00.540 William Cheng: OK. So again, the pipe itself is implemented in a producer consumer problem. And now we use file descriptor to actually to to access data that 287 00:36:02.310 --> 00:36:16.530 William Cheng: Oh, I am so, so, so there was what we're gonna do right inside the chakras. So we are going to close PR one. How do you close to one there. So Peter, what is right here is equal to fall. I'm going to close file descriptor entry over here for number four. So, this one will point to now. 288 00:36:17.670 --> 00:36:23.880 William Cheng: Okay, so. So this guy's when we're done with that it will look like this. Are we here at this, this one will pointing all over here, this entry is gone over here. 289 00:36:24.150 --> 00:36:35.190 William Cheng: And at the same time what the parent will do is that they will close p zero P of zeros file descriptor three over here. So we're going to turn this one to another pointer and then this will not pointed that anymore. So we're done with it will look like this. 290 00:36:35.730 --> 00:36:43.680 William Cheng: Okay, so now you can see that there's a path on the parent of the child, the path that we've the parent right to file descriptor one over here is going to going to the right of the park. 291 00:36:43.890 --> 00:36:48.900 William Cheng: And it can only be read from the reading of the Bible be here and the reading of the pipe is inside the child process. 292 00:36:50.100 --> 00:37:00.120 William Cheng: OK, so now there's a path as gay established from the parents into child. Right. So when this point on, you know, the parent over here can call. Right, right, right, right, right. And the child here called movie me to come up on the path. 293 00:37:01.770 --> 00:37:12.360 William Cheng: Okay, so, so this is how the UNIX prep work. So, you know. So again, you know, if you raise all the other invalid point over here at this will be only path. You can write and then you can read it from here. Yeah. 294 00:37:14.100 --> 00:37:20.970 William Cheng: Alright, so you actually, you saw the piping action your warm up one already. Right. What you want to play. There's a bunch of commands like this card f of 295 00:37:21.450 --> 00:37:30.150 William Cheng: Zero over here and there is a vertical bar, the vertical bar is the UNIX pipe character. Okay, so what it does. Over here is that, you know, again, you're, you're, you're 296 00:37:30.450 --> 00:37:35.760 William Cheng: Logging shot is very, very simple. All it does is every one line at a time. We look for the greater than symbols. 297 00:37:36.180 --> 00:37:40.650 William Cheng: You know, for you know output redirection look for the lesson symbol for input redirection 298 00:37:40.860 --> 00:37:47.460 William Cheng: They also look for the pipe character. If you see the pipe character. It means that on the left hand side over here we're going to run one child process. 299 00:37:47.670 --> 00:37:56.100 William Cheng: On the right hand side, you're going to run a different child process. And we're going to connect the standard output of the first process over here to the standard input of the second process. 300 00:37:56.640 --> 00:38:08.880 William Cheng: Okay, again, the way you connect that it's using a Unix pipe. Right. So this way when the first poll on over here right to standard out. He actually right, to the right end of the pipe. When the second program over here re from standard in a refund the standard 301 00:38:09.900 --> 00:38:10.890 William Cheng: Refund the beat of the pie. 302 00:38:12.750 --> 00:38:19.800 William Cheng: OK, so those of you who run into problems in warm up one, you know, try to sort of treat this as a file. So as it turns out I yeah so so when I 303 00:38:20.070 --> 00:38:26.460 William Cheng: When I run the program dot slash warm up one. So our lesson f0 computer using a Pac Man, there's actually a difference. 304 00:38:27.150 --> 00:38:35.280 William Cheng: OK. So again, if you remember from chapter one. When you do IO redirection over here. We're going to map standard into this particular file. So we're actually we're reading from a file. 305 00:38:36.000 --> 00:38:47.160 William Cheng: Okay, if you use a pipe over here you are using a producer consumer problem. And in this case, we're using a bounded buffer. So, therefore, you know, the buffer, you know, since the buffer is bounded. It can be the same size as a real file. 306 00:38:47.400 --> 00:38:54.120 William Cheng: Okay, so this fall over here. Maybe you know 10 megabytes long over here. What is the size of a pipe. Right. Since we're using producer. 307 00:38:54.570 --> 00:39:01.980 William Cheng: Consumer there's only maybe a few kilobytes over here. So in this case, you know, if you try to write too much data. Well, then the data will disappear from the pipe. 308 00:39:02.550 --> 00:39:12.510 William Cheng: Okay, so therefore the behavior over here is going to be very, very different. Now I'm not going to show you exactly, you know, with the code for doing this. But if you Google, you know, you know how to implement a system. 309 00:39:13.710 --> 00:39:17.280 William Cheng: To connect the standard out of a program to a standard you know program, you're going to find 310 00:39:17.550 --> 00:39:24.330 William Cheng: You know, sort of few lines of code, how to how to do that. Right. So again, it's only a few lines of code because, you know, in the end, you're logging shows very simple 311 00:39:25.140 --> 00:39:33.030 William Cheng: That I also just the last thing I want to sort of talk about the logging shot over here is that, you know, there's another character you can attach to your command line. 312 00:39:33.210 --> 00:39:38.010 William Cheng: And that's the ampersand character. The ampersand character meaning to run this program in the background. 313 00:39:38.490 --> 00:39:46.530 William Cheng: What does it mean to run a program the background all the means, again, running a program. The Badwater over here's why. So you're running a child processes HR process is run independently for the 314 00:39:46.860 --> 00:39:53.520 William Cheng: Hundred pair running in the background. That means that the logging shot over here. Well now wait for the child process to finish before giving the problem. 315 00:39:54.150 --> 00:39:59.610 William Cheng: Okay, so therefore the charge is running in parallel. And now the parent process will give you the prompt so you can type in another command. 316 00:40:00.510 --> 00:40:09.210 William Cheng: Okay so running things in Babylon is actually pretty simple. If you don't have the ampersand character me on mean. It means that you're running this program in the foreground. So what does it mean to run the program. 317 00:40:09.450 --> 00:40:19.890 William Cheng: That means that the parent process will wait for the child to die before it gives another prop. Okay, so that's all that means. So again, this can be implemented very, very, very in a straightforward manner. 318 00:40:21.840 --> 00:40:29.340 William Cheng: Alright, so the next thing I want to talk about is that, you know, there's also, you know, we talked about, you know, open read and write. So those offers sequential I O 319 00:40:29.730 --> 00:40:32.670 William Cheng: There's also another way to access the file is called random access 320 00:40:32.910 --> 00:40:43.680 William Cheng: So in this case, you know, what happened is that, you know, we have a file you want to actually go to access the file at a random location or I mean not use a random location you wanted. You want to sort of jump into the middle of the file and try to access data. 321 00:40:44.190 --> 00:40:47.730 William Cheng: Okay, so for example if you have a fall over here. That's one gigabyte in size. 322 00:40:48.300 --> 00:40:54.180 William Cheng: Okay, and you don't really want to go from the beginning, you know, because the data you want to access over here is actually 500 megabytes into the file. 323 00:40:54.900 --> 00:41:01.500 William Cheng: Okay. So this guy's. What do you want to do is that you want to move your cursor position to 500 megabytes per file and then you want to start reading from there. 324 00:41:02.070 --> 00:41:09.330 William Cheng: Okay, so there's a simple for you to do that. That's simply manipulate the cursor position. Okay, so, so the function of you is called LC 325 00:41:10.800 --> 00:41:18.450 William Cheng: LC function takes three arguments. The first argument says, which file you want to perform the Sikh operation on so again CIF just means change the cursor position. 326 00:41:18.780 --> 00:41:27.300 William Cheng: Okay. And the second one second argument over here is a how much you know how much do you want to me. So this will be the asset. Right. So if you have a cursor position over here you could so 327 00:41:27.600 --> 00:41:36.750 William Cheng: If you want to, you know, sort of move the cursor position forward you put a positive value over here if you want to move the cursor position backwards. You can also do that. So in this case, you can actually use a negative number. 328 00:41:37.080 --> 00:41:47.730 William Cheng: That the third arguments over here can take three different value if it's equal to six set six said that means that we are you know i know this offset needs to be measure from the beginning of the file. 329 00:41:48.390 --> 00:41:53.250 William Cheng: Okay, if this argument over here is see cursor. That means that you know this offset over here will be taken. 330 00:41:53.460 --> 00:42:01.920 William Cheng: Relative to the current cursor position right so if the current position and cause of disease 100 and if you add minus one, two, you're going to end up, you know, say your cursor position to 99 331 00:42:02.610 --> 00:42:11.160 William Cheng: Yeah, the last argument if there are also go here. If it's equal to seek in that in this case you are measuring the offside starting from the end of the file, right. Where's the end of the file. 332 00:42:12.060 --> 00:42:22.230 William Cheng: So for example, if I fall over. Here's 100 bytes law. Okay. That one's by slow, then the file all sudden he's had a fall over here is going to be 0123 all the way to 99 333 00:42:22.950 --> 00:42:27.570 William Cheng: Okay, so if the first 100 days long the opposite. You can use 02222 299 334 00:42:27.780 --> 00:42:35.940 William Cheng: As I mentioned before, in the in the UNIX system. If you want to make a far bigger. All you have to do is to put a cursor position after the end of it, and then you right to it. And now if I become bigger 335 00:42:36.270 --> 00:42:41.760 William Cheng: Okay, but when you try to read from a file, you can only read from the valor, you know, the sort of those 336 00:42:42.930 --> 00:42:51.630 William Cheng: Locations. Okay, so this case when you try to read one of our you can only go from zero to 99 you can never go to negative index over here or anything beyond 99 337 00:42:52.980 --> 00:42:57.960 William Cheng: Or so in this case the secant over here will be equal to the perverse opposition or 100 100 338 00:42:58.170 --> 00:43:07.020 William Cheng: Because that's the end of the far so the end of the file is actually beyond the current end right because the last fight over years 99 so when your cursor position or 99 you are not at the end. 339 00:43:07.740 --> 00:43:15.150 William Cheng: Okay. Only way your cursor position 100 then you're at the end you actually up your copy of the current end of the file. And that's where the Sikh Ennis 340 00:43:15.720 --> 00:43:21.000 William Cheng: Okay, so let's take a look at this call to see what it does. Over here, I'm going to perform a C Corp on this file over here. 341 00:43:21.270 --> 00:43:29.490 William Cheng: See cannot be equal to 100 right and then we subtract from or equal to 99 so now there's no option is that we actually was said. So here's the file object. 342 00:43:30.420 --> 00:43:37.860 William Cheng: That when this function returns successfully. So in this case, the cursor position over here will point to $99 you have the value of 99 343 00:43:39.210 --> 00:43:48.330 William Cheng: Alright, so again, when this function return over here whenever you turn a value that's not equal to negative one, that means that he has returned successfully and now the cursor position has been set to 100 minus once we put in 344 00:43:49.020 --> 00:43:51.660 William Cheng: There. And then what we'll do is always going to stay in an infinite loop. 345 00:43:51.930 --> 00:44:04.080 William Cheng: Well, the return value of yours, not equal to minus y. So that means a sequence successful, what we will do is that will read from the same file one bite into the buffer. So in this case we want to refund. This right over here at, where do we copy into while there's a 346 00:44:04.560 --> 00:44:12.510 William Cheng: There's a buffer data structure over here. We're going to copy this data into this buffer right here. Right. And when we perform the read operation over here. We also a dentist appointment. 347 00:44:12.960 --> 00:44:20.250 William Cheng: Okay, so in this case the reader will return one. So in this case, the cursor is, you know, if you will be 100 will point to beyond the end of the far right now. 348 00:44:20.580 --> 00:44:27.870 William Cheng: Okay, and that's okay to point to beyond the alpha, as long as we don't try to reframe around what we tried to refund the piano. And if I were going to get the end of our condition. 349 00:44:29.010 --> 00:44:38.400 William Cheng: So this card over here we're going to run one we're going to write a standard out. I want to stand out over here, this buffer of one character. So the data that we just read over here. We're going to print it onto the screen. 350 00:44:38.820 --> 00:44:41.220 William Cheng: Okay, so therefore this part over here is, don't go into the screen. 351 00:44:41.730 --> 00:44:46.020 William Cheng: Okay. And then what we do is, I'm going to perform LLC. Right. So getting the same file over here. 352 00:44:46.290 --> 00:44:56.160 William Cheng: Relative to the current cursive buzzer and conquer something go to 100 we're going to subtract two from. So now it's going to be equal to 9898 is going to point to the second path on the end of the file. 353 00:44:56.340 --> 00:44:58.890 William Cheng: And then we're going to go to the beginning of this infinite loop over here. 354 00:44:59.160 --> 00:45:04.560 William Cheng: Right. So this guy is the return value is not equal to minus y. So, therefore, we're going to read one by into the buffer over here. 355 00:45:04.710 --> 00:45:13.800 William Cheng: Going to read this part over here into the buffer. And then we're going to print it out again this fight over here will get displayed over here. So at this point the cursor position will be pointing to 99 356 00:45:14.040 --> 00:45:24.180 William Cheng: Yeah. And then we're going to subtract it from to over here and there will be 197 so to appointed this guy right here we come to the top over here read this data into buffer and then print it out. So what does this program do 357 00:45:24.930 --> 00:45:34.230 William Cheng: Right, this program print this 100 by file in reverse order from the last fight to the first by okay not a very useful program, but he sort of show you that this can be done. 358 00:45:35.040 --> 00:45:41.520 William Cheng: Guys. So eventually, when you go you know when you finish a copy in the first bite the first part over here it says. The Bible says zero 359 00:45:41.700 --> 00:45:49.500 William Cheng: The cursor position over here will be equal to one when you subtract two from it, you will get negative one. And that is going to return an error value and then and then this program will terminate. 360 00:45:50.910 --> 00:45:54.630 William Cheng: OK. So again, you know, please understand, you know, how this actually works. Yeah. 361 00:45:57.570 --> 00:46:07.170 William Cheng: So actually this is this is the same place at the end of lecture 11 from the summer of 2009 so I'm going to stop right here. And next time we're going to see 362 00:46:07.530 --> 00:46:13.650 William Cheng: You know how to how to implement other kinds of file inside of our system so far, we're going to talk about when we talk about a regular file. 363 00:46:14.040 --> 00:46:21.660 William Cheng: So what I was talking about semicolon device right at device just a major device number minus device number. We haven't really talked about how to implement a directory 364 00:46:22.110 --> 00:46:34.560 William Cheng: So next lecture, we're going to see how to implement directory. Right. So again, we're only doing this at the virtual the VM FS level chapter six. We're going to show you how to implement a directory inside the actual VA system that right