 The operating system is software just like any other program that you have on your computer. And when the computer is turned off, that software is stored on the disk. When you first turn the computer on, the computer needs to fetch decode and execute instructions. But it can't run the operating system until the operating system is in memory, along with all the other programs. That's why it's necessary for computers to boot up. When you first turn the computer on, it has to boot the operating system, which basically means there's a small program in read-only memory. This is a persistent, typically a piece of flash on the motherboard that stores what's called the bootloader. Now, the bootloader is a program that all it does is loads the operating system off the disk into RAM. And as soon as that program is done, it jumps into the operating system and the operating system has control over the computer from then on. So if you've ever wondered why a computer needs to boot up, this is the reason. We learned last week with the stored program concept that programs need to be stored in memory. Of course, the operating system is on disk. You have to get it into memory before it can run. So this rectangle represents the memory that a computer program can see. And typically, you're either going to have 32 or 64 bits. I'm just going to use 32 today because it's a little bit simpler to do the math. And with 32 bits of addresses, we've learned that you can go from address to zero. And I'll go ahead and write eight zeros because remember in hexadecimal, each one of these is four bits. So if I have eight hexadecimal digits, that's 32 bits, which is my address size on this machine. So the very highest address is going to be all f's. Now, 32 bits, if you were to count up all these addresses from zero to all f, you would have about four billion addresses. Or the way we just refer to that is four gigabytes. And you may have noticed when you go shopping for computers, if you have more than four gigabytes of memory, you need to buy a 64-bit machine. I mean, the reason is if you want to address higher than four billion addresses, you need more bits to represent that memory location. Now, the way memory is typically organized is it's divided in half. And this lower section here is reserved for the operating system. So the operating system with all the memory manager, the scheduler, the dispatcher, the file manager, all that code needs to be in memory. And so we're going to just save half of memory for that. The upper half of memory is divided up for each process to use. And it's organized into different segments. So we have one here called text. This is where the code is actually stored. And then on top of the text, we've got some data. We've got a heap. And we've got a stack. Now, you don't need to worry too much about this terminology. What I mainly want you to see is that memory is organized based on different types of data. And last week, I asked you the question on the quiz, how does the CPU tell the difference between memory use for code and memory use for data? And the answer was it really can't tell the difference. Well, one way that people try to avoid making mistakes is by organizing their memory in these segments. And so that way, if the computer ever were to jump up here into your heap or stack, it would know that, wait a minute, I'm not supposed to be fetching code from here. I should probably alert the operating system that something went wrong. So this is what memory looks like to an individual program. But the nice thing is, it's as if that program has all the memory to itself. And the reason is, is because of virtual memory. I can trick a computer program into thinking that all of this memory belongs to itself, but then use a translation process to actually put this into physical memory. Let me show you how that looks like. Now one of the neat things about virtual memory is it allows us to extend the size of our actual physical memory. So for example, on this computer, I have two gigabytes of physical memory, but I can go ahead and let a program think that it actually has four gigabytes of available memory. This is called virtual memory because it doesn't actually exist physically on the hardware. Now remember, like I showed you previously, the OS is sitting here in the lower half, and then the program is sitting up here in the upper half with all of its data. Now, if I do a load or a store hardware instruction in this memory, let's say I go to access this address right here. The operating system and the hardware work together to translate that process into a physical load or store. So you can imagine, and I'm not going to go over the details in this class because it's like two or three weeks of taking an operating systems course. But the basic idea is that address gets converted into a physical address lookup. So even though I'm using this address in my code, it's getting translated by the hardware into a physical location. Now, these locations don't have to have any relation to each other, right? So I might access this memory right here, and that's going to come out the other end and access a page, maybe in a completely different location relative to the other address. And of course too, I'm loading and storing instructions from the OS, and those are coming into physical memory as well. Now let me show you the beauty of this setup. I might have another program running right here. Let me just draw a box for process B now. And the operating system can notify the process that it also has four gigabytes of storage space. And of course, the operating system is the same in the first process as it is in the second process. So all of these memory addresses that I access virtually can correspond to the same physical location. That way I don't have to make a copy of the operating system into every single process. They can both map to the same physical location of memory. The other thing that's nice is all of this memory I'm not using in the process. So most processes only use a few megabytes or even a few hundred megabytes of memory. All of this wasted space that I'm not using doesn't have to be allocated on physical memory. So there is a way not only to extend the limits of memory, but also a way to reuse memory and to save space. The other thing that this scheme gives us is a technique called isolation. So isolation is really important in terms of security and in terms of stability of operating systems. I can't allow process A to access the memory of process B because that would allow you to write programs that crash other programs on purpose. And we don't want to do that in terms of both security and stability. So because the hardware and the operating system are doing this translation, it's impossible for one program to access the memory of another program. All they can do is access their own virtual address and that is controlled by the hardware and the operating system to actually go to a physical address on the hardware. There's one more concept that you should know about in terms of virtual memory and that is called paging. The reason why it's called paging is because the memory is actually organized into pages. Each one of these tiny little rectangles I've drawn is a page of memory. You can think of memory as a big book and we're just reading a page at a time. Well, the nice thing about paging is if you don't have enough memory for all of your program's needs, you could also have certain parts of memory correspond to a file on disk. So your hard disk becomes an extension of your physical RAM. Now you may have noticed on a computer, especially an old computer, if you have a lot of programs running at the same time, the hard disk starts to go... It makes this noise because it's constantly basically swapping pages in and out from memory to disk. So a page that I haven't used for a long time will get put back on disk. A page that I access in my program, if it's not in memory, it needs to come off of disk into memory. And so by swapping pages in and out of memory into disk, I'm able to extend the limits of my memory and also make it so that I do have up to 4 gigabytes of RAM, even if there's not 4 gigabytes physically installed on the machine. So these are all benefits of virtual memory and it's all work that the memory manager does as part of the operating system.