1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_SETAFFINITY_NP 3 2019-03-06 "Linux" "Linux Programmer's Manual"
28 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
29 CPU affinity of a thread
32 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
33 .B #include <pthread.h>
35 .BI "int pthread_setaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
36 .BI " const cpu_set_t *" cpuset );
37 .BI "int pthread_getaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
38 .BI " cpu_set_t *" cpuset );
40 Compile and link with \fI\-pthread\fP.
44 .BR pthread_setaffinity_np ()
46 sets the CPU affinity mask of the thread
48 to the CPU set pointed to by
50 If the call is successful,
51 and the thread is not currently running on one of the CPUs in
53 then it is migrated to one of those CPUs.
56 .BR pthread_getaffinity_np ()
57 function returns the CPU affinity mask of the thread
59 in the buffer pointed to by
62 For more details on CPU affinity masks, see
63 .BR sched_setaffinity (2).
64 For a description of a set of macros
65 that can be used to manipulate and inspect CPU sets, see
70 is the length (in bytes) of the buffer pointed to by
72 Typically, this argument would be specified as
73 .IR sizeof(cpu_set_t) .
74 (It may be some other value, if using the macros described in
76 for dynamically allocating a CPU set.)
78 On success, these functions return 0;
79 on error, they return a nonzero error number.
83 A supplied memory address was invalid.
86 .RB ( pthread_setaffinity_np ())
89 contains no processors that are currently physically on the system
90 and permitted to the thread according to any restrictions that
91 may be imposed by the "cpuset" mechanism described in
95 .RB ( pthread_setaffinity_np ())
97 specified a CPU that was outside the set supported by the kernel.
98 (The kernel configuration option
100 defines the range of the set supported by the kernel data type
102 used to represent CPU sets.)
103 .\" The raw sched_getaffinity() system call returns the size (in bytes)
104 .\" of the cpumask_t type.
107 .RB ( pthread_getaffinity_np ())
109 is smaller than the size of the affinity mask used by the kernel.
112 No thread with the ID
116 These functions are provided by glibc since version 2.3.4.
118 For an explanation of the terms used in this section, see
124 Interface Attribute Value
126 .BR pthread_setaffinity_np (),
127 .BR pthread_getaffinity_np ()
128 T} Thread safety MT-Safe
131 These functions are nonstandard GNU extensions;
132 hence the suffix "_np" (nonportable) in the names.
135 .BR pthread_setaffinity_np (),
136 the set of CPUs on which the thread will actually run is
137 the intersection of the set specified in the
139 argument and the set of CPUs actually present on the system.
140 The system may further restrict the set of CPUs on which the thread
141 runs if the "cpuset" mechanism described in
144 These restrictions on the actual set of CPUs on which the thread
145 will run are silently imposed by the kernel.
147 These functions are implemented on top of the
148 .BR sched_setaffinity (2)
150 .BR sched_getaffinity (2)
154 versions of these functions were provided that did not have a
157 Instead the CPU set size given to the underlying system calls was always
158 .IR sizeof(cpu_set_t) .
160 A new thread created by
161 .BR pthread_create (3)
162 inherits a copy of its creator's CPU affinity mask.
164 In the following program, the main thread uses
165 .BR pthread_setaffinity_np ()
166 to set its CPU affinity mask to include CPUs 0 to 7
167 (which may not all be available on the system),
169 .BR pthread_getaffinity_np ()
170 to check the resulting CPU affinity mask of the thread.
179 #define handle_error_en(en, msg) \e
180 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
183 main(int argc, char *argv[])
189 thread = pthread_self();
191 /* Set affinity mask to include CPUs 0 to 7 */
194 for (j = 0; j < 8; j++)
197 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
199 handle_error_en(s, "pthread_setaffinity_np");
201 /* Check the actual affinity mask assigned to the thread */
203 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
205 handle_error_en(s, "pthread_getaffinity_np");
207 printf("Set returned by pthread_getaffinity_np() contained:\en");
208 for (j = 0; j < CPU_SETSIZE; j++)
209 if (CPU_ISSET(j, &cpuset))
210 printf(" CPU %d\en", j);
216 .BR sched_setaffinity (2),
218 .BR pthread_attr_setaffinity_np (3),
219 .BR pthread_self (3),
220 .BR sched_getcpu (3),