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 in‐
31       spect 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 at‐
65       tributes(7).
66
67       ┌────────────────────────────────────────────┬───────────────┬─────────┐
68Interface                                   Attribute     Value   
69       ├────────────────────────────────────────────┼───────────────┼─────────┤
70pthread_setaffinity_np(),                   │ Thread safety │ MT-Safe │
71pthread_getaffinity_np()                    │               │         │
72       └────────────────────────────────────────────┴───────────────┴─────────┘
73

CONFORMING TO

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

NOTES

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

EXAMPLES

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

SEE ALSO

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

COLOPHON

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