proc.c
static struct proc *
allocproc(void)
{
  struct proc *p;
  // struct proc_stat* w;
  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++;
  p->info.pid = p->pid;
  p->priority = 60; // default priority value
  p->q_number = 0;
   

  // w->pid = p->pid;
  // w->num_run=0;
  // w->current_queue=0;
  // w->runtime=0;
  // w->ticks[0]=0;
  // w->ticks[1]=0;
  // w->ticks[2]=0;
  // w->ticks[3]=0;
  // w->ticks[4]=0;
  p->info.current_queue =0;
  q0[c0] = p;
  c0++;

// #ifdef MLFQ
//   cprintf("MOVE: %s with pid %d moved to queue 0 at rtime %d\n",p->name, p->pid,p->rtime);
// #endif
  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;
  p->ctime = ticks;
  p->rtime = 0;
  p->etime = 0;
  p->iotime = 0;
  p->ticks[0] = 0;
  p->ticks[1] = 0;
  p->ticks[2] = 0;
  p->ticks[3] = 0;
  p->ticks[4] = 0;
  p->shift =0;
  // p->info->pid = p->pid;
  p->info.runtime = p->rtime;
  p->info.num_run =0;
  p->info.ticks[0]=0;
  p->info.ticks[1]=0;
  p->info.ticks[2]=0;
  p->info.ticks[3]=0;
  p->info.ticks[4]=0;

  // #ifdef MLFQ
  // cprintf("MOVE: %s with pid %d moved to queue 0 at rtime %d\n",p->name, p->pid,p->rtime);
// #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++;
  p->info.pid = p->pid;
  p->priority = 60; 
  p->q_number = 0;
  p->info.current_queue =0;
  q0[c0] = p;
  c0++;
  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;
  p->ctime = ticks;
  p->rtime = 0;
  p->etime = 0;
  p->iotime = 0;
  p->ticks[0] = 0;
  p->ticks[1] = 0;
  p->ticks[2] = 0;
  p->ticks[3] = 0;
  p->ticks[4] = 0;
  p->shift =0;
  p->info.runtime = p->rtime;
  p->info.num_run =0;
  p->info.ticks[0]=0;
  p->info.ticks[1]=0;
  p->info.ticks[2]=0;
  p->info.ticks[3]=0;
  p->info.ticks[4]=0;
  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 ++ ; p -> info . pid = p -> pid ; p -> priority = 60 ; p -> q_number = 0 ; p -> info . current_queue = 0 ; q0 [ c0 ] = p ; c0 ++ ; 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 ; p -> ctime = ticks ; p -> rtime = 0 ; p -> etime = 0 ; p -> iotime = 0 ; p -> ticks [ 0 ] = 0 ; p -> ticks [ 1 ] = 0 ; p -> ticks [ 2 ] = 0 ; p -> ticks [ 3 ] = 0 ; p -> ticks [ 4 ] = 0 ; p -> shift = 0 ; p -> info . runtime = p -> rtime ; p -> info . num_run = 0 ; p -> info . ticks [ 0 ] = 0 ; p -> info . ticks [ 1 ] = 0 ; p -> info . ticks [ 2 ] = 0 ; p -> info . ticks [ 3 ] = 0 ; p -> info . ticks [ 4 ] = 0 ; return p ; }
PAGEBREAK: 32
Look in the process table for an UNUSED proc.
PAGEBREAK : 32 Look in the process table for an UNUSED proc .
// struct proc_stat* w; // default priority value // w->pid = p->pid; // w->num_run=0; // w->current_queue=0; // w->runtime=0; // w->ticks[0]=0; // w->ticks[1]=0; // w->ticks[2]=0; // w->ticks[3]=0; // w->ticks[4]=0; // #ifdef MLFQ //   cprintf("MOVE: %s with pid %d moved to queue 0 at rtime %d\n",p->name, p->pid,p->rtime); // #endif // Allocate kernel stack. // Leave room for trap frame. // Set up new context to start executing at forkret, // which returns to trapret. // p->info->pid = p->pid; // #ifdef MLFQ // cprintf("MOVE: %s with pid %d moved to queue 0 at rtime %d\n",p->name, p->pid,p->rtime); // #endif