Project 2b: xv6 Scheduler

Objectives

There are three objectives to this assignment:

  • To familiarize yourself with a real scheduler.
  • To change that scheduler to a new algorithm.
  • To make a graph.

Overview

In this project, you'll be putting a new scheduler into xv6. It is called a lottery scheduler , and is described in this chapter of the online book. The basic idea is simple: assign each running process a slice of the processor based in proportion to the number of tickets it has; the more tickets a process has, the more it runs. Each time slice, a randomized lottery determines the winner of the lottery; that winning process is the one that runs for that time slice.

Details

You'll need a couple of new system calls to implement this scheduler. The first is int settickets(int num) , which sets the number of tickets of the calling process. By default, each process should get one ticket; calling this routine makes it such that a process can raise the number of tickets it receives, and thus receive a higher proportion of CPU cycles. This routine should return 0 if successful, and -1 otherwise (if, for example, the user passes in a number less than one).

The second is int getpinfo(struct pstat *) . This routine returns some basic information about each running process, including how many times it has been chosen to run and its process ID. You can use this system call to build a variant of the command line program ps , which can then be called to see what is going on.

Tips

Most of the code for the scheduler is quite localized and can be found in proc.c ; the associated header file, proc.h is also quite useful to examine. To change the scheduler, not much needs to be done; study its control flow and then try some small changes.

You'll need to figure out how to generate random numbers in the kernel; some searching should lead you to a simple pseudo-random number generator, which you can then include in the kernel and use as appropriate.

You'll need to understand how to fill in the structure pstat in the kernel and pass the results to user space. The structure looks like what you see in here.

The Code

The source code for xv6 (and associated README) can be found in ~cs537-1/ta/xv6/ . Everything you need to build and run and even debug the kernel is in there.

You may also find the following readings about xv6 useful, written by the same team that ported xv6 to x86: xv6 book

Particularly useful for this project: Chapters 5.

What To Turn In

Beyond the usual code, you'll have to make a graph for this assignment. The graph should show the number of time slices a set of two processes receives over time, where one process has twice the number of tickets as the other. The graph is likely to be pretty boring, but should clearly show that your lottery scheduler works as desired.