title: How does sched_setaffinity() work?
body: <p>I am trying to understand how the linux syscall sched_setaffinity() works. This is a follow-on from my question <a href="https://stackoverflow.com/questions/663958/how-to-control-which-core-a-process-runs-on">here</a>.</p>
  
  <p>I have <a href="http://www.ibm.com/developerworks/linux/library/l-affinity.html" rel="nofollow noreferrer">this guide</a>, which explains how to use the syscall and has a pretty neat (working!) example. </p>
  
  <p>So I downloaded the Linux 2.6.27.19 <a href="ftp://ftp.kernel.org/pub/linux/kernel/v2.6/" rel="nofollow noreferrer">kernel sources</a>.</p>
  
  <p>I did a 'grep' for lines containing that syscall, and I got 91 results. Not promising.</p>
  
  <p>Ultimately, I'm trying to understand how the kernel is able to set the instruction pointer <em>for a specific core</em> (or processor.)</p>
  
  <p>I am familiar with how single-core-single-thread programs work. One might issue a 'jmp foo' instruction, and this basically sets the IP to the memory address of the 'foo' label. But when one has multiple cores, one has to say "fetch the next instruction at memory address foo, and set the instruction pointer for <em>core number 2</em> to begin execution there."</p>
  
  <p>Where, in the assembly code, are we specifying which core performs that operation?</p>
  
  <p>Back to the kernel code: what is important here? The file 'kernel/sched.c' has a function called sched_setaffinity(), but returns type "long" - which is inconsistent with its <a href="http://linux.die.net/man/2/sched_setaffinity" rel="nofollow noreferrer">manual page</a>. So what is important here? Which of these modules shows the assembly instructions issued? What module is reading the 'task_struct', looking at the 'cpus_allowed' member, and then translating that into an instruction? (I've also thumbed through the glibc source - but I think it just makes a call to the kernel code to accomplish this task.)</p>