0

In XV6, when a fork() is called, does the child execute in kernel mode or user mode?

This is the fork code in XV6:

// 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();

  // 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;

  release(&ptable.lock);

  return pid;
}

I did some research but even from the code I can't understand how it works. Understanding how it works in UNIX would also help

Sanved
  • 921
  • 13
  • 19
Nicolò Gasparini
  • 2,228
  • 2
  • 24
  • 53

2 Answers2

2

It is almost the exact copy of the parent process except the value of eax register and parent process information, so it will execute whichever context the parent process is in.

The fork() function here creates a new process structure by calling allocproc() and fills it with the values of the original process and maps the same page tables.

Finally, it sets the process state to RUNNABLE which allows the scheduler to run the new process along with the parent.

That means actual running is performed by the scheduler, not the fork code here.

Sedat Kapanoglu
  • 46,641
  • 25
  • 114
  • 148
0

What Sedat has written entirely correct. The forked process, or the child would run in the same context which it's parent was, i.e. either Kernel or User. In addition to that, I feel what confused you were the calls done by alloproc() like kalloc() and the attributes like kstack. These deal with setting up the new process in the system with regards to the page tables and the memory part.

Sanved
  • 921
  • 13
  • 19