![]() |
Windows Architecture Series: Part 5"Be careful what you shoot at. Most things in here don't react too well to bullets." - Sean Connery as Capt. Ramius in The Hunt for Red October (That was to get you in the mood to talk about submarines.) Submarines? Yes, submarines. I don't know if any of you all have served in the Navy or know someone who has, but it's probably apparent that
Practically, this means that there could be 200 sailors and only 128 bunks. Oops, not enough bunks for everyone! Fortunately, maybe only 100 sailors are sleeping at a time. That's doable, there's enough bunks then to accommodate the sleeping sailors - if they don't mind that someone else will be using that bunk when they are on watch ("...but someone is sleeping in my bed and they're still there!!!"). (BTW, officers and galley workers never have to share bunks - they get their own.) This is called "hot bunking". (It also can happen on other vessels during wartime, incidentally, but don't get me started on a history lesson or I could end up telling you all why it is that folks dress up to go to work... :-) So, how this works is: Sailor #1 gets off watch and wants to go to sleep. Sailor #1 wakes up Sailor #2 who is sleeping in the bunk. Sailor #2 gets up to go on watch and Sailor #1 gets into the [warm] bunk vacated by Sailor #2. Later, this is repeated with Sailor #2 returning to sleep in the bunk and Sailor #1 returning to watch. There are not enough bunks on the submarine for everyone to sleep, but yet everyone does manage to get sleep. Maybe we should call it "virtual bunking"? [A quick note: In order to complete the submarine analogy, you have to think of sailor's sleeping as an active task; don't think of it as sleeping - it's "using the bunk".] I'm sure it won't surprise you to hear that Windows does something similar to the bunking thing, only it's called "paging" or sometimes "swapping". The typical Windows system has maybe 128 MB of RAM these days. Windows itself will consume a decent chunk of that. Open some Office programs and maybe some graphics editing software (with some 1152 x 865 images), and, of course, Visual Basic, and that about eats it all up. Microsoft, evidently realizing it would continue to produce memory-eating software, incorporated what is termed "demand paging" into 32-bit memory architecture. OK, where does this "page" word come in? In the Mythological 4GB mini-lecture, I briefly mentioned paging in conjunction with Virtual Memory. And then went on to talk about nice acreages outside of town next to a wildlife refuge. Remember the tiny little apartments you and your cousin really lived in? (and the cows and the beets and the peppers, etc.) In the Windows world, those little apartments are called "pages" and they are 4k big (or small, however you want to look at that...) Much like clusters on the hard drive, if a resource doesn't take up the full 4k, well, that's too bad, because the full 4k gets allocated anyhow. That's about you need to know right now about pages. Oh, and 4k page=Sailor. Back to our analogy. Let's say you have 200 MB of programs that need to be loaded up and running at the same time and only 128 MB of RAM. They obviously can't all be making use of limited resources at once. They also all can't be processing at once because most machines only have one processor (that's another lecture - the one on preemptive multitasking and symmetric multiprocessing). Instead of shutting down idle programs, here's how Windows works: Program #1 needs to use the processor. In order to do this its resources, threads, data, etc. need to be loaded into physical RAM/the bunk. Program #2 just finished processing. Program #2 is kicked off into the Page/Swap file on disk and Program #1 takes its place in physical memory/the bunk. When it's Program #2's turn to process again, Program #1, et al, is kicked into the Page/Swap file and Program #2 is back in physical memory/the bunk. Neither Program #1 nor Program #2 has to be shut down/leave the submarine. They manage to get their work/using the bunk done. Strangely enough, there are officer-level programs. All the components that take care of memory, for instance, like the Virtual Memory Manager and the Scheduler, never have to leave physical memory/the bunk. (Harking back to our 4 acre analogy, these particular programs live in the northernmost acre of the Wildlife Refuge.) This moving of programs between physical memory and the hard drive is what most folks think of as being the only kind of virtual memory. I'm not sure why; after all, that's only the name of the dialog where you set up how much of your hard drive you want to use as idle space for running programs...
[I have 320 MB of RAM - that's why I have so much paging allocated. I like to have a lot of photos open in Photoshop at a time...] Going back to our submarine, what if there were 30 officers, 170 sailors and only 50 bunks? Well, knock off 30 bunks for the officers and that leaves 20 bunks to be distributed between 170 sailors. If 85 of those have to sleep at a time, we have a problem. Perhaps they could spend one hour in the bunk and then turn the bunk over to the next sailor and go on watch and then an hour later rinse and repeat. Hmmm. Doesn't sound like their sleep would be too efficient with all that moving back and forth. And a mistake is bound to happen with someone ending up on watch when he/she is supposed to be sleeping. If you have so little physical memory that this scenario is being played out in your machine, then you probably notice your hard drive making an awful noise called "thrashing". And if you've ever clicked the little unfold button on the "This application has performed an illegal operation and will be shut down" dialog and have seen the "invalid page fault" message, you've seen a process (or its minions) be on the hard drive when it was supposed to be in physical memory. (Lest you think "invalid page fault" is redundant, page faults happen all the time and there's nothing inherently bad about them. Whenever you load up a program it has to be read from the hard drive initially and this is a page fault. It only becomes invalid when the 4k page is not where it is supposed to be.) The only solutions to thrashing are:
So, that's the other part of Virtual Memory. We'll tie all this Virtual Memory stuff together sometime in the near future. Alrak's Course Resources ©2002-2007 Karla Carter. All rights reserved. This material (including, but not limited to, Mini-lectures and Challenge Labs) may not be reproduced, displayed, modified or distributed without the express prior written permission of the copyright holder. For permission, contact Karla. |