taskset

process managementLinux
The taskset command is one of the most frequently used commands in Linux/Unix-like operating systems. taskset Set or retrieve a process's CPU affinity

Quick Reference

Command Name:

taskset

Category:

process management

Platform:

Linux

Basic Usage:

taskset [options] [arguments]

Common Use Cases

    Syntax

    taskset [options] mask command [argument...]
    taskset [options] -p [mask] pid

    Options

    Option Description
    -a, --all-tasks Set or retrieve the CPU affinity of all tasks (threads) of the specified process
    -c, --cpu-list Specify CPUs by a comma-separated list or range of CPU numbers instead of a mask
    -p, --pid Operate on an existing process instead of launching a new command
    -h, --help Display help information and exit
    -V, --version Display version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the taskset command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    # Start a new process with CPU affinity set to CPU 0 taskset 0x1 command arg1 arg2
    # Set CPU affinity of an existing process to CPU 0 and CPU 1 taskset -p 0x3 1234
    # View the current CPU affinity of a process taskset -p 1234
    # Advanced Examples Advanced
    # Start a process on specific CPUs using hexadecimal mask taskset 0x5 ./cpu-intensive-process # (0x5 = binary 0101, which means CPUs 0 and 2)
    # Start a process on specific CPUs using decimal mask taskset 10 ./cpu-intensive-process # (10 = binary 1010, which means CPUs 1 and 3) # Run a process on all CPUs except CPU 0 taskset -c 1-7 ./application # (assuming 8-core system) # Set affinity for all threads of a process taskset -ap 0x3 1234 # Set CPU affinity using CPU list notation taskset -c 0,2,4 ./program # Set CPU affinity to a range of CPUs taskset -c 0-3 ./program # Move a running process to a different CPU taskset -p 0x8 $(pgrep process_name) # View CPU affinity in list format taskset -cp 1234 # Running process on odd-numbered CPUs taskset -c 1,3,5,7 ./program # Running process on even-numbered CPUs taskset -c 0,2,4,6 ./program # Isolate two applications on different CPUs taskset 0x3 ./application1 & taskset 0xC ./application2 & # (0x3 = CPUs 0,1 and 0xC = CPUs 2,3)

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `taskset` command is a Linux utility that allows users to control CPU affinity, which is the binding of a process to specific CPU cores. This tool is part of the util-linux package and serves as an interface to the `sched_setaffinity()` system call. CPU affinity is a technique used to improve performance by ensuring that a process runs on the same CPU or set of CPUs throughout its execution. By constraining a process to specific cores, you can: 1. **Improve Cache Efficiency**: Processes running on the same CPU can benefit from cache locality, as data loaded into CPU caches remains available for subsequent operations. 2. **Reduce CPU Migration Overhead**: Preventing the kernel from migrating processes between CPUs eliminates the overhead associated with context switching and cache invalidation. 3. **Isolate Workloads**: Critical applications can be pinned to dedicated cores to prevent interference from other processes. 4. **Optimize NUMA Systems**: On Non-Uniform Memory Access architectures, processes can be bound to CPUs that have the most efficient access to required memory regions. The `taskset` command supports two primary operations: 1. **Launching a new process with specific CPU affinity**: In this mode, you specify the CPU mask followed by the command to execute and its arguments. 2. **Modifying or viewing the CPU affinity of an existing process**: Using the `-p` option, you can specify a process ID to change or view its CPU affinity. CPU affinity is expressed as a bitmask, which can be specified in hexadecimal (prefixed with 0x), decimal, or octal (prefixed with 0) format. Each bit in the mask corresponds to a CPU core, with bit 0 representing CPU 0, bit 1 representing CPU 1, and so on. For example, a mask of 0x5 (binary 0101) would restrict a process to CPUs 0 and 2. For convenience, the `-c` option allows you to specify CPUs using a comma-separated list or range notation (e.g., 0,2,4-7) instead of calculating the bitmask manually. The `-a` option extends the operation to all threads of a multi-threaded process, which is important because CPU affinity is a per-thread attribute in Linux. While `taskset` is a powerful tool for performance tuning, it should be used judiciously. Restricting processes to too few CPUs can limit parallelism and throughput, while inappropriate CPU assignments on NUMA systems can lead to remote memory access penalties. Therefore, careful consideration of workload characteristics and system architecture is necessary when applying CPU affinity constraints.

    Related Commands

    These commands are frequently used alongside taskset or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the taskset command works in different scenarios.

    $ taskset
    View All Commands