proc.c
//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;
    #ifdef MLFQ
        p->priority = 0;
				p->tickers = 0;
				c0++;
				q0[c0] = p;   
				procstat_table.inuse[p->pid] = 1;
				procstat_table.priority[p->pid] = p->priority;
        for(int k =0 ;k<5;k++)
        {
          procstat_table.ticks[p->pid][k] = 0;
        }
				procstat_table.pid[p->pid] = p->pid;
    #endif
  release(&ptable.lock);
  return 0;
found:
  p->state = EMBRYO;
  p->pid = nextpid++;
  #ifdef PBS
  p->priority = 60;
  #endif
  #ifdef MLFQ
        procstat_table.inuse[p->pid] = 1;
				p->priority = 0;
				p->tickers = 0;
				c0++;
				q0[c0] = p;
				procstat_table.priority[p->pid] = p->priority;
        for(int k =0 ;k<5;k++)
        {
          procstat_table.ticks[p->pid][k] = 0;
        }
				procstat_table.pid[p->pid] = p->pid;
  #endif
  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->etime = 0;
  p->rtime = 0;
  p->iotime=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 ; #ifdef MLFQ p -> priority = 0 ; p -> tickers = 0 ; c0 ++ ; q0 [ c0 ] = p ; procstat_table . inuse [ p -> pid ] = 1 ; procstat_table . priority [ p -> pid ] = p -> priority ; for ( int k = 0 ; k < 5 ; k ++ ) { procstat_table . ticks [ p -> pid ] [ k ] = 0 ; } procstat_table . pid [ p -> pid ] = p -> pid ; #endif release ( & ptable . lock ) ; return 0 ; found : p -> state = EMBRYO ; p -> pid = nextpid ++ ; #ifdef PBS p -> priority = 60 ; #endif #ifdef MLFQ procstat_table . inuse [ p -> pid ] = 1 ; p -> priority = 0 ; p -> tickers = 0 ; c0 ++ ; q0 [ c0 ] = p ; procstat_table . priority [ p -> pid ] = p -> priority ; for ( int k = 0 ; k < 5 ; k ++ ) { procstat_table . ticks [ p -> pid ] [ k ] = 0 ; } procstat_table . pid [ p -> pid ] = p -> pid ; #endif 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 -> etime = 0 ; p -> rtime = 0 ; p -> iotime = 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 .
//WHEN A PROCESS IS ALLOCATED, SET PRIORITY TO HIGHEST(0) AND SET TICKS AS 0 FOR ALL QUEUES. //ASSIGN PROCESS FIRST TO HIGHEST PRIORITY QUEUE // Allocate kernel stack. // Leave room for trap frame. // Set up new context to start executing at forkret, // which returns to trapret. // TODO Might need to protect the read of ticks with a lock