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.
  • I have accepted your answer. However, this seems to work only when done from command line. If I do this using exec, it does not work. I have posted a new question for that - please see stackoverflow/69255696
    – R71
    Sep 20 at 14:08
  • More convenient link to @R71's new question: stackoverflow.com/questions/69255696
    – trlkly
    Sep 21 at 8:42
       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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.