1PTHREAD_SETAFFINITY_NP(3)  Linux Programmer's Manual PTHREAD_SETAFFINITY_NP(3)
2
3
4

NAME

6       pthread_setaffinity_np,  pthread_getaffinity_np  - set/get CPU affinity
7       of a thread
8

SYNOPSIS

10       #define _GNU_SOURCE             /* See feature_test_macros(7) */
11       #include <pthread.h>
12
13       int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
14                                  const cpu_set_t *cpuset);
15       int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
16                                  cpu_set_t *cpuset);
17
18       Compile and link with -pthread.
19

DESCRIPTION

21       The pthread_setaffinity_np() function sets the CPU affinity mask of the
22       thread thread to the CPU set pointed to by cpuset.  If the call is suc‐
23       cessful, and the thread is not currently running on one of the CPUs  in
24       cpuset, then it is migrated to one of those CPUs.
25
26       The  pthread_getaffinity_np() function returns the CPU affinity mask of
27       the thread thread in the buffer pointed to by cpuset.
28
29       For more details on CPU affinity masks, see sched_setaffinity(2).   For
30       a  description  of  a  set of macros that can be used to manipulate and
31       inspect CPU sets, see CPU_SET(3).
32
33       The argument cpusetsize is the length (in bytes) of the buffer  pointed
34       to   by  cpuset.   Typically,  this  argument  would  be  specified  as
35       sizeof(cpu_set_t).  (It may be some other value, if  using  the  macros
36       described in CPU_SET(3) for dynamically allocating a CPU set.)
37

RETURN VALUE

39       On  success,  these functions return 0; on error, they return a nonzero
40       error number.
41

ERRORS

43       EFAULT A supplied memory address was invalid.
44
45       EINVAL (pthread_setaffinity_np()) The affinity bit mask  mask  contains
46              no  processors  that  are currently physically on the system and
47              permitted to the thread according to any restrictions  that  may
48              be imposed by the "cpuset" mechanism described in cpuset(7).
49
50       EINVAL (pthread_setaffinity_np())  cpuset specified a CPU that was out‐
51              side the set supported by the kernel.  (The kernel configuration
52              option  CONFIG_NR_CPUS defines the range of the set supported by
53              the kernel data type used to represent CPU sets.)
54
55       EINVAL (pthread_getaffinity_np()) cpusetsize is smaller than  the  size
56              of the affinity mask used by the kernel.
57
58       ESRCH  No thread with the ID thread could be found.
59

VERSIONS

61       These functions are provided by glibc since version 2.3.4.
62

ATTRIBUTES

64       For   an   explanation   of   the  terms  used  in  this  section,  see
65       attributes(7).
66
67       ┌──────────────────────────┬───────────────┬─────────┐
68Interface                 Attribute     Value   
69       ├──────────────────────────┼───────────────┼─────────┤
70pthread_setaffinity_np(), │ Thread safety │ MT-Safe │
71pthread_getaffinity_np()  │               │         │
72       └──────────────────────────┴───────────────┴─────────┘

CONFORMING TO

74       These  functions are nonstandard GNU extensions; hence the suffix "_np"
75       (nonportable) in the names.
76

NOTES

78       After a call to pthread_setaffinity_np(), the set of CPUs on which  the
79       thread  will  actually  run is the intersection of the set specified in
80       the cpuset argument and the set of CPUs actually present on the system.
81       The  system  may  further  restrict the set of CPUs on which the thread
82       runs if the "cpuset" mechanism described in cpuset(7)  is  being  used.
83       These  restrictions  on the actual set of CPUs on which the thread will
84       run are silently imposed by the kernel.
85
86       These functions are implemented on top of the sched_setaffinity(2)  and
87       sched_getaffinity(2) system calls.
88
89       In glibc 2.3.3 only, versions of these functions were provided that did
90       not have a cpusetsize argument.  Instead the CPU set size given to  the
91       underlying system calls was always sizeof(cpu_set_t).
92
93       A  new  thread created by pthread_create(3) inherits a copy of its cre‐
94       ator's CPU affinity mask.
95

EXAMPLE

97       In the following program, the main thread uses pthread_setaffinity_np()
98       to  set its CPU affinity mask to include CPUs 0 to 7 (which may not all
99       be available on the system), and then calls pthread_getaffinity_np() to
100       check the resulting CPU affinity mask of the thread.
101
102       #define _GNU_SOURCE
103       #include <pthread.h>
104       #include <stdio.h>
105       #include <stdlib.h>
106       #include <errno.h>
107
108       #define handle_error_en(en, msg) \
109               do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
110
111       int
112       main(int argc, char *argv[])
113       {
114           int s, j;
115           cpu_set_t cpuset;
116           pthread_t thread;
117
118           thread = pthread_self();
119
120           /* Set affinity mask to include CPUs 0 to 7 */
121
122           CPU_ZERO(&cpuset);
123           for (j = 0; j < 8; j++)
124               CPU_SET(j, &cpuset);
125
126           s = pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
127           if (s != 0)
128               handle_error_en(s, "pthread_setaffinity_np");
129
130           /* Check the actual affinity mask assigned to the thread */
131
132           s = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
133           if (s != 0)
134               handle_error_en(s, "pthread_getaffinity_np");
135
136           printf("Set returned by pthread_getaffinity_np() contained:\n");
137           for (j = 0; j < CPU_SETSIZE; j++)
138               if (CPU_ISSET(j, &cpuset))
139                   printf("    CPU %d\n", j);
140
141           exit(EXIT_SUCCESS);
142       }
143

SEE ALSO

145       sched_setaffinity(2),    CPU_SET(3),    pthread_attr_setaffinity_np(3),
146       pthread_self(3), sched_getcpu(3), cpuset(7), pthreads(7), sched(7)
147

COLOPHON

149       This page is part of release 5.02 of the Linux  man-pages  project.   A
150       description  of  the project, information about reporting bugs, and the
151       latest    version    of    this    page,    can     be     found     at
152       https://www.kernel.org/doc/man-pages/.
153
154
155
156Linux                             2019-03-06         PTHREAD_SETAFFINITY_NP(3)
Impressum