<s>
Processor	B-Operating_System
affinity	I-Operating_System
,	O
or	O
CPU	O
pinning	O
or	O
"	O
cache	O
affinity	O
"	O
,	O
enables	O
the	O
binding	O
and	O
unbinding	O
of	O
a	O
process	B-Operating_System
or	O
a	O
thread	B-Operating_System
to	O
a	O
central	B-General_Concept
processing	I-General_Concept
unit	I-General_Concept
(	O
CPU	O
)	O
or	O
a	O
range	O
of	O
CPUs	B-General_Concept
,	O
so	O
that	O
the	O
process	B-Operating_System
or	O
thread	B-Operating_System
will	O
execute	O
only	O
on	O
the	O
designated	O
CPU	O
or	O
CPUs	B-General_Concept
rather	O
than	O
any	O
CPU	O
.	O
</s>
<s>
This	O
can	O
be	O
viewed	O
as	O
a	O
modification	O
of	O
the	O
native	O
central	O
queue	O
scheduling	O
algorithm	O
in	O
a	O
symmetric	B-Operating_System
multiprocessing	I-Operating_System
operating	O
system	O
.	O
</s>
<s>
Each	O
item	O
in	O
the	O
queue	O
has	O
a	O
tag	O
indicating	O
its	O
kin	O
processor	B-General_Concept
.	O
</s>
<s>
At	O
the	O
time	O
of	O
resource	O
allocation	O
,	O
each	O
task	O
is	O
allocated	O
to	O
its	O
kin	O
processor	B-General_Concept
in	O
preference	O
to	O
others	O
.	O
</s>
<s>
Processor	B-Operating_System
affinity	I-Operating_System
takes	O
advantage	O
of	O
the	O
fact	O
that	O
remnants	O
of	O
a	O
process	B-Operating_System
that	O
was	O
run	O
on	O
a	O
given	O
processor	B-General_Concept
may	O
remain	O
in	O
that	O
processor	B-General_Concept
's	O
state	O
(	O
for	O
example	O
,	O
data	O
in	O
the	O
cache	B-General_Concept
memory	I-General_Concept
)	O
after	O
another	O
process	B-Operating_System
was	O
run	O
on	O
that	O
processor	B-General_Concept
.	O
</s>
<s>
Scheduling	O
a	O
CPU-intensive	O
process	B-Operating_System
that	O
has	O
few	O
interrupts	O
to	O
execute	O
on	O
the	O
same	O
processor	B-General_Concept
may	O
improve	O
its	O
performance	O
by	O
reducing	O
degrading	O
events	O
such	O
as	O
cache	O
misses	O
,	O
but	O
may	O
slow	O
down	O
ordinary	O
programs	O
because	O
they	O
would	O
need	O
to	O
wait	O
for	O
that	O
CPU	O
to	O
become	O
available	O
again	O
.	O
</s>
<s>
A	O
practical	O
example	O
of	O
processor	B-Operating_System
affinity	I-Operating_System
is	O
executing	O
multiple	O
instances	O
of	O
a	O
non-threaded	O
application	O
,	O
such	O
as	O
some	O
graphics-rendering	O
software	O
.	O
</s>
<s>
Scheduling-algorithm	O
implementations	O
vary	O
in	O
adherence	O
to	O
processor	B-Operating_System
affinity	I-Operating_System
.	O
</s>
<s>
Under	O
certain	O
circumstances	O
,	O
some	O
implementations	O
will	O
allow	O
a	O
task	O
to	O
change	O
to	O
another	O
processor	B-General_Concept
if	O
it	O
results	O
in	O
higher	O
efficiency	O
.	O
</s>
<s>
For	O
example	O
,	O
when	O
two	O
processor-intensive	O
tasks	O
(	O
A	O
and	O
B	O
)	O
have	O
affinity	O
to	O
one	O
processor	B-General_Concept
while	O
another	O
processor	B-General_Concept
remains	O
unused	O
,	O
many	O
schedulers	O
will	O
shift	O
task	O
B	O
to	O
the	O
second	O
processor	B-General_Concept
in	O
order	O
to	O
maximize	O
processor	B-General_Concept
use	O
.	O
</s>
<s>
Task	O
B	O
will	O
then	O
acquire	O
affinity	O
with	O
the	O
second	O
processor	B-General_Concept
,	O
while	O
task	O
A	O
will	O
continue	O
to	O
have	O
affinity	O
with	O
the	O
original	O
processor	B-General_Concept
.	O
</s>
<s>
Processor	B-Operating_System
affinity	I-Operating_System
can	O
effectively	O
reduce	O
cache	O
problems	O
,	O
but	O
it	O
does	O
not	O
reduce	O
the	O
persistent	O
load-balancing	B-Application
problem	O
.	O
</s>
<s>
Also	O
note	O
that	O
processor	B-Operating_System
affinity	I-Operating_System
becomes	O
more	O
complicated	O
in	O
systems	O
with	O
non-uniform	O
architectures	O
.	O
</s>
<s>
For	O
example	O
,	O
a	O
system	O
with	O
two	O
dual-core	B-Architecture
hyper-threaded	B-Operating_System
CPUs	B-General_Concept
presents	O
a	O
challenge	O
to	O
a	O
scheduling	O
algorithm	O
.	O
</s>
<s>
There	O
is	O
complete	O
affinity	O
between	O
two	O
virtual	O
CPUs	B-General_Concept
implemented	O
on	O
the	O
same	O
core	O
via	O
hyper-threading	B-Operating_System
,	O
partial	O
affinity	O
between	O
two	O
cores	O
on	O
the	O
same	O
physical	O
processor	B-General_Concept
(	O
as	O
the	O
cores	O
share	O
some	O
,	O
but	O
not	O
all	O
,	O
cache	O
)	O
,	O
and	O
no	O
affinity	O
between	O
separate	O
physical	O
processors	O
.	O
</s>
<s>
As	O
other	O
resources	O
are	O
also	O
shared	O
,	O
processor	B-Operating_System
affinity	I-Operating_System
alone	O
cannot	O
be	O
used	O
as	O
the	O
basis	O
for	O
CPU	O
dispatching	O
.	O
</s>
<s>
If	O
a	O
process	B-Operating_System
has	O
recently	O
run	O
on	O
one	O
virtual	O
hyper-threaded	B-Operating_System
CPU	O
in	O
a	O
given	O
core	O
,	O
and	O
that	O
virtual	O
CPU	O
is	O
currently	O
busy	O
but	O
its	O
partner	O
CPU	O
is	O
not	O
,	O
cache	O
affinity	O
would	O
suggest	O
that	O
the	O
process	B-Operating_System
should	O
be	O
dispatched	O
to	O
the	O
idle	O
partner	O
CPU	O
.	O
</s>
<s>
However	O
,	O
the	O
two	O
virtual	O
CPUs	B-General_Concept
compete	O
for	O
essentially	O
all	O
computing	O
,	O
cache	O
,	O
and	O
memory	O
resources	O
.	O
</s>
<s>
In	O
this	O
situation	O
,	O
it	O
would	O
typically	O
be	O
more	O
efficient	O
to	O
dispatch	O
the	O
process	B-Operating_System
to	O
a	O
different	O
core	O
or	O
CPU	O
,	O
if	O
one	O
is	O
available	O
.	O
</s>
<s>
This	O
could	O
incur	O
a	O
penalty	O
when	O
process	B-Operating_System
repopulates	O
the	O
cache	O
,	O
but	O
overall	O
performance	O
could	O
be	O
higher	O
as	O
the	O
process	B-Operating_System
would	O
not	O
have	O
to	O
compete	O
for	O
resources	O
within	O
the	O
CPU	O
.	O
</s>
<s>
On	O
Linux	B-Application
,	O
the	O
CPU	B-Operating_System
affinity	I-Operating_System
of	O
a	O
process	B-Operating_System
can	O
be	O
altered	O
with	O
the	O
taskset(1 )	O
program	O
and	O
the	O
sched_setaffinity(2 )	O
system	O
call	O
.	O
</s>
<s>
The	O
affinity	O
of	O
a	O
thread	B-Operating_System
can	O
be	O
altered	O
with	O
one	O
of	O
the	O
library	O
functions	O
:	O
pthread_setaffinity_np(3 )	O
or	O
pthread_attr_setaffinity_np(3 )	O
.	O
</s>
<s>
On	O
SGI	O
systems	O
,	O
binds	O
a	O
process	B-Operating_System
to	O
a	O
set	O
of	O
CPUs	B-General_Concept
.	O
</s>
<s>
On	O
DragonFly	B-Application
BSD	I-Application
1.9	O
(	O
2007	O
)	O
and	O
later	O
versions	O
,	O
usched_set	O
system	O
call	O
can	O
be	O
used	O
to	O
control	O
the	O
affinity	O
of	O
a	O
process	B-Operating_System
.	O
</s>
<s>
On	O
NetBSD	B-Device
5.0	O
,	O
FreeBSD	B-Operating_System
7.2	O
,	O
DragonFly	B-Application
BSD	I-Application
4.7	O
and	O
later	O
versions	O
can	O
use	O
pthread_setaffinity_np	O
and	O
pthread_getaffinity_np	O
.	O
</s>
<s>
In	O
NetBSD	B-Device
,	O
the	O
psrset	O
utility	O
to	O
set	O
a	O
thread	B-Operating_System
's	O
affinity	O
to	O
a	O
certain	O
CPU	O
set	O
.	O
</s>
<s>
In	O
FreeBSD	B-Operating_System
,	O
cpuset	O
utility	O
is	O
used	O
to	O
create	O
CPU	O
sets	O
and	O
to	O
assign	O
processes	O
to	O
these	O
sets	O
.	O
</s>
<s>
In	O
DragonFly	B-Application
BSD	I-Application
3.1	O
(	O
2012	O
)	O
and	O
later	O
,	O
usched	O
utility	O
can	O
be	O
used	O
for	O
assigning	O
processes	O
to	O
a	O
certain	O
CPU	O
set	O
.	O
</s>
<s>
On	O
Windows	B-Device
NT	I-Device
and	O
its	O
successors	O
,	O
thread	B-Operating_System
and	O
process	B-Operating_System
CPU	B-Operating_System
affinities	I-Operating_System
can	O
be	O
set	O
separately	O
by	O
using	O
SetThreadAffinityMask	O
and	O
SetProcessAffinityMask	O
API	O
calls	O
or	O
via	O
the	O
Task	O
Manager	O
interface	O
(	O
for	O
process	B-Operating_System
affinity	O
only	O
)	O
.	O
</s>
<s>
macOS	B-Application
exposes	O
an	O
affinity	O
API	O
that	O
provides	O
hints	O
to	O
the	O
kernel	O
how	O
to	O
schedule	O
threads	B-Operating_System
according	O
to	O
affinity	O
sets	O
.	O
</s>
<s>
On	O
Solaris	B-Application
it	O
is	O
possible	O
to	O
control	O
bindings	O
of	O
processes	O
and	O
LWPs	O
to	O
processor	B-General_Concept
using	O
the	O
pbind(1 )	O
program	O
.	O
</s>
<s>
There	O
are	O
more	O
generic	O
interfaces	O
available	O
such	O
as	O
pset_bind(2 )	O
or	O
lgrp_affinity_get(3LGRP )	O
using	O
processor	B-General_Concept
set	O
and	O
locality	O
groups	O
concepts	O
.	O
</s>
<s>
On	O
AIX	B-Application
it	O
is	O
possible	O
to	O
control	O
bindings	O
of	O
processes	O
using	O
the	O
bindprocessor	O
command	O
and	O
the	O
bindprocessor	O
API	O
.	O
</s>
