I am wanting to affinitize the parent process to a particular core. In the code below, the variable core is a user supplied argument. Following that, I want to create NUM_CHILDREN processes and each one of them to be affinitized to one of the cores in a round robin manner. The child processes break out of the loop and do some more work (not shown in the code).
int child_core = 0;
CPU_ZERO(&mask);
CPU_SET(core,&mask);
if (sched_setaffinity(0, len, &mask) < 0)
{
perror("sched_setaffinity");
}
for(int i = 0 i < NUM_CHILDREN; i++)
{
pID = fork();
if (pID == 0)
{
/* child */
CPU_ZERO(&mask);
CPU_SET(child_core,&mask);
len = sizeof(mask);
if (sched_setaffinity(0, len, &mask) < 0)
{
perror("sched_setaffinity");
}
break;
}
/*parent does some work*/
child_core = (child_core+1)%6
}
The problem I am facing is that running sar -P ALL 1 100 shows that only a single core ( the core to which the parent has been affinitized ) is being utilized. I am trying to follow the solution mentioned here: Cpu affinity inherited by child process
Can someone please tell me how I can have the child processes to be affinitized to the proper cores.