OSDN Git Service

Update README
[linuxjm/LDP_man-pages.git] / original / man2 / sched_setaffinity.2
1 .\" Copyright (C) 2002 Robert Love
2 .\" and Copyright (C) 2006 Michael Kerrisk
3 .\"
4 .\" %%%LICENSE_START(GPLv2+_DOC_FULL)
5 .\" This is free documentation; you can redistribute it and/or
6 .\" modify it under the terms of the GNU General Public License as
7 .\" published by the Free Software Foundation; either version 2 of
8 .\" the License, or (at your option) any later version.
9 .\"
10 .\" The GNU General Public License's references to "object code"
11 .\" and "executables" are to be interpreted as the output of any
12 .\" document formatting or typesetting system, including
13 .\" intermediate and printed output.
14 .\"
15 .\" This manual is distributed in the hope that it will be useful,
16 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 .\" GNU General Public License for more details.
19 .\"
20 .\" You should have received a copy of the GNU General Public
21 .\" License along with this manual; if not, see
22 .\" <http://www.gnu.org/licenses/>.
23 .\" %%%LICENSE_END
24 .\"
25 .\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
26 .\" 2004-04-20 mtk - fixed description of return value
27 .\" 2004-04-22 aeb - added glibc prototype history
28 .\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
29 .\"     migration and that CPU affinity is a per-thread attribute.
30 .\" 2006-02-03 mtk -- Major rewrite
31 .\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
32 .\" separate CPU_SET(3) page.
33 .\"
34 .TH SCHED_SETAFFINITY 2 2014-12-31 "Linux" "Linux Programmer's Manual"
35 .SH NAME
36 sched_setaffinity, sched_getaffinity \- \
37 set and get a thread's CPU affinity mask
38 .SH SYNOPSIS
39 .nf
40 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
41 .B #include <sched.h>
42 .sp
43 .BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
44 .BI "                      const cpu_set_t *" mask );
45 .sp
46 .BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
47 .BI "                      cpu_set_t *" mask );
48 .fi
49 .SH DESCRIPTION
50 A thread's CPU affinity mask determines the set of CPUs on which
51 it is eligible to run.
52 On a multiprocessor system, setting the CPU affinity mask
53 can be used to obtain performance benefits.
54 For example,
55 by dedicating one CPU to a particular thread
56 (i.e., setting the affinity mask of that thread to specify a single CPU,
57 and setting the affinity mask of all other threads to exclude that CPU),
58 it is possible to ensure maximum execution speed for that thread.
59 Restricting a thread to run on a single CPU also avoids
60 the performance cost caused by the cache invalidation that occurs
61 when a thread ceases to execute on one CPU and then
62 recommences execution on a different CPU.
63
64 A CPU affinity mask is represented by the
65 .I cpu_set_t
66 structure, a "CPU set", pointed to by
67 .IR mask .
68 A set of macros for manipulating CPU sets is described in
69 .BR CPU_SET (3).
70
71 .BR sched_setaffinity ()
72 sets the CPU affinity mask of the thread whose ID is
73 .I pid
74 to the value specified by
75 .IR mask .
76 If
77 .I pid
78 is zero, then the calling thread is used.
79 The argument
80 .I cpusetsize
81 is the length (in bytes) of the data pointed to by
82 .IR mask .
83 Normally this argument would be specified as
84 .IR "sizeof(cpu_set_t)" .
85
86 If the thread specified by
87 .I pid
88 is not currently running on one of the CPUs specified in
89 .IR mask ,
90 then that thread is migrated to one of the CPUs specified in
91 .IR mask .
92
93 .BR sched_getaffinity ()
94 writes the affinity mask of the thread whose ID is
95 .I pid
96 into the
97 .I cpu_set_t
98 structure pointed to by
99 .IR mask .
100 The
101 .I cpusetsize
102 argument specifies the size (in bytes) of
103 .IR mask .
104 If
105 .I pid
106 is zero, then the mask of the calling thread is returned.
107 .SH RETURN VALUE
108 On success,
109 .BR sched_setaffinity ()
110 and
111 .BR sched_getaffinity ()
112 return 0.
113 On error, \-1 is returned, and
114 .I errno
115 is set appropriately.
116 .SH ERRORS
117 .TP
118 .B EFAULT
119 A supplied memory address was invalid.
120 .TP
121 .B EINVAL
122 The affinity bit mask
123 .I mask
124 contains no processors that are currently physically on the system
125 and permitted to the thread according to any restrictions that
126 may be imposed by the "cpuset" mechanism described in
127 .BR cpuset (7).
128 .TP
129 .B EINVAL
130 .RB ( sched_getaffinity ()
131 and, in kernels before 2.6.9,
132 .BR sched_setaffinity ())
133 .I cpusetsize
134 is smaller than the size of the affinity mask used by the kernel.
135 .TP
136 .B EPERM
137 .RB ( sched_setaffinity ())
138 The calling thread does not have appropriate privileges.
139 The caller needs an effective user ID equal to the real user ID
140 or effective user ID of the thread identified by
141 .IR pid ,
142 or it must possess the
143 .B CAP_SYS_NICE
144 capability.
145 .TP
146 .B ESRCH
147 The thread whose ID is \fIpid\fP could not be found.
148 .SH VERSIONS
149 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
150 The system call wrappers were introduced in glibc 2.3.
151 Initially, the glibc interfaces included a
152 .I cpusetsize
153 argument, typed as
154 .IR "unsigned int" .
155 In glibc 2.3.3, the
156 .I cpusetsize
157 argument was removed, but was then restored in glibc 2.3.4, with type
158 .IR size_t .
159 .SH CONFORMING TO
160 These system calls are Linux-specific.
161 .SH NOTES
162 After a call to
163 .BR sched_setaffinity (),
164 the set of CPUs on which the thread will actually run is
165 the intersection of the set specified in the
166 .I mask
167 argument and the set of CPUs actually present on the system.
168 The system may further restrict the set of CPUs on which the thread
169 runs if the "cpuset" mechanism described in
170 .BR cpuset (7)
171 is being used.
172 These restrictions on the actual set of CPUs on which the thread
173 will run are silently imposed by the kernel.
174
175 There are various ways of determining the number of CPUs
176 available on the system, including: inspecting the contents of
177 .IR /proc/cpuinfo ;
178 using
179 .BR syconf (3)
180 to obtain the values of the
181 .BR _SC_NPROCESSORS_CONF
182 and
183 .BR _SC_NPROCESSORS_ONLN
184 parameters; and inspecting the list CPU directories under
185 .IR /sys/devices/system/cpu/ .
186
187 .BR sched (7)
188 has a description of the Linux scheduling scheme.
189 .PP
190 The affinity mask is a per-thread attribute that can be
191 adjusted independently for each of the threads in a thread group.
192 The value returned from a call to
193 .BR gettid (2)
194 can be passed in the argument
195 .IR pid .
196 Specifying
197 .I pid
198 as 0 will set the attribute for the calling thread,
199 and passing the value returned from a call to
200 .BR getpid (2)
201 will set the attribute for the main thread of the thread group.
202 (If you are using the POSIX threads API, then use
203 .BR pthread_setaffinity_np (3)
204 instead of
205 .BR sched_setaffinity ().)
206
207 A child created via
208 .BR fork (2)
209 inherits its parent's CPU affinity mask.
210 The affinity mask is preserved across an
211 .BR execve (2).
212 .SS C library/kernel ABI differences
213 This manual page describes the glibc interface for the CPU affinity calls.
214 The actual system call interface is slightly different, with the
215 .I mask
216 being typed as
217 .IR "unsigned long\ *" ,
218 reflecting the fact that the underlying implementation of CPU
219 sets is a simple bit mask.
220 On success, the raw
221 .BR sched_getaffinity ()
222 system call returns the size (in bytes) of the
223 .I cpumask_t
224 data type that is used internally by the kernel to
225 represent the CPU set bit mask.
226 .SH SEE ALSO
227 .ad l
228 .nh
229 .BR lscpu (1),
230 .BR nproc (1),
231 .BR taskset (1),
232 .BR clone (2),
233 .BR getcpu (2),
234 .BR getpriority (2),
235 .BR gettid (2),
236 .BR nice (2),
237 .BR sched_get_priority_max (2),
238 .BR sched_get_priority_min (2),
239 .BR sched_getscheduler (2),
240 .BR sched_setscheduler (2),
241 .BR setpriority (2),
242 .BR CPU_SET (3),
243 .BR pthread_setaffinity_np (3),
244 .BR sched_getcpu (3),
245 .BR capabilities (7),
246 .BR cpuset (7),
247 .BR sched (7)
248 .SH COLOPHON
249 This page is part of release 3.79 of the Linux
250 .I man-pages
251 project.
252 A description of the project,
253 information about reporting bugs,
254 and the latest version of this page,
255 can be found at
256 \%http://www.kernel.org/doc/man\-pages/.