Reputation: 1415
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.
Upvotes: 2
Views: 2033
Reputation: 4164
I think your method needs to have the parent process increment the counter for child processes, and your affinity code needs to be executed for all processes.
Try this:
/* your call args and return type may vary, just an illustration */
void doSetup()
{
int child_core = 0;
for(int i = 0 i < NUM_CHILDREN; i++)
{
pid_t pID = fork();
if (pID == 0)
{
/* child */
break;
}
else
{
/* parent only */
child_core = (child_core+1)%6
}
}
/* all processes */
cpu_set_t mask;
CPU_ZERO(&mask);
CPU_SET(child_core,&mask);
size_t len = sizeof(mask);
if (sched_setaffinity(0, len, &mask) < 0)
{
perror("sched_setaffinity");
}
else
{
/* child process tasks calls here */
}
return;
}
There are some more examples and discussion at this link at IBM DeveloperWorks
Hope this helps.
Upvotes: 1