Mohan pd.
Mohan pd. Author of The Coders Blog. Also a professional fullstack javascript developer working with various tech company and client around world.

How to run any process on single core or set CPU affinity in Linux.

Feb 27, 2019 · 1 min
How to run any process on single core or set CPU affinity in Linux.

Linux user can set or retrieve a process’s CPU affinity from terminal. If you want to limit some process/program to only user single or more core then taskset can do it for you.

There are the application like VLC which only user single core by default while playing video though you can change the number of core to use in VLC setting. Recently, I was working project related to video playback in bigger screen and while experimenting with processor and its uses, the result show that cpu’s with lower processing power can play video with great performance while using only one core of CPU. While playing multiple video at the same time it might affect the performance.

Taskset is used to set or retrieve the CPU affinity of a running process given its pid, or to launch a new command with a given CPU affinity. CPU affinity is a scheduler property that “bonds” a process to a given set of CPUs on the system. The Linux scheduler will honor the given CPU affinity and the process will not run on any other CPUs.

taskset --cpu-list 0
This command will run the program on 0th core

taskset --cpu-list 0-2, 6
This command will run the program on 0, 1, 2 and 6th processor.

taskset -p
This will return current affinity mask.

Note that the Linux scheduler also supports natural CPU affinity: the scheduler attempts to keep processes on the same CPU as long as practical for performance reasons. Therefore, forcing a specific CPU affinity is useful only in certain applications.