Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

taskset sets or reads the CPU affinity of a Linux task, or starts a command restricted to selected logical CPUs. Use a hexadecimal mask such as 0x3, or the clearer --cpu-list form such as 0-2,6. A successful change restricts where the task may run; it does not promise that the thread has already moved to a new CPU.

What the Linux taskset command does

taskset controls CPU affinity, a scheduler property that limits a task to a set of logical CPUs. Once an affinity mask is accepted, Linux will not run that task outside the selected set. Without an explicit restriction, Linux already uses natural affinity and generally tries to keep a task on its current CPU when practical, so pinning is most useful for workloads with a specific placement requirement.

The command uses the kernel’s sched_setaffinity(2) interface. That interface is per-thread, which matters when a program has multiple threads.

Syntax at a glance

taskset [options] mask command [argument...]
taskset [options] -p [mask] pid
Form Purpose
taskset mask command Launch a command with the supplied affinity.
taskset --cpu-list list command Launch a command using explicit CPU numbers.
taskset -p pid Read the affinity of an existing task.
taskset -p mask pid Set the affinity of an existing task.
taskset -a ... Apply a read or set operation to all threads associated with the addressed PID.

Understanding the hexadecimal CPU mask

Unless you use --cpu-list (or -c), taskset expects a hexadecimal mask. The least-significant bit is logical CPU 0; the next bit is CPU 1, and so on. A set bit includes that CPU.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Command mask Selected logical CPUs Reason
0x1 CPU 0 Only bit 0 is set.
0x3 CPUs 0 and 1 Bits 0 and 1 are set.
0x32 CPUs 1, 4 and 5 The set bits correspond to those CPU numbers.

The mask describes logical CPUs, not necessarily physical cores. On systems with simultaneous multithreading, two logical CPUs may share one physical core.

Start a new command on selected CPUs

Use a hexadecimal mask

taskset 0x3 ./worker --input data.bin

This starts ./worker with access to logical CPUs 0 and 1. Arguments after the command are passed to that program.

Use readable CPU numbers

taskset --cpu-list 0-2,6 ./worker

The list accepts comma-separated CPUs and ranges. A range can include a stride: --cpu-list 0-10:2 selects CPUs 0, 2, 4, 6, 8 and 10. The short option for --cpu-list is -c.

Inspect or change an existing PID

Read the current affinity

taskset -p 1234

Replace 1234 with the process ID. Reading affinity is broadly permitted, including for processes you do not own.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Set a hexadecimal mask

taskset -p 0x3 1234

Set a CPU list

taskset -pc 0-2,6 1234

Here -p selects an existing PID and -c changes the mask syntax to a CPU list.

Processes, threads and the -a option

-p addresses an existing task rather than launching a command. Because Linux affinity is per-thread, changing one addressed task does not automatically change every thread in a multithreaded process.

Add -a (or --all-tasks) when the operation should cover all tasks, meaning all threads associated with that PID:

taskset -ap 0x3 1234
taskset -apc 0-2,6 1234

Use the non--a form when you intentionally want to target only the addressed task.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Permissions and common failures

  • You can normally change affinity for a process you own.
  • Changing another user’s process requires the Linux CAP_SYS_NICE capability. Run the command with an account or service configuration that has that capability when appropriate.
  • A mask or CPU list must name CPUs that are valid and available to the task. An invalid or disallowed selection causes the underlying affinity call to fail.
  • Check the PID and whether you intended one thread or all threads before retrying.

The command’s exit status in setting mode follows the underlying sched_setaffinity(2) call, so a nonzero status indicates that the kernel rejected the requested operation.

What a successful taskset change guarantees

When taskset returns success, the kernel has accepted the new restriction and the program is scheduled on a CPU allowed by that affinity. It does not guarantee immediate migration to a particular selected CPU. A running thread may remain on its current CPU if that CPU is still in the new mask; it simply cannot later run outside the mask.

When taskset is the right tool

Choose taskset when you need direct, per-task CPU affinity at launch time or after a process has started. Its main choices are launch-time versus post-launch control, hexadecimal masks versus readable CPU lists, one task versus all threads, and the privileges available to the caller.

Affinity alone does not provide cgroup-wide or system-wide resource controls. For those requirements, use an appropriate cgroup or service-level resource-management mechanism in addition to, or instead of, per-task affinity.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.