proc.c
static struct proc*
allocproc(void)
{
  struct proc *p;
  char *sp;

  acquire(&ptable.lock);

  for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
    if(p->state == UNUSED)
      goto found;

  release(&ptable.lock);
  return 0;

found:
  p->state = EMBRYO;
  p->pid = nextpid++;

  release(&ptable.lock);

  // Allocate kernel stack.
  if((p->kstack = kalloc()) == 0){
    p->state = UNUSED;
    return 0;
  }
  sp = p->kstack + KSTACKSIZE;

  // Leave room for trap frame.
  sp -= sizeof *p->tf;
  p->tf = (struct trapframe*)sp;

  // Set up new context to start executing at forkret,
  // which returns to trapret.
  sp -= 4;
  *(uint*)sp = (uint)trapret;

  sp -= sizeof *p->context;
  p->context = (struct context*)sp;
  memset(p->context, 0, sizeof *p->context);
  p->context->eip = (uint)forkret;

  #ifdef PROC_TIME
    // set time data in proc struct 
    cmostime(&(p->begin_date));
    p->ticks_begin = 0;
    p->ticks_total = 0;
    p->sched_times = 0;
  #endif
  #ifdef LOTTERY_SCHED
    // set initial nice value for lottery sched
    p->nice_value = DEFAULT_NICE_VALUE;
  #endif
  return p;
}
//PAGEBREAK: 32
// Look in the process table for an UNUSED proc.
// If found, change state to EMBRYO and initialize
// state required to run in the kernel.
// Otherwise return 0.
32
Look in the process table for an UNUSED proc.
If found, change state to EMBRYO and initialize
state required to run in the kernel.
Otherwise return 0.
32 Look in the process table for an UNUSED proc . If found change state to EMBRYO and initialize state required to run in the kernel . Otherwise return 0 .
static struct proc*
allocproc(void)
{
  struct proc *p;
  char *sp;
  acquire(&ptable.lock);
  for(p = ptable.proc; p < &ptable.proc[NPROC]; p++)
    if(p->state == UNUSED)
      goto found;
  release(&ptable.lock);
  return 0;
found:
  p->state = EMBRYO;
  p->pid = nextpid++;
  release(&ptable.lock);
  if((p->kstack = kalloc()) == 0){
    p->state = UNUSED;
    return 0;
  }
  sp = p->kstack + KSTACKSIZE;
  sp -= sizeof *p->tf;
  p->tf = (struct trapframe*)sp;
  sp -= 4;
  *(uint*)sp = (uint)trapret;
  sp -= sizeof *p->context;
  p->context = (struct context*)sp;
  memset(p->context, 0, sizeof *p->context);
  p->context->eip = (uint)forkret;
  #ifdef PROC_TIME
    cmostime(&(p->begin_date));
    p->ticks_begin = 0;
    p->ticks_total = 0;
    p->sched_times = 0;
  #endif
  #ifdef LOTTERY_SCHED
    p->nice_value = DEFAULT_NICE_VALUE;
  #endif
  return p;
}
static struct proc * allocproc ( void ) { struct proc * p ; char * sp ; acquire ( & ptable . lock ) ; for ( p = ptable . proc ; p < & ptable . proc [ NPROC ] ; p ++ ) if ( p -> state == UNUSED ) goto found ; release ( & ptable . lock ) ; return 0 ; found : p -> state = EMBRYO ; p -> pid = nextpid ++ ; release ( & ptable . lock ) ; if ( ( p -> kstack = kalloc ( ) ) == 0 ) { p -> state = UNUSED ; return 0 ; } sp = p -> kstack + KSTACKSIZE ; sp -= sizeof * p -> tf ; p -> tf = ( struct trapframe * ) sp ; sp -= 4 ; * ( uint * ) sp = ( uint ) trapret ; sp -= sizeof * p -> context ; p -> context = ( struct context * ) sp ; memset ( p -> context , 0 , sizeof * p -> context ) ; p -> context -> eip = ( uint ) forkret ; #ifdef PROC_TIME cmostime ( & ( p -> begin_date ) ) ; p -> ticks_begin = 0 ; p -> ticks_total = 0 ; p -> sched_times = 0 ; #endif #ifdef LOTTERY_SCHED p -> nice_value = DEFAULT_NICE_VALUE ; #endif return p ; }
PAGEBREAK: 32
Look in the process table for an UNUSED proc.
PAGEBREAK : 32 Look in the process table for an UNUSED proc .
// Allocate kernel stack. // Leave room for trap frame. // Set up new context to start executing at forkret, // which returns to trapret. // set time data in proc struct  // set initial nice value for lottery sched