Issue
I'm writing an application that needs to be executed on a specific core of a processor. For Example: If we have 4 cores and i want to execute code on 2nd core only. I need help how to do this.
Solution
The programmer can prescribe his/her own affinities (hard affinities) but use the default scheduler unless a you have a good reason not to.
Here is a C/C++ function to assign a thread to a certain core from Kernel scheduler API:
#include <sched.h>
int sched_setaffinity(pid_t pid, unsigned int len, unsigned long * mask);
It sets the current affinity mask of process pid
to *mask
. len
is the system word size (sizeof(unsigned int long)
)
To query affinity of a running process:
$ taskset -p 3935
Example output:
pid 3935's current affinity mask: f
Answered By - Adnan Qureshi Answer Checked By - Pedro (WPSolving Volunteer)