A visualizer for exploring the effect of priorities on the Linux scheduler.
SchedViz-Linux
Origin
At KU Leuven Faculty Engineering Technology and Hasselt University, we got a course about operating systems and C. In this course we’ve discussed the main responsibilities of an operating system including task management and CPU scheduling. The latter, decides which task is allowed to run on the CPU at a given time slice and when to switch to other tasks. This can be implemented rather simply by using
- First Comes First Servers (FCFS)
- Shortest/Longest Job First (SJF/LJF) But these are non-preemptive or cooperative (tasks are allowed to run to their full completion) ways to arange CPU scheduling and have some major flaws. Some more advanced and effectient pre-emptive scheduling algorithms are:
- Priority based
- Round-Robin based The former one was demonstrated by a program made by Moritz KLammler but I couldn't find the exact source code so I pushed it to my personal GitHub repository.
Priority based scheduling
A priority in context of CPU scheduling is explicity indicating that a given task is more important than another, whereby each task is assigned a number so they can be fully ordered to determine which is most important.
Implementation
The program starts with creating child processes using fork()
. The number of child processes depends on what the COLUMNS
variable is set to.
Every child process has an incrementing counter. When the counter reaches the specific priority PERIOD
value, the color of the terminal cells changes.
The order of the colors is the following:
Usage
Here, we can see how a process is impacted by the priority. The priority values range from -20 to 19, where -20 is the highest priority and 19 is the lowest. To change the priority of a process, we can use setpriority()
.
Default task priority (0)
In the first example, we make use of a task's default priority value. We can see that the jobs simply increase their counter and alter their colors in this instance. There appear to be lines moving through the terminal as well. This is because simultaneously started processes will update their colors at the same time.
Lower task priority (19)
When we use a lower priority, we can see that the majority of the processes start in red and change at the same time (and in a different way than before). This is because the processes in the scheduler occur (relatively) infrequent, and they practically are in sync.
It will eventually fall out of sync, but the initial stage is what matters.
Higher task priority (-20)
If we increase the priority to the highest possible value, we can see that the processes move from top to bottom based on color. This means that the top ones are already updating/scheduled while the bottom ones aren't. This is because they get a lot of CPU time, hence high priority.
This too will eventually get out of sync.
License
SchedViz-Linux, and the project this was based on by Moritz KLammler, is released under the MIT License.