This repository was created as a project for Operating Systems course in Fall of 1402. This project replaces the default xv6 scheduler with the Completely Fair Scheduler (CFS) algorithm.
Besides from the implementation of a few extra syscalls, Most of the changes (implementation of the red black tree data structure and CFS) are contained inside proc.c. There is also a rudimentary test file called schedulertest.c that is compiled alongside other user programs, and can be executed while running xv6 by typing in the command schedulertest.
The CFS works by maintaining a model of "virtual runtime" for each process. Each process is assigned a priority value based on its dynamic behavior and the amount of CPU time it has consumed. The CFS aims to distribute the CPU fairly among all processes, regardless of their priority.
When a new process is created or an existing process becomes eligible for execution, the CFS adds it to a red-black tree data structure called the "rbtree". The position of each process in the tree is determined by its virtual runtime, with processes that have consumed less CPU time being closer to the left.
During scheduling, the CFS selects the process with the smallest virtual runtime from the rbtree for execution. The selected process is then allocated a maximum time slice (known as the "timeslice" or "quantum") to run on the CPU. The length of the time slice is calculated using the following formula:
"sched latency" and "min granularity" are scheduler tunables which decide the scheduler period, the period in which all run queue tasks are scheduled at least once. min granularity decides the minimum time a task will be be allowed to run on CPU before being pre-empted out. The way sched latency and min_granularity determine the scheduelr period is as follows:
If
After a process finishes its time slice or when a process with a lower virtual runtime is in the tree, it is reinserted into the rbtree with an updated virtual runtime. This ensures that processes that have consumed more CPU time are placed deeper in the tree, giving them a lower priority for the next scheduling decision. the virtual runtime of a process is calculated by the following formula:
The weight of each process is determined by its nice value ranging from -20 to 19. The weight is derived from the following formula:
To build xv6 on an x86 ELF machine (like Linux or FreeBSD), run
make. On non-x86 or non-ELF machines (like OS X, even on x86),
you will need to install a cross-compiler gcc suite capable of producing x86
ELF binaries (see
https://pdos.csail.mit.edu/6.828/).
Then run make TOOLPREFIX=i386-jos-elf-. Now install the QEMU
PC simulator and run make qemu.
The code in the files that constitute xv6 is Copyright 2006-2018 Frans Kaashoek, Robert Morris, and Russ Cox.