How to allocate processor to process at startup

Linux allows to allocate a processor-core to a running process (ie, having a pid) using this command:

taskset -pc core pid

This assumes that the process is already running, and its pid is available, and we change the core that is assigned to it. So, the core will be switched the next time the process goes out of CPU during context switching or time sharing.

But is it possible to assign the core upfront, right when the process is being run? As far as I know, the answer is negative, but just wanted to confirm with the experts, just in case if I am missing something.

(I had asked this question on stackoverflow/69250190, but I was advised to post the question here.)


To assign the core when the process is being run, the process must be launched using the taskset command:

taskset [options] core-mask command [argument...]

The CPU affinity is represented as a bitmask, with the lowest order bit corresponding to the first logical CPU and the highest order bit corresponding to the last logical CPU.

For example, the following launches the command to run on the first two cores:

taskset 03 sshd -b 1024

It's also possible to specify the cores as a list using the -c parameter:

-c, --cpu-list
    Interpret mask as numerical list of processors instead of a
    bitmask. Numbers are separated by commas and may include
    ranges. For example: 0,5,8-11.

SYNOPSIS
       taskset [options] mask command [argument...]

       taskset [options] -p [mask] pid

The default mode (without the -p option) already is to accept the CPU mask and a command name. So you should be able to use, for example, taskset core /bin/whatever.