Operating Systems


Assignment Part 1 (traps)

xv6 defines two structures that hold saved registers for a process: struct trapframe on sheet 06, and struct context on sheet 20. Explain a situation in which a suspended process will have three sets of saved registers in its kstack.

Assignment Part 2 (context switching)

Read: swtch.S and proc.c (focus on the code that switches between processes, specifically scheduler and sched). Also process creation: sys_fork() and copyproc().

In this part of the homework you will investigate how the kernel switches between two processes.

Assignment:

Suppose a process that is running in the kernel calls sched(), which ends up jumping into scheduler().

Turn in: Where is the stack that sched() executes on?

Turn in: Where is the stack that scheduler() executes on?

Turn in: When sched() calls swtch(), does that call to swtch() ever return? If so, when?

Now think back to gcc calling conventions and the invariants that gcc expects any function, including swtch, to maintain. Compare these invariants with what swtch actually implements, and the state that our kernel maintains in a struct context.

Turn in: Could swtch do less work and still be correct? Could we reduce the size of a struct context? Provide concrete examples if yes, or argue for why not.

Surround the call to swtch() in scheduler() with calls to cprintf() like this:

  cprintf("a");
  swtch(&cpu->scheduler, &proc->context);
  cprintf("b");

Similarly, surround the call to swtch() in sched() with calls to cprintf() like this:

  cprintf("c");
  swtch(&proc->context, cpu->scheduler);
  cprintf("d");

Rebuild your kernel and boot it on QEMU. With a few exceptions you should see a regular four-character pattern repeated over and over.

Turn in: What is the four-character pattern?

Turn in: The very first characters are ac. Why does this happen?