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 given PID. all of them. the whole squad. ๐Ÿซถ
  • -c, --cpu-list โ€” Interpret mask as 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 existing PID and do not launch a new task. If PID is zero, then operate on the taskset process. (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: -pc grouped, 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 ยฉ 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