]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_setaffinity_np.3
getdents.2, pthread_attr_init.3, pthread_create.3, pthread_getattr_np.3, pthread_seta...
[thirdparty/man-pages.git] / man3 / pthread_setaffinity_np.3
CommitLineData
5655765f
MK
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.\"
940c8ce2 24.TH PTHREAD_SETAFFINITY_NP 3 2008-11-11 "Linux" "Linux Programmer's Manual"
5655765f
MK
25.SH NAME
26pthread_setaffinity_np, pthread_getaffinity_np \- set/get
27CPU affinity of a thread
28.SH SYNOPSIS
29.nf
30.B #define _GNU_SOURCE
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
38Compile and link with \fI\-pthread\fP.
39.SH DESCRIPTION
40The
41.BR pthread_setaffinity_np ()
42sets the CPU affinity mask of the thread
43.I thread
44to the CPU set pointed to by
45.IR cpuset .
46If the call is successful,
47and the thread is not currently running on one of the CPUs in
48.IR cpuset ,
49then it is migrated to one of those CPUs.
50
51The
52.BR pthread_getaffinity_np ()
53function returns the CPU affinity mask of the thread
54.I thread
55in the buffer pointed to by
56.IR cpuset .
57
58The argument
59.I cpusetsize
60is the length (in bytes) of the buffer pointed to by
61.IR cpuset .
62Normally this argument would be specified as
63.IR sizeof(cpu_set_t) .
64The constant
65.B CPU_SETSIZE
66specifies a value one greater than the
67maximum CPU number that can be stored in a CPU set.
68
69For more details on CPU affinity masks,
70as well as a description of a set of macros
71that can be used to manipulate and inspect CPU sets, see
72.BR sched_setaffinity (2)
73for details.
74.SH RETURN VALUE
75On success, these functions return 0;
76on error, they return a non-zero error number.
77.SH ERRORS
78.TP
79.B EFAULT
80A supplied memory address was invalid.
81.TP
82.B EINVAL
83.RB ( pthread_setaffinity_np ())
84The affinity bit mask
85.I mask
86contains no processors that are physically on the system.
87.TP
88.BR EINVAL
89.RB ( pthread_setaffinity_np ())
90.I cpuset
91specified a CPU that was outside the range
92permitted by the kernel data type
93.\" cpumask_t
94used to represent CPU sets.
95.\" The raw sched_getaffinity() system call returns the size (in bytes)
96.\" of the cpumask_t type.
97This range is determined by the kernel configuration option
98.BR CONFIG_NR_CPUS .
99.TP
100.B EINVAL
ae107f58 101.RB ( pthread_getaffinity_np ())
5655765f
MK
102.I cpusetsize
103is smaller than the size of the affinity mask used by the kernel.
104.TP
105.B ESRCH
106There is no thread matching
107.IR thread
108(e.g., perhaps that thread has already terminated and been joined).
109.SH VERSIONS
110These functions are provided by glibc since version 2.3.4.
111.SH CONFORMING TO
c94081aa
MK
112These functions are non-standard GNU extensions;
113hence the suffix "_np" (non-portable) in the names.
5655765f
MK
114.SH NOTES
115These functions are implemented on top of the
116.BR sched_setaffinity (2)
117and
118.BR sched_getaffinity (2)
119system calls.
120
121In glibc 2.3.3 only,
122versions of these functions were provided that did not have a
123.I cpusetsize
124argument.
125Instead the CPU set size given to the underlying system calls was always
126.IR sizeof(cpu_set_t) .
127
128A new thread created by
129.BR pthread_create ()
130inherits a copy of its creator's CPU affinity mask.
131.SH EXAMPLE
132In the following program, the main thread uses
133.BR pthread_setaffinity_np ()
134to set its CPU affinity mask to include CPUs 0 to 7
135(which may not all be available on the system),
136and then calls
137.BR pthread_getaffinity_np ()
138to check the resulting CPU affinity mask of the thread.
139
140.nf
141#define _GNU_SOURCE
142#include <pthread.h>
143#include <stdio.h>
144#include <stdlib.h>
145#include <errno.h>
146
940c8ce2
MK
147#define handle_error_en(en, msg) \\
148 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
149
5655765f
MK
150int
151main(int argc, char *argv[])
152{
153 int s, j;
154 cpu_set_t cpuset;
155 pthread_t thread;
156
157 thread = pthread_self();
158
159 /* Set affinity mask to include CPUs 0 to 7 */
160
161 CPU_ZERO(&cpuset);
162 for (j = 0; j < 8; j++)
163 CPU_SET(j, &cpuset);
164
165 s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
166 if (s != 0)
940c8ce2 167 handle_error_en(s, "pthread_setaffinity_np");
5655765f
MK
168
169 /* Check the actual affinity mask assigned to the thread */
170
171 s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
172 if (s != 0)
940c8ce2 173 handle_error_en(s, "pthread_getaffinity_np");
5655765f
MK
174
175 printf("Set returned by pthread_getaffinity_np() contained:\\n");
176 for (j = 0; j < CPU_SETSIZE; j++)
177 if (CPU_ISSET(j, &cpuset))
178 printf(" CPU %d\\n", j);
179
180 exit(EXIT_SUCCESS);
181}
182.fi
183.SH SEE ALSO
cbdc74c9 184.BR sched_getcpu (3),
5655765f 185.BR sched_setaffinity (2),
d67ec7da
MK
186.BR sched_setscheduler (2),
187.BR pthread_attr_setaffinity_np (3),
e7d2bb65 188.BR pthread_self (3),
5655765f
MK
189.BR cpuset (7),
190.BR pthreads (7)