OSDN Git Service

47e370ba8dd4ed4673e20d2fef94045493ba74fa
[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 .\" 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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\"
24 .TH PTHREAD_SETAFFINITY_NP 3 2010-09-10 "Linux" "Linux Programmer's Manual"
25 .SH NAME
26 pthread_setaffinity_np, pthread_getaffinity_np \- set/get
27 CPU affinity of a thread
28 .SH SYNOPSIS
29 .nf
30 .BR "#define _GNU_SOURCE" "             /* See feature_test_macros(7) */"
31 .B #include <pthread.h>
32
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 );
37 .sp
38 Compile and link with \fI\-pthread\fP.
39 .SH DESCRIPTION
40 The
41 .BR pthread_setaffinity_np ()
42 function
43 sets the CPU affinity mask of the thread
44 .I thread
45 to the CPU set pointed to by
46 .IR cpuset .
47 If the call is successful,
48 and the thread is not currently running on one of the CPUs in
49 .IR cpuset ,
50 then it is migrated to one of those CPUs.
51
52 The
53 .BR pthread_getaffinity_np ()
54 function returns the CPU affinity mask of the thread
55 .I thread
56 in the buffer pointed to by
57 .IR cpuset .
58
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
63 .BR CPU_SET (3).
64
65 The argument
66 .I cpusetsize
67 is the length (in bytes) of the buffer pointed to by
68 .IR cpuset .
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
72 .BR CPU_SET (3)
73 for dynamically allocating a CPU set.)
74 .SH RETURN VALUE
75 On success, these functions return 0;
76 on error, they return a nonzero error number.
77 .SH ERRORS
78 .TP
79 .B EFAULT
80 A supplied memory address was invalid.
81 .TP
82 .B EINVAL
83 .RB ( pthread_setaffinity_np ())
84 The affinity bit mask
85 .I mask
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
89 .BR cpuset (7).
90 .TP
91 .BR EINVAL
92 .RB ( pthread_setaffinity_np ())
93 .I cpuset
94 specified a CPU that was outside the set supported by the kernel.
95 (The kernel configuration option
96 .BR CONFIG_NR_CPUS
97 .\" FIXME . ?
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
102 .\" cpumask_t
103 used to represent CPU sets.)
104 .\" The raw sched_getaffinity() system call returns the size (in bytes)
105 .\" of the cpumask_t type.
106 .TP
107 .B EINVAL
108 .RB ( pthread_getaffinity_np ())
109 .I cpusetsize
110 is smaller than the size of the affinity mask used by the kernel.
111 .TP
112 .B ESRCH
113 No thread with the ID
114 .I thread
115 could be found.
116 .SH VERSIONS
117 These functions are provided by glibc since version 2.3.4.
118 .SH CONFORMING TO
119 These functions are nonstandard GNU extensions;
120 hence the suffix "_np" (nonportable) in the names.
121 .SH NOTES
122 After a call to
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
126 .I cpuset
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
130 .BR cpuset (7)
131 is being used.
132 These restrictions on the actual set of CPUs on which the thread
133 will run are silently imposed by the kernel.
134
135 These functions are implemented on top of the
136 .BR sched_setaffinity (2)
137 and
138 .BR sched_getaffinity (2)
139 system calls.
140
141 In glibc 2.3.3 only,
142 versions of these functions were provided that did not have a
143 .I cpusetsize
144 argument.
145 Instead the CPU set size given to the underlying system calls was always
146 .IR sizeof(cpu_set_t) .
147
148 A new thread created by
149 .BR pthread_create (3)
150 inherits a copy of its creator's CPU affinity mask.
151 .SH EXAMPLE
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),
156 and then calls
157 .BR pthread_getaffinity_np ()
158 to check the resulting CPU affinity mask of the thread.
159
160 .nf
161 #define _GNU_SOURCE
162 #include <pthread.h>
163 #include <stdio.h>
164 #include <stdlib.h>
165 #include <errno.h>
166
167 #define handle_error_en(en, msg) \\
168         do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
169
170 int
171 main(int argc, char *argv[])
172 {
173     int s, j;
174     cpu_set_t cpuset;
175     pthread_t thread;
176
177     thread = pthread_self();
178
179     /* Set affinity mask to include CPUs 0 to 7 */
180
181     CPU_ZERO(&cpuset);
182     for (j = 0; j < 8; j++)
183         CPU_SET(j, &cpuset);
184
185     s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
186     if (s != 0)
187         handle_error_en(s, "pthread_setaffinity_np");
188
189     /* Check the actual affinity mask assigned to the thread */
190
191     s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
192     if (s != 0)
193         handle_error_en(s, "pthread_getaffinity_np");
194
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);
199
200     exit(EXIT_SUCCESS);
201 }
202 .fi
203 .SH SEE ALSO
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),
209 .BR cpuset (7),
210 .BR pthreads (7)