Debian Codegraph @

schedutils/taskset.1

raw
1 '\" t
2 .\" Title: taskset
3 .\" Author: [see the "AUTHOR(S)" section]
4 .\" Generator: Asciidoctor 2.0.20
5 .\" Date: 2026-05-18
6 .\" Manual: User Commands
7 .\" Source: util-linux 2.42.1
8 .\" Language: English
9 .\"
10 .TH "TASKSET" "1" "2026-05-18" "util\-linux 2.42.1" "User Commands"
11 .ie \n(.g .ds Aq \(aq
12 .el .ds Aq '
13 .ss \n[.ss] 0
14 .nh
15 .ad l
16 .de URL
17 \fI\\$2\fP <\\$1>\\$3
18 ..
19 .als MTO URL
20 .if \n[.g] \{\
21 . mso www.tmac
22 . am URL
23 . ad l
24 . .
25 . am MTO
26 . ad l
27 . .
28 . LINKSTYLE blue R < >
29 .\}
30 .SH "NAME"
31 taskset \- set or retrieve a process\*(Aqs CPU affinity
32 .SH "SYNOPSIS"
33 .sp
34 \fBtaskset\fP [options] \fImask command\fP [\fIargument\fP...]
35 .sp
36 \fBtaskset\fP [options] \fB\-p\fP [\fImask\fP] \fIpid\fP
37 .SH "DESCRIPTION"
38 .sp
39 The \fBtaskset\fP command is used to set or retrieve the CPU affinity of a running process given its \fIpid\fP, or to launch a new \fIcommand\fP with a given CPU affinity. CPU affinity is a scheduler property that "bonds" a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs. Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications. The affinity of some processes like kernel per\-CPU threads cannot be set.
40 .sp
41 The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU. Not all CPUs may exist on a given system but a mask may specify more CPUs than are present. A retrieved mask will reflect only the bits that correspond to CPUs physically on the system. If an invalid mask is given (i.e., one that corresponds to no valid CPUs on the current system) an error is returned. The masks may be specified in hexadecimal (with or without a leading "0x"), or as a CPU list with the \fB\-\-cpu\-list\fP option. For example,
42 .sp
43 \fB0x00000001\fP
44 .RS 4
45 is processor #0,
46 .RE
47 .sp
48 \fB0x00000003\fP
49 .RS 4
50 is processors #0 and #1,
51 .RE
52 .sp
53 \fBFFFFFFFF\fP
54 .RS 4
55 is processors #0 through #31,
56 .RE
57 .sp
58 \fB0x32\fP
59 .RS 4
60 is processors #1, #4, and #5,
61 .RE
62 .sp
63 \fB\-\-cpu\-list 0\-2,6\fP
64 .RS 4
65 is processors #0, #1, #2, and #6.
66 .RE
67 .sp
68 \fB\-\-cpu\-list 0\-10:2\fP
69 .RS 4
70 is processors #0, #2, #4, #6, #8 and #10. The suffix ":N" specifies stride in the range, for example 0\-10:3 is interpreted as 0,3,6,9 list.
71 .RE
72 .sp
73 When \fBtaskset\fP returns, it is guaranteed that the given program has been scheduled to a legal CPU.
74 .SH "OPTIONS"
75 .sp
76 \fB\-a\fP, \fB\-\-all\-tasks\fP
77 .RS 4
78 Set or retrieve the CPU affinity of all the tasks (threads) for a given PID.
79 .RE
80 .sp
81 \fB\-c\fP, \fB\-\-cpu\-list\fP
82 .RS 4
83 Interpret \fImask\fP as numerical list of processors instead of a bitmask. Numbers are separated by commas and may include ranges. For example: \fB0,5,8\-11\fP.
84 .RE
85 .sp
86 \fB\-p\fP, \fB\-\-pid\fP
87 .RS 4
88 Operate on an existing PID and do not launch a new task.
89 If PID is zero, then operate on the \fBtaskset\fP process.
90 .RE
91 .sp
92 \fB\-h\fP, \fB\-\-help\fP
93 .RS 4
94 Display help text and exit.
95 .RE
96 .sp
97 \fB\-V\fP, \fB\-\-version\fP
98 .RS 4
99 Display version and exit.
100 .RE
101 .SH "USAGE"
102 .sp
103 The default behavior is to run a new command with a given affinity mask:
104 .RS 4
105 \fBtaskset\fP \fImask\fP \fIcommand\fP [\fIarguments\fP]
106 .RE
107 .sp
108 You can also retrieve the CPU affinity of an existing task:
109 .RS 4
110 \fBtaskset \-p\fP \fIpid\fP
111 .RE
112 .sp
113 Or set it:
114 .RS 4
115 \fBtaskset \-p\fP \fImask pid\fP
116 .RE
117 .sp
118 When a cpu\-list is specified for an existing process, the \fB\-p\fP and \fB\-c\fP options must be grouped together:
119 .RS 4
120 \fBtaskset \-pc\fP \fIcpu\-list pid\fP
121 .RE
122 .sp
123 The \fB\-\-cpu\-list\fP form is applicable only for launching new commands:
124 .RS 4
125 \fBtaskset \-\-cpu\-list\fP \fIcpu\-list command\fP
126 .RE
127 .SH "PERMISSIONS"
128 .sp
129 A user can change the CPU affinity of a process belonging to the same user. A user must possess \fBCAP_SYS_NICE\fP to change the CPU affinity of a process belonging to another user. A user can retrieve the affinity mask of any process.
130 .SH "EXIT STATUS"
131 .sp
132 \fBtaskset\fP returns 0 in its affinity\-getting mode as long as the provided PID exists.
133 .sp
134 \fBtaskset\fP returns 0 in its affinity\-setting mode as long as the underlying \fBsched_setaffinity\fP(2) system call does. The success of the command does not guarantee that the specified thread has actually migrated to the indicated CPU(s), but only that the thread will not migrate to a CPU outside the new affinity mask. For example, the affinity of the kernel thread kswapd can be set, but the thread may not immediately migrate and is not guaranteed to ever do so:
135 .sp
136 $ ps ax \-o comm,psr,pid | grep kswapd
137 .br
138 kswapd0 4 82
139 .br
140 $ sudo taskset \-p 1 82
141 .br
142 pid 82\(cqs current affinity mask: 1
143 .br
144 pid 82\(cqs new affinity mask: 1
145 .br
146 $ echo $?
147 .br
148 0
149 .br
150 $ ps ax \-o comm,psr,pid | grep kswapd
151 .br
152 kswapd0 4 82
153 .br
154 $ taskset \-p 82
155 .br
156 pid 82\(cqs current affinity mask: 1
157 .br
158 .sp
159 In contrast, when the user specifies an illegal affinity, taskset will print an error and return 1:
160 .sp
161 $ ps ax \-o comm,psr,pid | grep ksoftirqd/0
162 .br
163 ksoftirqd/0 0 14
164 .br
165 $ sudo taskset \-p 1 14
166 .br
167 pid 14\(cqs current affinity mask: 1
168 .br
169 taskset: failed to set pid 14\(cqs affinity: Invalid argument
170 .br
171 $ echo $?
172 .br
173 1
174 .br
175 .SH "EXAMPLES"
176 .sp
177 Print the current CPU affinity as a list.
178 .sp
179 $ taskset \-pc 0
180 .br
181 pid 1355988\(cqs current affinity list: 0\-47
182 .br
183 .SH "AUTHORS"
184 .sp
185 Written by Robert M. Love.
186 .SH "COPYRIGHT"
187 .sp
188 Copyright © 2004 Robert M. Love. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
189 .SH "SEE ALSO"
190 .sp
191 \fBchrt\fP(1),
192 \fBnice\fP(1),
193 \fBrenice\fP(1),
194 \fBsched_getaffinity\fP(2),
195 \fBsched_setaffinity\fP(2)
196 .sp
197 See \fBsched\fP(7) for a description of the Linux scheduling scheme.
198 .SH "REPORTING BUGS"
199 .sp
200 For bug reports, use the \c
201 .URL "https://github.com/util\-linux/util\-linux/issues" "issue tracker" "."
202 .SH "AVAILABILITY"
203 .sp
204 The \fBtaskset\fP command is part of the util\-linux package which can be downloaded from \c
205 .URL "https://www.kernel.org/pub/linux/utils/util\-linux/" "Linux Kernel Archive" "."