1SCHED_SETAFFINITY(2) Linux Programmer's Manual SCHED_SETAFFINITY(2)
2
3
4
6 sched_setaffinity, sched_getaffinity - set and get a process's CPU
7 affinity mask
8
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 cpu_set_t *mask);
15
16 int sched_getaffinity(pid_t pid, size_t cpusetsize,
17 cpu_set_t *mask);
18
20 A process'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 process (i.e., setting the affinity mask
24 of that process to specify a single CPU, and setting the affinity mask
25 of all other processes to exclude that CPU), it is possible to ensure
26 maximum execution speed for that process. Restricting a process to run
27 on a single CPU also avoids the performance cost caused by the cache
28 invalidation that occurs when a process ceases to execute on one CPU
29 and 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 process whose ID
36 is pid to the value specified by mask. If pid is zero, then the call‐
37 ing process 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 process specified by pid is not currently running on one of the
42 CPUs specified in mask, then that process is migrated to one of the
43 CPUs specified in mask.
44
45 sched_getaffinity() writes the affinity mask of the process 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 process is returned.
49
51 On success, sched_setaffinity() and sched_getaffinity() return 0. On
52 error, -1 is returned, and errno is set appropriately.
53
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 process
59 according to any restrictions that may be imposed by the
60 "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 process does not have appro‐
67 priate privileges. The caller needs an effective user ID equal
68 to the real user ID or effective user ID of the process identi‐
69 fied by pid, or it must possess the CAP_SYS_NICE capability.
70
71 ESRCH The process whose ID is pid could not be found.
72
74 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
75 The system call wrappers were introduced in glibc 2.3. Initially, the
76 glibc interfaces included a cpusetsize argument, typed as unsigned int.
77 In glibc 2.3.3, the cpusetsize argument was removed, but was then
78 restored in glibc 2.3.4, with type size_t.
79
81 These system calls are Linux-specific.
82
84 After a call to sched_setaffinity(), the set of CPUs on which the
85 process will actually run is the intersection of the set specified in
86 the mask argument and the set of CPUs actually present on the system.
87 The system may further restrict the set of CPUs on which the process
88 runs if the "cpuset" mechanism described in cpuset(7) is being used.
89 These restrictions on the actual set of CPUs on which the process will
90 run are silently imposed by the kernel.
91
92 sched_setscheduler(2) has a description of the Linux scheduling scheme.
93
94 The affinity mask is actually a per-thread attribute that can be
95 adjusted independently for each of the threads in a thread group. The
96 value returned from a call to gettid(2) can be passed in the argument
97 pid. Specifying pid as 0 will set the attribute for the calling
98 thread, and passing the value returned from a call to getpid(2) will
99 set the attribute for the main thread of the thread group. (If you are
100 using the POSIX threads API, then use pthread_setaffinity_np(3) instead
101 of sched_setaffinity().)
102
103 A child created via fork(2) inherits its parent's CPU affinity mask.
104 The affinity mask is preserved across an execve(2).
105
106 This manual page describes the glibc interface for the CPU affinity
107 calls. The actual system call interface is slightly different, with
108 the mask being typed as unsigned long *, reflecting the fact that the
109 underlying implementation of CPU sets is a simple bit mask. On suc‐
110 cess, the raw sched_getaffinity() system call returns the size (in
111 bytes) of the cpumask_t data type that is used internally by the kernel
112 to represent the CPU set bit mask.
113
115 taskset(1), clone(2), getcpu(2), getpriority(2), gettid(2), nice(2),
116 sched_get_priority_max(2), sched_get_priority_min(2),
117 sched_getscheduler(2), sched_setscheduler(2), setpriority(2),
118 CPU_SET(3), pthread_setaffinity_np(3), sched_getcpu(3),
119 capabilities(7), cpuset(7)
120
122 This page is part of release 3.53 of the Linux man-pages project. A
123 description of the project, and information about reporting bugs, can
124 be found at http://www.kernel.org/doc/man-pages/.
125
126
127
128Linux 2013-02-11 SCHED_SETAFFINITY(2)