Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XV6: ptable initialization

Tags:

c

proc

xv6

I'm talking about:

struct {
struct spinlock lock;
struct proc proc[NPROC];
} ptable;

which resides in proc.c file.

Can someone please explain where it is initialized? Because, in proc.c I've never seen something (process) being added to it.

To be more precise, let's say I'm looking at the scheduler code:

void
scheduler(void)
{
 struct proc *p;
 for(;;){
 // Enable interrupts on this processor.
 sti();
 // Loop over process table looking for process to run.
 acquire(&ptable.lock);
 for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){
  if(p−>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−>state = RUNNING;
  swtch(&cpu−>scheduler, proc−>context);
  switchkvm();
  // Process is done running for now.
  // It should have changed its p−>state before coming back.
  proc = 0;
  }
 release(&ptable.lock);
 }
}

In:

for(p = ptable.proc; p < &ptable.proc[NPROC]; p++){

you can see that we are looping thorough each process in ptable. My question is, how did they get there? Thanks!

like image 216
Aladin Avatar asked Jan 08 '23 14:01

Aladin


1 Answers

You won't find the initialization in xv6's code. Here's why.

C initializes proc's int and enum variables to 0. When ptable is implemented, struct proc proc[NPROC]; creates an array of 64 processes whose fields are initialized to 0 by the language. 0 happens to be the value of the UNUSED enumeration.

allocproc loops through ptable.proc looking for state=UNUSED, then initializes the first one it finds to all the needed values. So there's no need to explicitly initialize the structures in the array.

like image 167
Dave Swanson Avatar answered Jan 15 '23 15:01

Dave Swanson