Wednesday, April 11, 2012

Multitasking: Paging and Swapping

Multitasking: Paging and Swapping

Apr 11, 2012
In modern operating systems there are lessons learned from the concept of multitasking that can also be applied to human beings. These lessons have to do with how computer systems deal with limited memory to allow applications to exceed the amount of memory available, as well as how many applications can all run on the same system. Since both computers and humans have finite memory, I thought it was a topic worth exploring.
A brief discussion on computer architecture using simple math is necessary to understand the basic fundamentals. Disk drives are cheap and slow, whereas memory is fast but expensive.   Memory is sometimes confused with disk drives because both use capacity terms such as gigabytes. The big difference is access speed – speed in both the time it takes to go access memory, as well as the speed at which you can then transfer that data. We will concentrate on access speed because it is the most important of the two variables. It takes 200 nanoseconds (billionths of a second) to access memory, whereas it takes on average 9 milliseconds (thousandths of a second) for a disk drive to move or seek to a given spot on a hard disk.
Nanoseconds and milliseconds both sound fast. But, it is important to understand the magnitude of the differences, so let’s compare the access times of memory and disk. A disk drive accessing information at 9 milliseconds can also be stated as taking 9,000,000 nanoseconds. This means accessing information from disk is 45,000 times longer than from memory. Let’s put it in layman’s terms. We will round up the access time of memory to keep the math easy. Let’s say for comparison purposes it takes 1 second to go to memory. Keeping that ratio, a request seeking to a spot on disk drive would then take 12.5 hours. Now here is the kicker – your operating system is doing this THOUSANDS and THOUSANDS of times per second. This is why your best bang for the buck when buying any computer is to put your money into lots of memory. 
At the beginning of the home computer revolution, it used to be that the size of your program you were running could never exceed the available memory in your computer after you factor out the operating system and any start-up programs. Computer architects realized, from day one, that the speed and amount of memory is an absolutely critical component in computer design. The concept of virtual memory was applied from other operating systems to the home computer market back in the 1980s. Virtual memory is the memory management framework that treats RAM and disk drive storage in a similar fashion. The bottom line on virtual memory is it allows the amount of memory that all of your applications and operating system needs to exceed the amount of physical RAM in a computer. How does this work?
When you power up your Mac or PC, the first step is loading the operating system, all the necessary start-up programs, and finally the login screen. All of these programs move from slow disk drives to fast memory. When you start up your applications, the same process happens – slow disk drive to fast memory. If you keep clicking on software applications that you can run, eventually your PC or Mac slows down.
Is the number of applications limited by the amount of physical memory you have? No, virtual memory allows the amount of memory needed with your applications to exceed the physical memory. The operating system will allow you to keep starting up applications. We have all seen this before when a system has become just dirt slow. When a system has too many applications running, it slows down because it is “thrashing,” or is spending a good deal of its time paging in applications to run in physical memory and then paging out applications from memory to an area of disk called swap space so new applications can be paged in to run. A page is simply a defined size of a program or data.
Think of dividing applications into pages and your data into pages. These pages are read in before you need to run them or use them on your computer.  What does an operating system do when it realizes that it is spending too much time paging applications in and out? The operating system decides enough is enough and instead of paging in/out nonstop, it gives each application more time to run and to actually accomplish something. It does this by swapping out entire processes versus dealing with applications and data on a page-by-page basis.
Let’s look at another layman’s example of paging in and paging out of applications. Imagine memory is your dishwasher, your sink is your disk drive and applications are your dishes. If you have a huge sink, a small dishwasher and lots of dishes, you will spend every minute of every day loading, running and unloading the dishwasher. If you have a huge dishwasher, small sink and just a medium amount dishes, you will only need to run the dishwasher every other day. This demonstrates the importance balance in any type of system.
A more interesting question to ponder at this point is how does your operating system deal with the problem of having a memory shortage and how does this relate to humans?
When you are at your job and have countless tasks you are working on, if you are going from one task, to another task, to a different task, you are exactly like a computer’s operating system which is thrashing or spending most of its time paging in/out. Many studies have been done demonstrating that when someone is interrupted when doing a complicated task, it takes 8 minutes to reload all of the thoughts to get back to the same point you were at prior to the interruption.
While having lunch with my good longtime friend Brian Raymor in Seattle, the topic of human multitasking came up. Brian was kind enough to send me the Multitasking Stanford Study done  and conducted by Stanford researchers Eyal Ophir, Clifford Nass and Anthony Wagner. The article was written by Adam Gorlick and Jack Hubbard.
It is a great study. The link above also has a video with it. This is a must read article in my opinion. There is a great statement made in the video, "multitaskers are lousy at multitasking."
Here are a couple of points from the article:
"High-tech jugglers are everywhere – keeping up several e-mail and instant message conversations at once, text messaging while watching television and jumping from one web site to another while plowing through homework assignments. But after putting about 100 students through a series of three tests, the researchers realized those heavy media multitaskers are paying a big mental price.”
"They're suckers for irrelevancy," said communication Professor Clifford Nass, one of the researchers whose findings are published in the Aug. 24 edition of the Proceedings of the National Academy of Sciences. "Everything distracts them."
The recommendation to reboot or start killing off applications are the two most obvious suggestions to make your system responsive again. How do humans stop paging, reboot or kill off applications? Bottom line is your memory and your computer’s memory are both very precious.  You’re not a computer, but you have a lot in common with computers – balancing the number of tasks, setting priorities and not getting caught up where you are doing nothing but paging in/out tasks is critical. Even an operating system understands the difference between being busy and being productive and so should you.