Part of the OSTEP notes
Which processes run when?
If you have several processes all running concurrently, it makes sense to divide time equally between them. If you have several processes waiting on a network request, it makes sense to run different processes while those ones wait. Some processes (like moving the mouse pointer) are more important than others (like updating the on-screen digital clock)
OSs have lots of process states, but it’s possible to divide them into three categories:
Some of these ideas were taken from operations management :)
To measure scheduling policies you’d better have a metric to measure them by.
However, it’s not always clear how to minimize these metrics for a set of processes (is it okay to give 10 tasks a short turnaround if it makes 1 task have a long turnaround). One way to combine the metric is to average the turnaround time for all processes.
First in first out. The first task submitted is worked until completion, then the next, then the next. It’s simple!
Problem: imagine the first task takes 1 minute and the later tasks take 10 seconds. It would be better to complete the small tasks before working on the large task. This is the “convoy effect” (like when you’re on the highway stuck behind a convoy of semi trucks)
Sort the jobs by duration and do the shorter ones first.
Problem: How do you know how long a job will take before you start on it? (You just have to estimate.)
Problem: What if you’re already working on a job when new, shorter jobs come in? (Preemption)
This is true time slicing. Work on the job for only a small amount of time, context-switch away to other tasks. You switch between them every “scheduling quantum”.
Requires far more context switches than other methods. Either context switches should be cheap, or you can increase the duration of each time-slice
Consider the process states again. If some are blocked on a disk/IO/network resource then there is no need to schedule that process. In a round-robin setting, you can take the process out of the round-robin loop while it is blocked.
The previous scheduling systems use some sense of “task duration”, but we don’t actually know how long a process will take when it’s submitted.
We can use a multi level feedback queue. (“MLFQ”)
Note that “priority number” doesn’t have anything to do with niceness levels or the Windows concept of task priority
A simple scheme:
Imagine this in relation with the “shortest job first” theoretical strategy. There’s no way to tell whether a job will be long or short. So just assume every task is short, and if it runs so long it needs to be preempted a few times, it must be a long task instead (and other short tasks will get higher priorities)
Problems:
This fixes the ability to game the system by yielding right before you were about to be preempted
They’ll settle down to their rightful places again. This improves fairness and accounts for changing behavior.
Just in case.
Increase the prioirity of tasks the user deems important
You can do this lottery-style (each process gets a probability to run on a given timeslice, with more important tasks given higher probabilities) or round-robin style (where you interleave between all tasks in the system, with more important tasks given wider slices).
Actually pretty easy to do lottery with a linked-list of tasks &
the total number of tickets in the system. Pick a random number
i
between 0 and the total number of tickets, walk the
linked list, and subtract each task’s ticket count from i
.
When it goes negative that task is the winner. This is an algorithm for
“weighted random choice” basically
Round-robin scheduling is the same idea but deterministic pretty much!
Linux’s “niceness” concept is a user-set bonus (or penalty) to the priority level of a process. Allows users to let their favorite processes run more often while still preventing them from starving out other processes.
Linux uses something deemed the “completely fair scheduler” which takes into account the cost of a context switch & always tries to pick the task which has run for the least amount of “virtual time”. Virtual time is accrued at different rates depending on the nice level of each process.