 Come on. All right. Welcome to operating systems. Let's see if I need more volume All right. Oh, that works. Well, come on All right, so welcome back. So this is operating systems It is really hot in this room and it's kind of disgusting. So Yeah, great. All right off to a great start so Let's just dive into it and ask why we're even here. So why do you have to take this course? Why is it a useful course? Well at the very core of it Understanding operating systems will make you a better programmer Because you will actually understand what is going on with your computer and how to use it more effectively So there's only two types of software You'll be writing if you're writing software, which is probably all of you at some point in your life No matter if it's Javascript. No matter if it's Python. No matter if it's whatever You either write software that interacts with the operating system or it is the operating system Those are your only two choices in life. That's it. So you're so Knowing this course if you're using anything that uses the operating system, that's pretty much everything Otherwise you're going to be writing an operating system and then taking an operating systems course It's probably a good idea if you want to write an operating system So I'm John Ailsen your instructor I will save you the Pain of trying to pronounce and spell my last name and I will give you some simple tricks So that in red exit out terrible looks awful kind of a mess elf that's simple right Christmas is going to come up by the end of the semester and We'll be able to enjoy Christmas at the end. So hey elves will be in season there. That's easy to spell I hope only three letters The next one is sin elf sin Perfectly valid pronunciation my last name go with that makes it easy so if You mash them together you get elf sin and then if you need to spell my last name Stick a yo after the E. That's it Then hey you spelled my last name without with just remembering three simple little things that aren't that disaster of spelling So that's an easy way to remember it in case you actually have to use it for whatever reason Why would you need it? Well all of the public course material will be on my website So it's elf sin calm But spelled properly because elf sin calm costs too much money and hey It's my last name anyways, and there's not that many of me So all the course Lecturer pds the links to the lab website Links to the YouTube everything Will be public so you don't have to log into anything if you just want to look at some course material for some reason on the bus or whatever so easily accessible don't have to log in and Anything private will be on corkis. So right now corkis is a bunch of links. Eventually. You're only gonna have your grades on there And that's pretty much it So how the course will be structured you will do the labs on git lab if you use github before Then it's quite similar to that if you haven't used github before you will eventually in your career so git lab is a good introduction to that and That's how all your labs will be done There'll be no zipping files and dropping them here or whatever you just Submit your code to the server. That's it. You don't have to do anything special You don't have to write submit you don't have to do anything You just do normal software engineering things which if you don't know you will learn and That's it. No fuss. No must. No weird other additional systems You might see that people are also using the discord chat. So there's no piazza. No nothing I found discord a lot better and less annoying and it's a lot more interactive. So Use the discord. There's a class discord last Semester when I tried it a hundred percent of the students signed up for it So all of you should sign up for it like 41 percent of you have already done this before I started last last lecture So it's probably hopefully even higher now. So that's great. And then the Lectures will be streamed on YouTube and the recordings will be there. So don't have to worry about missing anything So do you want to sign into the discord? It's elfson.com slash discord and it's also the link to that's on the corkis and Through doing that it will also it'll ask your u2 or id whatever that's hooked up to the central off system and if you use that link it will also set up your git lab account for free so you don't have to do that and Also, join you to the discord server and all that fun stuff. So that's great And as you can see in the lectures, there's a space for that for chat So if you want to ask some questions there, you can if I miss it And don't see it then you can just hey flag me down class and tell me that hey There's a question there that I need to answer but I'll try and monitor it as best I can With that said lecture attendance is still important since it is much much faster for me to get feedback For you and sometimes for you to provide feedback to me Because sometimes you can do it passively if I look around and see a bunch of what the WTF faces then I know that I probably screwed up and I'll probably screw up in this lecture because the first one had some problems So hey, you can ask me questions if anything is unclear and hopefully we can resolve it in the lecture. It's just much faster We'll also have some live coding and we'll be able to create some happy accidents And I'll hopefully be able to explain some of the things you tell me to do Because that's always fun, especially if you tell me to do something that's outright insane Tell me to do it. I'll do it. We'll see if we can break my system if you can break my system You get something I Guess I probably can't give you grades, but hey, I'll give you something And if there's anything I can do to make attending lectures better Let me know first would be a call to them to turn on the AC because This room is awful So let's hope that is a call. So let's get the boring stuff out of the way and the evaluation for this course So you'll have we have 12 weeks for the course You will have six labs and they'll be due every two weeks and you might be looking at the list That's on the slides right now and say hey John what the hell there's lab zero. That's clearly seven labs not six. Why are you trying to sneak in a lab? well, I split Lab one into two parts because most of it is set up and getting familiar with the system so lab zero is all set up that you don't need any prior knowledge for and And that's it. So it's just to get you set up and then you at the end of it You do a one-line source code change prove you can use the server That's it for code changes one line and spoiler alert. It's not a complicated line It is your name and then you answer five questions on crowd mark and then which are mostly just feedback things and then that's it and Unlike in other operating system courses where I found it was always kind of weird at the end of the course You've never installed an operating system or know how to do that or have practice doing that Well lab zero guess what you are installing your own operating system to a virtual machine And it will be yours to break or use or whatever happens to happen with it But you will install an operating system from scratch. So later on in your career You'll be using virtual machines whether you like it or not. That's just how things are And hey, if some family member breaks their computer and can't afford windows Hey, you can at least install linux for them. So that'll be a great skill to have Um See what else? All right. So yeah, there'll also be a midterm exam The way this is placed is it is after reading week because we get a fall reading week So it's after reading week because that makes sense And don't worry that reading week does not eat into your lab time So calendar wise you'll have three weeks for one of the labs But again, they're supposed to be every two weeks. So you can use reading week to study for the exam That's kind of the idea for it or you know Not be engineers and try and have a life on reading week. That would be good So hopefully one of those is and then we'll have a final lots of fun All right academic honesty, so you can study together discuss concepts and discord Post unrelated code to labs and discord or at least general code is fine Most of this course is a lot of reading code being able to explain what the hell it's doing because some of it will be very surprising to you Um Yeah, as long as it's not directly your lab code go ahead post it. That's okay. Do not post your lab code Do not post your lab code on github blah blah blah. Don't do any of that Technically against academic honesty. You get in a lot of crap for it. I have to do a lot of paperwork It's the whole thing. You don't want to do that. Trust me Your employers also don't want to see your course code on github They want to see you doing projects and trust me at the end of this course You have lots of projects you can do you can take something you want to do and just Apply the concepts of this course and make it better Lots of fun things you can do with this course and again Cheating not going to be tolerated. It's only going to hurt you because even if you Write some code. It doesn't matter most of this course is reading code and being able to explain what the hell is going on So just writing code is not that big of a deal Uh, if you just kind of pound it together and it kind of works and you can't explain it Well, that's not going to help you for exams and not going to help you For anything else, right? You don't want to be the person that needs to read stack overflow over and over again You want to be the person that provides the explanation for stack overflow, right? so The recommended books just complement the lectures you don't need them But if you need an additional resource you can have them. So it's offering system three easy pieces It's free. It's you can download the pdfs. I think it also has a web version So you can go ahead and use that just to complement the lectures It this course doesn't follow that exactly but it follows more or less along the same lines and just kind of Ignore some of the more boring stuff and tries to get explain some of the things in more detail This course will be in c. So if you don't know c, uh, you should know c. You should definitely know c We will Say what you should know Hey, there we go Here's skills you should practice again if you need number one c programming and debugging again All kernels are pretty much written in c all low level software is pretty much written in c Eventually something is going to pass through c at one point or another Guess what javascript will use c Pretty much directly. I think it uses c plus plus and some implementations But at some point you're going to end up with c. It's actually a fairly small language after you get over the hell That is pointers. It's actually pretty small and fairly concise And yeah, you should know debugging some gdb would be okay printf debugging Uh, you might run into some errors with that But we'll kind of explain what printf actually does in this course. So you'll be able to you know use it more effectively Being able to convert between hex binary and decimal is a good skill to have Because some of the things we'll be doing in this course have some low level details Especially when we get into some memory things You will definitely find that as an asset if you can just quickly convert between them or at least have some references So when we get to that part I'll show you some tricks to kind of remember some good reference points And then you can kind of figure it out from there Little endian and big endian. So hopefully you know what those term means from your computer organization class And most importantly When we get into how memory actually works it will be difficult So understanding how you think memory works is good Even though we will completely destroy all the notions you have of that Just know that memory More or less Is byte addressable like every memory address corresponds to one byte and A memory address is basically we can use it as a pointer and that's pretty much it and then past that We will make it much much much much more complicated So again, please provide any feedback. This course Is challenging. I've tried to rework it a bit that we spend more time on the challenging parts and get some more practice With it and you actually get to play with some things that are more closer to what's really going on Um, it's not really feasible to have you create your own operating system in just a semester But hopefully you'll get enough of a flavor of it and we'll be reading parts of one where you'll actually get a pretty good Idea where you could do it if you wanted to So during these things you can ask questions, right? Remember that all Programs again, even if they're written in python javascript, whatever they're going to interact with the os We're going to find ways to figuring out how they interact with the os and being able to explain that so We can ask anything about any applications you've pretty much ever written and hopefully by the end of this course You'll be able to explain it Because I can guarantee you most of you Um If you tried to explain hello world in terms of operating systems, some of you probably actually It's expected that none of you actually know how to do it and none of you actually knows how hello world works so We'll dispel that in the next lecture, but at the core of it. This is The main job of an operating system is to manage resources And that's about it At the core of it an operating system should stay out of the way and you should not know it's there as much as possible So, you know A bunch of you that have used windows before have probably sworn at windows because you realize windows Is doing something when your application doesn't work anymore. Ideally you never want to see windows. You want to just Use the internet play a game Watch a video do whatever you don't want windows to actually do anything except Not screw up and that's pretty much it to stay out of your way So all your applications are just going to request resources So that's what you wrote in APS 105. All you did was write like c programs And they were applications. They ran on top of an operating system It could have been windows mac os linux, whatever it ran on top of some operating system and It use that it request resources. So even if you try and run a program that's requesting resources You're requesting time on a cpu to run So no matter what your program if you're running it is going to request some type of resources Other resources that might request while memory That's the thing might request some disk space, you know access to files Things like that, but all of that at the end of the day is going to be managed by the operating system So the operating system is going to manage the resources and the operating system is what talks to hardware So in 243 your computer organization class you learn some Some things about how the hardware actually works Well, we'll see the other part of that that's in between the application and the hardware where the real magic happens So you know some details. So we're going to not discuss some low level details about how some specific hardware works We're going to be generally how pretty much every operating system works So there is three core operating system concepts So the first is virtualization and the idea behind that is You only have one of a particular resource But you're going to share it between multiple things by mimicking it and Trying to create an illusion that each of those things has its own complete independent version of it so for instance You are creating a virtual machine and installing an operating system And it is called a virtual machine because you're installing an operating system directly on it That operating system thinks it has complete control of that machine Even though physically I have one laptop right here and I only have one physical machine But I'm going to have two operating systems running on it I'll have macOS running on it and for the purpose of this course I'll also have linux running on it. So it's virtualizing my machine It's creating two independent copies of it that Each operating system thinks it has full control over But realistically we'll get into some caveats where you know one has a bit more control than the other But for the most part they both have complete control over their own virtual machine The next one is concurrency So your days of having a single program that starts execution at main and Starts executing and follows if branches and stuff Well, that is over. Your life is going to get infinitely more complicated. Don't worry. I see some worried faces already I'll be here to help you through it, but it will kind of be it'll start off Kind of like a disaster, but we'll get through it so A way to think of this is like hey If I gave you a deck of cards and told you to sort it That would be pretty easy, right? You could probably sort a deck of cards But if I told you hey You have 52 people now. I'll give each of them a single card and I want you to sort All 52 of you sort that deck of cards 52 times faster than that one single person One that's probably not going to work and two if one of you tried to explain how you would do that without killing each other You would probably spend a while trying to explain that One solution could just be hey pick the person at the front throw all the cards at them and then make them Sort it and then hey, that's probably sorter than or that's probably slower than one person So That's probably not a good solution But you kind of get the idea why it would probably be hard The next is persistence, which is going to be pretty much at the very end of the course Once you survive the first two-thirds of the course Uh persistence will not be a big deal and you you won't care that much But the first part of the course virtualization concurrency We'll get through it. It will be difficult, but you will actually know how things work, which will be hopeful hopefully good All right, so here's a quote. I was introduced to in my fourth year um, so i'm introducing it to you in your third year because this is Insanely true All problems in computer science can be solved with another level of indirection So instead of solving the problem You make a simpler problem to solve and then you solve that problem and then you map one to the other and you're all good so We're going to think about How programs even execute now. So our first abstraction in this course is called a process So Whenever you wrote hello world or anything like that Whenever you compiled that program you got an executable at the end of that, right? Well that executable we're going to call a program and that executable is actually nothing that special We're going to rip it apart and see how truly not special it is but it's just a file and all that file contains is a bunch of It telling the offering system what to do and then aside from that there's your instructions Which is basically like your assembly instructions from you know computer organization And any data that's required to run and that's it And then what a process is is when you actually run that program. So when I try to Run that program it creates a process that process executes that program So why did I say hey? What's the difference between a process and a program? Well the process is an execution of that program. It's at a certain point And we can run multiple processes at the same time In fact, that's like one of your main goals as an offering system is to run a whole bunch of different processes Most of you probably have more than one thing running on your computer like You probably have a web browser open that probably has lots of tabs You probably have you know Discord on you probably have whatever else you have lots of things running And they're all running as a process and your offering system gets to manage all of those So the basic requirements from it. So We already know that hey virtualization is a thing and if we're executing a program Well, we know physically on our cpu. There's only one set of registers And if multiple processes can run at the same time Well, if they could all overwrite each other's registers, you would probably be in a nothing would ever work No program would ever work. It would as soon as it's tried to switch to another one that monkey with the registers It would never work So the first thing that would be in a process would probably be something like virtual registers And all that means is each process would have its own independent set of registers So that one process can't interfere with another So if two processes have different sets of virtual registers, they're independent So other things a process would have because it is running Well, it would have a stack And it would have a heap right which are both located in memory Okay So here's my first question to you knowing that's a thing How would I be able to run two different programs at the same time in terms of memory? Like how would I manage? How should the operating system manage physical memory if I have two different processes each using memory? So for example, say I'm running hello world in one process And then in another process, it's some program that just counts up one every second so If I run both of these at the same time, they're both going to be processes Hopefully they have virtual registers. So they won't interfere with each other But they'll be using physical memory too so How would you think, you know, they interact with your operating system in terms of memory like Anyone with any guess Yep Yeah, so that seems reasonable. So if my operating system is managing physical memory Well, the hello world program, I'll just give Some memory and then if I'm the operating system and I control all physical memory It's not really a reach to think that hey, it probably knows what else is free So the next process that comes in I'll allocate it some space for its stack and they'll be In different regions of memory and they'll be, you know, they won't interfere with each other So that seems reasonable. Anyone argue with that? Okay, reasonable, great So the stacks for each process, they need to be in physical memory Operating system keeps track of physical memory just gives them different physical addresses Still no one thinking of any issues. Oh, yep Okay. Yeah, first problem is well, if they're in physical memory and if it's one than the other The other could use more space than it got allocated and then start screwing with the other person's stack That would probably not be good. Yep If you run out of memory that could also not be good. Yeah Yeah, so one argument is they might use different amounts of memory Specifically for the stack Funnily enough Each process gets the same amount of stack space no matter what it is pretty much I think the default number now is every process gets two megabytes of memory for the stack And if you use more than that, that's that stack overflow message so Yeah, running into each other's stack might be a problem But you know if we believe the operating system is all magical It may be able to monitor some processes stack and then if it tries to run into another process Could say no and then you overrain your stack and just get rid of it. All right So so far it kind of works any other issues we can think of Okay. Well, what about something you might not have thought about global variables? so Most of you probably don't know how global variables work. Well How they work is when your compiler creates that program Well, that program is just sitting there and Any memory it accesses the compiler has to know about and give an address So if you have a global variable the compiler just needs to pick an address for it For each variable, whatever you compile it, right? So say you have a global variable x the compiler might decide that hey that should be at address 1000 So anytime I access the global variable x it actually corresponds to address a thousand And that's it So that might be okay If we had like some global registry of addresses Maybe so we could say hey chrome. You're allowed to use address 4000 and up Firefox you're allowed to use address 6000 to 7000 something like that But would there be any Real bad issues with that? Yeah Yeah, well one big problem is That's just kind of a contract if you just make your own program and say hey, I'll just access, you know address 5000 or whatever then you could access each other's memory that wouldn't be good Another thing would be that hey because you have to set aside certain addresses for each Process and they have to be independent and known ahead of time Well That space is still used even if that process isn't running So if you said hey, you know chrome you get address 7000 or whatever. I said 4000 if chrome isn't running No other process could use that address because you don't know if later They might start chrome and then there'd be a conflict and you'd be fighting over the same memory address And it wouldn't be great So Here's what it would probably look like if we could say hey You know we could Have room for two processes to just use whatever memory they want for global addresses And we'll just make sure that Two of them aren't running at the same time. Maybe that's probably a bad solution. But hey, it's a solution And then you know here's where the size is so Programs have the same amount of space for the stack But they could have Any number of global variables they want or use any memory at runtime they want So that's where the whole memory usage discrepancy is going to come into play like Hello world is not going to use the same amount of memory as chrome running, you know 165 tabs open where it uses gigabytes and gigabytes of memory So That's not going to work whole lot of issues Anyone think what might be a good solution to this memory problem if Sharing physical memory is a pain in the butt. What did we learn so far? Yeah Virtual memory will this Virtualize it. So whatever we're trying to share We'll just stick the word virtual in front of it and make sure that means that each process if it has Virtual memory even if we're not quite sure what that entails If each process has virtual memory We should even knowing that one sense thing Know that each process would have its own independent view of memory and then suddenly all these problems go away so if each Process has its own independent view of memory and it can't monkey around with each other's memory addresses And all that fun stuff all the issues kind of disappear Oops And there's also a more fun problem that comes up. So Having multiple processes that are different Is way easier than having multiple processes of the same program So here's a simple little program All it does is Let's switch to this Oops All right. So here's our simple little program. Hopefully everyone can read that. Can you read in the back? Okay? Yeah, okay So here's our simple program. It creates a global integer called global initialize it to zero And then in main it will create a local variable called local We We are so clever with names and That's it. So the global variable is a global variable the local variable is allocated on the stack and then In the program we have a while loops a while one just means this loops forever So all we do is increment local increment Global and then we're just going to print out its value and then the sleep function just Puts this process since we can now say the word process puts this process to sleep Where it doesn't execute or do anything for one second and then later the offering system can go ahead and execute it again So now if we run this Well here it's running Counting up and then I can switch over to a different terminal and I'm running two of them at the same time So now this one Is counting up. It's at what five six seven and this one's already at 16 So there's two processes. They're both running the same program But clearly they're independent of each other because they're have different values And they're not mucking with each other if they didn't use virtual memory and There was just physical memory that global variable would have to be shared And if one changed it the other would change it So if this said 41 then this one would also say 41 instead of like 35 when I switched to it All right, any fun questions with that or anything you want me to do for this So there's some experiments you could do. Yep. Yep. Yeah. So both of these processes As far as each process is concerned it has access to all the memory and it has its own cpu It's running on so The operating system maintains that illusion but to that process right whenever you wrote hello world You thought there was one cpu. You thought you used all of memory. That was it So you might not have thought of it before of running two of the same program at the same time, but You can kind of explain that there's some magic happening here It's sharing the cpu by probably switching back and forth between them And doing some time something to maintain some virtual registers And they're also not interfering with each other in terms of memory. So there's probably something like virtual memory We can do this experiment, but it's not going to work as well as I thought So we can actually see their addresses So if we want to be sadistic instead of printing out their values We can print out their addresses and see Although this isn't quite going to do what I intended it to do, but let's go ahead and try it anyways So that's just a warning that we don't have to care about So if I run here, I get two addresses And I will switch over quickly run here and get two addresses So I'll just stop them and I can see that the local variable is at this Random looking address And let's see if they're the same between them. So this is six nine Six c and if I switch over to here kill it and go up It's clearly different. It is zero three fc so They could be different actually different physical addresses We're not quite sure by looking at this whether they're virtual or physical So that's probably kind of a lame test And then this global variable is zero zero four four and if we look here we get zero zero four four They look kind of the same. They're not really the same. So this one after zero zero four four There is a c a and for this one it's a db But they look suspiciously more similar so And spoiler alert if I turned off some setting I forgot to do it the beginning of the first lecture When I printed off the address of the global variables between the two processes running at the same time If I change the setting And I remember to do that they would have actually been the same address Which usually breaks people's brains because there's two things running at the same time But hey, we know that there is some type of virtualization going on here So any questions about that Don't worry if it gets slightly confusing because we're going to explain all this by the time The course is done and you will all be experts Okay, so what did we find? well We found that the address of the local was not the same between the two processes and we found that the address of global Was kind of similar but not the same between the two processes So Yeah, it definitely needs It needs virtual memory, but this doesn't really illustrate it Again because I forgot to turn off some setting But ideally the global addresses would have been the same between the two processes And they will have still behaved completely independently and nothing will have gone wrong between them So this is where we can add virtual memory to our picture of a process And suddenly everything we know about it is now virtual So a process would have its virtual registers so that it can execute independently of another process without being interfered with And two it would need some virtual memory so that no other process again can interfere with it It can pick its own memory addresses It thinks it has complete control over memory And that makes it really it makes it a lot simpler to compile If you happen to write a compiler, you know in fourth year You're going to love virtual memory because Finding addresses is just you picking a number of a hat and saying this looks good to me I'll use that and I'll let the operating system figure it out Unfortunately for you that I'll let the operating system figure it out is this course So you will be the one that figures it out So the stack and the heap are both in memory. They both be in virtual memory again I'll harp on this again that each process would have its own independent view of memory So that you can't monkey with another process And I'll also show some example code from this class So all of the code will be in the materials repository located there It's a clickable link if you download the pdf If you have signed into GitLab and I've created a repository for you which will give you an email then you have access to this If not, I will have to give you access to it Whenever you sign into the discord So so this is the instruction if you want to compile the code So you go into the directory for the lecture of the code you want to see You type meson setup build and it will create Build directory for you which sets up all the compilers and does all that stuff for you So you might have used make or something like that before This is a more readable version of make that is not a complete pain in the butt to write if you ever have to write your own build files. This is a lot simpler And then to compile you just Do meson compile dash capital C And then give it the build directory and it will run the compiler for you And link everything together if it's more complicated than that and do all that fun stuff And then there'll be a bunch of executables in a build directory So here for the first lecture, there's like a count example and then read four bytes and At the end you'll get a executable called read four bytes So let's dispel some magic and kind of look at this. So this is Okay, if this code is a bit scary right now I will run through it and see how much of it you're familiar with I expect you're not going to be familiar with A bunch of it So Thankfully it starts off with main if you don't know that starts off with main you are in deep trouble Yikes That's the part you should know and then pass that we're going to see the The arguments passed in it checks the length of the arguments. That's what argc is It's looking for exactly two arguments if there's not exactly two arguments It's going to just exit. So everyone familiar with that up to this point Hopefully that doesn't look Yeah, so At main we're dealing with these two arguments here This integer is the number of arguments and this is just an array of strings And argc says how many strings are in this array? I see a very confused So it's an array of character pointers Yeah, so character points and strings if you're in the c world, they mean the same thing So I'll use the word string and character and character pointer fairly interchangeably So this is an array of c strings. I'll call them c strings then So this is an array of c strings and this is how many elements are in the array So I checked for two always Which we will learn is just kind of a convention The first argument is the name of the program like the zero with argument. It's supposed to always exist That is not necessarily true because that was the result of that That not necessarily being true was the Caused a very large security vulnerability, which we'll see at the end of the course, but hey fun fact And then here We're expecting the argument to just be a c string. That is a file name And all this code will do is open a file and then read the first four bytes from that file I expect none of you have used open before but pop your hand if you used f open before We got a few Okay, we have a few how many of you have written programs that interacted with files at all Okay of you that have not used f open. What did you use? Yeah C plus plus so f streams and stuff like that okay, so Under the hood So as long as you're running on macOS or linux, it's actually doing this whether it's c plus plus c javascript whatever if you're interacting with files This is what eventually it will boil down to so If you used f open It kind of looks the same to this you give it, you know an argument You give it a file name and you get a file pointer back But that is just an abstraction from the c library because The standard c library needs to work for windows mac linux everything. It's meant to work for everything which is And to do that it abstracted a file to a file pointer And then it changes the actual implementation of the file pointer when it points to base off whatever operating system you're using So specifically for this course will be explaining things in terms of linux But mac is most things apply to a mac as well And windows is whole whole another can of worms, which is awful So eventually once you get into software development, you will hate windows. It took me until my fourth year again To hate windows and stop developing for it because it's a mess and you can't explain anything, but Hey, we're going to use linux So Open will open a file It just gives a file name and then this weird Number this magic number Just says I want to open the file to read from the file. That's all it gets And if you used f open it would return a file pointer Which is supposed to be a handle to that file On linux or unixes You don't get a pointer you get a number back So you get something called a file descriptor which we'll explain in the next lecture a bit more But basically you just get a number that represents that open file And at the end of this course you'll be able to exactly know what that number should be But for now just a number We're going to have some function that checks if that number is valid So you don't have to worry about this Most of it will be like reading api documentation to see What valid return values are but for this I just have a helper function to check for errors and then here So we create an array of characters of size four You should know all about arrays if you don't know about arrays you will have a very bad time Also, you should probably brush up on linked lists if you don't know about linked lists They're used all over the place in offering systems as well So this declares a four character array called buffer and That's it. So it's four bytes long and It would be at a memory address and that would be valid for four bytes If you go over that four byte limit you're you know Playing within valid memory and bad things are going to happen And it's not going to be good so on macOS or linux to get information out of a file there is a function called read You tell it what file descriptor you want to read from which represents that open file So it needs to be the same of whatever we got from open And then you give it an address where you want to start writing bytes to And then this argument is how many bytes you want the offering system to write to that array So in this case, I use size of buffer which should be four there. It's four bytes long So I tell the offering system you can start at the beginning address and you can write up to four bytes So it's going to return the number of bytes. It actually Wrote to that buffer. So that's how many bytes you can successfully read from it that are valid So I will check for an error. If not, I'll say the read failed And in here I have a for loop again, you should definitely know how for loop works it will Iterate over every index that was read So zero all the way up to bytes read which is hopefully it read four bytes So zero one two three each time through the loop It extracts a character from that buffer. So first time through the loop It's zero then one then two then three And then it's going to print it out. So this you might not know So strings are all ASCII encoded. So what this Does is take advantage of the magic numbers in ASCII So if that byte is any number between 32 and less than 128 It means it can actually print it as a character that you can see and understand Like a b c d whatever Or whatever and if it can't print the character What it's going to do is just print the value of the byte. So in hex So we can play around with this and dispel some magic because The ultimate magic is This is hello world So this is a fully functioning program that you can execute as a process And what this process will do is print hello world And it is only 168 bytes and it works Which is kind of amazing but kind of Silly how simple it could actually be because that's actually not that many bytes I for Fun you can go compile hello world and see and see how big that file is And compare it to this and tell me if you could ever explain how hello world actually works I'm guessing the answer is no and we'll explain this in the next lecture but First since we have I guess a minute we can Use that read for bytes for a bit of fun And I promise this is not a coincidence so we can look at Read for bytes So we can actually look at that file and I will do this quickly Oh my So the first four bytes of that file are 7f which is some gibberish and then the letters Elf which I swear it does not involve me at all. It just happens to be coincidence and This is the magic of operating systems all that means is if the first four bytes of that Of a file is that it means it's an executable And your operating system would try and execute it so you can run this on any program your hello world It's going to look the same And that's fun. All right, so that's it See you next lecture