OSDN Git Service

d913538976112298bb8b4d98f5d238483a7c75a1
[linuxjm/LDP_man-pages.git] / original / man3 / pthread_setaffinity_np.3
1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\"     <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_SETAFFINITY_NP 3 2014-05-21 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
29 CPU affinity of a thread
30 .SH SYNOPSIS
31 .nf
32 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
33 .B #include <pthread.h>
34
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 );
39 .sp
40 Compile and link with \fI\-pthread\fP.
41 .fi
42 .SH DESCRIPTION
43 The
44 .BR pthread_setaffinity_np ()
45 function
46 sets the CPU affinity mask of the thread
47 .I thread
48 to the CPU set pointed to by
49 .IR cpuset .
50 If the call is successful,
51 and the thread is not currently running on one of the CPUs in
52 .IR cpuset ,
53 then it is migrated to one of those CPUs.
54
55 The
56 .BR pthread_getaffinity_np ()
57 function returns the CPU affinity mask of the thread
58 .I thread
59 in the buffer pointed to by
60 .IR cpuset .
61
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
66 .BR CPU_SET (3).
67
68 The argument
69 .I cpusetsize
70 is the length (in bytes) of the buffer pointed to by
71 .IR cpuset .
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
75 .BR CPU_SET (3)
76 for dynamically allocating a CPU set.)
77 .SH RETURN VALUE
78 On success, these functions return 0;
79 on error, they return a nonzero error number.
80 .SH ERRORS
81 .TP
82 .B EFAULT
83 A supplied memory address was invalid.
84 .TP
85 .B EINVAL
86 .RB ( pthread_setaffinity_np ())
87 The affinity bit mask
88 .I mask
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
92 .BR cpuset (7).
93 .TP
94 .BR EINVAL
95 .RB ( pthread_setaffinity_np ())
96 .I cpuset
97 specified a CPU that was outside the set supported by the kernel.
98 (The kernel configuration option
99 .BR CONFIG_NR_CPUS
100 .\" FIXME . ?
101 .\" Loic Domaigne commented: it seems that in the future the
102 .\" kernel developers want to make cpumask_t dynamic, so
103 .\" CONFIG_NR_CPUS might become obsolete in the future.
104 defines the range of the set supported by the kernel data type
105 .\" cpumask_t
106 used to represent CPU sets.)
107 .\" The raw sched_getaffinity() system call returns the size (in bytes)
108 .\" of the cpumask_t type.
109 .TP
110 .B EINVAL
111 .RB ( pthread_getaffinity_np ())
112 .I cpusetsize
113 is smaller than the size of the affinity mask used by the kernel.
114 .TP
115 .B ESRCH
116 No thread with the ID
117 .I thread
118 could be found.
119 .SH VERSIONS
120 These functions are provided by glibc since version 2.3.4.
121 .SH CONFORMING TO
122 These functions are nonstandard GNU extensions;
123 hence the suffix "_np" (nonportable) in the names.
124 .SH NOTES
125 After a call to
126 .BR pthread_setaffinity_np (),
127 the set of CPUs on which the thread will actually run is
128 the intersection of the set specified in the
129 .I cpuset
130 argument and the set of CPUs actually present on the system.
131 The system may further restrict the set of CPUs on which the thread
132 runs if the "cpuset" mechanism described in
133 .BR cpuset (7)
134 is being used.
135 These restrictions on the actual set of CPUs on which the thread
136 will run are silently imposed by the kernel.
137
138 These functions are implemented on top of the
139 .BR sched_setaffinity (2)
140 and
141 .BR sched_getaffinity (2)
142 system calls.
143
144 In glibc 2.3.3 only,
145 versions of these functions were provided that did not have a
146 .I cpusetsize
147 argument.
148 Instead the CPU set size given to the underlying system calls was always
149 .IR sizeof(cpu_set_t) .
150
151 A new thread created by
152 .BR pthread_create (3)
153 inherits a copy of its creator's CPU affinity mask.
154 .SH EXAMPLE
155 In the following program, the main thread uses
156 .BR pthread_setaffinity_np ()
157 to set its CPU affinity mask to include CPUs 0 to 7
158 (which may not all be available on the system),
159 and then calls
160 .BR pthread_getaffinity_np ()
161 to check the resulting CPU affinity mask of the thread.
162
163 .nf
164 #define _GNU_SOURCE
165 #include <pthread.h>
166 #include <stdio.h>
167 #include <stdlib.h>
168 #include <errno.h>
169
170 #define handle_error_en(en, msg) \\
171         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
172
173 int
174 main(int argc, char *argv[])
175 {
176     int s, j;
177     cpu_set_t cpuset;
178     pthread_t thread;
179
180     thread = pthread_self();
181
182     /* Set affinity mask to include CPUs 0 to 7 */
183
184     CPU_ZERO(&cpuset);
185     for (j = 0; j < 8; j++)
186         CPU_SET(j, &cpuset);
187
188     s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
189     if (s != 0)
190         handle_error_en(s, "pthread_setaffinity_np");
191
192     /* Check the actual affinity mask assigned to the thread */
193
194     s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
195     if (s != 0)
196         handle_error_en(s, "pthread_getaffinity_np");
197
198     printf("Set returned by pthread_getaffinity_np() contained:\\n");
199     for (j = 0; j < CPU_SETSIZE; j++)
200         if (CPU_ISSET(j, &cpuset))
201             printf("    CPU %d\\n", j);
202
203     exit(EXIT_SUCCESS);
204 }
205 .fi
206 .SH SEE ALSO
207 .BR sched_setaffinity (2),
208 .BR pthread_attr_setaffinity_np (3),
209 .BR pthread_self (3),
210 .BR sched_getcpu (3),
211 .BR cpuset (7),
212 .BR pthreads (7),
213 .BR sched (7)
214 .SH COLOPHON
215 This page is part of release 3.67 of the Linux
216 .I man-pages
217 project.
218 A description of the project,
219 information about reporting bugs,
220 and the latest version of this page,
221 can be found at
222 \%http://www.kernel.org/doc/man\-pages/.