斯坦福-CS110-计算机系统原理笔记-八-

103 阅读1小时+

斯坦福 CS110 计算机系统原理笔记(八)

P3:Lecture 3 Unix v6 Filesystem - main - BV1ED4y1R7RJ

Welcome。 Welcome back to CS110。 How is the summit one going? You guys started it。

I know a bunch of people started。 I've seen you in, office hours。 There will be more office hours。

If not today and tomorrow definitely on Sunday and then next week as well, we'll have a full list。

available。 So I hope it's going all right。 And I hope it's not bringing that too many memories of like pointer math and things that we're kind of tricky to learn。

but you're getting back up to speed。 And then you're learning a little more C++, et cetera。

Let's see。 Sunday at noon。 Lab sign up will open up。 There are most of the labs sections。

whatever you want to call them, are on Fridays right now。 Like next week。

many of you will be in your section instead of here。 There are a few sections on Thursday。

So if you do need a Thursday section, sign up, try to get on there and sign up as early as possible。

If you're like, oh, I don't can't do Friday。 Although most of you should be able to do Friday for what it's worth。

But if you can't do that, then get on there and do that。

If for some reason you can't find a time that fits your schedule。

let me know and we'll try to figure something out。 You do have to go to some section。

but we will figure it out。 So that's going to open up on Sunday at noon and there's a link on the class website that will go straight to the sign up page。

I think that's about it for announcements。 I think that's about it。 All right。 Let's get going。

So today we're actually not going to see any code today。

So what we're going to do though is we are going to learn about the Unix version six file system。

Now, why do I say it like that? We're not just learning about file systems in general。

We're going to learn about a very specific one to show you an idea of how a file system is built。

So not necessarily the way you would build one today or the way people have built them for the last 20 years or so。

I think the version six files came out in 1978 or something like that when computers were actually somewhat different。

But it still works and you can still run an emulator and actually use that kind of file system。

So generally think of this as a case study in how to build a file system。 So again。

don't think this is the only way to do it。 Don't even think this is the best way to do it necessarily。

but it is a way to do it。 And it's a pretty good one。

And there were some very clever people who came up with it。

So that's what we're going to talk about today。 And in fact。

the version six file system is exactly the file system that you need to program assignment to for。

And I'm going to go over three examples at the end of class today。

which should help you think through how you're going to code that up。

So stick around to the end and we'll see those examples。 All right。 Okay。

So if you've done CS 107 or even 106 B, you know that memory。

at least RAM in your computer is we can think of it as one big long array。

Right, going from address zero to address whatever the maximum address or the maximum number of bytes of memory in your computer。

One long array, you can, you could access it with either pointer math or just an array。

And if the operating system allowed you to do that。

In fact, if you took CS 107 E, you'll realize that, oh。

if I have a bare metal hardware sort of system, I can access all sorts of memory。

which also means that you can dereference null pointers and nothing actually happens until later in your program when something crashes wildly。

But anyway, think of it as one big long byte addressable array。 That's the RAM system。

And when I say byte addressable, what does that mean?

It means that you can adjust one byte of memory at a time。 You can't do one bit at a time。

Right now, you can do masking and you can do bit masking and so forth。

But you're always working on at least one byte at a time。

Meaning that if you want to read something from memory, you're going to read eight bits out of it。

And if you write something in memory, you're going to write eight bits to it。 Okay。

That's how RAM works。 Now, the disk drive on your computer, and in fact。

many of you guys don't even have disk drives in your computer。

You have an SSD, which is a solid state drive, which I guess is a, it's not a disk drive。

but it is a memory system that allows you to store information that persists when you turn your laptop off or crashes or whatever。

That's what disks are all about。 They are generally relatively slow。

although SSDs are much faster because flash memory is pretty fast。

It used to be that disks, in fact, some of you probably still have these kind of disks on the computer。

used to be a cylinder that was actually spinning around and has little read heads that read one byte or。

well, they read more than one byte at a time, depending on the type of thing。

You read what we call a block at a time。 And they would forward that to the operating system and the operating system can say。

save data as well。

Okay。 So the, so a disk has these things called blocks on them, okay, or sectors。

You can kind of use those interchangeably。 I'll talk about the difference in a minute。

But we can assume that there's some fixed amount of bytes per sector。 A lot of times it's 512。

On some disks it's as much as 4096 maybe, but it's a relatively small amount。

But that is the smallest chunk of data you can read or write from a drive。 Okay。

If you want to read one byte from a drive, you can't。 You have to read all 512。

And if you want to write one byte, you can't, you have to write all 512。

Now, if you write one by, that you can keep track of up the other 512。

the operating system does or the hard drive itself does or what have you。

But that's the way it works。 Okay。 The size of the sector just happens to do with the disk itself or the。

like the type of disk it is and so forth。 Okay。 So, and you, it's a trade-off。

How big your sector is。 It's just a trade-off on how fast it is and a couple of other different things。

This is what it might look like and this should look very similar to the memory system, right?

You've got sector zero, sector one, sector two, et cetera。

A sector zero would be bytes zero through 511。 That's probably a little small to read。 But anyway。

that's the, like this is 512 bytes and then 512 bytes and you just access it that way。 Okay。

So that's how the hard drive or the solid state drive would work。

The API that the hard drive makes available to the operating system is actually very simple。

It's read and write basically。 You can read a sector and you can write a sector and that's about it。

You give a sector number and you read it, you give a sector number and you write it。

It's a little more nuanced than that but for the most part you can think of it that way。 And again。

you have to write a whole sector or read a whole sector at once。 Okay。

That's the part that below that level you don't need to worry about。 Okay。

We're going to talk about kind of a much higher level of basically how do you take this thing here and this memory kind of model and map files to it and map information to it that persists。

et cetera。

Okay。 All right。 Like I said, and I've already done this once or twice。

you may hear me say block and sector kind of interchangeably。

It turns out that the sectors are defined on the disk themselves, sector zero, sector one。

et cetera。 You can have another abstraction where it might be that multiple sectors are considered a block。

So you might have two sectors per block。

For our purposes and to make things a little simpler except in the case where we sometimes need to think of sector versus block。

you don't really normally need to do that。 We are going to just consider them being the same。

So if I say block, think sector, think of the chunk of memory on the drive that you can read and then you can write to。

That's what we're going to continue going forth with this lecture。 Okay。 All right。

So there is a diagram here。 Again, it might be a little hard to see from all the way in the back。

So pull it up on your slides。 You can't see it from all the way in the back。

But there's a diagram here that we're going to talk about。 Now, as I said。

this is one way to do a file system。 It happens to be the way some people decided to do it for a Linux or Unix version six back in the late 70s。

And what it does is it basically allows the files to be written on here so that you can quickly find them depending on your definition quick。

And then you can read the data from the drive in a way that is more or less efficient。 Okay。

And we'll see what we mean about that as we go along。

Okay。 The first block, the first sector or block here is special。 Okay。 It's block zero。

It's called the boot block。 And we are not going to worry about that for the class。

But the boot block is basically what happens when you turn your computer on。 Well。

the first thing it needs to do is it needs to start the computer up。

And you can do that in a kind of a couple different ways。

The first thing that Peter does is it actually reads from its own BIOS。

which is ROM memory inside the computer itself。 And if you don't have a hard drive attached to your computer at all。

it will usually read something from that BIOS, look for a hard drive and then not find it and say something like can't find a hard drive or can't find any boot drive or something like that。

And then the first block, sector one is going to be on your system。 And that's almost universal。

Like hard drives kind of do that and they know to do that。

The second block is called the super block。 And the super block contains information about the file system itself as far as interacting with the operating system。

Okay。 And again, these are things that are generally like put there by either when you format your drive or put there when the drive is created to begin with。

We will not need to worry about those two sectors at all。 Okay。

The follow on sectors are where things start to get interesting。 Okay。

The rest of the disk is made up of two different parts。 There is a netta data part。

And then there is an actual data part of the drive。 Okay。 This diagram。

it makes it look like if you broke this up, there's a whole bunch of metadata here。 This whole part。

it's not really that much。 It's in fact less than 10%。

but we needed to make this at least big enough so that you could see it to the extent that you do。

Now, with metadata and data sharing this one long abstract kind of array sort of thing。

you might be starting to sweat a little because it sounds a little bit like a heap allocator。 Right?

And you might be going, "Oh no。 Now I've got a deal with this。

Like metadata sharing the same space with the data。 And every time I put use some metadata。

I take away from the actual disk space for the data itself。" And so forth。 And that's true。

although I think this is set up a little less like integrated so that we don't have to worry about it too too much。

Okay。 The details of how the file system work itself are going to be enough to keep you busy。

So you don't have to worry too much about it being too much like the heap allocator。

But that's basically the same idea。 You've got all this memory。

You need to store both of the data itself that you're trying to store and metadata about the stuff you're going to store so you can find it。

So you can access it so you can get information about it whether it's a directory or a file or how big it is and so forth。

Okay。 What the permissions are and all that that's set in the operating system in the thing itself。

Okay。 So that's the basic idea of a file system of this file system。 Okay。

You've got a couple blocks that we don't worry about。 We've got a whole bunch of metadata in here。

And then we've got all the rest of it being data。 And this is 90% of the drive。 Okay。

And depending on the file system that percentage can go up or down, I don't know if you can ever。

if you ever really run out of that metadata space, how the file system actually deals with it。

It might just say you look at a space or it might be able to take more data out of the rest of the hardware。

I'm not 100% sure how that works。 And it's really not that pertinent。 Okay。 All right。

So we have file。

File payloads。 That's the actual data, much like the payload that you've dealt with in 107 if you did a heap allocator assignment。

And they're stored in 512 byte chunks。 Well, that sounds like a good。

that sounds like a block size or sector size and in fact it is。 Okay。 So they。

the file payloads are stored 512 bytes at a time。 Unlike the heap allocator, when you assign a file。

like when you have a file, it can use multiple blocks in different places on the drive。

It does not have to be contiguous。 Okay。 Which is good because it might。

it means that you can set up your file and then add to it and then you don't have to go moving all this data around。

Okay。 There's no real like necessary。 Although, I should say that your hard drive and your drive can what we call fragment。

which means you've got all these different pieces all over the place。

And it is not quite as efficient to grab that data。

Depending on the buffering on your drive and so forth。 But again。

that's beyond the scope of what we're talking about now。 For now。

all we need to know is 512 byte chunks for your file。 Okay。

If you have a file that is one byte long, that file takes up 512 bytes on the drive。 Too bad。 Okay。

That's the way it goes plus the metadata。 Alright。 Why is that the case?

Because 512 is the smallest amount that we can deal with。 And so therefore。

we set it up so that our file has at least 512 bytes taken off the drive。 Okay。

This is why tiny little files can actually end up using a lot more space than you might think。

Because the underlying space has got to be done in 512 byte chunks。 Now。

that's for this operating system。 And that's for this file system。

It may be that more advanced file systems have another way of dealing with this so that you don't have that limitation。

I'm not exactly sure about the newest ones or whatever。 But again。

this is one way of doing it and it's a way that seems to work。 Okay。 512 bytes is not that much。

Even though you do that。 And most files are not one or two bytes generally。 But that's that's it。

Even if it was a zero byte file, by the way, we'd have one block associated with it and zero bytes for that。

Okay。 When it's not 512, if it's more than 512, well, it takes more blocks in 512 byte chunks。 Okay。

The last one, if the file size is not a multiple of 512, is just a partial block。

So if you have a file that's 513 bytes, you have two blocks, right? And 768, and so forth。 Or 512。

5, 1,025 would be one more。 Yeah。 What's the benefit of storing it as blocks of 512?

And what was the last part? Well, that's a good question。

Is it just to make the calculations better? It's the way they set up the drives。

And it's a little historical in that drive for setup that it wants to be able to read that much and write that much。

That's really what it is。 Why is it 512? It's multiple of two。 I mean。

that's the big reason why it's 512。 But the ability for it to do just those chunks。

we had to find the quantum, if you will, somewhere。 So they said, let's make it 512。

If we had made it one byte, drives would be way too slow。 If we made it four megabytes。

it would still be too slow because there would be other issues going on。 Good question。 Anyway。

other questions before we keep going on this。 What we're going to do, basically。

is I'm going to go through this stuff and then we're going to do three big examples that, well。

they're not that big。 But three examples of doing this that I think you'll see how this works if you are a little bit confused about it。

OK, so this diagram down here, again, it's a little bit hard to see。 But let me bump this up here。

There are actually two files on here that we'll see。 A 32 byte file and a 1028 byte file。

OK, and here's how this works in this kind of color coded for you here。 OK。

there is the file that is in green。 Again, I apologize to people who can't differentiate the colors。

But the filing green here is a file that has three blocks associated, 1,025, 1,027, 1,028。

And each one of those blocks is 512 bytes。 And the file itself happens to only have 1。

028 bytes associated with it。 So it uses a full block here and then a full block here and then a tiny little part of the next block。

So the way that goes。

OK, and the other file was just a 32 byte file, which is here, which is going-- well。

it's actually-- it turns out it's over here。 But the information about it is here。

And it uses this block here, 32 bytes worth。 That's that。 OK, question。

[ Inaudible ], Right, there's good question。 Would it be possible for files to share a block?

Not with this operating system。 OK, so if you have 10 files and they're all one byte。

you need 10 different 512 byte blocks。 Some other file system may have that, but this one does not。

Good question。 Other questions? OK, so let's move on。

We need to track which blocks are used to store those payloads, right?

The whole point of this is if we've got a file spread out among the disk here, well。

we better be able to find it, right? And you might think, oh。

maybe we'll have some sort of linked lists where it goes on and then finds the other--。

that's one way to do it。 It's not the way this does it。 But that is one way to do it。

The way this does it is it has the blocks that are used for the file listed in a particular place on the drive in that metadata area。

OK, down here。 OK。 And they will-- it lists them in what we call an i-node。 OK。

And an i-node is a 32-bit data structure for this operating system。

It might be different for others。 But for this operating system, it's 32 bytes。

I'm sorry if I said bits。 It's 32 bytes data structure。

And it stores the information about a single file。 OK。 You get things like the file size。

You get the permissions。 You get when it was created or modified, right?

And you get the blocks that are all there。 And the important ones that we care about are the file type。

That could be either a directory or a file or a link, as it turns out。 And also the file size。

which is going to be critical。 And in fact, the file size is the part that people get most confused about when they look at this operating-- when they look at this file system。

OK。 And what it is is this block or this i-node, OK, lists all that information。

And it has space for up to eight different block numbers。

The block numbers are the ones that show which block-- tell which blocks in order the file is located in。

OK。 So if you've ever had your hard drive crash, by the way-- and there's various ways of hard drives crashing。

But sometimes it can crash。 And if it wipes out the metadata here。

you will almost never be able to get your files back。

Because they're spread all around the disk and they're just data。

And so there's no way to reconcile where they are。

So the various drive manufacturers do fancy things like they keep backups of this area somewhere else on the drive。

And they do things that try to make it so that if your car drive does crash。

you can get the data back。 But in this case, if the metadata is gone, your file is just out there。

512 bytes at a time, just out there。 So there's no real way to get that back easily。 Yeah。

[INAUDIBLE], Ah, that's such a good question。 What if your file is more than eight blocks long?

We will get there。 But that's a very good follow-on question。 OK。

But we'll get there and they thought of that, of course。 Because that's a good question。

Because if 512 times eight is like 4,096, and even in 1978, files were bigger than four kilobytes。

So that's a good question。 Right? So we'll have to deal with that。

But keep that in your mind for another few minutes。 OK。 Regardless, the i-nodes themselves, well。

they go into blocks。 OK? Because they're, that's part of the thing。

And what it is is you can fit 16 i-nodes。 This diagram is a little bit off。

This actually only had this diagram shows that there's only four i-nodes per block。

Don't worry about the numbers specifically down there。 Just know that for this file system。

you can store 1632 bite i-nodes in one sector or one block。 OK。

That's going to be critical when you do your assignment。 OK。

And each one of those refers to a particular file or a particular directory on the disk。

And when I say directory, a directory is a file。 It's just a special type of file。

You'll see what you'll see what that means when we do the examples。 OK。 All right。

So let's look a little bit more at what this i-node actually does。

So the i-node 2 here, OK, which would be down down here in green。

Now I'm going to make this a little bigger so you can see it。

But the one in green here is i-node number 2。 This is considered i-node number 1。

So it's the zero-earth one in the block。 It's considered one。

I don't know why they named they numbered them starting at one。

You'll probably have to do a little off by one kind of arithmetic when you do your assignment。

But anyway, the point is we're going to look at i-node 2, which happens to be right there。

It's in block 2 or sector 2 offset one, I guess, which is block 2。

That makes sense。 That's where it is。 For now you don't need to know more details than that。

And what it says in it in this case is it says, and I guess it's offset 16 if you talk。

if you are talking about, let's see。

Oh, sorry, that's offset 16 for the 4, but don't worry about that。

For now it's going to be offset of 32 bytes in that case。

In this case it tells that it's a regular file。 It says how big it is, critical。

And then it says how many blocks it's going to take。 So if you know the file size。

then you can start to figure out how many blocks it needs to take。

And that is going to be very important to finding out how you can traverse these i-nodes。

The blocks that are listed are 1,027, 1,028 and 1,025 in that order。

So if you look at it here, the first part of the file is here, beginning of the file。

the second part of the next 512, the first 512 bytes are here, the second 5,000。

I got that wrong again。 It says 1,027 first。 That's the first one。 Thank you。 And then it says 1。

028 is the next 512 bytes and here's the final 512 bytes。 Now look。

the operating system is going to try to put these things in order。 If it's got a whole bunch of。

or the disk is going to depending on which, but the i- this is the operating system。

at least initially, is going to try to put it in order。 Why? Because that just makes the most sense。

But if it can't, it will find, it's got a list of free blocks and it just picks the next one that it can do。

And this is where fragmentation comes into play and you have to be a little bit concerned about that sometimes。

If you've been using your disk for a long time, sometimes I think your computer is slowing down。

And it may be because your disk needs to be defragmented。 Meaning, hey。

take all these parts of the files that are so far apart and put them closer together。

Why would that matter except that they're right next to each other?

The disk sometimes will read more than 512 bytes because it can and it buffers that and so forth。

So that's the big idea there。 Alright, questions on how that works。

And we'll get to the question there。 Yeah。 [inaudible], Yeah, yeah, yeah, yeah。

The offset should be each one of these。 Again, Ryan came up with this and he had a different reason for doing it this way。

These should be, you should get 16 i-nodes per block。 So there are 32 bytes each, 16。

So that's when you do your assignment, you'll get that。

What I think I'll do is I'll update this diagram and make it so the slides are actually correct for what we're talking about here。

Okay。 Alright。 And by the way, because it is 1028, as I already mentioned, the first two blocks。

1027, 1028, completely filled。 This one only a little bit partially filled。 And by the way。

how could this happen? Well, maybe this file was bigger at one point or there was another file here that freed up some space and then this one got changed。

And then you're always saving, right? Whenever you save, if it's bigger or smaller。

the disk will accommodate that in whatever way it can't by deleting the。

by freeing a block or taking a new block。 And so it's just going to happen as it goes along。 Okay。

Alright。

So, as I said, the not in contiguous or sorted order。

you really have no idea until you actually read off the numbers there about what it is。 Okay。

And this is what I kind of just said about how it could be for one reason or the other。 You might。

and we've already talked about this too, where you might get a file system that does reuse some blocks for kind of double up on blocks。

This one does not。

Alright。 The files I nodes tell us where to find the payload。 But as I said。

it's also stored on the disk itself。 Okay。 That's the part that is the 32 bytes per i number。 Okay。

And you have to be able, you have to know that, right?

Because you're going to be reading 512 byte chunks off the disk。

And so if you want to find i node 3, you can do the math, the calculations say, "Oh。

i node 3 must be in the first block because there's 16 in there。 i node 16 is also。

I guess i know 16 will also be in the end of the first block。

i node 17 is going to be in the first node, first part of the next block。 Okay。

So you have to do those calculations。 And when you get to your assignment next week。

you will start to think about those。 Okay。 And as I said。

you can store 16 of these side by side in a block。 Alright。 And last time, I think。

last time I'm going to mention it, if you think this is the only way to do it, it's not。

If you do have to know how to do this for your assignment, that's correct。 Yeah。 [inaudible]。

Good question。 If you have a block that has partial, and the question was。

if you had a block that had partial, it was partially filled and then you added more data and still didn't fill it。

it would just fill right up to the, it's going to fill。 It will edit the block。

What it will do is it will read in the block, do the, make the change。

then write the whole thing back。 Again, that's what it'll actually do。 Okay。

And the operating system will take care of a lot of that for you。 Good question。

Okay。 So, as humans, we like to remember, we don't like numbers so much。 Okay。

And this is not a comment on like math education in the United States or anything。

This is just saying that we don't like to remember。

we're not as good at remembering numbers as we are words because that's the way we are。

Words are easier to remember。 It would not be so good if you had, if I said, Hey。

I just put a spreadsheet in Dropbox at 70881, slash, and that's how you had to refer to it。 Right。

You go a little crazy trying to remember those things。 Okay。 This is an emoji I found today。

It's called like screaming face emoji or something like that。 I also found another, you know。

there's a, you know, there's a thumbs up and there's thumbs down and all those。

There's actually a middle finger emoji。 I didn't know that until I don't think it's listed like you can't like pull it up in your phone and there it is like you got to kind of figure know how to do it。

I think。 But anyway, I didn't put that one out here。 But you probably could。

If you've got a file name that was like this, you could probably want that emoji。 But anyway。

we don't like that。 So what do we do instead? Of course, we use names, right?

And we actually have paths and in Linux and Unix, the paths are an English like name or I should say a string。

which is separated by slashes and you've been doing this, right?

So you've got a path that's slash user slash class, less Cs 110 slash W。O。U。 slash index。html。

And that is going to be what we could remember。 I mean。

I can actually remember that when I can't remember all those different numbers。 Okay。

So we need some sort of translation layer。 Okay。 We need some sort of virtualization, if you will。

between going from the numbers, which the computer likes to the words with impacts, which we like。

Okay。 And so that's handled with these special types of files called directories。 Right。

And that's what we're going to have to do。 And the directories are just files。

meaning that a directory has an i number associated with it。

You follow blocks in that i number to an actual file that has the, well, not there。

I guess it would be here that has a file in it。 In fact, I'll zoom in on that over here。

It has information in it about the, about the files that are in that directory。

Okay。 And it says the name of the file, which by the way is limited to 14 characters。

And there's a little tip for your assignment next week。 It's limited to 14 characters。

And there is no trailing zero at the end。 Okay。 If it doesn't, if it uses up all 14 characters。

That makes sense。 Like there's no, if it uses all 14, they don't bother with the new。

with the zero because they know, you know that it's a maximum 14。 It's the way it goes。

the way it's described it。 Remember, back when these things were created。

memory was still kind of important。 So we ended up with things like this and things like the Y2K problem。

Some of you guys were actually born, anybody born after in 2000 or after? Yeah, youngsters。

The Y2K problem for what it's worth is, and I don't know if you've even heard of this。

but this is the big thing back then, was in 1997 or so。

somebody realized that people only use that most programs only use two digits for the date。

And so they would use the last two digits, like 1993 or 1994, whatever。 They would use 94。 Well。

what happens when you get up to 2000? It rolls over to zero。

and then all of a sudden you don't know, you think you're in 1900 instead of 2000。

And then people literally thought the world was going to die, like。

and like explode or whatever at that point。 And it didn't, of course。 But most programs those days。

and in fact many programs these days were written in Coball。

which is a language from like the 1950s。 And if you were a Coball program in 1999。

you could in 2000 buy a vacation home, because lots of people were hiring you to go fix the code that's been 30 years old or whatever。

Anyway, so anyway, so as far as, as far as files are go, a directory is a file。 Oh。

I didn't show you what else is in there。

A directory is a file that has the name and an i number associated with it。 Okay。

it has the i number in the directory。 So if you're looking up a file name。

you know what i number to go to。 Okay, buy this little, buy this little file。 And it is a real file。

The operating system actually keeps the details of that file from you。 You can't go and say, oh。

let me get the actual data out of that file to say what to see what the files in there are specifically。

It's all hidden from you。 You don't get access to that。

Okay, all right, how do we do this? Well, like you actually probably could put a file name inside an i node。

But the problem is, file names are kind of long。 In fact。

you have the whole path associated with the file name。 They can get very, very, very long。

And we want to keep the i nodes themselves small。 We want to keep them 32 bytes。 And in fact。

if you try to keep this entire path here in one i node, you'd run out of space。

It's more than 32 bytes immediately。 So we don't keep the names there。

This is why we keep them in the actual file associated with the directory itself。 Okay。

you also don't want to try to search through the actual linear linear research through the i nodes。

There might be a lot of i nodes。 There might be millions of them。

So you don't want to have to try to do that every time you're looking for a file。 Okay。

that would be kind of slow。 Okay, there are better ways to do it。 Question。

Do file i numbers always start at two? Okay, that's a good question。

And we'll get there in a little bit。 The first i node that you care about is the one right here that's at sector two。

offset zero。 And that happens to be the root i node, i number two, right there。 So I should say。

yeah, it's actually in this file system, it's one, newer ones it is two。

But that's the one that's the directory for root。

Now we're going to know where to start。 And we'll see how that manifests itself in a minute。

Question。

Good question。 Some of the super block, I believe。

keeps track of what i nodes have been used or not。 I'm not sure exactly how。

like the mechanics of that part of it, but it does。

the operating systems keep in the disk itself and say, hey。

these ones are failed and these ones aren't。 Other questions on this so far? Okay。

we're getting there。 Yeah。 So is this the directory that maps the i-numbers of the file that it uses in any way that relates to directories。

how many like computers, like sort of files? Good question。

Are the directories that the file names in here for directory。

are they related to the directories on your computer?

They are exactly the directories on your computer。 That's what they are, in fact。

So you've got slat, you've got a file for the root, you've got a file for user。

You've got a file for class。 You've got a file for CS110, et cetera。 And literally on the。

in each one of those has its own i-note associated with it。 Good question。 Yeah。

I suppose it doesn't follow up。 So in the i-note itself。

is there something that tells me if it's in use or not used? For the i-notes themselves。

is it something that tells you if it's in use or not in use? Yes, same thing。 I mean。

I think it's the operating system or the disc itself has a list of which i-notes are not used and just like the blocks that are not used。

Yeah。 Good question。 Anybody else? All right。 Let's move on a little bit。 So what do we do for this?

Okay。 We have a directory file type。 I've already kind of mentioned this a little bit。

We don't really need to change our model to do this because we are going to simply say a directory is a special kind of file。

but it really is just a file。 We don't even make any changes。

We just lay this abstraction onto the system we've already got and we just say, great。

Now you're going to have to search through a bunch of files to find your directory。

but it's going to be relatively fast because you can only have to search through a limited number to actually find that。

Okay。 So it's not too bad。 You, the file payload, the, this is again。

I think this is actually supposed to be, hang on, that one, the file payload is series 16 bytes。

livers of foreign table mapping names to i numbers。 Oh yeah, sorry。 That was right。 This is the。

what we just talked about down here, we just talked about down here with the thing。

Each one of these is 16 bytes long。 Okay, you have 14 bytes with a file name and then two bytes for the number。

the i number。

That's what the 16 bytes, bytes, slivers are。

The file name is created at np3。 Yeah。 Whatever that directory is, yes, that's good。

That's a very good question was, hey, wait a minute, what directory is this one stored in。

It's wherever the directory that np3。mp3 lives。

That's the file associated with this file here。 Yes, good question。 Okay。 All right。 So。

when we do the examples, it'll start to become a little more, little more clear。 Okay。

Again, the OS hides this from you。 It's not like you can go and look at these files yourself。

Even though they are just files, the operating system won't let you。

Because I'm not going to let you look inside that directory file。 That's for a media use。

So it goes。 Okay。 All right。 Let's look at block 1024 on here。 Okay。 That's this one over。

That's this one。 We already, we already did look at this one as it turns out。

The directory contains two files。 Okay。 The total file size there is actually 32。 Why?

Because each one of these slivers takes 16 bytes。

Okay。 And it, the first row of the table is the first file, second row is the second file。

And you can look through these。 And in fact, when you do your assignment starting next week on this stuff。

you will have a struct that you will lay over this information so you can walk through those files one after the other after the other。

Okay。 So it's just a bunch of 32 bytes of data and it's 16 bytes chunks for each file。 Okay。

And again, you might ask, wait, what if there were more than 16 times 512 divided by 16 number of files in the directory?

Yeah, it's going to need multiple blocks to store that file。 But it's just like any other file。

You hopefully have some abstraction where you can say。

give me an entire file or one chunk at a time。 And that's exactly what the program you will write does and you'll have to abstract that away next week。

Okay。 All right。 So, so what you're going to do is you're going to search through these files。

If you are looking for a directory or a file that is somewhere down the path, you're going。

this file will hold instead of saying a。mp3。

You might be looking in the slash directory and you might be looking for the user directory。 Well。

that's going to be user and it's going to have an i number and you're going to go to there and keep doing the process。

either recursively or iteratively depending on how you decide to do it。

Okay。 Like I said, we're going to do examples where you're going to see this in action。

All right。 What does the file look up actually like how does it actually work? Well。

if you were looking for users slash users slash class last season one 10 slash example。 Okay。 First。

we find the i node for the slash。 Now, I've already mentioned that you know where that one is。

It happens to be the first i node or i node number i number one i node number one that you go look at。

So that's where you start。 So you don't need to go like figure something else out。 You say, okay。

I know where it's listed on the draw or what sector it is on the drive, the i number。

and you go there。 Okay。 So you search that payload。 In other words。

you go to the file associated with that and you search for the user token and then you find the i number associated with that and then you go to the i node table。

You find that you find out where the file is for that。 You go to that file。 You read the contents。

In there, you will hopefully find a slash class or a class file。

And then you go to the class i node and you find the i number with that。

You go to there and you find the file associated with that。

You go read the file and then you find the CS 110 file inside that。 Okay。

And then inside that same thing。 It's the same process again。 Look at the i number。

Go to the block associated with that and then go and find the i number associated with the file。

Go to that i number。 Find all the blocks associated with that file and read the file。

That's how the process works。 Okay。 It's relatively fast。 Okay。

You have to look through it most one, two, three, four different files to get to that。

And most paths aren't that many different, that many levels down。 Okay。 Question, how's it?

When you're searching in a directory and you're trying to find like the next directory or file。

you want people in your search or you want them to sort of order an alphabet。 Good question。

The question was, hey, is this like a binary search or something? It is not。

You do have to do a linear search for the files。 Now。

it may be that the files are in alphabetical order, but I do not think they are at all。

You cannot guarantee that anyway。 What happens is when you create a new file。

it just gets tagged down to the end of the, it's just on VM。

They don't resort it or anything like that。 So good question, but you do have to do that。 But again。

directories really don't have that many files in them。

And it's pretty quick to search through 32-bit or 16-bit files。

It's not our 16-bit entries in the file。 Relatively fast。 Question。 [ Inaudible ]。

Good question。 Are these relative offsets or are they actually explicit ones?

They are the i number associated with that。 So it's not an offset or anything。

It's just go to i number two starting at sector two in the file system。

So in other words, starting here, counting down here until you get to that number。

That's how it works。 It's not an offset。 [ Inaudible ], It's not necessarily in that sector either。

It's in the i-nodes。 It's somewhere in the i-number。 And so it's in the i-nodes somewhere。

And you just have to figure out, you do a little math。 Trust me。

for the next assignment you're going to do some arithmetic。 That's really it。 Yeah。 [ Inaudible ]。

Correct。

[ Inaudible ], Yeah, two bytes is, what, 16,000? 16,000 different i-nodes。

Over this operating system, that was all you could add。 Or whatever。

You probably couldn't add more than 16,000 files。 But when the hard drive was five megabytes。

it didn't really matter that much。 Question。 [ Inaudible ], A good question。

What happens if there are two file names that have 14 characters?

The first 14 characters are the same, but they're different file names。

They aren't different file names。 There is no way to make a file name longer than 14 bytes on this operating system。

You're limited to that。 In fact, DOS was that way。 Microsoft DOS was that way for years。

It was an 8 plus 3。 In other words, 8 characters meant 3 character extension。

And then they had to do some dead jumps and big hoops to make it so they could put long file names into windows when they did it that way。

Yeah。 It was a limitation。 Nobody really thought it may have mattered if they could do that。

So, yeah。 Limitations back then that were kind of important。 Good questions。 Anybody else? Yeah。 Oh。

over here。 Sorry。 Mr。 Yeah。 [ Inaudible ], A good question。 You limited 32 files per director。 No。

You can have as many as you want。 And it wouldn't be 32。 Yeah。 I guess it would be well。 512。

5 or 6。 32。 Yeah。 32。 But you just have more blocks associated with it。 Just like any other file。

[ Inaudible ], Yes。 Your director is a file which can be one block or multiple blocks。 Up to 8。

And we haven't quite gotten to this answer。 I was like, "Whoa。

what happens when there's more than 8?" We'll get there。 Yeah。 [ Inaudible ], Nope。

The blocks do not have to be full。 Good question。 [ Inaudible ], Yeah。

This is a little bit they do fall show up。 A good question was, "Do hidden files show up?" I mean。

they do。 I didn't represent them here。 So, yeah。 And 。 。 。 you show up。 If that's your question。

Anybody else? Okay。 So, let's move on。 We got some big stuff still happening here。

Okay。 This was the question that came up earlier。 All right。 The question was。

"I nodes can only store 8 block numbers。" Okay。 They're just limited to that。 All right。 Why?

Because that's the way they built it。 Okay。 So that means, ostensibly。

that you're limited to 8 by 512 or 4,000。 Or 4,000。 And 96 bytes worth of data for a file。 Well。

even back then, as I said, that was far smaller than files you might want to create。 Okay。 So。

what did they do? Well, they had to figure out some other method to do that。

And what they did was they said, "Let's do what we call indirect addressing。" Okay。 So。

if you had blocks like 2001 to 2008, right? You would be full。 Right。

Because you have 8 of those times。 You have 4,096 bytes worth。 And then the file would be full。

Okay。 Once it gets bigger than that, a flag is set。 It says, "Uh-oh。

Now we have what we call a large file。" Okay。 And the large file switches everything so that instead of listing the 8 blocks for that list the data。

it lists blocks that themselves list blocks for data。

Okay。 So what that basically means is, if you have a block, before it had 8 numbers in it。

and each one of those, you'd go to the first one and that would be your data and the second would be your data and so forth。

Now, it's going to have 8 numbers in it。 In fact, it is 7。 We'll get to that in a few minutes。

And what it would, what it means is each one of those points to a block。 And inside here。

the block itself is filled with numbers that are other blocks that are your data。

Okay。 It's an indirect inception kind of thing where you've got your indirectly referring to your file by saying。

"Okay, great。 Let's have each one of these numbers go to a block。

" And that block has a whole bunch of numbers in them to do that。

Now, these are 2 byte numbers。 Blocks are 512 bytes each。

meaning that you can have 256 different blocks per indirect block。

Okay。 Why? Because you've got one indirect block pointing to here a block of。

I know block number pointing to this block。 Inside here, you have 256 more numbers。

Each one of those numbers points to a block of actual data in that order。

Okay。 That's called indirect addressing。 And you can do that for every block of the original 8 blocks。

except it's only 7, and we'll get to why in a minute。

Okay。 But what does that mean? That means if you did store all 8 blocks as indirect blocks, okay。

well, how much data can we store now?

We could store 8 of those blocks, good block numbers。 So we have 8 of them in the i-node。

Each one has 256 other block numbers。 Each one of those can store 512 bytes。

8 times 256 times 5 under 12 is 1 megabyte。 That's how big your file could be with indirect addressing。

Okay。 What questions do you have about that so far? That takes a few minutes to process。

First time I saw it, I went, "Oh, what's going on there?" Yeah。

[ Inaudible ]。

That is a good question。 The question was, "Is the number of levels of indirection limited?

" For now, all we consider is one level of indirection。 We'll get to that in a second。

You're thinking one side out of two sides ahead。 But yes。 For now, just think there is no other。

for this case, one level of indirection。 All we need to worry about。

We'll get to the other in a second。 Somebody else? Yes。 What's that? You changed your mind。 Okay。

Anybody else have questions about this?

If you're using indirect addressing, the best in every file。

Is it actually using indirect addressing or something?

Good question。 Do we have all the blocks using indirect addressing or just some of them?

You automatically switch to a full indirection。

It needs the operating system and the drive has to do the moving of your files。

The minute it gets bigger than 4096。

we've got to switch things here。 And then you end up going with that。

And then if you go back to 4096, it'll switch back again。 And so forth。

It's pretty dumb in that sense。 But it does then。 Other questions about that? Okay。

So you might be saying to yourself, great。 In 1977 or 1978。

a million byte file was pretty big and they just left it at that。

Well, even in 1978, a million byte file, one megabyte。

although it was a big proportion of the available hard drives in general。

was not big enough to store all the data that you might want to。

Okay。 So they said, well, now what do we do? Right。 And you kind of almost hit on what we do。

But what we do is we say, all righty then。

If you have a indirect addressing, why not go to doubly indirect addressing? Okay。

And it's not as bad as you think。

Okay。 You might think, well, now we just go and once it gets bigger and we just do everything over again like we did before。

It's not quite that bad。 Okay。

That would probably lead to when you hit that threshold。

you'd all of a sudden have to change things a lot and it would be more movement than they wanted to do。

But what they did was they said, okay, if you use more than seven of your indirect blocks。

the eighth indirect block is a doubly indirect block。 And what that means is。

and this is really inception, right?

You have the seven block numbers that are going to indirect blocks。

Each one of those goes to one that has 256 entries in it。 The final block number。

the eighth block number points to a block, okay, which is filled with block numbers, 256 of them。

Each one of which goes to a block which has 256 numbers in it。

which each one points to a data file or data part of your data。

Okay。 It's an indirect scheme because you can do that, right?

And they just explicitly say just that eighth block and only that eighth block。

if you need to use it, should be interpreted as a doubly indirect block。

Okay。 So there's a couple that I'll have a summary down here goes to the details here。 And again。

you'll have to figure these details out for assignment next week。 And it sounds like, oh。

this is crazy, but not as bad as you think once you understand what's happening。

[ Inaudible ]。

Is the eighth, let me explain the eighth block again。 Okay。 Let me do it this way。 Okay。 You have a。

you have the i node here。 Okay。 And the i node has seven numbers in it。 Okay。 We'll call them zero。

one, two, three, four, five。

And six。 And those are all singly indirect。 Okay。 They each point to one。

they each point to a block which has 256 data block pointers, basically, or beta block i nodes。

Okay。 And each one of those goes to a data point。 Okay。 And that last one here, okay。

the eighth one is a special one。 And it is in, it's a regular number just like the others that points to a block。

And when you go to that block, you will find 256 numbers。

Each one of those numbers points to a block which has 256 numbers in it which each point to a data。

Does that make sense? As far as that answer your question as far as what's happening there?

There's no other, like there's no other abnormalities to it。 And it's always the eight one。

And it's always the one that is used if it's necessary。 That's something in the back there。 No? Yes。

So, you can see the indirect block shows that the string does just all numbers。

They are all two byte numbers。 Yep。 And that's it。 And they're just two byte i numbers。

That's all they are。 Okay。 All right。 Question? So, if your only one is in the indirect。

If you want to use, yes。 If your file is small enough so that it only fills in the first seven indirect block numbers。

that's all you do。 The minute you go over to that eighth one。

now it's a double indirect and you have to do it with a family。 But it's, but it's not, there's no。

there's no decision making at that point。 The only real decision is, have I gotten。

have I gotten bigger than four thousand nine? And then four thousand ninety six。

And then it goes to the indirect method。 And in the indirect method。

there's one of those blocks that's doubly if it's needed。 It's always the last one。 That's that。

Okay。 All right。 So, let's go。 Yep。

Are the indirect blocks considered files? Are the indirect blocks still considered files?

The indirect blocks are, no, they're just blocks in that case。 They're not files themselves。

They don't have associated i numbers with them。 No, they, they, well, they, they have block number。

They're not have i numbers。 They're not files。 Good question。

Okay。 Let's summarize。 And by the way, here's the final calculation, by the way。 Okay。

You can have up to seven plus 256 indirect blocks。 Why?

Because you have seven from the seven that you've got there in direct。 Right。

Plus you have 256 more indirect blocks because of the block that is now doubly indirect block。

For a total of 263 indirect blocks, each one of which points to has 256 blocks associated with it。

Okay。 And each one of those 256 blocks, so each one is 512 bytes。

meaning that you can have a total of now 34 megabytes for a file in the Unix version six file system。

Okay。 34 megabytes today is zero。 Right。 So, you know, an mp3 is bigger than 30。

34 megabytes or what have, you know, most things are bigger than 34 megabytes these days。

But back then, that was a big deal。 I'm going to date myself。

but my first computer didn't even have a hard drive for its work。 But the。

when I did get a hard drive for it was 20 megabytes and I went, well, I will never fill that up。

And, and it took like, and it took hours to like back it up and so forth。

the flappy disks and whatever。 But it, but back then 34 megabytes was still was pretty big back then。

Right。 For the number of things that you wanted to do。 And if you did need bigger files, well。

you'd do just like we do today, you would have to break them into different files and somehow keep that separate from the file system。

And go look, I just know that mine, I'm going to do this myself for files that are bigger than that。

You would chunk them up yourselves。 But anyway, point is now we've got an act up to 34 megabytes。

Okay。 All right。 So, to summarize。 Okay。 If a file is less than 4,096 bytes, okay。

we take those that i number and you go to the i node, associate with that i number。

and it's got eight block numbers in there。 And it also says the file size, by the way。

so you know how to interpret this。

But you go and you find the eight block numbers are all used as a direct address block。

So all eight of those have 512 bytes, bytes associated with the block。 You go there。

you find the data in that order。 And it doesn't have to use all eight。

You can use one or two update。

The instant you go above 4,096, you are now a large file。 And for large files。

the first seven block numbers are indirectly addressed, meaning that each one is not a large file。

You can use one block number。 And you can use one block number。 And you can use one block number。

which is 256 numbers in it。 Each one of those goes to data。 And you do it in order。

If you need to use the eight block, that eight block is now doubly indirectly addressed。

meaning that it refers to a block, which has 256 block numbers, which are。

which each refer to a block, which has 256 block numbers。

Which refers to 512 different ones。 Okay。 That's how it works。 Question。

So the two byte number is a block number, not a non-number。 Correct。 That's a good question。

All right。 And again, this goes to the question of are they files or not。 They're not。

Once they're blocks that are associated with this scheme, they are not no longer considered files。

Okay。 So let's, yeah。 Question。 Sorry。 Yeah。

You mentioned setting flags to show indirect addressing where to be。 >> Correct。 Good question。

Where do you store those flags that show indirect addressing? The I number itself。

Remember it has six, it has eight numbers associated with the bytes。 It's 32 bytes long。

There's 16 bytes worth that are just there for data, metadata。 And it has the file type。

it has the size, it has the flags that are set, whether it's large or not。 Good question。

And you will see one, when you do this, when you do the assignment associated with this。

we've showed you structs that you can use that can map right over that data。 Anybody else? Okay。

Let's do a couple of examples。 All right。 We'll do three examples here。

We are going to try to locate a file called local slash, slash local slash files, slash fairy tale。

txt。 And then we want to read that file。 Okay。 It's the first thing we're going to do。

Then we're going to look at reading a file called slash, me file or med file。

which is a medium size, the large file。 Okay。 It's bigger than 4096, but not as big as 917,504。

which would be using all seven of those indirect blocks。 Okay。

Then we will go and read a file called big slash big file, which is a large file。

which is bigger than 917,504 bytes, but smaller than 34 megabytes。 Okay。 And by the way。

at that limit right there, you do have other overhead because now you've got at least two more blocks that have to be used for your metadata and whatever。

but it's not the, not the biggest deal in the world when your file is that big anyway。 It's a tiny。

tiny amount of extra data that you're using。

Okay。 All right。 So hopefully I came up with this diagram that actually will make sense。 Okay。

Hopefully it's big enough too。 So we are going to look for slash local slash file slash fairy tale。

txt。 Here's the steps on how you do that。 Okay。 You first look for the slash directory。 Okay。

We know where that lives。 There's no calculations necessary。 It lives at i number one。 Okay。

And i number one happens to be I did not put sector zero and sector one here。 This is sector two。

sector three, sector four。 I node one lives at sector two offset zero。 That is where the file。

that is where the i node lives that tells you where to go look for your root directory。 Okay。 No。

no calculations necessary in that sense or no thinking, no like decision making there。

You go there and you look it up and that set。 Okay。 So what does this say? Well。

what I've got on here is these are the black numbers in this column here。 Okay。

These are the file size。 I just threw that in just so we can kind of check and see the size of the size。

And then these are the i numbers associated with the i node。 Those are in order, of course, right?

You get one, two, three, four all the way up to however many you have。

Remember that you only get 16 of them per block。 Okay。

This is the arithmetic part you have to deal with。 Now we're doing it kind of abstractly。

You will have to figure out the arithmetic a little bit later。 Okay。 All right。 So anyway。

you go to i node one。 That's the root。 You say, okay。

what block numbers are associated with that file? 25。 Okay。 And it's only 80 bytes long。

So I know I'm only going to have to do one block number and it's singly address or it's directly address。

not indirectly address。 So you go to block 25, which happens to be down here。

And you look through the, and you know that it's a directory because it's definitely a directory because that's the whole point。

And of the root。 And you look through and it's got some files in it。 Now in this time。

so I did put the dot and dot and dot in there。 Okay。

And you look through and you read that file and you go, is that I'm looking for local, by the way。

That's the first thing I'm looking for。 Is that local? No。 Is that local? No。 Is that local? Yep。

Well, that means that it says, oh, locals i number is 16。 So what do you do?

You go up to block i node number 16 and that must be local。 Okay。 And so you do that and you go。

okay, what do I need to do? Well, it's a thousand and one bytes long。

And I look at block 27 first and then I'll read 54 if I need to。 And you go to block 27。

which is down here。 And you say, okay, let me read through this。 Now I'm looking for files。 Okay。

because that's the next part of our path。 And so in five not files, not files。 Oh。

there's files at i number 31。 You see how this is like recursive or iterative or whatever?

Just keep doing that all the way through the thing。 Sorry。 Sorry。 Keeps for me。 It's iterative guys。

It's either whichever one you want, right? I would do it iteratively。 It's just easier that way。

I think。 But anyway, so you go down here and you say, okay, great。 Files is at 31。 Okay。

So you go to 31, which is here。 And you go, oh, great。 That means I go to block 32。

which is over here。 Now I'm looking for fairytale。txt。 And I go down here and I say, okay, well, 32。

I go to 32。 And I look at that's not fairytale。 There it is。 Block 47。 And now we found our file。

Right。 And now we know it's a file。 We can check and see if it's a file or a director if we want to。

Now we go on there。 We go to 47, i number 47, which happens to be over here。 And you go, great。

It is a 1057 byte long file。 And so you go, okay, great。 Let me start reading it。

I'll go to block 80。 And because it's a 1057, we know that it's less than our large file differentiator。

Right。 So it's just going to be direct blocks。 That's nice。 So you go to block 80。 And you go to 80。

And that's going to say once upon a time there was a vast forest because they're fairytale。 Right。

And then you read the next one。 And this may not be in order。 In fact, it isn't in this case。

You go to 89 and says, that's a die in the Princess Solar Star to Google, which blah, blah。 Right。

And then you go back and that's an expert。 And then you finally go to 87 and you get to 87 and they lived half the ever after。

Now, until you get to 87, how much data are you going to read from each one of these?

That's actually kind of a trick question。 How much data are you going to always read from a sector?

512 bytes。 How much is relevant for 80 and 89? All 512。 How much is relevant for 87?

Well, you have to do the calculation。 You have to do a little either modding and dividing and that arithmetic stuff。

Right。 Because you need to know you will read 512 bytes。 In fact。

the disk is not going to try to figure the disk doesn't say, hey, you want to read 7 bytes?

I'll get you 7 bytes。 No。 It says, here's 512 bytes。 You do that what you want。 It might be garbage。

Some of it's garbage。 And some of it is the end of your file。 You do the math to figure that out。

Okay。 So that's how that works。 All right。 And then we found it and then we're done。 So that's that。

Questions on that。 Straight forward enough, I hope。 I mean。

given that this is new and we able to do it。 You could look through that and do it that way。 Okay。

Good。 All right。 So then let's look at the next one here。

Now we want to read a file called slash need file or med file。

Right。 It's a medium sized file。 Well, how do you do it? First we have to find it by the way。 Right。

If you're going to do that, I could have said, oh, look, we know it's a design node number。 Well。

let's just actually find it。 What do we do? We go to the root directory。 We know where that lives。

And we go there and we say, and it's the same one as before because I just kind of copied and pasted this over there and changed a couple of things。

Anyway, you go to block 25。 You go to 25 and start reading through the files。 It's not that one。

It's not that one。 It's not that one。 Oh, there it is。 It's the file we're looking for。

It is a file。 You can go to the file and find out that it's actually a file。

So you go to i number 16, which is here。 And once you're here, you say, oh, okay。

how big is this file? 800,000 bytes。 Well, that's bigger than 4096。

so I'm going to start using my indirect addressing。 Okay。 So you go great。 There's one, two, three。

four, five, six different I know associated with this。 I hope I counted that right anyway。

And what you do is you go to the first one。 You say, okay, let's go to block 26。 Well。

down in block 26, I know that these are not -- this is not a file。 These are more block numbers。

Two bytes each。 I go to the first one and I go, okay, 80。 Well, that's where my file starts。 Okay。

so I go to file starts as it was the best of times, it was the worst of times, right there。

And that's the first one。 Well, then I go to the next one and that goes to 87。 And 87 is over here。

It was the spring of hope, it was winter, despair, etc, etc。 Okay。

And you do the same thing all the way through this list, right?

And that's going to be 256 different blocks, right, of 512 bytes。 And then you go to the next one。

right? And you do the same exact thing。 And you go to 30 and you go, okay, great。

And now I'm going to go to block 89 and I go to 89 and the accident is happening getting out of the car。

And that is the 257th data block。 Okay, because it's indirectly addressed。

And you keep doing that all the way through in that particular order。

going back here every time you get through the 256, going back down there, doing 256 of those, etc。

Until you get to the last one, which is the last one of block 59, and you need to count, right?

Because it might not be all 256 used, right? It might just be, you know, however many you need。

You need to kind of recognize that。 For the last one。

there's going to be some off by whatever errors depending on whether it's 512 block bytes or not。

Good。 Everybody with us on that one? Okay。 Hopefully that's not too bad。 The last one。

let's take a look。 The last one is now we're going to try to read/large file。

We first have to find it and then read it。 Okay, same thing。

You first go to the root, you find it, you have to go to block 25, you go to block 25。

which is a file。 You read the file。 You know that these are each 16 bytes each。 You go down。

you look, you're not in large file。 There's the large file。 It happens to be at i number 16。

You go up to i number 16 and you go, holy smokes, this is an 18 megabyte。 Long file。 And you go。

great。 Now it's not, you have to know that, but it's not quite as critical to know that until you get to that last block there or that last block number。

Right? Remember, all of the first seven ones are exactly indirect like we just did in the previous one。

Okay? So what do you do? You go to 26 and you go down to 26 and you read off 80 and you go to 80 and this was the beginning of the file and then you go to 41。

which I don't have on here and that's the next one and 82, et cetera。 Okay?

And then you're done with this one。 And then you do the same thing for 35 and 32 and 58 and 59。

And then you finally get to block 30 and block 30。 You go to block 30 and you go to, oh。

I gotta go to 87。 But I know that it's doubly indirect。

So you go to 87 and 87 has more numbers in it。 And the first number。

you go to the file there and that happens to be the 1793rd data block because you're counting all this up。

Right? And then you do, for you go through each one of these。

then you go back to block 30 again and you read 114 and 114 has 256 different numbers。 Right?

And those go。 And those each have 256 different numbers, et cetera, et cetera。 Yeah? Should I what?

Should I what?

Should I have 87? So 87 should have, let's see, 87, did I do it wrong? I might have done it wrong。

Let's see。 So 87 was from here。 87 should be, or 87 goes to 89 and that's a block number。

That's another number。 Which goes to, let's see, which goes to the actual data。 Yeah。 Make sense?

You go to 30, that has a block number。 Right? If it was indirect, that would point to data。

But now it points to a block which has a block pointing to data。

Should it have 256? It has 256 of them here。 Right。 Yes。 And that's an, oh。

I mean it could have 256 there。 It may not。 But it could have up to a hundred percent。

It doesn't have to if there's a, not necessarily。 One for a team。 Oh, sorry。 Yes。 You're right。

Because of this。 Yes。 You could have that。 You could have this be the last one, which goes to them。

And then it's only, correct。 Because of the way I've done it。 All right。 So。 Questions。 Yes。

[ Inaudible ], You know, I don't know if I did the math right。 But yeah。

It would have however many are necessary to get up to the maximum file size in there。 Yes。

It does not have to have 256 here。 Right。 That would be if you had almost 34 megabytes worth。 Yes。

correct。 Other questions on this? It kind of, hopefully it makes sense。 I mean。

the first time you see it, you go, oh man, it's so complicated。

It is pretty complicated for what it's worth。 But it's not un-learnable。 I mean。

you can certainly learn it。 And you just have to know how to do it。 And my final comment。

which was exactly my first comment, was don't worry about that this is the only way to do it。

There are many hundreds of probably thousands of different file systems。 Another question。 Yes。

Say again。 One, two, three, one, two, three, four, five, six。 Oh, I should have done another one。

Sorry。 Thank you。 That should be an aid。 Thank you。 Keeping me honest。 I'll fix that。 Thanks。

Anyone else? All right。 We'll see you on Monday。 Don't forget to sign up for labs on Sunday。

P4:Lecture 4 Filesystem Data Structures, System Calls and Intro to Multiprocessing - main - BV1ED4y1R7RJ

Welcome。 Welcome to week two CS110。 I hope I've already had a nice weekend。 I。

apologize for the lab sign up fiasco。 It was only half an hour but there were。

some people who are definitely concerned about getting signed up。 It looks like。

most people have signed up。 I can tell already that the Thursday times were, pretty important。

It looks like most people wanted to do the Thursday times。

But in fact there's still some spots in the earlier time。 There's plenty of。

Friday times and you guys are signed up for the class during this term so it。

should be all right hopefully。 If for some reason you are not able to get your。

laptop in or whatever let me know。 We'll try to figure something else out。 But。

most part looks like a lot of people got what they wanted and some people might。

be didn't so just let me know if there's a big issue。 Okay so we are on to just。

re-plugging my tablet in here because it seems to be giving me fits。

I'm going to set this works maybe not。 Maybe we don't do this out。 Hold on one second。

I want everything。 This doesn't work。 It doesn't work。 Okay there we go。

All right so today we are going to continue with file systems。

We're going to talk specifically about some data structures that the operating。

system has set up for the for keeping track of files and for keeping track of。

your processes as well as it turns out。 And then and remember this is the way。

Unix does it or this is the way Linux does it。 It's not the way everything does it。

but it happens to be there Linux does it and it happens to be a pretty good。

system for keeping track of your files for for what it's worth。 So we're going to。

do that then we're going to talk about system calls。 We have seen some system。

calls already and we've used some but we're going to talk about the details of。

wait what does it mean to have a system call versus a regular function。

All right and then finally we should have time to start getting into the first。

kind of whoa this is a different way of programming which is multi processing。

Okay so I hope I hope you enjoy that part because it's actually kind of neat。

once you start going whoa that's really I didn't really know you could do that。

for the program so that's what I think makes makes this stuff fun。 All right the。

assignment going all right assignment one going okay do Wednesday no there are no。

extensions of a allowed on no like late days for the first assignment and then。

the second assignment will be out on Wednesday as well all right come to。

off-sours we're still having trouble。 Okay so Linux when you run a program your。

program ends up in what's called a process or it has a process that is。

basically saying it's given a number and it says this program is running and。

that's the process but it's running under okay we'll get to the details about how。

that can change a little a little later but Linux maintains data structures to。

keep track of these processes of course it does right the operating system needs。

to know about your your programs that are running and so there's some data。

structure that it keeps track so it keeps track of all the different parts of。

all the different things that are associated with your program okay they're。

called process control blocks okay they're called process control blocks and they。

have lots of information in them okay and they are stored in this thing called。

the process table which is organized by the operating system okay process control。

box store lots of things one thing that they score a store is called the。

descriptor table and the descriptor table is a data structure that holds。

information about the files you've opened or the files that have been open。

for you or the file like things that have been open for you or that you've。

opened like for instance networking is modeled using a file the terminal is。

actually modeled using the file you can print to the terminal just like or you。

can read and write from the terminal just like you read and write from a file。

that's one of the beautiful things about Unix is they treat everything like a。

file if you can this turns out but anyway they keep this thing called the。

descriptor table and each process maintains its own set of descriptors okay。

which is the things that it has open at the time and we all they always get 0 1。

and 2 for free those are standard out standard in or standard in standard out。

and standard error they kind of get those for free okay most often they are what。

we call bound to the terminal in other words you type something and it goes to。

standard in you print out something from your program it goes to either standard。

out or the standard error and those are the normal things now we will spend lots。

of time in this course talking about hey what happens if we remap standard。

into something else to reading in from a file or standard in standard or the。

output of another program we'll talk lots about that as as the course goes。

goes along okay the descriptors as we've seen you use read write close etc open to。

actually interact with the files descriptors and then the process control。

block keeps track of all the different all the details of that okay if you do an。

open on a file you generally get a very small integer as the file descriptor。

because 0 1 and 2 are taken and then it just kind of goes up from there and it's。

your own file descriptors are fairly small numbers generally okay all right now。

this this diagram kind of goes down many levels here not many a few levels and we。

kind of understand each little level as we go if the file descriptor if a。

descriptor is in use in other words it's an open file okay it maintains a link。

to this thing called the open file table entry okay so if you have a file it's。

open you've got this thing called an open file table entry which has some。

details about the file okay it has things like it takes it takes into account。

whether or not it's a read only file or read write file or write only file or。

so forth it takes it has and that's the mode part of it it also has this thing。

called the cursor which just like the cursor on your screen tells you like。

where in the file you're reading from okay so it turns out that you can do that。

it also turns out that multiple programs can open the same file and be reading。

from different parts of it at the same time okay so lots of lots of files could。

be opened by various different programs and in fact they end up having this。

cursor saying hey this program is at this point in the file and this program is。

at this point that's the cursor okay it's also got this thing called the ref。

count now the ref count discusses how or basically keeps track of how many。

descriptors across all processes actually refer to that session okay and。

that's important because if you close the file it may be some other file needs to。

leave it open or something so it's not going to close it and just has this。

reference kind of that we will talk about what this other one this v node is in a。

second but if you were to do something like say open some filing as read only。

well this is what you might end up with you might end up with mode being read。

because you've opened is read only cursor being at zero because it was just。

opened and the cursor is right at the beginning ref count is one maybe you're。

the only one in the system that has that file open it's pretty likely and the。

V-Nope will get to in a second okay so that's how that that's how that works。

all right and in fact this is for process one thousand and one which you。

can't see because that's way too small but process ID one thousand one later。

today we'll talk about what process IDs actually are all about all right so what。

else do we have well multiple and all the different processes on your computer。

are take each have their own process control blocks and their own descriptor。

tables but they all point to a common open file table it's actually a little。

hard to read to I think but that's the open file table and this is where you have。

all of the details of that that we talked about on the previous page but it may be。

more than one file might or more than one process may point to it okay now it。

turns out if you have a cursor like if two files are pointing to if two files。

are at different places in the in the actual file then you need to have two。

different things you're pointing at okay but that's the basic deal there's one。

open file table for all of the final resources and so you can share them I。

mean if 25 programs are opening a file you want to be able to share them question。

yes the yellow and the green and the blue are the process control blocks for a。

process so your programs running it might be the yellow one my programs。

running it might be the green one and remember we can all log on to the same。

time we can be running different programs at the same time we each get our own。

process control blocks for the offer the operating system does it now this is。

hidden commute by the way it's not like you can go and dig into your process。

control block the operating system keeps track of that good question any of the。

questions at this point yeah so a good question how do you keep track of which。

file has reached which cursor these will actually point to different ones that if。

they're at different places in the file so then you got I think you've got another。

one that's open that has a different cursor associated with it that's because。

you might be reading in different places so you're gonna have two copies of that。

in that case okay and we'll talk about the Vino does in a second - that's another。

one all right so we want to be able to keep track of all these files that are。

shared okay in the case of 0 1 and 2 up here okay this this has a 0 and a 0 and a。

0 and a 0 that's standard in and they all happen to point down to this first one。

right here we look down here and then this one's all the way over here and they all。

point to that one why because they're all reading from the same standard in。

depending on which program happens to have control of the terminal at that。

point or the whatever the keyboard is going okay can't type in a keyboard and。

have to go to two different programs as it turns out okay it's only gonna go to。

one program basically I mean there's a little more nuance than that but it says。

basically the idea you also they also by the way all their standard outs go down。

to this one okay and this one actually is interesting if you if you happen to。

be running one program and and this is a standard act for like your programs that。

you want if they offer like one kind of session if they all are printing things。

out they all go to the terminal so you can run for instance make which you do。

for your assignments right and make calls g plus plus will make may have some。

output messages they go to the terminal and so does g plus plus because they。

share the same file descriptor in that case okay the same they share the same。

entry in the open file table in other words and that's just so that you can can。

all they can all go to the terminal as it turns out okay so that's the basics of。

that well guess what we can talk about these things called a V node so the V node。

is just a structure that holds the information about the file kind of like a。

cache and when we say cache we mean a place in memory that is storing something。

instead of going back to the original location right it's stored in RAM。

memory and it's very fast and that's what's happening with the V node here okay the。

V node is basically saying it's basically storing the details about what kind of。

file it is a regular file as a directory etc it also stores the reference count。

how many things have this file open and then it's stores a bunch of other details。

needed to actually access the files a bunch of pointers and then it stores a。

copy of the i node which we talked about last week which is the information about。

a particular file so if a file is open you don't need to go back in if you want。

to read from it or go back in and go and try to find it again in the file system。

you've got the information about the i node right there okay so that's the details。

and this gets updated as the file gets written to and so forth but and then that。

gets that gets pulled in so those are the levels of the data structures that the。

operating system and your program keep track of in order to in order to keep。

track of what files we have open yes why does it store the ref count in two places。

that's probably a good question it may be depending on who's reading it when or。

which part of the which part of the opposite reading it when it'll go to。

either the it'll go to either V node or the open file table entry I'm not sure。

why it does go but if they should be I think they should be locked up yeah。

if you have different if you have you mean like this one might this one might。

also point that would be a bad example of one but that might also point there。

yeah so the ref count down here is how many of the open file table entries。

point to that V node like you could have the same file open in two different ways。

okay because of you've opened and and that's I think that's the answer there。

question it is a copy from the disk yeah this let me show you the next one right。

here not the next one I'll get it I'll get to it what it basically does that just。

reads it from the disk and then stores it in memory so you don't have to go look。

it up again on the disk just faster that way faster you can do that the better if。

you're you're dealing with things like memory, that's a good question the question is is the open file table sorted in any way。

no it's not sorted in any particular way you basically keep links to it it's not。

like you're searching through the whole thing to find thing generally you have a。

like if your process opens a file it this green process might open this file。

well it's got a link right there and it knows how to search that very quickly。

and then it'll find here and then this might point to a V node that'll be so。

it's it's not slow process not and the things aren't giant either there might。

be thousands of files open but honestly thousands is small and computer speak so。

it's not a huge huge amount other questions right so this is a good。

question I should remember the answer the question was hey if I'm started if I。

read from some file with this one another file is also pointing to it at the same。

time which cursor is getting updated I believe it may actually create a whole。

new entry in that case if it needs to have different cursors in different。

places like if if one if if it's if it's reading through it needs to know where。

it's reading from and so I think there's another one that created I'll look at it。

I'm not a hundred percent sure and where that one differentiates yeah anybody。

else okay let's move on to another one here okay um there well I've already kind。

of talked about this there's one system wide V node table basically because。

everybody has to share these things because you don't want to have them all。

independent if you can help them help that okay it's kind of like an alias it。

says hey look somebody's opened this file why keep multiple copies of it for。

each person that's or each file that's opened it let's just keep it all in one。

place and then there's some coordination that has to go to deal with the cursors。

and so forth all right and like I said the well first of all none of this is。

really available to you just as the user the kernel keeps track of this you。

don't really want that by the way I mean you don't want you as the user to be。

able to walk through the open file table and see other people's files and what。

other programs have files open or be able to change any details about that so。

you want that to be somewhat secure and then this little i-node is and this is。

basically separate from the file system by the way the file system is kind of on。

the disk and you keep a copy in memory of for instance this little yellow i-node。

slice in memory okay and I'm not I'm not 100% sure when that actually gets。

updated so but it like it gets modified that probably propagates through at some。

point but it may not be immediate depending on the file system you're using。

okay so those are the big things now if you read the not the big textbook but the。

smaller textbook the one that's online this is all laid out in that pretty。

explicitly as well so if you need more details about it go with that all right。

that's file systems like more details about that let's move on to system calls。

this is where some interesting things happen system calls are as I've said。

I've said this a number of times system calls are the way your program interacts。

with your hardware and a network and things that are probably not so great to。

have your program directly accessing why because you guys are mean and would。

write malicious programs right like that's the basic idea this didn't always start。

this way Unix when it first started out you it was a very open sort of system。

and everybody it was in a research environment nobody was gonna go muck around。

in somebody else's files but when they when it started getting more and more。

and more users they realized hey we better figure out how to make this so。

that there's good security and a system call is the way that works okay so we've。

seen some system calls already we've seen open and read and write and close and。

stat L staff there's all system calls okay we'll see lots and lots of more。

system calls because remember this class is all about interacting with the。

operating system and the functions are different okay we write functions or you。

wrote some functions for 107 or 107 e that would have gone into let's say Lib C or。

just your user functions or lib standard C++ system calls have to be。

privileged okay they have to access their own data structures that your。

program shouldn't have access to and they should be able to be partitioned off。

from the rest of the user okay and so that's what you really want to be able。

to able to do okay open needs access to the open file descriptor table and well。

or the open and you don't want to be able to make the user have access to that okay。

in other words we shouldn't be able to get access to privileged information yet。

we still need to be able to open and close files okay so that's the bottom。

line up so we need to have a different call and return model now if you took CS。

107 or CS 107 e you'll remember that each process or maybe you don't and maybe。

this was never explicit made explicit to you I mean 107 e those you took that it。

was pretty straightforward that the one program that was running has access to。

all of the memory in the system for 107 or for 106 B or whatever you run a。

program it thinks it has access the entire system memory okay your program as far。

as its concerned can write and read from any part in memory now you can get。

segfaults and things because you're not actually given that from the operating。

system you can't just willy-nilly do it but that's the operating system saying。

look here's your portion but in the bigger picture it's not like if you were。

able to access a different portion of memory some other program would be also。

accessing it your program thinks it has all of memory this is going to become。

very important in a few minutes but the 64-bit address space first of all it's。

gigantic okay two of 64 is a very very very big number and as in the way the。

operating system in the hardware works it like I said makes it seem like your。

program has access to the entire memory system but it really does it this is a。

virtualization layer what happens is you've got virtual memory which is the。

addresses that your program thinks it's writing to and then that gets translated。

into what we call physical memory which is the operating system in the hardware。

saying okay this program actually has the memory here and this program has the。

memory up here even though they both think they're writing to the same place okay。

so so it turns out that you can you can do that I wanted to show you an example。

of that so I wrote a program and I was going to run it in two places at the same。

time and show that it was the same memories place but it turns out that they。

that the operating system plays some games for that so it doesn't ever put the。

same one even nearby it probably would if you if you if you tried to get a huge。

amount of memory but but for you know for the purposes of this I couldn't quite。

demonstrate it because the it was also a security measure - by the way I don't。

know if you remember from CS 107 if you took that when you run a program the。

stack ends up at a slightly different place and there's these canaries in there。

that say whether or not the stack gets overwritten by a malicious user or。

something those are all security issues I think it's the same same sort of thing。

but anyway no process uses all to hunt to the 64 bytes okay most programs use a。

very very small amount okay all right that's kind of the setup here there are。

lots of different segments in the memory system you talked about some of these in。

one oh six or one or one oh seven or one oh seven e you've got the stack the stack。

is where your user program keeps all its local variables keeps its arrays and。

keeps its kind of local data for functions the stack also is where function。

calls get pushed on to all the data for function calls get pushed on to and you've。

got the function calls the function calls in order on the stack okay you've also。

got the heap you guys wrote a heap allocator and heap allocators get memory in bigger。

chunks generally and that's a different portion of memory there's also the code。

section or also called the text section that's where your code is held okay。

there's a data section which is which has part of the data for like global。

variables things like that okay and the this this whole thing is managed basically。

by the operating system for your process okay the operating system says here's。

some memory for your process here's your stack here's your heap here's the data。

here's the code go and it's it could look a very similar program to program but。

again it's kind of abstracted a little bit away okay all right there's other。

segments that you may not have talked about okay there's the shared 107 e。

definitely talks about this there's the shared library portion and the BSS。

portion and the read only data segment those are all segments that they're。

specialized uses read only data as you can imagine is read only data you're。

only allowed to read to it you're not allowed to write to it they've got the。

shared libraries if you have a function like printf right if you have printf。

well there might be many many many programs all using printf at the same。

time to print out the individual terminals well why duplicate that code。

for every user just put it in one code location and have that user or have the。

user jump to that code location and run the shared code that's a good way to。

save some memory okay and that's the way shared shared libraries work okay all。

right so how do programs do function calls you should maybe remember this from。

107 you have a stack pointer right and you have all these other registers in。

here as well okay to call a function what do you do well you put a bunch of data。

inside the registers rdi rsi rdx hopefully this is me。

so it's up to some of us coming back to you from 107 right our CX etc if you run。

out of registers in other words the one two three four five six you start putting。

them on the stack and there's a very well defined way of doing that okay and。

again this is some people decided hey this is the way it's gonna be so this is。

the way we're gonna do it it's not like this is some come down from high sort of。

you have to do it it's somebody decided this and said that's what we're gonna do。

the first the first parameters always gonna go into rdi the second parameters。

always gonna rsi etc okay and then you do this call queue or call。

assembly instruction and that jumps to the program code for your for your other。

function in your own user's base and then it does the call and then you do a。

return and it comes back that should be relatively familiar okay from doing。

107 or 107 E although in 107 E you didn't have these register names they were。

nice like R0 R1 R2 here there there are these these historical names from x86。

days the original x86 days okay so that's how function calls work well what's。

the the big deal with that well this does not encapsulate the memory that we use。

in other words it doesn't hide that memory from anything else okay if you。

have your let's say you have your main stack frame here okay and then you call a。

function just a regular function called load files well that's gonna go on the。

stack okay and then you do your IF stream or whatever that might go on the stack。

and then that's gonna call other like read and write function system calls but in。

this case remember from 107 where you did all that like sneaky stuff with I。

don't know if you still do this with like ATM machines and trying to pack in and。

whatever because you can go and you can you can go and modify your own stack and。

whatever you can do that but that's not very secure okay in other words its。

privacy is not the prime concern here basically they're saying look if you own。

the stack for your program sure you want to muck around in your own stack go。

right ahead who cares you might crash your program or you might modify something。

for your own program but it doesn't matter right that doesn't matter it all。

the things change everything changes when you all of a sudden have to go to when。

you have to go to the system and get information from the system okay so now。

we get to system calls well system calls okay like open they as we said I said。

a hundred times already they shouldn't be exposed and they are need to be stored。

in a region of memory that your program does not have access to okay it can't be。

a shared library it can't be something that your program could actually code。

that you write modify just shouldn't be allowed okay so what do we do well we。

break up the memory diagram into a space that has the kernel stack in it okay you。

are not allowed to write to there and if you tried to the operating system would。

stop you anyway okay but the kernel stack resides in memory but your programs do。

not have access to that okay you basically have to tell the operating system hey。

I am about to do something or I want you to do something for me that involves。

something I shouldn't have a direct access to okay so it needs to be in a。

region of memory that the user can't touch and it needs to be executed in a。

privileged what we call super user mode okay so that that mode can have access。

that this is not your code anymore okay it's the code that's owned by the。

kernel okay the kernel owns the that code and you have to say to the kernel。

hey go do this for me okay it's called kernel space and it's got the stack。

segment and it's got its own instructions as well okay we can't use call queue any。

more to do this okay because the minute we use call queue that stays within our。

own address space we could we could do that so we need a different way of doing。

it what is that different way well that different way is to do a thing called a。

sys call and it's an actual instruction I'm going to show it to you in a second。

okay and what it is is you place an opcode in other words an operation you。

want to do all the operations that are the system codes have and have a number。

associated with them if you want to read you've got one for the operation code if。

you want to write it's to open happens to be three we'll see that in a minute。

open is three etc okay they all have their own opcodes that are based in the。

kernel defines them okay and then you place arguments same as before you place。

them in a bunch of registers now it happens for some reason it goes RSI R。

DIRS R DX R10 instead of RCX I don't know why that's changed but some other。

reason for that not important for this class at all but that's that's the way。

that goes and then what you do what you do is you perform what's called an。

interrupt one or seven E folks you'll know what interrupts are but an interrupt is。

basically saying hey operating system stop my program and handle this and the。

operating system goes oh okay your program is not gonna be stopped and then。

I'm gonna go do I'm gonna look at the registers and do whatever you want and。

then I'm gonna start your program up again that's basically what an interrupt。

is doing it's interrupting your program until whatever you're asking get to get。

done gets done okay we're gonna see that a lot in this course of your program。

waiting around for something else to get done being in some sort of interrupted。

state or sleep state or something like that okay so you put all these things in。

the in the registers you do this cis call it handles it and then it returns back。

to you with the result as you might expect in our a X except for a couple。

things there's a question oh such a good question the question was yeah this。

question was what happens if you have more than six arguments you don't there。

are no system calls that are that have more than six arguments in them that's by。

the rules because of that exact reason you don't have access to put them on the。

stack so they they get put in the just in the registers you have and no system。

calls need more there are there are ways of getting around that by the way you。

can but but in general there are no system calls that have more than six other。

questions yeah yeah let me talk about the interrupt handler good question the。

question was just like what's what's the steal the interrupt handler it's。

another function that's actually in the kernel space that runs and that function。

goes okay now I'm in kernel space on the on the kernel I get to do and I get to。

access all things you can't affect that function at all but you can call it and。

that didn't calling it through the cis call method that we have here that's。

how you do it so what's the bottom line on that this is how you tell the。

operating system please do something for me because I'm not allowed you're not。

allowing you to do this system calls or wait the way you do that and then there's。

some some details about what happens if you return to the regular function and。

there was an error if you if you have a return value that is negative what it。

does is it sets the error number or air no which is that global variable to the。

positive value of that negative value why that's just the way they've done it。

and then you update r a x to actually contain negative one so if you do a。

system call and you get back negative one you know that you've that you've got。

an issue and and then you go and check the error number okay yeah air number。

air number air no is per process yes it's part of the sea library yes。

yes so when you call the when you when you do the interrupt the current knows。

that your process is the one that in a yeah and it knows where to find those。

details good question anybody else all right so that's how system calls work。

I'm not gonna ask you too many more deep too many details about this I'm not。

gonna certainly not gonna like make you do any assembly coding or anything like。

that for this but I do want to just show you what happens when when you do。

this remember the copy function that we had the copy function opened up two files。

opened up a file to read from opened up about the right right to and then。

copied all the data out of one into the other let's look at that in gdb and let's。

actually let's see let's stop it on line 19 we'll break on line 19 and we'll run。

the program okay whoops I gotta run it with let's say copy that see to copy copy。

dot see okay all right so now we stopped online 19 we're about to step into the。

system call if I do yes to actually step into the system call you'll see that it。

goes into cis call template we haven't quite called the actual system call yet。

because we have to do the setup in other words we have to set the we have to set。

the parameters and we have to actually do that cis call but we can do disassemble。

here and we'll notice that you have things like the a couple things going on here。

that this is not really that important but it's gonna move some of some detail。

like move 0 to remember I said open was 0 to for the。

operation code well there goes right there okay and and I think I said our。

DI is the where the actual name of the pro the first argument remember open here's。

what open looks like it is let's see let's just list 19 again whoops。

list there it is so that's the open call is our V1 which I type is copy dot see。

okay is there all right so let's actually do this let's do this for I。

  • or if you this will actually list all the different instructions as I'm going。

through it let's step into the instructions one at a time okay it's gonna。

do a couple things that aren't that important but then it's going to move。

0 to which is the opcode into a E ax or our ax and then it's gonna do the。

cis call okay and it's gonna do the syscall so if we look our AX is going to。

be to well that's the opcode for open if we print out our DI let's cast it to a。

char star our DI it should be there we go copy that see okay so it's done the。

setup and it's about to call the function for us okay and do the system call now。

you might ask you oh great can I step into the system call kind of impossible。

to step into a system call because the operating system is running your program。

which is running GDB or I guess it's running GDB which is running your program。

and stepping into the operating system would mean trying to like stop the。

operating system and it just wouldn't work it turns out it would be impossible。

to if you want to debug a kernel you actually generally have to do it from。

another computer that's talking to the other computer basically through like a。

connection of some sort it's tricky to debug a kernel because it's got to be。

running and you can't really debug it that easily or put it in some other。

virtual machine or something like that so anyway if we let's just actually do S I。

see what happens it actually jumps it actually let's see I believe it jumped。

back oh yeah jump back to our thing if we finish let's do p。rax yeah we had a。

positive return value which is going to mean there was a success in that case。

okay so that's how that's how system calls work it's kind of like calling a。

function it's just you don't say call you say syscall which stops your process。

and turns over to the kernel okay well questions you have about that system。

calls okay relatively straightforward I mean that shouldn't look too new to you。

just know that most of the functions we're going to use our system calls because。

of the type of class that this is okay here's the summary summary is we use。

system calls because we don't want the user to have access to sensitive parts。

great we can't do that using our regular function call why because we own that。

memory we're allowed to touch on that memory so our code is accessing things。

that it shouldn't be that would be bad because we could be malicious okay and。

then the way this happens is through an interrupt called syscall and that stops。

your program transfers over to the operating system and does that and then。

once the kernel is running it's in control does the opening closing reading。

from a disk whatever and then afterwards it returns back to your program okay all。

right what questions you have about those right now system calls anything else。

okay again this is this is in the this is talked about in the in the book in。

fact I think it's talked about in the bright you know how how how her hand book。

as well okay let us move on to a fun topic called multi-processing okay so far we。

have been talking about programs that ever since 106 a 106 B 107 generally your。

programs are running a single process okay a process that is going along and。

might be doing lots of things but it's one process at a time okay it used to be。

that one process was all computers could actually run anybody know what。

computer that one is on the board says it on there you kind of ABC it's an IBM。

personal computer from about 1981 this was actually the computer I took to。

college it did it did not have a hard drive in it like I said and I said the。

other day it had a maximum of 640 kilobytes of memory which is not that much it, ran at 4。0 4。

77 megahertz your computers these days run at giga hertz so。

it was relatively slow but it actually had one process available at a time you could。

run one thing on to the time and that was all you got now along the way。

somebody some clever people did write a program or some various programs that。

allowed you to kind of do multi processing but it was very rudimentary。

and it crashed a lot and it was didn't it didn't work particularly well and。

your computer the computer was slow enough that doing multiple things at once。

didn't really make sense anyway okay well these days we have really fast。

computers okay we have computers that are giga hertz and that are running really。

at very high frequencies okay and so we want to be able to run multiple programs。

at the same time okay this is called multi processing all right and you will。

tell your computer to do things concurrently in other words you're gonna。

write a program and it's gonna be doing two different things potentially。

exactly the same time that's kind of interesting okay I think that's that's。

very interesting what we can do to show you a little bit about the process on。

the computer in fact I will pull this up here let's see processing okay we are。

gonna open up I think we're going to open up get PIDX。c so when your program is。

running as I said before we kind of talked about this a couple times it has a。

process ID associated with it it is a process and it has a process。

identification identifier associated with it okay and we can actually get the。

identifier if you want by doing the following PIDT PID equals get PID okay。

PID underscore T is just an integer okay you're all why don't we just call。

it integers well they wanted to make it its own type it's just typed after to an。

integer okay you can do that and then you can say print F my process ID percent。

D it's just an integer PID and return zero and that should do it except for I need a。

semicolon after that there we go okay make get get PIDX okay get PIDX。

process ID was 15,787 great process ID 15 788 process ID 15 789 now if I wait a。

little bit right if we wait a little bit it's probably not gonna be 90 but it is。

in that case okay so sometimes there are lots of other processes being。

started and stopped on like myth 55 right now that will probably start going。

up in in order let's see if it goes up there we go we got a few more that kind。

of started up in there right but but but that's what that's what episode。

you're just given a number when your program's running and that's your。

process ID okay all right the process ID allows the operating system to keep。

track of you so that know it can use your process control blocks and it can do。

all that it can use your process ID number to say you get to run for a chunk。

of time and then some other program gets to run for a chunk of time and this is。

why I can move my this is why I can have the program my program running and I can。

also do things like right on the board while the program is the other program。

is running in the background the operating system is fast enough that it。

allows your computer to seem like multiple programs are running at once。

most of the time there there's only one thing running at a time except for the。

fact that your computers these days all have multiple processors in them and in。

fact the myth machines all have four or eight different cores in them which are。

individual processors which means they actually do run more than one program。

at a time okay you can actually see this if you go to I don't think they have。

this I think I installed this but we can we can do it but if you type H top okay。

what H top does is it gives you a list of all the different programs that are。

running on the system from all the other users to and you think wait a minute I。

can know what other users are running you can as it turns out that's not hidden。

from from you and it also in fact this myth machine has eight different。

processors here and you can see them all running nobody's really doing anything。

right now like this myth happens to be very not busy right later in the quarter。

will be in class and we'll look at this and it'll be like going crazy because。

some 107 assignments going you know they're doing some crazy things or whatever。

okay but that's what's going on here you can actually look at this and it tells。

you what percentage of each processor is being used most of the time it's pretty。

idle but right now not much is really going on okay so that's it that's what。

processes are okay and like I said your computers these days have multiple。

processors which means they can run multiple programs at the same time。

literally it's not just time shifting or time splitting between the each process。

it's literally two are running at the same time okay we'll see how this becomes。

important very soon okay so we have a new system call okay the new system call。

is called fork and it's obviously it's I mean kind of like a fork you eat from。

but it really is like a fork in the road okay this when we say fork what fork does。

is it says the fork is a system call that allows you to create a new process。

from your program so that now your program is going to literally go down to。

different code paths the exact same time okay that's what fork does did you have。

a question okay so this is what fork actually does okay we're gonna see why。

that we might want to do that you might be able to start thinking about why you。

might want to do that but it's but for for the types of programs we're running。

in this class multiple processes become very important here's what fork does and。

this is a little subtle and people always tend to forget this so I'm gonna。

explicitly say what it does fork creates one more new process okay you've already。

got a process fork creates one more process that's a lot of people for some。

reason last quarter thought that it created to it does not fork creates one。

and now there are two if each one of those forked right now there would be。

four right because each one of those creates one more and then there's a。

total of four etc but your process if you fork it creates one more process and。

that is called the child process because the parent is the one creating it okay。

and the fork call by the way the child and parent and this is the most。

interesting part about it they both start executing on the next line after。

fork it's not like you fork and the one the the child goes off somewhere else it。

does this in the probe probe the parent goes off no no they both go to the next。

instruction and now you've got two programs two processes running the next。

line in your code which is kind of interesting you'll see how it works in。

a minute okay fork returns a PID T or an integer but it's only kind of a PID。

okay here's how it goes the parent when there when you call fork the parents。

return value is the PID of the newly created child okay so you create a child。

it's a new process it gets a process ID the parent gets that written in return。

okay so the parent actually can keep track of all its children that it creates。

you can fork multiple times by the way so you can fork and you get back the the。

ID of your child that you just forked the child gets back zero right the child。

gets back zero if the child got back its own PID it wouldn't make much sense because。

then it wouldn't like you wouldn't be able to differentiate between the child。

and the parent if the parent got back the child's PID and the child got back the。

parent's PID how do we know who's who we still can't make that decision right so。

the way it works the parent gets back the child PID and the child gets back zero。

which means you're the child PID okay you are the child now look if the child。

wants to get its own PID great it can it can call get PID but the return from。

fork is zero if it's the child process that makes sense this confused people for。

like for a while in other times I've taught this not not exactly sure why so。

I'm being very specific oh good question if you're in the child。

process you can do get P PID to get your parents process if you want yeah and。

this is a good this is good if you want to be let's say sending signals back and。

forth between the parent and the child we'll talk about that later in the course。

okay but yes you can if you want to you don't get it for this by default in fact。

it's most often that the child could care less what its parents PID is it turns。

out but you can get it if you want to okay now here's the other interesting part。

about this all the memory between the parent and the child is identical I can't。

draw the straight line at the bottom of this tablet all the memory is identical。

but it is not shared okay so I create an integer int a and then I fork right on。

the next line the parent has an integer int a the child has an integer。

int a the parent let's say the pointer to the parent's version of a is 0 x 1 2 3。

4 the child's version is 0 x 1 2 3 4 the pointer to that if the child changes。

a the parents does not change how does that work any ideas yeah exactly the。

answer was it doesn't it's not really the same exact memory they are now they。

are now separate memory but they think they're the same part of memory now what。

it does is you think wait a minute if I have a huge amount of code and I fork。

isn't this gonna be really slow because it's got to somehow has to copy all the。

data from like the parent to the child it does this thing called copy on right in。

other words the operating system is clever enough not to make any copies。

until either the parent or the child changes a value so it's kind of shared in。

the sense that the operating system is keeping track of it all but the instant。

you change a value in one in the child of the parent it is not reflected in the。

opposite the child be your parent if you're the child not reflected you make it。

the operating system makes a copy of it is okay great now you have your value and。

you have your value okay it's a very interesting interesting concept as it。

turns out it's right they did this for lots of reasons they did it so that they。

can make it so that programs can go to the next line and not worry about any of。

the data and they also did it in the way they did so that it's fast so that's。

kind of me okay what questions you have on that how fork works yeah。

good question so we're gonna see actually we're not quite gonna see exactly that。

but the question was what happens when the parent process is terminated the。

child process keeps going yeah process child process keeps going now it turns。

out that you might end up going back to the terminal because your parent is。

what's running in the terminal and the child process might end it sometime later。

after you type some other command or whatever so it's a little bit but of。

course we have a way of dealing with that which we'll talk about like how to。

make sure that your parent doesn't end before your child does and so forth but。

that's it the other interesting thing about it although not particularly。

relevant is that if the child or if the parent ends the child's parent now becomes。

root actually which is like the main underlying process doesn't have a parent。

anymore parent died unfortunately sad but that's that's the way that goes good, question yeah。

Oh sure yeah this is the question is look if you wanted to pass along the。

PID to the child you could call get PID before you fork save it in a value and。

then it's available later sure or the child can just call get PPID which is。

get parent PID so either one but yeah there's there's ways of doing that there。

is there are ways of sharing data by the way but we're not going to cover that。

really in here you'll see one example in a lab about sharing data but most of it。

is not most of it doesn't need to be shared as it turns out I asked it we asked。

a pretty good in my humble opinion question on the final last quarter where a lot。

of people thought oh no we have to share memory but you actually didn't have to。

as turned out but so you won't really be responsible for doing much of that okay。

all right the reason the parent the child get these two different term values is。

simply to differentiate them okay the first thing you will often do in a。

program is if PID equals equals zero do this because I'm the child oh else do。

this and that just says okay now I've got two of the process going in different。

directions so that's generally what you do sometimes we want them both to do。

exactly the same thing for various reasons but most of the time we do want。

to make a decision say look if I'm the child I'm gonna go down this path if I'm。

the parent I'm gonna be on this path they start at the exact same point though。

right so that's the difference there all right so let's look at a program okay。

we're gonna we're gonna write another program called basic fork oops oh no I've。

done it again hang on there we go cursor there we go okay basic fork dot no not。

not the binary how about dot C there we go okay so what we're gonna do in here。

okay is we are going to actually just print out greetings from a process and。

then the per parent of that process okay so that's what it's gonna look like。

there okay and then we're going to call fork okay and we do a little bit of error。

checking if the PID is negative one it means the fork failed why would a fork。

fail maybe the operating system ran on memory or some other thing happened you。

will almost never see that actually that's not true you will see that if you try。

to do thousands of processes your program is limited at least on our system。

to about a thousand maybe a thousand twenty four different process so if you。

tried to do forking such that it just kept creating many many more processes。

you get limited about a thousand twenty four there's a thing called a fork bomb。

which would which is where you just basically try to just do as many if you。

do kind of recursively call for or you call it a loop or just everybody forks。

and it just uses up all your processes this is why they had to limit it because。

you could take down a system by doing the fork bomb which is very easy to do as。

it turns out but anyway you were gonna check it there and then we're going to。

print out buy buy from process your the process that we are going to get from。

calling get PID and then we're going to also print out the parent process okay。

everybody see how that's gonna happen remember by the time the program gets to。

this line here there are now two processes going the original parent and。

the child and that's the difference okay now we haven't done any checking to see。

who's the parent the child we don't really care for for this for this one。

right now okay make basic fork it's already done yep okay basic fork and。

there we go all right greetings from process 26385 with parent 2892 that's the。

shell as it turns out okay and then it says buy buy from process 26385 with。

parent 2892 because it ended and then it says buy buy from process 26386 which。

has parent a parent of 26385 and that is the parent that created okay so we can。

run this a number of times again you probably will not see this with this。

tiny little program but it turns out that the ordering of those two final lines。

completely arbitrary okay why because who knows who gets to go next a parent of。

a child it is completely arbitrary and non deterministic this is the problem。

this is one of the harder parts about this class is that you're not dealing。

with non-determinism okay debugging some of these things is very tricky if you。

don't if you don't have a real clear idea of what you're expecting because you。

can run a program two or three times in a row and it can come out absolutely。

different each time but still be perfectly correct okay so that's a little bit a。

little bit tricky to deal with sometimes question good question that was a very。

good question why didn't the print-off statements like bang into each other and。

print out like that so print F actually has some buffering in it and the way the。

print-off function actually works it's going to generally keep like one call at。

a time is going to get through and it will print all the way to a new line now。

you are printing multiple things at once and some other person's printing multiple。

things at once they might get interlaced but one line at a time not gonna get。

interlaced good question though yeah you will see some interlacing but not not。

one line at a time generally yeah when we get to C++ using C out。

Jerry Kane actually created a library that makes it so it doesn't do that and。

C out either which is nice yeah yeah so this is a good question the question was。

wait a minute when the parent process that when when you do four like one's。

going and the other's going maybe maybe not at the same time okay two。

different processes now the operating system treats it just like any other。

two different processes if there's only one processor available the child gets。

to run for a little time the parent gets to run for a little time the child gets。

run for a little time the parent gets run for a little time and it's just shifted。

back and forth if there's two processors it actually could be that both are。

running on their own processor at exactly the same time literally the same time。

yeah but that's the that's a different there so the operating system is taking。

care of that and saying great now even other process I better time slice it so。

it gets a little bit of time to keep running yeah we're gonna see lots of。

those sorts of examples as we go along okay all right so that I just showed you。

the output of this the as I said the original process has its parent being。

the shell and then this is non deterministic so we have to be a little。

careful of that when we're writing these programs because you can't。

necessarily predict the order question yeah anybody else I'm sure you have lots。

of questions on the stuff as we go a little bit farther okay fork is called。

twice are called once fork returns twice right it returns to your program twice。

in two separate processes okay as I said all segments of data are。

faithfully replicated all right the heap is replicated pointers are。

replicated file descriptors are replicated which is interesting it means that as。

it turns out if you have a fork after you've opened a file and they both try to。

read from the file the first one to read gets part of the file the next one's。

cursor is actually moved over and gets more of the other the rest of it so it's。

actually duplicated at that level as it turns out okay the we will talk about。

how to deal with open file descriptors as we go along as well okay what else。

any other question on that all right now like I mentioned debugging this is a。

bit tricky you might want to say you might be saying yourself hey how do I。

actually go and debug this stuff well it's a little tricky you can do it in。

gdb I've given you the commands here you can go look at the actual you can go look。

at the actual code to do to kind of the gdb trace if you want to I'm not going to。

go over that right now but you can say for instance set detach on fork to off。

basically what it normally does is gdb if you have a fork it just lets that child。

go and it continues with the parent and the child processes on its own if you。

set detach on fork off what it does is it actually goes down the path of the。

child gdb keeps track of the child process you can do these things you can。

do this thing called info in theoryers which is the two different all the。

different processes it gives you a list you can switch between them if you want。

to that's that's down here and you can also say oh if I'm going to continue I'm。

gonna continue one of the processes and then start debugging the other one you。

can do that I would say most people probably don't get to this level but if。

you want to and you're milk and you're debugging you and I really can't。

figure this out this is how you would do it okay it's not not that often that you。

do that but just know that it's there in case you go ah I have to go debug this。

and gdb I have no idea how that go look at the trace and you'll see how to do it。

okay all right like I said the only real difference is the process ID and the。

return value from fork okay you get a new product you get a different process。

ID if you're the child from the parent and either return value from fork is is。

different okay parent gets the product PID child gets zero not its PID but that's。

how we differentiate it from the parent okay and that's how we make them go in。

different directions if we want to okay let's see another difference oh let's。

actually this might be a duplicate so I hold on no not really it's kind of got。

the it's replicating much information but there's no like I said there's no。

default sharing okay you can wait in the parent we're gonna get to that in a few。

minutes where you can say hey I want to wait for my children in fact we're gonna。

talk about that for much like for much of a multi processing material we're gonna。

talk about hey how does the parent wait for all of its children appropriately。

how does that work we're gonna get to that you can't wait for your parent as it。

turns out not really allowed the way it goes why I don't know they built it that。

way you're not really allowed to wait for your parent turns out when a child。

process ends the parent can get notified or the parent can wait for that but it。

doesn't go the other direction I'm not exactly sure why but that's the way they。

that's where they built it all right let's look at another program okay we're。

gonna look at a little program of a tree of forks okay a tree of forks I think let's。

see is this the one is it hang on oops there we go。

there we go let's let's look at the see if we're gonna do which which one we're。

gonna do we want to look at well let's just look at it here and then I'll go。

run it as we go here's what this is gonna do okay this is going to basically do a。

for loop around a bunch of fork calls okay so what we're gonna do is we are。

going to basically have a string called K-trail okay and we are going to find。

how long it is we're going to loop through all the letters in that string。

or all the characters in that string we're gonna print them out at one letter。

at a time but in the meantime we're going to fork okay and then in this case it's。

an assert that just says look at the PID is assert the PID is greater than equal。

to zero which would make it so that if anything failed it would crash the program。

or stop the program and then it will keep going down okay what do you think is。

gonna happen here if the string is ABCD oops if the string is ABCD what is going。

to happen in here the first thing is gonna happen is what it's gonna print out。

okay and then we're gonna end up with here's the parent we're gonna end up with。

a child process okay and then the for loop is going to come back here remember。

it's going to the same it's got the same value of I it's got the same value for。

the string and same value all that okay but now there are two things going through。

here I is going to be incremented to one okay and then it is going to print。

what from the child be and then what's gonna get printed from the parent be do。

we know what order that's gonna happen we have no idea but it doesn't matter because。

they're both big right but then what's gonna happen with the parent the parent。

is going to create a child and the child is going to create a child and then。

everybody is gonna go back up here and that they're all gonna be on see now how。

many versions of see are we gonna get four one two three four not necessarily。

in that order and then all four are gonna each create another child and。

another child another child another child they get all and they all create a child。

how many different D's are we gonna have eight different D's let's try it okay。

let's try it I have to figure out which program this is because I think I forgot。

which one is we are gonna look for trail length hang on trail like start a see。

fork puzzle that's the one okay let's see fork oh no hang on in for might might not。

be actually written oh it's not written hold on copy for I have I have written。

original let's see to fork puzzle that see okay yes make for puzzle okay okay okay。

ready a B C D A B C B D C D C D D oh not the order we necessarily would have。

hoped for right it's not one a two B's three four C's eight D's but there are。

that many right one a I see one a one two let's see two B's one two three four。

C's and then eight D's I assume okay all right let's try running it again oh。

what happened there why do I have my come my line here and a D way over here any。

ideas what's up the parent finished first good answer yeah the parent finished。

first and one of the children was still doing its printing after the pair finished。

the shell got it back and did that right so we can we can do it a few more time。

there you go got the same sort of thing again right that time it happened to。

work okay but remember non deterministic and that's the way it goes so let's spend。

a minute just making sure everybody understands what's happening in this。

program what questions do you have about it you get why there are one a two B's, forces etc yeah。

yes so the question was okay I get that it's I get that why they're all the。

letters there why they're all those letters why is it out of order is it。

because you have these multiple processes given time from the operating。

system it's exactly that and it turns out that the parent in there must have。

gotten the like gotten the signal to keep going through all of its D's before。

one of the children did and in some cases the child and children all in before。

the parents so it's it's completely non deterministic in that sense you can't。

control it okay in that sense there are ways to control what happens we will get。

to that many times you don't want to necessarily hinder them from going in a。

particular order but sometimes you need them to go in a particular order so we'll。

talk about that too questions so if you have a parent and then you own a child。

then there is an additional form that ends up with grandchildren let's say yes。

so grandchildren not the children yes the child finishes and then basically。

disappears but those grandchildren are done is there new parents the original。

parents or is it the terminal will they return to this that's a good question I。

believe that anytime your parent the question was hey what happens when the。

children's children died they their parent now become the grandparent and。

so forth no I don't I think it just becomes all the way to root I think if。

your parent dies you're just kind of become an orphan right so yeah I think。

that's what that's what happens about it, root is not the same as shell root is like the kernel basically running it's。

running there might be some other program but yeah it's it's not the shell。

it's out this shell can end and the other program will continue as well unless。

there's some other method to kill them yeah good yeah。

yeah so the question was hey explain again what happens with the D at the end。

let's see if we can get it to work no no there we go okay so why did that D。

show up after my terminal line showed up well remember there's the parent。

process which when it ends the terminal says oh you've ended here's a here's a。

prompt again type something else right if the parent process ends before all of。

its children do then it will do that and the child can still be running and。

print out to there we're gonna see we're gonna see an example of that I guess on。

Wednesday where we're gonna get into some of these crazy ones where you go oh。

wow it really does do that like it just it will print out you can type LS and it。

will still be putting stuff out for your program and whatever right you can you。

can do that but let me let me show you try something that's for I in let's see。

let's do this sequence one through 100 do echo I done no echo dollars on I done。

there we go okay that's gonna print out this is bash it's a different program。

I'm just gonna print out a hundred numbers in a row but if we do this and we。

say sleep for one second in between it's gonna print one two three etc right if we。

do this and we can run this what we call in the background I don't think I've ever。

done this before it is now running and I can type LS and my program is running。

along in the background and doing its thing right I mean I can even it's even。

crazier than that I can do like them and then oh boy you know so it'll do this。

until it gets up to 100 and it's just two processes running at the same time and。

they're both trying to print out the terminal and they don't care that you。

know one's going and one's not so that's what yeah so there you go that'll just。

keep going right until you do that I think I can still end of it or whatever。

right you can actually you can't you can put in the foreground and then kill it。

but you will learn all about this when you do the Stanford shell assignment which。

is coming up in a few weeks where you you have to build this and it you end up。

with things like this but yeah but does that answer your question about how it's。

happening yeah yeah, like in your parents it's like you know what happens。

the question was what what's the significance of when the parent process。

dies before the child does nothing really I mean there's no real significance。

except for the fact that you can end up back in the shell and you might not want。

to be like we don't like it that it printed out that D after our after our。

prompt we want it to be like look finish everything then print out the thing and。

we'll we'll see examples that straight straight away on Wednesday yeah yes why。

are there always one child we do have multiple children we didn't know oh。

lies are always one they're both there are that's a good question they're all。

going roughly the same time right it's such a fast program they're roughly going。

at the same time it could very easily come out there's two I mean you just don't。

know but they're they're they're pretty much doing it all at the same time before。

the and then the parent will we'll just end almost at the same time and sometimes。

one will be out but there's no significance to just having one yeah good。

question all right what else yeah good question if we look at the program the。

question was can you choose to just do one fork each time well sure how about。

this what if we wanted to stop the program at the end at the end of what if。

we wanted to stop the program so let's look at fork puzzle dot see okay okay so。

we have the fork there okay let's do let's see what would be interesting here if。

we did if we said this if PID equals zero now we know we're in the child right we。

could say oh I don't know print f by you know whatever and then we could also do。

exit zero or we can do return zero or whatever we want was to return zero return。

zero and that will actually say okay I'm done and not go through the fork right。

for puzzle puzzle right and now it's gonna just do that parent only all the。

other children did there like guys but nobody ever got back in the for loop so。

you can just use to have you have to figure out the logic for that yeah good。

question anybody else yes, ah that's a good question question was if we had one processor and you were。

forking the process are you necessarily going to are you ever gonna make the。

program faster because that probably not no I mean if you're trying to do a。

certain amount of work and you have one processor to do it unless there's some。

other thing going on like for instance you're waiting for network or you're。

waiting for a file be read or something like that that can take some time other。

than that no you're not gonna save time this is not necessarily a time saving。

sort of thing it's kind of a logic thing where we want something to happen look。

the shell is doing exactly this it's forking a process and running whatever。

you tell it to run and so so we'll use that met more than just this kind of。

funny like oh do lots of things for multiple processes yeah good question。

all right I think we will see you guys on Wednesday。