(Back to index)


Confusion about pre-emptive multitasking

For some reason many people have a misconception about how multitasking works in pre-emptive multitasking operating systems. The typical misconception is: "If a program takes 100% of the CPU, then nothing else gets any CPU - other applications, the OS, etc."

The misconception is that if a program takes 100% of CPU time, there's no CPU time left for anything else. Most importantly, there's no CPU time left for the operating system to perform task switching so that other programs will get their share of the CPU. Some people go so far as being worried if a program is taking 100% CPU time because then they fear that the system will get completely halted for as long as that program is running. They seem to think that a program should take at most something like 99.5% of CPU time so that the OS can get "in between" that 0.5% of CPU time in order for task switching to work.

That's not how pre-emptive multitasking with modern CPUs works. This is a complete misconception. (It's not even how cooperative multitasking worked in older operating systems, such as Windows3 of MacOS 9, but that's another story.)

At no point does the OS lose control of the system or is hindered by the CPU-hog program in any way whatsoever. (But don't confuse this with programs which allocate enormous amounts of RAM. That's a completely different issue.)

CPUs support a thing called a clock interrupt. This means that at certain intervals (the interval can be set up by the OS) a clock circuit sends a signal to the CPU, which causes the CPU to stop whatever it was doing and jump to a predefined location (which also can be set up by the OS). This predefined location is usually the task manager of the OS.

The task manager has a list of processes, categorized by their priority, whether they are idle or not (there exist OS system calls which a process can call to make itself idle) and other details.

If only one process is non-idle, the task manager simply resumes the execution of that process. The effective result of this is that that process gets 100% of CPU time (well, more precisely something like 99.9999% because the task manager takes that 0.0001% to check the list of processes).

It's impossible for a process to block a pre-emptive OS completely. When the clock signal interrupts the CPU, the CPU just stops executing that process, period. There's no way around it. When that happens, the OS is free to do whatever it wants. If another process has just started, the OS can share CPU time with it, no problem. If they have equal priorities and both are CPU-intensive, they will get 50% each.

So you don't have to worry in any way if you see a program using 100% of CPU. That's not a problem for the OS or for any other program. The OS is still in full control (thanks to the clock interrupt of the CPU).

(As stated earlier, programs which allocate humongous amounts of memory can be a real problem, but that's not related to CPU time in any way, and the issue is completely different.)


(Back to index)