xv6 -Implementing ps, nice system calls and priority scheduling

Harshal Shree
4 min readMay 31, 2020

Hi everyone! If you’ve come to this post, I assume you would already have some basic insights about the xv6 operating system.

ps and nice system calls walkthrough

The ps (i.e., process status) command is used to provide information about the currently running processes, including their process identification numbers (PIDs). A process, also referred to as a task, is an executing (i.e., running) instance of a program.

The nice system call is used to change the priority of a given process.

So let’s dive into the implementation now.

System call interface maintains the table of all the system call and associates each with a number. So first, we update this table. In syscall.h, add the following two system calls. Here, I have given the numbers 22 and 23 to the system calls. The cps is for the ps system call and chpr (change priority) is for the nice system call.

Next, in the PCB of the process, we have to add a new attribute ‘priority’. The PCB of the process is stored in proc.h file.

In the struct proc in the proc.h file, add a new attribute ‘priority’ of int data type.

Next, we have to include the declaration of these functions in defs.h and users.h files.

Next, we have to include the definition of the cps and chpr functions in proc.c

Next, in sysproc.c, we have to define a function in which our cps and chpr functions will be called.

Next, we have to make some minor changes in the usys.S file. The ‘.S’ extension indicates that this file has assembly level code and this file interacts with the hardware of the system.

Next, we open the sysproc.c file and add the two system calls.

Next, we have to create a ps.c and nice.c file in which our cps and chpr functions will be called respectively.

Now that we have our system calls done, we have to work on the process priority assignment. For this, firstly we define the default priority of a process in the allocproc function in the proc.c file.

Here, I have assumed higher the number, lower is the priority of the process.

Now, the child process is expected to have higher priority than the parent process. So we have to change the priority of child process when it is created. For this, we will make the changes in exec.c file.

Now, we have to create a c program which creates a number of child process as mentioned by the user and consumes CPU time for testing our system calls and scheduling. So we create a new file dpro.c(dummy program)and write the following code:

Now, we make the appropriate changes in the Makefile. In Makefile, under ‘UPROGS’, add the following:

_ps\ 
_dpro\
_nice\

Also in the EXTRAS section of the Makefile, add nice.c, dpro.c and ps.c

Voila! We have implemented the system calls successfully.

Priority based round robin scheduling walkthrough

Priority based Round-Robin CPU Scheduling algorithm is based on the integration of round-robin and priority scheduling algorithm. It retains the advantage of round robin in reducing starvation and also integrates the advantage of priority scheduling. Existing round robin CPU scheduling algorithm cannot be implemented in real time operating system due to their high context switch rates, large waiting time, large response time, large turnaround time and less throughput. The proposed algorithm improves all the drawbacks of round robin scheduling algorithm.

For implementing this, we make the required changes in scheduler function in proc.c file.

And we are done! We have implemented the system calls and changed the scheduling policy in xv6. Now, let us try it out.

Final Working

In the demonstration below, we have called dpro functions twice simultaneously which creates a process scenario as in the ps command below.

Initially, the “dpro” function with pid 9 is in “RUNNABLE” state with the default priority 10.

Using the nice system call, we change the priority of the process with pid 9 to 1 which makes the that process a higher priority.

So when we call the ps command again, we see the process with pid 9 is in “RUNNING” state and the previous running process comes into “RUNNABLE” state.

I hope this was helpful! This was my first try at writing a post so do let me know about it in the comments section.

For more guidance, refer to my github repository https://github.com/Harshalshree/Xv6-OS-with-custom-modifications

Happy Coding!

--

--