I have 8 CPUs being implemented on ARM's big.LITTLE archiectuture. 0-3 as LITTLE CPUs and 4-7 as big CPUs.
I am trying to set the CPU affinity of an executing program using the following code:
// Set CPU affinity to multiple cores
void set_multiple_CPU_affinity(int *cpu_nums, size_t n, pid_t pid){
cpu_set_t mask;
CPU_ZERO(&mask);
for(int i = 0; i < n; i++)
{
CPU_SET(cpu_nums[i], &mask);
}
if (sched_setaffinity(pid, sizeof(mask), &mask) == -1) {
printf("[!] Error in sched_setaffinity");
perror("sched_setaffinity");
}
else {
print_CPU_affinity(pid); // Code to print out CPU affinity set to
}
}
int main(int argc, char** argv)
{
int controlled_CPU_affinity[] = {4, 5, 6, 7};
size_t n = sizeof(controlled_CPU_affinity) / sizeof(controlled_CPU_affinity[0]);
set_multiple_CPU_affinity(controlled_CPU_affinity, n, 0);
// More code here
}
My question is as follows:
When I go on to check using system trace (task monitor), I could see that the CPU affinity of program changes to CPU 3 instead regardless of setting up the affinity to certain CPUs. How can I force the affinity to be set to a core till the program finishes execution?