Linux

Linux taskset Command Tutorial For Beginners [ How-To]

Insight: Linux taskset Command Tutorial For Beginners [ How-To]

taskset command in Linux is used to set or retrieve a process’s CPU affinity. CPU affinity is a scheduler property that “bonds” a process to a given set of CPUs on the system. It can help you to command which CPU core should be used to a run a specific process.

Ubuntu Linux Kernel

Linux taskset Command Tutorial For Beginners [ How-To]

Synopsis of taskset

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

 

Options

-p, –pid
operate on an existing PID and not launch a new task
-c, –cpu-list
specify a numerical list of processors instead of a bitmask. The list may contain multiple items, separated by comma, and ranges. For example, 0,5,7,9-11.
-h, –help
display usage information and exit
-V, –version
output version information and exit

 

 

Example:

If you want taskset to display CPU affinity of an already running specifi process, then you need to run the following command.

taskset -p [PID]

PID referes to the ID of the specific process whose CPU affinity you want to obtain:

taskset -p 8564

Here, we are obtaining the CPU affinity of process with the id of 8564. Following is the output:

pid 8564's current affinity mask: f

Above you might see something interesting. There is a hexadecimal value ‘f’ in above output which means that the specific process can run on any of the 4 processor cores: 0,1,2,3.

Meanwhile, To obtain the ouput in CPU range then execute the following command:

taskset -cp 8564

Following is the output:

pid 8564's current affinity list: 0-3

Let’s check another command to assign a range of CPUs while changing affinity. Run the following command to assign a range of CPUs while changing affinity:

READ THIS ALSO  Beginner's Guide To Delete Or Remove A User On Ubuntu

 

taskset -cp 0,2 8564

Following is the output:

pid 8564's current affinity list: 0
pid 8564's new affinity list: 0,2

Now, Command below will show you the method to launch a process with predefined CPU affinity:

taskset 0xa firefox

 

 

Note: Let us know if there is any error in this article.