2

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.

Community
  • 1
  • 1
user1274878
  • 1,275
  • 4
  • 25
  • 56

1 Answers1

1

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.

paisanco
  • 4,098
  • 6
  • 27
  • 33
  • I do not want to move the parent process around. I want it to be bound to a particular core throughout its execution. Can you please point out what could be the problem in my code? – user1274878 Jun 08 '14 at 19:50
  • According to the man page for sched_setaffinity, "A child created via fork(2) inherits its parent's CPU affinity mask. The affinity mask is preserved across an execve(2)" I'd interpret that to mean that the core assignment has to be moved on the parent while making the child assignments. The parent would then retain its assignment after all children are assigned. I'll stand corrected if other users point out why that's not correct. – paisanco Jun 08 '14 at 20:05
  • Can you please explain how the sched_setaffinity call will be executed by /* all processes */. The child processes exit the loop immediately, so they will never get to the affinity. – user1274878 Jun 08 '14 at 20:26
  • Good catch, sorry. Edited it. – paisanco Jun 08 '14 at 20:57