Home > Articles

How Multitasking Works at the Hardware Level

  • Print
  • + Share This
For applications to run as efficiently as possible, every programmer who writes multitasking code — and that means just about everyone — needs to understand how the underlying hardware manages the process. In this book chapter, you'll discover how the Pentium 4 does the trick.
This chapter is from the book

The Previous Chapter

As background material, this chapter provided a basic description of the single-task OS and application environment.

This Chapter

As background material, this chapter provides a very basic introduction to the multitask OS environment.

The Next Chapter

As background material, this chapter provides a very basic introduction to the problems that a multitask OS must be prepared to deal with.


It is incorrect to say that a multitasking OS runs multiple programs (i.e., tasks) simultaneously. In reality, it loads a task into memory, permits it to run for a while and then suspends it. It suspends the program by creating a snapshot, or image, of all or many of the processor's registers in memory. In the IA32 architecture, the image is stored in a special data structure in memory referred to as a Task State Segment (TSS) and is accomplished by performing an automatic series of memory write transactions. In other words, the exact state of the processor at the point of suspension is saved in memory.

Having effectively saved a snapshot that indicates the point of suspension and the processor's complete state at the time, the processor then initiates another task by loading it into memory and jumping to its entry point. Based on some OS-specific criteria, the OS at some point makes the decision to suspend this task as well. As before, the state of the processor is saved in memory (in this task's TSS) as a snapshot of the task's state at its point of suspension.

At some point, the OS makes the decision to resume a previously-suspended task. This is accomplished by reloading the processor's registers from the previously-saved register image (i.e., its TSS) by performing a series of memory read transactions. The processor then uses the address pointer stored in the CS:EIP register pair to fetch the next instruction, thereby resuming program execution at the point where it had been suspended earlier.

The criteria that an OS uses in making the decision to suspend a program is specific to that OS. It may simply use timeslicing—each program is permitted to execute for a fixed amount of time (e.g., 10ms). At the end of that period of time, the currently executing task is suspended and the next task in the queue is started or resumed. The OS may assign priority levels to programs, thereby permitting a higher priority program to "preempt" a lower priority program that may currently be running. This is referred to as preemptive multitasking. The OS would also choose to suspend the currently executing program if the program needs something that is not immediately available (e.g., when it attempts an access to a page of information that is currently not in memory, but resides on a mass storage device).

  • + Share This
  • 🔖 Save To Your Account