xv6-l/proc.c
int
fork(void)
{
  int i, pid;
  struct proc *np;
  struct proc *curproc = myproc();


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

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

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

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

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

  pid = np->pid;

  acquire(&ptable.lock);

  np->state = RUNNABLE;

  //brand new
  qpush(np);

  for(i=1;i<MAXSHAREDPG;i++)
  {
    np->sharedrec[i]=0;
  }

  cli();
  release(&ptable.lock);

  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)
{
  int i, pid;
  struct proc *np;
  struct proc *curproc = myproc();
  if((np = allocproc()) == 0){
    return -1;
  }
  if((np->pgdir = copyuvm(curproc->pgdir, curproc->sz)) == 0){
    kfree(np->kstack);
    np->kstack = 0;
    np->state = UNUSED;
    return -1;
  }
  np->sz = curproc->sz;
  np->parent = curproc;
  *np->tf = *curproc->tf;
  np->tf->eax = 0;
  for(i = 0; i < NOFILE; i++)
    if(curproc->ofile[i])
      np->ofile[i] = filedup(curproc->ofile[i]);
  np->cwd = idup(curproc->cwd);
  safestrcpy(np->name, curproc->name, sizeof(curproc->name));
  pid = np->pid;
  acquire(&ptable.lock);
  np->state = RUNNABLE;
  qpush(np);
  for(i=1;i<MAXSHAREDPG;i++)
  {
    np->sharedrec[i]=0;
  }
  cli();
  release(&ptable.lock);
  return pid;
}
int fork ( void ) { int i , pid ; struct proc * np ; struct proc * curproc = myproc ( ) ; if ( ( np = allocproc ( ) ) == 0 ) { return -1 ; } if ( ( np -> pgdir = copyuvm ( curproc -> pgdir , curproc -> sz ) ) == 0 ) { kfree ( np -> kstack ) ; np -> kstack = 0 ; np -> state = UNUSED ; return -1 ; } np -> sz = curproc -> sz ; np -> parent = curproc ; * np -> tf = * curproc -> tf ; np -> tf -> eax = 0 ; for ( i = 0 ; i < NOFILE ; i ++ ) if ( curproc -> ofile [ i ] ) np -> ofile [ i ] = filedup ( curproc -> ofile [ i ] ) ; np -> cwd = idup ( curproc -> cwd ) ; safestrcpy ( np -> name , curproc -> name , sizeof ( curproc -> name ) ) ; pid = np -> pid ; acquire ( & ptable . lock ) ; np -> state = RUNNABLE ; qpush ( np ) ; for ( i = 1 ; i < MAXSHAREDPG ; i ++ ) { np -> sharedrec [ i ] = 0 ; } cli ( ) ; release ( & ptable . lock ) ; 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 proc. // Clear %eax so that fork returns 0 in the child. //brand new