Context Switch Question Answer

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

Q. Context switch adds an overhead. Why? A. 1. In a multitasking computer environment, overhead is any time not spent executing tasks.

It is overhead because it is always there, even if nothing productive is going on. Context switching is a part of the overhead but not the only part. n idle computer !no active tasks" incurs 1##$ overhead %ust waiting for something to do. When one or more tasks are executing it may appear they execute simultaneously, hence multitasking, but in reality only one task really has use of the central processing unit !the C&'" at any given time. (his is changing with the introduction of multicore processors, containing two or more C&'s that can excecute multiple tasks !or threads" concurrently. )ut tasks executing on each core must still compete for C&' time. nd there lies the overhead* the time it takes to idle a currently executing task and prepare the C&' to execute the next task is the context switching time. Context switching time can vary from a few microseconds for very high performance machines to many milliseconds. It is the time needed to save the current state of the C&' in a non+volatile location !usually , -", retrieve the state of a suspended task !also usually from , - but sometimes from disk", replace the current state, and hand over execution in the C&' to the next task. (he operating system handles all these chores !using C&' time of course" and it is totally non+productive overhead from the point of view of executing tasks. .f course, if it didn/t exist, only one task would own the C&' forever, and that can make for very messy programming. 0ometimes this is done with embedded processors which have a single task to perform, but most real world processes re1uire multiple tasks that can be blocked and unblocked at random intervals for various reasons. 2or example, it doesn/t make sense to idle the C&' waiting for a disk head to position itself to read or write data. 0o that disk read or write task will be blocked until the hardware notifies the C&' it is ready. In the meantime another unblocked task will be allowed to run. gain, the operating system handles the context switching among the tasks after deciding which tasks are allowed to run, in what order, and for how long. 3. )ecause it takes time away from processing the task!s" at hand. 2or example, if there is only one task running, the processor can give it 1##$ of its processing time. (here is no overhead of context switching. If there are two tasks to be run simultaneously by giving them alternate e1ual time slices, then in theory each task would receive 4#$ of the processing time. )ut to switch tasks, the processor has to save the current state of the running task, then recall the state in which it left the other task, and then resume running the other task. (his is called context switching and takes some of the processor time. 0o each task receives less than 4#$ of the processor/s time. If each task receives 54$ of the processor/s time, then the remaining

1#$ is the overhead consumed by context switching. If your real 1uestion is why is it called overhead, instead of 6time wasted by managers6, that/s because the managers have more power than the grunts.

You might also like