1TASKSET(1) User Commands TASKSET(1)
2
3
4
6 taskset - set or retrieve a process's CPU affinity
7
9 taskset [options] mask command [argument...]
10
11 taskset [options] -p [mask] pid
12
14 The taskset command is used to set or retrieve the CPU affinity of a
15 running process given its pid, or to launch a new command with a given
16 CPU affinity. CPU affinity is a scheduler property that "bonds" a
17 process to a given set of CPUs on the system. The Linux scheduler will
18 honor the given CPU affinity and the process will not run on any other
19 CPUs. Note that the Linux scheduler also supports natural CPU affinity:
20 the scheduler attempts to keep processes on the same CPU as long as
21 practical for performance reasons. Therefore, forcing a specific CPU
22 affinity is useful only in certain applications. The affinity of some
23 processes like kernel per-CPU threads cannot be set.
24
25 The CPU affinity is represented as a bitmask, with the lowest order bit
26 corresponding to the first logical CPU and the highest order bit
27 corresponding to the last logical CPU. Not all CPUs may exist on a
28 given system but a mask may specify more CPUs than are present. A
29 retrieved mask will reflect only the bits that correspond to CPUs
30 physically on the system. If an invalid mask is given (i.e., one that
31 corresponds to no valid CPUs on the current system) an error is
32 returned. The masks may be specified in hexadecimal (with or without a
33 leading "0x"), or as a CPU list with the --cpu-list option. For
34 example,
35
36 0x00000001
37 is processor #0,
38
39 0x00000003
40 is processors #0 and #1,
41
42 FFFFFFFF
43 is processors #0 through #31,
44
45 0x32
46 is processors #1, #4, and #5,
47
48 --cpu-list 0-2,6
49 is processors #0, #1, #2, and #6.
50
51 --cpu-list 0-10:2
52 is processors #0, #2, #4, #6, #8 and #10. The suffix ":N" specifies
53 stride in the range, for example 0-10:3 is interpreted as 0,3,6,9
54 list.
55
56 When taskset returns, it is guaranteed that the given program has been
57 scheduled to a legal CPU.
58
60 -a, --all-tasks
61 Set or retrieve the CPU affinity of all the tasks (threads) for a
62 given PID.
63
64 -c, --cpu-list
65 Interpret mask as numerical list of processors instead of a
66 bitmask. Numbers are separated by commas and may include ranges.
67 For example: 0,5,8-11.
68
69 -p, --pid
70 Operate on an existing PID and do not launch a new task.
71
72 -h, --help
73 Display help text and exit.
74
75 -V, --version
76 Print version and exit.
77
79 The default behavior is to run a new command with a given affinity
80 mask:
81 taskset mask command [arguments]
82
83 You can also retrieve the CPU affinity of an existing task:
84 taskset -p pid
85
86 Or set it:
87 taskset -p mask pid
88
89 When a cpu-list is specified for an existing process, the -p and -c
90 options must be grouped together:
91 taskset -pc cpu-list pid
92
93 The --cpu-list form is applicable only for launching new commands:
94 taskset --cpu-list cpu-list command
95
97 A user can change the CPU affinity of a process belonging to the same
98 user. A user must possess CAP_SYS_NICE to change the CPU affinity of a
99 process belonging to another user. A user can retrieve the affinity
100 mask of any process.
101
103 taskset returns 0 in its affinity-getting mode as long as the provided
104 PID exists.
105
106 taskset returns 0 in its affinity-setting mode as long as the
107 underlying sched_setaffinity(2) system call does. The success of the
108 command does not guarantee that the specified thread has actually
109 migrated to the indicated CPU(s), but only that the thread will not
110 migrate to a CPU outside the new affinity mask. For example, the
111 affinity of the kernel thread kswapd can be set, but the thread may not
112 immediately migrate and is not guaranteed to ever do so:
113
114 $ ps ax -o comm,psr,pid | grep kswapd
115 kswapd0 4 82
116 $ sudo taskset -p 1 82
117 pid 82’s current affinity mask: 1
118 pid 82’s new affinity mask: 1
119 $ echo $?
120 0
121 $ ps ax -o comm,psr,pid | grep kswapd
122 kswapd0 4 82
123 $ taskset -p 82
124 pid 82’s current affinity mask: 1
125
126 In contrast, when the user specifies an illegal affinity, taskset will
127 print an error and return 1:
128
129 $ ps ax -o comm,psr,pid | grep ksoftirqd/0
130 ksoftirqd/0 0 14
131 $ sudo taskset -p 1 14
132 pid 14’s current affinity mask: 1
133 taskset: failed to set pid 14’s affinity: Invalid argument
134 $ echo $?
135 1
136
138 Written by Robert M. Love.
139
141 Copyright © 2004 Robert M. Love. This is free software; see the source
142 for copying conditions. There is NO warranty; not even for
143 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
144
146 chrt(1), nice(1), renice(1), sched_getaffinity(2), sched_setaffinity(2)
147
148 See sched(7) for a description of the Linux scheduling scheme.
149
151 For bug reports, use the issue tracker at
152 https://github.com/util-linux/util-linux/issues.
153
155 The taskset command is part of the util-linux package which can be
156 downloaded from Linux Kernel Archive
157 <https://www.kernel.org/pub/linux/utils/util-linux/>.
158
159
160
161util-linux 2.39.2 2023-06-14 TASKSET(1)