1 .\" man2/sched_setaffinity.2 - sched_setaffinity and sched_getaffinity man page
3 .\" Copyright (C) 2002 Robert Love
4 .\" and Copyright (C) 2006 Michael Kerrisk
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, write to the Free
23 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
26 .\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
27 .\" 2004-04-20 mtk - fixed description of return value
28 .\" 2004-04-22 aeb - added glibc prototype history
29 .\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
30 .\" migration and that CPU affinity is a per-thread attribute.
31 .\" 2006-02-03 mtk -- Major rewrite
33 .TH SCHED_SETAFFINITY 2 2006-02-03 "Linux" "Linux Programmer's Manual"
35 sched_setaffinity, sched_getaffinity, CPU_CLR, CPU_ISSET, CPU_SET, CPU_ZERO \
36 \- set and get a process's CPU affinity mask
39 .B #define _GNU_SOURCE
42 .BI "int sched_setaffinity(pid_t " pid ", unsigned int " cpusetsize ,
43 .BI " cpu_set_t *" mask );
45 .BI "int sched_getaffinity(pid_t " pid ", unsigned int " cpusetsize ,
46 .BI " cpu_set_t *" mask );
48 .BI "void CPU_CLR(int " cpu ", cpu_set_t *" set );
50 .BI "int CPU_ISSET(int " cpu ", cpu_set_t *" set );
52 .BI "void CPU_SET(int " cpu ", cpu_set_t *" set );
54 .BI "void CPU_ZERO(cpu_set_t *" set );
57 A process's CPU affinity mask determines the set of CPUs on which
58 it is eligible to run.
59 On a multiprocessor system, setting the CPU affinity mask
60 can be used to obtain performance benefits.
62 by dedicating one CPU to a particular process
63 (i.e., setting the affinity mask of that process to specify a single CPU,
64 and setting the affinity mask of all other processes to exclude that CPU),
65 it is possible to ensure maximum execution speed for that process.
66 Restricting a process to run on a single CPU also prevents
67 the performance cost caused by the cache invalidation that occurs
68 when a process ceases to execute on one CPU and then
69 recommences execution on a different CPU.
71 A CPU affinity mask is represented by the
73 structure, a "CPU set", pointed to by
75 Four macros are provided to manipulate CPU sets.
81 respectively add and remove a given CPU from a set.
83 tests to see if a CPU is part of the set; this is useful after
84 .BR sched_getaffinity ()
86 The first available CPU on the system corresponds to a
88 value of 0, the next CPU corresponds to a
90 value of 1, and so on.
93 (1024) specifies a value one greater than the maximum CPU
94 number that can be stored in a CPU set.
96 .BR sched_setaffinity ()
97 sets the CPU affinity mask of the process whose ID is
99 to the value specified by
103 is zero, then the calling process is used.
106 is the length (in bytes) of the data pointed to by
108 Normally this argument would be specified as
109 .IR "sizeof(cpu_set_t)" .
111 If the process specified by
113 is not currently running on one of the CPUs specified in
115 then that process is migrated to one of the CPUs specified in
118 .BR sched_getaffinity ()
119 writes the affinity mask of the process whose ID is
123 structure pointed to by
127 argument specifies the size (in bytes) of
131 is zero, then the mask of the calling process is returned.
134 .BR sched_setaffinity ()
136 .BR sched_getaffinity ()
138 On error, \-1 is returned, and
140 is set appropriately.
144 A supplied memory address was invalid.
147 The affinity bit mask
149 contains no processors that are physically on the system,
150 .\" The following can only (?) occur with the raw sched_getaffinity()
151 .\" system call (MTK, 3 Feb 2006):
154 is smaller than the size of the affinity mask used by the kernel.
157 The calling process does not have appropriate privileges.
159 .BR sched_setaffinity ()
160 needs an effective user ID equal to the user ID or effective user ID
161 of the process identified by
163 or it must possess the
168 The process whose ID is \fIpid\fP could not be found.
170 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
171 The library interfaces were introduced in glibc 2.3.
172 Initially, the glibc interfaces included a
177 argument was removed, but this argument was restored in glibc 2.3.4.
180 These system calls are Linux specific.
182 The affinity mask is actually a per-thread attribute that can be
183 adjusted independently for each of the threads in a thread group.
184 The value returned from a call to
186 can be passed in the argument
190 as 0 will set the attribute for the current thread,
191 and passing the value returned from a call to
193 will set the attribute for the main thread of the thread group.
197 inherits its parent's CPU affinity mask.
198 The affinity mask is preserved across an
201 This manual page describes the glibc interface for the CPU affinity calls.
202 The actual system call interface is slightly different, with the
205 .IR "unsigned long *" ,
206 reflecting that the fact that the underlying implementation of CPU
207 sets is a simple bit mask.
209 .BR sched_getaffinity ()
210 system call returns the size (in bytes) of the
212 data type that is used internally by the kernel to
213 represent the CPU set bit mask.
219 .BR sched_get_priority_max (2),
220 .BR sched_get_priority_min (2),
221 .BR sched_getscheduler (2),
222 .BR sched_setscheduler (2),
226 .BR sched_setscheduler (2)
227 has a description of the Linux scheduling scheme.