Multitasking - What is it?
Processes and Threads
A processis the running version of a computer program. It is given its own private space in the system memory which it uses to store its code and data. The context of the process is the entire contents of this memory, and some more data about the system’s state. This way, when the context of a process is loaded into the memory, it can start executing from when it left off.
Processes are sometimes called “virtual machines”, because each process is given the illusion that it is the only one running on the system. Thus, many virtual machines run on the same physical machine, all managed by the operating system.
A thread is a sub-process created by a “parent” process. It runs in the same memory area as the process that creates it, and shares data with other threads created by that process. One process may create many threads to do different things at the same time. For example, while one thread of Microsoft Word brings your input onto the screen, another thread checks your spelling as you type, and yet another is saving backups of your document—just in case.
Multiprogramming To Multithreading
Multitasking, simply put, is the ability of an operating system to do many different things at the same time. It sounds simple enough, but how do you do many different things at the same time when all you have at your disposal is a processor that can only process one instruction at a time?
How to check how many threads are created by a Process
In Windows XP, hit [Ctrl] + [Shift] + [Esc] to bring up the Task Manager. Under the Performance tab, you will see “Threads” and “Processes” in the box labelled “Totals”. Now go to your Start Menu and select a program (let’s say Microsoft Excel). You will notice that while only one process has been added, the number of threads has increased by four or five.
The first answer was multiprogramming. Many programs would be loaded into memory, and one would run first. Now, if this program was waiting for a device (like a drive) to process its data, valuable CPU time would be wasted. Instead, the context of the program would be stored in a temporary location, and a different program would be allowed to use the CPU. This way, more work could get done, and faster.
The multiprogramming approach soon gave way to a ‘timeshare’ approach. The idea was that each program should ‘give back’ resources to the OS every so often, so that these resources could now be given to different programs. This was called Co-operative Multitasking. Developers would write programs that would save their context after running for a fixed time and hand control over to other programs. While this sped things along quite well, there were a couple of very serious flaws. If the programmer had made a mistake, the program might well end up hogging all the system’s resources. At best, it would end up being the only useful program; at worst, it could crash and bring the whole system down with it.
Today’s OSes use Multithreading—they prefer to switch between threads rather than whole processes. Context-switching for threads is faster because they don’t have their own memory context— they run in the context of the process that created them. Rather than leaving it up to processes to multitask among themselves, the OS kernel takes charge and assigns them ‘time-slots’, within which they can use the system’s resources. After their time has expired, the OS saves their context, loads the context of another process, and resumes execution from there. This is called Preemptive Multitasking.
There are many ways for an OS to schedule the time that threads enjoy the use of system resources. The simplest of the lot is the round robin method. Each thread is loaded turn by turn, and once the OS has reached the last thread, it returns to the first and starts all over again. Most OSes use priority-based scheduling, where higher priority threads are given bigger chunks of time. The priority of the thread is sometimes coded in by the programmer, but the operating system is the ultimate authority in this matter. It puts its own critical processes at a higher priority, and if it can give a thread the priority level it asks for, it does.
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!




January 24th, 2008 at 11:08 am
Nice sum up ! thanks
February 3rd, 2008 at 12:14 pm
Informative article. Appreciate the time you took researching it.