proc.c
int
fork(void)
{

  cprintf("Start our fork code\n");
  int i, pid;
  struct proc *np;

  // Allocate process.
  if((np = allocproc()) == 0){
    return -1;
  }

  // Copy process state from p.
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;

  // Clear %eax so that fork returns 0 in the child.
  np->tf->eax = 0;

  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);

  safestrcpy(np->name, proc->name, sizeof(proc->name));

  pid = np->pid;

  acquire(&ptable.lock);

  np->state = RUNNABLE;
  insert(np);

  release(&ptable.lock);

  if(np->p_level == high){
    insert1(np);
  }else if(np->p_level == middle){
    insert2(np);
  }else{
    insert3(np);
  }
  cprintf("Using fork for creating process\n");

  return pid;
}
// Create a new process copying p as the parent.
// Sets up stack to return as if from system call.
// Caller must set state of returned proc to RUNNABLE.
Create a new process copying p as the parent.
Sets up stack to return as if from system call.
Caller must set state of returned proc to RUNNABLE.
Create a new process copying p as the parent . Sets up stack to return as if from system call . Caller must set state of returned proc to RUNNABLE .
int
fork(void)
{
  cprintf("Start our fork code\n");
  int i, pid;
  struct proc *np;
  if((np = allocproc()) == 0){
    return -1;
  }
  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = proc->sz;
  np->parent = proc;
  *np->tf = *proc->tf;
  np->tf->eax = 0;
  for(i = 0; i < NOFILE; i++)
    if(proc->ofile[i])
      np->ofile[i] = filedup(proc->ofile[i]);
  np->cwd = idup(proc->cwd);
  safestrcpy(np->name, proc->name, sizeof(proc->name));
  pid = np->pid;
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  insert(np);
  release(&ptable.lock);
  if(np->p_level == high){
    insert1(np);
  }else if(np->p_level == middle){
    insert2(np);
  }else{
    insert3(np);
  }
  cprintf("Using fork for creating process\n");
  return pid;
}
int fork ( void ) { cprintf ( " \n " ) ; int i , pid ; struct proc * np ; if ( ( np = allocproc ( ) ) == 0 ) { return -1 ; } if ( ( np -> pgdir = copyuvm ( proc -> pgdir , proc -> sz ) ) == 0 ) { kfree ( np -> kstack ) ; np -> kstack = 0 ; np -> state = UNUSED ; return -1 ; } np -> sz = proc -> sz ; np -> parent = proc ; * np -> tf = * proc -> tf ; np -> tf -> eax = 0 ; for ( i = 0 ; i < NOFILE ; i ++ ) if ( proc -> ofile [ i ] ) np -> ofile [ i ] = filedup ( proc -> ofile [ i ] ) ; np -> cwd = idup ( proc -> cwd ) ; safestrcpy ( np -> name , proc -> name , sizeof ( proc -> name ) ) ; pid = np -> pid ; acquire ( & ptable . lock ) ; np -> state = RUNNABLE ; insert ( np ) ; release ( & ptable . lock ) ; if ( np -> p_level == high ) { insert1 ( np ) ; } else if ( np -> p_level == middle ) { insert2 ( np ) ; } else { insert3 ( np ) ; } cprintf ( " \n " ) ; return pid ; }
Create a new process copying p as the parent.
Create a new process copying p as the parent .
// Allocate process. // Copy process state from p. // Clear %eax so that fork returns 0 in the child.