title: multiple processes stuck on same cpu after multiple 'fork's (Linux/C)
body: <p>On a multi-core machine, why doesn't the CPU automatically move a process to a new core if there are multiple processes all on the same core at full capacity?</p>
  
  <p>Here is a sample program which reproduces the problem I'm seeing:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdlib.h&gt;
  
  void RunClient(int i) {
    printf("Starting client %d\n", i);
    while (true) {
    }
  }
  
  
  int main(int argc, char** argv) {
    for (int i = 0; i &lt; 4; ++i) {
      pid_t p_id = fork();
      sleep(3);
      if (p_id == -1) {
        perror("fork");
      } else if (p_id == 0) {
        RunClient(i);
        exit(0);
      }
    }
  
    return 0;
  }
  </code></pre>
  
  <p>This works as expected (when I check top, I see all 4 processes are running at 100%). </p>
  
  <p>However, if I remove the "sleep(3)" line, then sometimes multiple processes are set to the same CPU and thus not running at full capacity (for example, one process might be at 100% while the other 3 are each at 33%). What's going on and how do I fix it?</p>
  
  <p>I know I can manually set the CPU affinity, but that seems like a fragile solution? </p>