TASKSET(1)
๐ TASKSET(1) โ the taskset page, but make it cute ๐
okay bestie, welcome to taskset, a User Commands classic, living in the util-linux cinematic universe. pull up a chair, grab a boba, we're about to talk about which CPU your little process is allowed to live on. yes really. it's iconic. ๐
โจ NAME โจ
taskset โ set or retrieve a process's CPU affinity ๐ฆ
โจ SYNOPSIS โจ
like, here's the shape of it:
taskset [options] mask command [argument...]
taskset [options] -p [mask] pid
two moods. launch a new thing with a vibe, or go poke an existing thing. ๐
โจ DESCRIPTION โจ
so. the taskset command sets or retrieves the CPU affinity of a running process if you hand it a pid, OR it launches a whole new command with a given CPU affinity. either way, chef's kiss. ๐
and what even is CPU affinity? it's a scheduler property that "bonds" a process to a given set of CPUs on the system. the Linux scheduler will honor that affinity and the process will not run on any other CPUs. periodt.
but here's the tea โ โ the Linux scheduler also supports natural CPU affinity: it tries to keep processes on the same CPU as long as practical, for performance reasons. so forcing a specific CPU affinity is only useful in certain applications. not everything needs to be micromanaged, bestie. and heads up: the affinity of some processes, like kernel per-CPU threads, cannot be set. sorry. ๐
๐งฎ how the mask actually works
the CPU affinity is represented as a bitmask. lowest order bit = first logical CPU. highest order bit = last logical CPU. that's the whole vibe.
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 you give an invalid mask โ like one that corresponds to no valid CPUs on the current system โ an error is returned. ๐
masks can be given in hexadecimal (with or without a leading "0x"), or as a CPU list with the --cpu-list option. examples, because examples ate:
0x00000001โ is processor #0,0x00000003โ is processors #0 and #1,FFFFFFFFโ is processors #0 through #31,0x32โ is processors #1, #4, and #5,--cpu-list 0-2,6โ is processors #0, #1, #2, and #6.
and the fancy one:
--cpu-list 0-10:2โ 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 the 0,3,6,9 list. the way that math just works. ๐ง
๐ when taskset returns, it is guaranteed that the given program has been scheduled to a legal CPU. no cap.
โจ OPTIONS โจ
-a,--all-tasksโ Set or retrieve the CPU affinity of all the tasks (threads) for a givenPID. all of them. the whole squad. ๐ซถ-c,--cpu-listโ Interpretmaskas numerical list of processors instead of a bitmask. Numbers are separated by commas and may include ranges. For example:0,5,8-11.-p,--pidโ Operate on an existingPIDand do not launch a new task. IfPIDis zero, then operate on thetasksetprocess. (not me operating on myself ๐ญ)-h,--helpโ Display help text and exit.-V,--versionโ Display version and exit.
โจ USAGE โจ
default behavior is to run a new command with a given affinity mask:
taskset mask command [arguments]
you can also retrieve the CPU affinity of an existing task:
taskset -p pid
or set it:
taskset -p mask pid
when a cpu-list is specified for an existing process, the -p and -c options must be grouped together:
taskset -pc cpu-list pid
and the --cpu-list form is applicable only for launching new commands:
taskset --cpu-list cpu-list command
๐ธ lowkey important:-pcgrouped, not-p -c, when you're doing cpu-list on an existing process. write that down.
โจ PERMISSIONS โจ
a user can change the CPU affinity of a process belonging to the same user. ๐ซถ
a user must possess CAP_SYS_NICE to change the CPU affinity of a process belonging to another user. that's the boundary, bestie. respect it.
but โ a user can retrieve the affinity mask of any process. read access is free, it's giving open-book exam. ๐โจ
โจ EXIT STATUS โจ
taskset returns 0 in its affinity-getting mode as long as the provided PID exists.
taskset returns 0 in its affinity-setting mode as long as the underlying sched_setaffinity(2) system call does.
โ ๏ธ okay so real talk: the success of the command does not guarantee that the specified thread has actually migrated to the indicated CPU(s). it only guarantees 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. ๐ญ
watch how it goes:
$ ps ax -o comm,psr,pid | grep kswapd
kswapd0 4 82
$ sudo taskset -p 1 82
pid 82โs current affinity mask: 1
pid 82โs new affinity mask: 1
$ echo $?
0
$ ps ax -o comm,psr,pid | grep kswapd
kswapd0 4 82
$ taskset -p 82
pid 82โs current affinity mask: 1
see? set it, returned 0, and the thread is still chillin on CPU 4. it's giving "I'll get to it". ๐
in contrast, when the user specifies an illegal affinity, taskset will print an error and return 1:
$ ps ax -o comm,psr,pid | grep ksoftirqd/0
ksoftirqd/0 0 14
$ sudo taskset -p 1 14
pid 14โs current affinity mask: 1
taskset: failed to set pid 14โs affinity: Invalid argument
$ echo $?
1
yep. error printed, 1 returned. no notes. ๐
โจ EXAMPLES โจ
print the current CPU affinity as a list. slay:
$ taskset -pc 0
pid 1355988โs current affinity list: 0-47
โจ AUTHORS โจ
Written by Robert M. Love. ๐
โจ COPYRIGHT โจ
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.
๐ซถ that's the legal bit, keep it in your heart.
โจ SEE ALSO โจ
chrt(1), nice(1), renice(1), sched_getaffinity(2), sched_setaffinity(2)
See sched(7) for a description of the Linux scheduling scheme. ๐
โจ REPORTING BUGS โจ
For bug reports, use the issue tracker <https://github.com/util-linux/util-linux/issues>.
(fr fr, if something's broken, tell the maintainers, not your group chat. they actually fix things. ๐โจ)
โจ AVAILABILITY โจ
The taskset command is part of the util-linux package which can be downloaded from Linux Kernel Archive <https://www.kernel.org/pub/linux/utils/util-linux/>.
and that's the whole page bestie!! ๐ this one's from util-linux 2.42.4, dated 2026-09-21 โ go forth and pin your processes to a CPU, main character energy only ๐ ๐
๐ฆ where this page comes from
- arch rolling: util-linux 2.42.4-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4-Clause-UC AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND ISC AND LGPL-2.1-or-later AND LicenseRef-PublicDomain
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms