1pthread_setaffinity_np(3)  Library Functions 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

LIBRARY

10       POSIX threads library (libpthread, -lpthread)
11

SYNOPSIS

13       #define _GNU_SOURCE             /* See feature_test_macros(7) */
14       #include <pthread.h>
15
16       int pthread_setaffinity_np(pthread_t thread, size_t cpusetsize,
17                                  const cpu_set_t *cpuset);
18       int pthread_getaffinity_np(pthread_t thread, size_t cpusetsize,
19                                  cpu_set_t *cpuset);
20

DESCRIPTION

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

RETURN VALUE

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

ERRORS

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

ATTRIBUTES

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

STANDARDS

73       GNU; hence the suffix "_np" (nonportable) in the names.
74

HISTORY

76       glibc 2.3.4.
77
78       In glibc 2.3.3 only, versions of these functions were provided that did
79       not have a cpusetsize argument.  Instead the CPU set size given to  the
80       underlying system calls was always sizeof(cpu_set_t).
81

NOTES

83       After  a call to pthread_setaffinity_np(), the set of CPUs on which the
84       thread will actually run is the intersection of the  set  specified  in
85       the cpuset argument and the set of CPUs actually present on the system.
86       The system may further restrict the set of CPUs  on  which  the  thread
87       runs  if  the  "cpuset" mechanism described in cpuset(7) is being used.
88       These restrictions on the actual set of CPUs on which the  thread  will
89       run are silently imposed by the kernel.
90
91       These  functions are implemented on top of the sched_setaffinity(2) and
92       sched_getaffinity(2) system calls.
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 <err.h>
105       #include <errno.h>
106       #include <pthread.h>
107       #include <stdio.h>
108       #include <stdlib.h>
109
110       int
111       main(void)
112       {
113           int s;
114           cpu_set_t cpuset;
115           pthread_t thread;
116
117           thread = pthread_self();
118
119           /* Set affinity mask to include CPUs 0 to 7. */
120
121           CPU_ZERO(&cpuset);
122           for (size_t j = 0; j < 8; j++)
123               CPU_SET(j, &cpuset);
124
125           s = pthread_setaffinity_np(thread, sizeof(cpuset), &cpuset);
126           if (s != 0)
127               errc(EXIT_FAILURE, s, "pthread_setaffinity_np");
128
129           /* Check the actual affinity mask assigned to the thread. */
130
131           s = pthread_getaffinity_np(thread, sizeof(cpuset), &cpuset);
132           if (s != 0)
133               errc(EXIT_FAILURE, s, "pthread_getaffinity_np");
134
135           printf("Set returned by pthread_getaffinity_np() contained:\n");
136           for (size_t j = 0; j < CPU_SETSIZE; j++)
137               if (CPU_ISSET(j, &cpuset))
138                   printf("    CPU %zu\n", j);
139
140           exit(EXIT_SUCCESS);
141       }
142

SEE ALSO

144       sched_setaffinity(2),    CPU_SET(3),    pthread_attr_setaffinity_np(3),
145       pthread_self(3), sched_getcpu(3), cpuset(7), pthreads(7), sched(7)
146
147
148
149Linux man-pages 6.04              2023-03-30         pthread_setaffinity_np(3)
Impressum