sched_setaffinity not work properly

hi, i have a problem with the function sched_setaffinity().
I have a pc with 2 quadcore processor, i want to run a process on a core, on internet i found the function sched_settaffinity()

this is my code:


int main()
{
   cpu_set_t mask;

   CPU_ZERO(&mask);

   CPU_SET(2, &mask);

   if(sched_setaffinity(0, sizeof(cpu_set_t), &mask) != 0)
   {
      printf("error %d", errno);
      exit(1);
   }
   
   while(1)
   {
      printf("counter = %d", counter++);
   }

   return 0;
}

with this code i would expect the cpu 0 usage to be 100%, but if i see the system manager this is not the case. All cores are used and the CPU 0 usage is not on 100%.

Why this happens?
which is my error?

thanks

For this to work, you need to remove the “printf” statement. You can replace it with any calculations. I don’t know what is the effect of printf. I hope someone can shed some light on the reason.

Just a quick correct to your code
1-If you want to set the work on CPU 0 , then set the mask value to 1 and not 2