14

I want to set the cpu affinity of a process on linux when it is starting.

There are methods like sched_setaffinity and taskset, but they need the processid of the process. They may cause potential migration like a process was started on a core but after the use of sched_setaffinity/taskset, they were migrated to another core.

What I want to do is to start a new process on a specific core from the beginning.

ajay saini
  • 305
  • 1
  • 2
  • 8
  • Why do you want to do that? Often, the kernel is better than you to choose a core to run some task... – Basile Starynkevitch Mar 16 '13 at 16:02
  • I am working on a checkpointing module which checkpoints a process. I want to save the cpu-affinity information for this process and start this process from the last checkpoint with the same affinity. – ajay saini Mar 17 '13 at 07:38

1 Answers1

23

taskset can be used both to set the affinity of a running process or to launch a process with a certain affinity, see

Synopsis

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

The below command will launch Google Chrome browser in CPU 1 & 2 (or 0 and 1). The mask is 0×00000003 and command is “google-chrome”.

taskset 0×00000003 google-chrome
sup2069
  • 9
  • 2
amdn
  • 11,314
  • 33
  • 45