Home

contextswitch

Contextswitch, commonly referred to as a context switch, is the process by which a central processing unit stops executing one task and begins executing another. In multitasking operating systems, the CPU divides its time among multiple processes or threads, giving the illusion of parallelism even on single-processor systems.

During a context switch, the operating system saves the state of the currently running task so it

Context switches can be triggered by interrupts, system calls, I/O completions, or timer events, and can be

Performance considerations are central to context switching. Each switch incurs overhead from saving and restoring state,

can
resume
later.
This
saved
state
typically
includes
the
program
counter,
stack
pointer,
and
CPU
registers,
as
well
as
memory-management
information
and
other
per-task
data.
The
saved
information
is
stored
in
a
data
structure
such
as
a
process
control
block
(PCB)
or
a
thread
control
block
(TCB).
The
scheduler
then
loads
the
state
of
the
next
task
and
transfers
control
to
its
resume
point.
either
preemptive
or
cooperative.
In
preemptive
systems,
the
kernel
may
forcibly
switch
tasks
based
on
scheduling
policies,
while
cooperative
systems
require
tasks
to
yield
control
voluntarily.
potentially
causing
cache
misses
and
TLB
reloads.
In
multi-core
systems,
coordination
between
cores
may
be
required,
including
actions
like
TLB
shootdowns
to
invalidate
stale
translations.
Operating
system
designers
optimize
the
path
of
saving/restoring
state
and
minimize
switching
frequency
through
scheduling
strategies,
hardware
support,
and
techniques
such
as
lazy
state
saving
for
certain
components.