title: XV6: ptable initialization
body: <p>I'm talking about:</p>
  
  <pre><code>struct {
  struct spinlock lock;
  struct proc proc[NPROC];
  } ptable;
  </code></pre>
  
  <p>which resides in proc.c file.</p>
  
  <p>Can someone please explain where it is initialized?
  Because, in proc.c I've never seen something (process) being added to it.</p>
  
  <p>To be more precise, let's say I'm looking at the scheduler code:</p>
  
  <pre><code>void
  scheduler(void)
  {
   struct proc *p;
   for(;;){
   // Enable interrupts on this processor.
   sti();
   // Loop over process table looking for process to run.
   acquire(&amp;ptable.lock);
   for(p = ptable.proc; p &lt; &amp;ptable.proc[NPROC]; p++){
    if(p−&gt;state != RUNNABLE)
    continue;
    // Switch to chosen process. It is the process’s job
    // to release ptable.lock and then reacquire it
    // before jumping back to us.
    proc = p;
    switchuvm(p);
    p−&gt;state = RUNNING;
    swtch(&amp;cpu−&gt;scheduler, proc−&gt;context);
    switchkvm();
    // Process is done running for now.
    // It should have changed its p−&gt;state before coming back.
    proc = 0;
    }
   release(&amp;ptable.lock);
   }
  }
  </code></pre>
  
  <p>In:</p>
  
  <pre><code>for(p = ptable.proc; p &lt; &amp;ptable.proc[NPROC]; p++){
  </code></pre>
  
  <p>you can see that we are looping thorough each process in ptable. My question is, how did they get there?
  Thanks!</p>