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
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() sets the CPU affinity mask of  the  thread
22       thread to the CPU set pointed to by cpuset.  If the call is successful,
23       and the thread is not currently running on one of the CPUs  in  cpuset,
24       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 non-zero
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

CONFORMING TO

64       These functions are non-standard GNU extensions; hence the suffix "_np"
65       (non-portable) in the names.
66

NOTES

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

EXAMPLE

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

SEE ALSO

135       sched_getcpu(3),      sched_setaffinity(2),      sched_setscheduler(2),
136       pthread_attr_setaffinity_np(3), pthread_self(3), cpuset(7), pthreads(7)
137

COLOPHON

139       This page is part of release 3.22 of the Linux  man-pages  project.   A
140       description  of  the project, and information about reporting bugs, can
141       be found at http://www.kernel.org/doc/man-pages/.
142
143
144
145Linux                             2008-11-27         PTHREAD_SETAFFINITY_NP(3)
Impressum