<s>
In	O
computing	O
,	O
a	O
context	B-Operating_System
switch	I-Operating_System
is	O
the	O
process	B-Operating_System
of	O
storing	O
the	O
state	O
of	O
a	O
process	B-Operating_System
or	O
thread	B-Operating_System
,	O
so	O
that	O
it	O
can	O
be	O
restored	O
and	O
resume	O
execution	B-General_Concept
at	O
a	O
later	O
point	O
,	O
and	O
then	O
restoring	O
a	O
different	O
,	O
previously	O
saved	O
,	O
state	O
.	O
</s>
<s>
This	O
allows	O
multiple	O
processes	O
to	O
share	O
a	O
single	O
central	B-General_Concept
processing	I-General_Concept
unit	I-General_Concept
(	O
CPU	O
)	O
,	O
and	O
is	O
an	O
essential	O
feature	O
of	O
a	O
multitasking	B-Operating_System
operating	I-Operating_System
system	I-Operating_System
.	O
</s>
<s>
In	O
a	O
traditional	O
CPU	O
,	O
each	O
process	B-Operating_System
-	O
a	O
program	O
in	O
execution	B-General_Concept
-	O
utilizes	O
the	O
various	O
CPU	B-General_Concept
registers	I-General_Concept
to	O
store	O
data	O
and	O
hold	O
the	O
current	O
state	O
of	O
the	O
running	O
process	B-Operating_System
.	O
</s>
<s>
However	O
,	O
in	O
a	O
multitasking	B-Operating_System
operating	I-Operating_System
system	I-Operating_System
,	O
the	O
operating	O
system	O
switches	O
between	O
processes	O
or	O
threads	B-Operating_System
to	O
allow	O
the	O
execution	B-General_Concept
of	O
multiple	O
processes	O
simultaneously	O
.	O
</s>
<s>
For	O
every	O
switch	O
,	O
the	O
operating	O
system	O
must	O
save	O
the	O
state	O
of	O
the	O
currently	O
running	O
process	B-Operating_System
,	O
followed	O
by	O
loading	O
the	O
next	O
process	B-Operating_System
state	O
,	O
which	O
will	O
run	O
on	O
the	O
CPU	O
.	O
</s>
<s>
This	O
sequence	O
of	O
operations	O
that	O
stores	O
the	O
state	O
of	O
the	O
running	O
process	B-Operating_System
and	O
the	O
loading	O
of	O
the	O
following	O
running	O
process	B-Operating_System
is	O
called	O
a	O
context	B-Operating_System
switch	I-Operating_System
.	O
</s>
<s>
The	O
precise	O
meaning	O
of	O
the	O
phrase	O
"	O
context	B-Operating_System
switch	I-Operating_System
"	O
varies	O
.	O
</s>
<s>
In	O
a	O
multitasking	B-Operating_System
context	O
,	O
it	O
refers	O
to	O
the	O
process	B-Operating_System
of	O
storing	O
the	O
system	O
state	O
for	O
one	O
task	O
,	O
so	O
that	O
task	O
can	O
be	O
paused	O
and	O
another	O
task	O
resumed	O
.	O
</s>
<s>
A	O
context	B-Operating_System
switch	I-Operating_System
can	O
also	O
occur	O
as	O
the	O
result	O
of	O
an	O
interrupt	B-Application
,	O
such	O
as	O
when	O
a	O
task	O
needs	O
to	O
access	O
disk	B-Device
storage	I-Device
,	O
freeing	O
up	O
CPU	O
time	O
for	O
other	O
tasks	O
.	O
</s>
<s>
Some	O
operating	O
systems	O
also	O
require	O
a	O
context	B-Operating_System
switch	I-Operating_System
to	O
move	O
between	O
user	B-Operating_System
mode	I-Operating_System
and	O
kernel	B-Operating_System
mode	O
tasks	O
.	O
</s>
<s>
The	O
process	B-Operating_System
of	O
context	B-Operating_System
switching	I-Operating_System
can	O
have	O
a	O
negative	O
impact	O
on	O
system	O
performance	O
.	O
</s>
<s>
Context	B-Operating_System
switches	I-Operating_System
are	O
usually	O
computationally	O
intensive	O
,	O
and	O
much	O
of	O
the	O
design	O
of	O
operating	O
systems	O
is	O
to	O
optimize	O
the	O
use	O
of	O
context	B-Operating_System
switches	I-Operating_System
.	O
</s>
<s>
Switching	O
from	O
one	O
process	B-Operating_System
to	O
another	O
requires	O
a	O
certain	O
amount	O
of	O
time	O
for	O
doing	O
the	O
administration	O
saving	O
and	O
loading	O
registers	O
and	O
memory	B-Architecture
maps	I-Architecture
,	O
updating	O
various	O
tables	O
and	O
lists	O
,	O
etc	O
.	O
</s>
<s>
What	O
is	O
actually	O
involved	O
in	O
a	O
context	B-Operating_System
switch	I-Operating_System
depends	O
on	O
the	O
architectures	O
,	O
operating	O
systems	O
,	O
and	O
the	O
number	O
of	O
resources	O
shared	O
(	O
threads	B-Operating_System
that	O
belong	O
to	O
the	O
same	O
process	B-Operating_System
share	O
many	O
resources	O
compared	O
to	O
unrelated	O
non-cooperating	O
processes	O
)	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
,	O
context	B-Operating_System
switching	I-Operating_System
involves	O
loading	O
the	O
corresponding	O
process	B-Operating_System
control	I-Operating_System
block	I-Operating_System
(	O
PCB	O
)	O
stored	O
in	O
the	O
PCB	O
table	O
in	O
the	O
kernel	B-Operating_System
stack	B-Application
to	O
retrieve	O
information	O
about	O
the	O
state	O
of	O
the	O
new	O
process	B-Operating_System
.	O
</s>
<s>
CPU	O
state	O
information	O
including	O
the	O
registers	O
,	O
stack	B-General_Concept
pointer	I-General_Concept
,	O
and	O
program	B-General_Concept
counter	I-General_Concept
as	O
well	O
as	O
memory	O
management	O
information	O
like	O
segmentation	B-General_Concept
tables	I-General_Concept
and	O
page	B-General_Concept
tables	I-General_Concept
(	O
unless	O
the	O
old	O
process	B-Operating_System
shares	O
the	O
memory	O
with	O
the	O
new	O
)	O
are	O
loaded	O
from	O
the	O
PCB	O
for	O
the	O
new	O
process	B-Operating_System
.	O
</s>
<s>
To	O
avoid	O
incorrect	O
address	O
translation	O
in	O
the	O
case	O
of	O
the	O
previous	O
and	O
current	O
processes	O
using	O
different	O
memory	O
,	O
the	O
translation	B-Architecture
lookaside	I-Architecture
buffer	I-Architecture
(	O
TLB	O
)	O
must	O
be	O
flushed	O
.	O
</s>
<s>
This	O
negatively	O
affects	O
performance	O
because	O
every	O
memory	O
reference	O
to	O
the	O
TLB	O
will	O
be	O
a	O
miss	O
because	O
it	O
is	O
empty	O
after	O
most	O
context	B-Operating_System
switches	I-Operating_System
.	O
</s>
<s>
Furthermore	O
,	O
analogous	O
context	B-Operating_System
switching	I-Operating_System
happens	O
between	O
user	O
threads	B-Operating_System
,	O
notably	O
green	B-Operating_System
threads	I-Operating_System
,	O
and	O
is	O
often	O
very	O
lightweight	O
,	O
saving	O
and	O
restoring	O
minimal	O
context	O
.	O
</s>
<s>
In	O
extreme	O
cases	O
,	O
such	O
as	O
switching	O
between	O
goroutines	O
in	O
Go	B-Application
,	O
a	O
context	B-Operating_System
switch	I-Operating_System
is	O
equivalent	O
to	O
a	O
coroutine	B-Architecture
yield	O
,	O
which	O
is	O
only	O
marginally	O
more	O
expensive	O
than	O
a	O
subroutine	O
call	O
.	O
</s>
<s>
There	O
are	O
three	O
potential	O
triggers	O
for	O
a	O
context	B-Operating_System
switch	I-Operating_System
:	O
</s>
<s>
Most	O
commonly	O
,	O
within	O
some	O
scheduling	O
scheme	O
,	O
one	O
process	B-Operating_System
must	O
be	O
switched	O
out	O
of	O
the	O
CPU	O
so	O
another	O
process	B-Operating_System
can	O
run	O
.	O
</s>
<s>
This	O
context	B-Operating_System
switch	I-Operating_System
can	O
be	O
triggered	O
by	O
the	O
process	B-Operating_System
making	O
itself	O
unrunnable	O
,	O
such	O
as	O
by	O
waiting	O
for	O
an	O
I/O	B-General_Concept
or	O
synchronization	O
operation	O
to	O
complete	O
.	O
</s>
<s>
On	O
a	O
pre-emptive	O
multitasking	B-Operating_System
system	O
,	O
the	O
scheduler	O
may	O
also	O
switch	O
out	O
processes	O
that	O
are	O
still	O
runnable	O
.	O
</s>
<s>
To	O
prevent	O
other	O
processes	O
from	O
being	O
starved	O
of	O
CPU	O
time	O
,	O
pre-emptive	O
schedulers	O
often	O
configure	O
a	O
timer	O
interrupt	B-Application
to	O
fire	O
when	O
a	O
process	B-Operating_System
exceeds	O
its	O
time	O
slice	B-Application
.	O
</s>
<s>
This	O
interrupt	B-Application
ensures	O
that	O
the	O
scheduler	O
will	O
gain	O
control	O
to	O
perform	O
a	O
context	B-Operating_System
switch	I-Operating_System
.	O
</s>
<s>
Modern	O
architectures	O
are	O
interrupt	B-Application
driven	O
.	O
</s>
<s>
This	O
means	O
that	O
if	O
the	O
CPU	O
requests	O
data	O
from	O
a	O
disk	B-Application
,	O
for	O
example	O
,	O
it	O
does	O
not	O
need	O
to	O
busy-wait	B-Operating_System
until	O
the	O
read	O
is	O
over	O
;	O
it	O
can	O
issue	O
the	O
request	O
(	O
to	O
the	O
I/O	B-General_Concept
device	I-General_Concept
)	O
and	O
continue	O
with	O
some	O
other	O
task	O
.	O
</s>
<s>
When	O
the	O
read	O
is	O
over	O
,	O
the	O
CPU	O
can	O
be	O
interrupted	O
(	O
by	O
a	O
hardware	O
in	O
this	O
case	O
,	O
which	O
sends	O
interrupt	B-Application
request	O
to	O
PIC	B-Architecture
)	O
and	O
presented	O
with	O
the	O
read	O
.	O
</s>
<s>
For	O
interrupts	B-Application
,	O
a	O
program	O
called	O
an	O
interrupt	B-General_Concept
handler	I-General_Concept
is	O
installed	O
,	O
and	O
it	O
is	O
the	O
interrupt	B-General_Concept
handler	I-General_Concept
that	O
handles	O
the	O
interrupt	B-Application
from	O
the	O
disk	B-Application
.	O
</s>
<s>
When	O
an	O
interrupt	B-Application
occurs	O
,	O
the	O
hardware	O
automatically	O
switches	O
a	O
part	O
of	O
the	O
context	O
(	O
at	O
least	O
enough	O
to	O
allow	O
the	O
handler	O
to	O
return	O
to	O
the	O
interrupted	O
code	O
)	O
.	O
</s>
<s>
Often	O
only	O
a	O
minimal	O
part	O
of	O
the	O
context	O
is	O
changed	O
in	O
order	O
to	O
minimize	O
the	O
amount	O
of	O
time	O
spent	O
handling	O
the	O
interrupt	B-Application
.	O
</s>
<s>
The	O
kernel	B-Operating_System
does	O
not	O
spawn	O
or	O
schedule	O
a	O
special	O
process	B-Operating_System
to	O
handle	O
interrupts	B-Application
,	O
but	O
instead	O
the	O
handler	O
executes	O
in	O
the	O
(	O
often	O
partial	O
)	O
context	O
established	O
at	O
the	O
beginning	O
of	O
interrupt	B-Application
handling	I-Application
.	O
</s>
<s>
Once	O
interrupt	B-Application
servicing	O
is	O
complete	O
,	O
the	O
context	O
in	O
effect	O
before	O
the	O
interrupt	B-Application
occurred	O
is	O
restored	O
so	O
that	O
the	O
interrupted	O
process	B-Operating_System
can	O
resume	O
execution	B-General_Concept
in	O
its	O
proper	O
state	O
.	O
</s>
<s>
When	O
the	O
system	O
transitions	O
between	O
user	B-Operating_System
mode	I-Operating_System
and	O
kernel	B-Operating_System
mode	O
,	O
a	O
context	B-Operating_System
switch	I-Operating_System
is	O
not	O
necessary	O
;	O
a	O
mode	O
transition	O
is	O
not	O
by	O
itself	O
a	O
context	B-Operating_System
switch	I-Operating_System
.	O
</s>
<s>
However	O
,	O
depending	O
on	O
the	O
operating	O
system	O
,	O
a	O
context	B-Operating_System
switch	I-Operating_System
may	O
also	O
take	O
place	O
at	O
this	O
time	O
.	O
</s>
<s>
The	O
state	O
of	O
the	O
currently	O
executing	O
process	B-Operating_System
must	O
be	O
saved	O
so	O
it	O
can	O
be	O
restored	O
when	O
rescheduled	O
for	O
execution	B-General_Concept
.	O
</s>
<s>
The	O
process	B-Operating_System
state	O
includes	O
all	O
the	O
registers	O
that	O
the	O
process	B-Operating_System
may	O
be	O
using	O
,	O
especially	O
the	O
program	B-General_Concept
counter	I-General_Concept
,	O
plus	O
any	O
other	O
operating	O
system	O
specific	O
data	O
that	O
may	O
be	O
necessary	O
.	O
</s>
<s>
This	O
is	O
usually	O
stored	O
in	O
a	O
data	O
structure	O
called	O
a	O
process	B-Operating_System
control	I-Operating_System
block	I-Operating_System
(	O
PCB	O
)	O
or	O
switchframe	B-Operating_System
.	O
</s>
<s>
The	O
PCB	O
might	O
be	O
stored	O
on	O
a	O
per-process	O
stack	O
in	O
kernel	B-Operating_System
memory	O
(	O
as	O
opposed	O
to	O
the	O
user-mode	O
call	B-General_Concept
stack	I-General_Concept
)	O
,	O
or	O
there	O
may	O
be	O
some	O
specific	O
operating	O
system-defined	O
data	O
structure	O
for	O
this	O
information	O
.	O
</s>
<s>
Since	O
the	O
operating	O
system	O
has	O
effectively	O
suspended	O
the	O
execution	B-General_Concept
of	O
one	O
process	B-Operating_System
,	O
it	O
can	O
then	O
switch	O
context	O
by	O
choosing	O
a	O
process	B-Operating_System
from	O
the	O
ready	O
queue	O
and	O
restoring	O
its	O
PCB	O
.	O
</s>
<s>
In	O
doing	O
so	O
,	O
the	O
program	B-General_Concept
counter	I-General_Concept
from	O
the	O
PCB	O
is	O
loaded	O
,	O
and	O
thus	O
execution	B-General_Concept
can	O
continue	O
in	O
the	O
chosen	O
process	B-Operating_System
.	O
</s>
<s>
Process	B-Operating_System
and	O
thread	B-Operating_System
priority	O
can	O
influence	O
which	O
process	B-Operating_System
is	O
chosen	O
from	O
the	O
ready	O
queue	O
(	O
i.e.	O
,	O
it	O
may	O
be	O
a	O
priority	B-Application
queue	I-Application
)	O
.	O
</s>
<s>
The	O
instruction	O
is	O
stored	O
in	O
the	O
instruction	B-General_Concept
register	I-General_Concept
and	O
the	O
program	B-General_Concept
counter	I-General_Concept
is	O
incremented	O
.	O
</s>
<s>
However	O
,	O
certain	O
special	O
instructions	O
require	O
system	B-Operating_System
calls	I-Operating_System
that	O
require	O
context	B-Operating_System
switch	I-Operating_System
to	O
wait/sleep	O
processes	O
.	O
</s>
<s>
A	O
system	B-Operating_System
call	I-Operating_System
handler	O
is	O
used	O
for	O
context	B-Operating_System
switch	I-Operating_System
to	O
kernel	B-Operating_System
mode	O
.	O
</s>
<s>
A	O
display(data x )	O
function	O
may	O
require	O
data	O
x	O
from	O
the	O
Disk	B-Application
and	O
a	O
device	O
driver	O
in	O
kernel	B-Operating_System
mode	O
,	O
hence	O
the	O
display( )	O
function	O
goes	O
to	O
sleep	O
and	O
waits	O
on	O
the	O
READ	O
operation	O
to	O
get	O
the	O
value	O
of	O
x	O
from	O
the	O
disk	B-Application
,	O
causing	O
the	O
program	O
to	O
wait	O
and	O
a	O
wait	O
for	O
function	O
call	O
to	O
the	O
released	O
setting	O
the	O
current	O
statement	O
to	O
go	B-Application
to	O
sleep	O
and	O
wait	O
for	O
the	O
syscall	B-Operating_System
to	O
wake	O
it	O
up	O
.	O
</s>
<s>
To	O
maintain	O
concurrency	B-Operating_System
however	O
the	O
program	O
needs	O
to	O
re-execute	O
the	O
new	O
value	O
and	O
the	O
sleeping	O
process	B-Operating_System
together	O
again	O
.	O
</s>
<s>
Context	B-Operating_System
switching	I-Operating_System
itself	O
has	O
a	O
cost	O
in	O
performance	O
,	O
due	O
to	O
running	O
the	O
task	O
scheduler	O
,	O
TLB	B-Architecture
flushes	I-Architecture
,	O
and	O
indirectly	O
due	O
to	O
sharing	O
the	O
CPU	B-General_Concept
cache	I-General_Concept
between	O
multiple	O
tasks	O
.	O
</s>
<s>
Switching	O
between	O
threads	B-Operating_System
of	O
a	O
single	O
process	B-Operating_System
can	O
be	O
faster	O
than	O
between	O
two	O
separate	O
processes	O
,	O
because	O
threads	B-Operating_System
share	O
the	O
same	O
virtual	B-Architecture
memory	I-Architecture
maps	O
,	O
so	O
a	O
TLB	B-Architecture
flush	I-Architecture
is	O
not	O
necessary	O
.	O
</s>
<s>
The	O
time	O
to	O
switch	O
between	O
two	O
separate	O
processes	O
is	O
called	O
the	O
process	B-Operating_System
switching	O
latency	O
.	O
</s>
<s>
The	O
time	O
to	O
switch	O
between	O
two	O
threads	B-Operating_System
of	O
the	O
same	O
process	B-Operating_System
is	O
called	O
the	O
thread	B-Operating_System
switching	O
latency	O
.	O
</s>
<s>
The	O
time	O
from	O
when	O
a	O
hardware	O
interrupt	B-Application
is	O
generated	O
to	O
when	O
the	O
interrupt	B-Application
is	O
serviced	O
is	O
called	O
the	O
interrupt	B-General_Concept
latency	I-General_Concept
.	O
</s>
<s>
Switching	O
between	O
two	O
processes	O
in	O
a	O
single	B-Operating_System
address	I-Operating_System
space	I-Operating_System
operating	I-Operating_System
system	I-Operating_System
can	O
be	O
faster	O
than	O
switching	O
between	O
two	O
processes	O
in	O
an	O
operating	O
system	O
with	O
private	O
per-process	O
address	O
spaces	O
.	O
</s>
<s>
Context	B-Operating_System
switching	I-Operating_System
can	O
be	O
performed	O
primarily	O
by	O
software	O
or	O
hardware	O
.	O
</s>
<s>
Some	O
processors	O
,	O
like	O
the	O
Intel	B-General_Concept
80386	I-General_Concept
and	O
its	O
successors	O
,	O
have	O
hardware	O
support	O
for	O
context	B-Operating_System
switches	I-Operating_System
,	O
by	O
making	O
use	O
of	O
a	O
special	O
data	O
segment	O
designated	O
the	O
task	B-Device
state	I-Device
segment	I-Device
(	O
TSS	O
)	O
.	O
</s>
<s>
A	O
task	O
switch	O
can	O
be	O
explicitly	O
triggered	O
with	O
a	O
CALL	O
or	O
JMP	O
instruction	O
targeted	O
at	O
a	O
TSS	O
descriptor	O
in	O
the	O
global	B-General_Concept
descriptor	I-General_Concept
table	I-General_Concept
.	O
</s>
<s>
It	O
can	O
occur	O
implicitly	O
when	O
an	O
interrupt	B-Application
or	O
exception	O
is	O
triggered	O
if	O
there	O
's	O
a	O
task	B-Device
gate	I-Device
in	O
the	O
interrupt	B-General_Concept
descriptor	I-General_Concept
table	I-General_Concept
(	O
IDT	O
)	O
.	O
</s>
<s>
As	O
with	O
other	O
tasks	O
performed	O
in	O
hardware	O
,	O
one	O
would	O
expect	O
this	O
to	O
be	O
rather	O
fast	O
;	O
however	O
,	O
mainstream	O
operating	O
systems	O
,	O
including	O
Windows	B-Application
and	O
Linux	B-Application
,	O
do	O
not	O
use	O
this	O
feature	O
.	O
</s>
<s>
Hardware	O
context	B-Operating_System
switching	I-Operating_System
does	O
not	O
save	O
all	O
the	O
registers	O
(	O
only	O
general-purpose	O
registers	O
,	O
not	O
floating	B-Algorithm
point	I-Algorithm
registers	O
—	O
although	O
the	O
TS	O
bit	O
is	O
automatically	O
turned	O
on	O
in	O
the	O
CR0	B-Operating_System
control	B-Operating_System
register	I-Operating_System
,	O
resulting	O
in	O
a	O
fault	O
when	O
executing	O
floating-point	B-Algorithm
instructions	O
and	O
giving	O
the	O
OS	O
the	O
opportunity	O
to	O
save	O
and	O
restore	O
the	O
floating-point	B-Algorithm
state	O
as	O
needed	O
)	O
.	O
</s>
<s>
Associated	O
performance	O
issues	O
,	O
e.g.	O
,	O
software	O
context	B-Operating_System
switching	I-Operating_System
can	O
be	O
selective	O
and	O
store	O
only	O
those	O
registers	O
that	O
need	O
storing	O
,	O
whereas	O
hardware	O
context	B-Operating_System
switching	I-Operating_System
stores	O
nearly	O
all	O
registers	O
whether	O
they	O
are	O
required	O
or	O
not	O
.	O
</s>
