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

NAME

6       sched_setaffinity,   sched_getaffinity,  CPU_CLR,  CPU_ISSET,  CPU_SET,
7       CPU_ZERO - set and get a process's CPU affinity mask
8

SYNOPSIS

10       #include <sched.h>
11
12       int sched_setaffinity(pid_t pid, unsigned int cpusetsize,
13                             cpu_set_t *mask);
14
15       int sched_getaffinity(pid_t pid, unsigned int cpusetsize,
16                             cpu_set_t *mask);
17
18       void CPU_CLR(int cpu, cpu_set_t *set);
19       int CPU_ISSET(int cpu, cpu_set_t *set);
20       void CPU_SET(int cpu, cpu_set_t *set);
21       void CPU_ZERO(cpu_set_t *set);
22

DESCRIPTION

24       A process's CPU affinity mask determines the set of CPUs on which it is
25       eligible  to run.  On a multiprocessor system, setting the CPU affinity
26       mask can be used to obtain performance benefits.  For example, by dedi‐
27       cating one CPU to a particular process (i.e., setting the affinity mask
28       of that process to specify a single CPU, and setting the affinity  mask
29       of  all  other processes to exclude that CPU), it is possible to ensure
30       maximum execution speed for that process.  Restricting a process to run
31       on  a single CPU also prevents the performance cost caused by the cache
32       invalidation that occurs when a process ceases to execute  on  one  CPU
33       and then recommences execution on a different CPU.
34
35       A  CPU  affinity mask is represented by the cpu_set_t structure, a "CPU
36       set", pointed to by mask.  Four macros are provided to  manipulate  CPU
37       sets.   CPU_ZERO()  clears a set.  CPU_SET() and CPU_CLR() respectively
38       add and remove a given CPU from a set.  CPU_ISSET() tests to see  if  a
39       CPU  is  part  of  the  set;  this  is useful after sched_getaffinity()
40       returns.  The first available CPU on the system corresponds  to  a  cpu
41       value  of  0,  the next CPU corresponds to a cpu value of 1, and so on.
42       The constant CPU_SETSIZE (1024) specifies a value one greater than  the
43       maximum CPU number that can be stored in a CPU set.
44
45       sched_setaffinity()  sets the CPU affinity mask of the process whose ID
46       is pid to the value specified by mask.  If pid is zero, then the  call‐
47       ing  process is used.  The argument cpusetsize is the length (in bytes)
48       of the data pointed to by mask.  Normally this argument would be speci‐
49       fied as sizeof(cpu_set_t).
50
51       If  the process specified by pid is not currently running on one of the
52       CPUs specified in mask, then that process is migrated  to  one  of  the
53       CPUs specified in mask.
54
55       sched_getaffinity() writes the affinity mask of the process whose ID is
56       pid into the cpu_set_t structure pointed to by  mask.   The  cpusetsize
57       argument  specifies  the size (in bytes) of mask.  If pid is zero, then
58       the mask of the calling process is returned.
59

RETURN VALUE

61       On success, sched_setaffinity() and sched_getaffinity() return  0.   On
62       error, -1 is returned, and errno is set appropriately.
63

ERRORS

65       EFAULT A supplied memory address was invalid.
66
67       EINVAL The affinity bitmask mask contains no processors that are physi‐
68              cally on the system, or cpusetsize is smaller than the  size  of
69              the affinity mask used by the kernel.
70
71       EPERM  The  calling  process does not have appropriate privileges.  The
72              process calling sched_setaffinity() needs an effective  user  ID
73              equal to the user ID or effective user ID of the process identi‐
74              fied by pid, or it must possess the CAP_SYS_NICE capability.
75
76       ESRCH  The process whose ID is pid could not be found.
77

CONFORMING TO

79       These system calls are Linux specific.
80

NOTES

82       The affinity mask is  actually  a  per-thread  attribute  that  can  be
83       adjusted  independently for each of the threads in a thread group.  The
84       value returned from a call to gettid(2) can be passed in  the  argument
85       pid.
86
87       A  child  created  via fork(2) inherits its parent's CPU affinity mask.
88       The affinity mask is preserved across an execve(2).
89
90       This manual page describes the glibc interface  for  the  CPU  affinity
91       calls.   The  actual  system call interface is slightly different, with
92       the mask being typed as unsigned long *, reflecting that the fact  that
93       the underlying implementation of CPU sets is a simple bitmask.  On suc‐
94       cess, the raw sched_getaffinity() system  call  returns  the  size  (in
95       bytes) of the cpumask_t data type that is used internally by the kernel
96       to represent the CPU set bitmask.
97

HISTORY

99       The CPU affinity system calls were introduced in  Linux  kernel  2.5.8.
100       The  library  interfaces  were introduced in glibc 2.3.  Initially, the
101       glibc interfaces included a cpusetsize argument.  In glibc  2.3.3,  the
102       cpusetsize  argument  was  removed,  but  this argument was restored in
103       glibc 2.3.4.
104

SEE ALSO

106       clone(2),   getpriority(2),   gettid(2),   nice(2),    sched_get_prior‐
107       ity_max(2),      sched_get_priority_min(2),      sched_getscheduler(2),
108       sched_setscheduler(2), setpriority(2), capabilities(7)
109
110       sched_setscheduler(2) has a description of the Linux scheduling scheme.
111
112
113
114Linux                             2006-02-03              SCHED_SETAFFINITY(2)
Impressum