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

NAME

6       sched_setaffinity,  sched_getaffinity  -  set  and  get  a thread's CPU
7       affinity mask
8

SYNOPSIS

10       #define _GNU_SOURCE             /* See feature_test_macros(7) */
11       #include <sched.h>
12
13       int sched_setaffinity(pid_t pid, size_t cpusetsize,
14                             const cpu_set_t *mask);
15
16       int sched_getaffinity(pid_t pid, size_t cpusetsize,
17                             cpu_set_t *mask);
18

DESCRIPTION

20       A thread's CPU affinity mask determines the set of CPUs on which it  is
21       eligible  to run.  On a multiprocessor system, setting the CPU affinity
22       mask can be used to obtain performance benefits.  For example, by dedi‐
23       cating  one CPU to a particular thread (i.e., setting the affinity mask
24       of that thread to specify a single CPU, and setting the  affinity  mask
25       of  all  other  threads  to exclude that CPU), it is possible to ensure
26       maximum execution speed for that thread.  Restricting a thread  to  run
27       on  a  single  CPU also avoids the performance cost caused by the cache
28       invalidation that occurs when a thread ceases to execute on one CPU and
29       then recommences execution on a different CPU.
30
31       A  CPU  affinity mask is represented by the cpu_set_t structure, a "CPU
32       set", pointed to by mask.  A set of macros for manipulating CPU sets is
33       described in CPU_SET(3).
34
35       sched_setaffinity()  sets  the CPU affinity mask of the thread whose ID
36       is pid to the value specified by mask.  If pid is zero, then the  call‐
37       ing  thread  is used.  The argument cpusetsize is the length (in bytes)
38       of the data pointed to by mask.  Normally this argument would be speci‐
39       fied as sizeof(cpu_set_t).
40
41       If  the  thread specified by pid is not currently running on one of the
42       CPUs specified in mask, then that thread is migrated to one of the CPUs
43       specified in mask.
44
45       sched_getaffinity()  writes the affinity mask of the thread whose ID is
46       pid into the cpu_set_t structure pointed to by  mask.   The  cpusetsize
47       argument  specifies  the size (in bytes) of mask.  If pid is zero, then
48       the mask of the calling thread is returned.
49

RETURN VALUE

51       On success, sched_setaffinity() and sched_getaffinity() return  0.   On
52       error, -1 is returned, and errno is set appropriately.
53

ERRORS

55       EFAULT A supplied memory address was invalid.
56
57       EINVAL The  affinity bit mask mask contains no processors that are cur‐
58              rently physically on the system  and  permitted  to  the  thread
59              according  to  any  restrictions  that  may be imposed by cpuset
60              cgroups or the "cpuset" mechanism described in cpuset(7).
61
62       EINVAL (sched_getaffinity()   and,    in    kernels    before    2.6.9,
63              sched_setaffinity())  cpusetsize is smaller than the size of the
64              affinity mask used by the kernel.
65
66       EPERM  (sched_setaffinity()) The calling thread does not have appropri‐
67              ate  privileges.  The caller needs an ef