October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

Linux taskset Command: Set and Check CPU Affinity

Use Linux taskset to read or set CPU affinity for a running task, launch commands on selected logical CPUs, decode hexadecimal masks, and avoid common PID and thread-scope mistakes.
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.

Free tools Windows power users keep installed

One-click scans. No signup required.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.