1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_SETAFFINITY_NP 3 2010-09-10 "Linux" "Linux Programmer's Manual"
26 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
27 CPU affinity of a thread
30 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
31 .B #include <pthread.h>
33 .BI "int pthread_setaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
34 .BI " const cpu_set_t *" cpuset );
35 .BI "int pthread_getaffinity_np(pthread_t " thread ", size_t " cpusetsize ,
36 .BI " cpu_set_t *" cpuset );
38 Compile and link with \fI\-pthread\fP.
41 .BR pthread_setaffinity_np ()
43 sets the CPU affinity mask of the thread
45 to the CPU set pointed to by
47 If the call is successful,
48 and the thread is not currently running on one of the CPUs in
50 then it is migrated to one of those CPUs.
53 .BR pthread_getaffinity_np ()
54 function returns the CPU affinity mask of the thread
56 in the buffer pointed to by
59 For more details on CPU affinity masks, see
60 .BR sched_setaffinity (2).
61 For a description of a set of macros
62 that can be used to manipulate and inspect CPU sets, see
67 is the length (in bytes) of the buffer pointed to by
69 Typically, this argument would be specified as
70 .IR sizeof(cpu_set_t) .
71 (It may be some other value, if using the macros described in
73 for dynamically allocating a CPU set.)
75 On success, these functions return 0;
76 on error, they return a nonzero error number.
80 A supplied memory address was invalid.
83 .RB ( pthread_setaffinity_np ())
86 contains no processors that are currently physically on the system
87 and permitted to the thread according to any restrictions that
88 may be imposed by the "cpuset" mechanism described in
92 .RB ( pthread_setaffinity_np ())
94 specified a CPU that was outside the set supported by the kernel.
95 (The kernel configuration option
98 .\" Loic Domaigne commented: it seems that in the future the
99 .\" kernel developers want to make cpumask_t dynamic, so
100 .\" CONFIG_NR_CPUS might become obsolete in the future.
101 defines the range of the set supported by the kernel data type
103 used to represent CPU sets.)
104 .\" The raw sched_getaffinity() system call returns the size (in bytes)
105 .\" of the cpumask_t type.
108 .RB ( pthread_getaffinity_np ())
110 is smaller than the size of the affinity mask used by the kernel.
113 No thread with the ID
117 These functions are provided by glibc since version 2.3.4.
119 These functions are nonstandard GNU extensions;
120 hence the suffix "_np" (nonportable) in the names.
123 .BR pthread_setaffinity_np (),
124 the set of CPUs on which the thread will actually run is
125 the intersection of the set specified in the
127 argument and the set of CPUs actually present on the system.
128 The system may further restrict the set of CPUs on which the thread
129 runs if the "cpuset" mechanism described in
132 These restrictions on the actual set of CPUs on which the thread
133 will run are silently imposed by the kernel.
135 These functions are implemented on top of the
136 .BR sched_setaffinity (2)
138 .BR sched_getaffinity (2)
142 versions of these functions were provided that did not have a
145 Instead the CPU set size given to the underlying system calls was always
146 .IR sizeof(cpu_set_t) .
148 A new thread created by
149 .BR pthread_create (3)
150 inherits a copy of its creator's CPU affinity mask.
152 In the following program, the main thread uses
153 .BR pthread_setaffinity_np ()
154 to set its CPU affinity mask to include CPUs 0 to 7
155 (which may not all be available on the system),
157 .BR pthread_getaffinity_np ()
158 to check the resulting CPU affinity mask of the thread.
167 #define handle_error_en(en, msg) \\
168 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
171 main(int argc, char *argv[])
177 thread = pthread_self();
179 /* Set affinity mask to include CPUs 0 to 7 */
182 for (j = 0; j < 8; j++)
185 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
187 handle_error_en(s, "pthread_setaffinity_np");
189 /* Check the actual affinity mask assigned to the thread */
191 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
193 handle_error_en(s, "pthread_getaffinity_np");
195 printf("Set returned by pthread_getaffinity_np() contained:\\n");
196 for (j = 0; j < CPU_SETSIZE; j++)
197 if (CPU_ISSET(j, &cpuset))
198 printf(" CPU %d\\n", j);
204 .BR sched_setaffinity (2),
205 .BR sched_setscheduler (2),
206 .BR pthread_attr_setaffinity_np (3),
207 .BR pthread_self (3),
208 .BR sched_getcpu (3),