why is context switching bad for computer performance?


context switching, which occurs when a process is paused by the cpu and another process starts running can affect performance of a computer systems for a couple of reasons. The act of switching the context is not in itself very time consuming. The real performance hit comes from the fact that different processes have different virtual memory address spaces so all the caches in the system need to be invalidated and flushed. Since there is such a huge difference in how fast a cpu can run compared to how fast instructions and data can be retrieved from main memory, these things are cached close to the cpu in onboard caches. Even a pointer to a mapping of virtual addresses to physical addresses are cached so that memory access is faster. When a new process gets loaded by the cpu with a different address space and mapping of virtual to physical addresses, all of the data in the caches is invalid and needs to be flushed. This leaves the cpu with empty caches that have to be rebuilt from scratch for the new process, meaning there will be a big slowdown in retrieving instructions and data since these will now be coming from main memory, which is much slower than the cpu's cache.