2

On Linux context switching can occur because an application makes a system call, or because the scheduler swaps processes out of the CPU for another process.

On Windows what are the possible causes of a context switch? Specifically, from the literature I have read, high thread count is the only cause of high context switching I have run into so far. Are there others?

Finally, is the cost of swapping out two threads of the same process different than the cost of swaping out two different processes?

4 Answers 4

5

The ability to do context switching is an essential component for any multitasking OS. The same type of things that cause context switching in Linux would cause context switching in Windows as well.

All a high thread count means is that the CPU will be forced to switch contexts more often to fulfill the processing requirements of the scheduler.

1
  • Are you sure about this response? Were have you read that windows context switch follow the same process of the linux context switch? Dec 8, 2019 at 12:42
3

Finally, is the cost of swapping out two threads of the same process different than the cost of swaping out two different processes?

Yes, it is different. Two threads inside a process share the same Virtual Address space, so a context switch between htreads inside the same process does not have to invalidate the virtual-to-physical translation. A context switch between threads of different processes has to invalidate the current VA-to-PA translation mapping. This translation occurs in the processor TLB registers and traditionally a context switch incurred a TLB flush, which is costly as the the new process will start slowly building back the TLB entries by generating soft page-faults. Modern CPUs though have more clever strategies to deal with with this problem, see the Context Switch paragraph on the Wikipedia link above.

2

Context switching is the act of saving the current threads running state so that it can restore a different thread's state so that it can run on the process. It ocurs at the discretion of the kernel dispatcher.

The kernel dispatcher performs thread scheduling and context switching. The dispatcher can call thread switches for various reasons:

  • A thread's quantum has run out. A quantum is the select amount of time that a thread is allowed to run before being swapped out for a thread at the same priority.
  • Another higher priority process may need access to the processor and preempt the current one
  • The current threads priority drops through action of the program or the OS
  • The thread's processor affinity changes
1

From the Windows Internals Book, 5th Edition:

context switch The procedure of saving the volatile machine state associated with a running thread, loading another thread's volatile state, and starting the new thread's execution.

process The virtual address space and control information necessary for the execution of a set of thread objects.

thread An entity within a process that Windows schedules for execution. A thread includes the contents of a set of volatile registers representing the state of the processor; two stacks, one for the thread to use while executing in kernel mode and one for executing in user mode; a private storage area for use by subsystems, run-time libraries, and DLLs; and a unique identifier called a thread ID (also internally called a client iD).

If you want to deep dive down into this further there is a chapter available for download as a PDF Chapter 5: Processes, Threads and Jobs.

You must log in to answer this question.