SCHEDULER(9)

NAME
scheduler - process scheduling subsystem

SYNOPSIS
#include <sys/sched.h>

DESCRIPTION
This page documents the default “time-sharing” scheduler in NetBSD. It is likely that in the future a new scheduler subsystem will be developed which will allow new scheduling algorithms to be implemented.

NetBSD's scheduler algorithm is based on “multilevel feedback queues”. All processes that are runnable are assigned a scheduling priority that determines in which run queue they are placed. The currently running process does not exist on a run queue. In selecting a new process to run, the system scans the run queues from highest to lowest priority and chooses the first process on the first non-empty queue. If multiple processes reside on a queue, the system runs them round robin in the order found on the queue. If a process uses up the time slice allocated to it, it is placed at the end of the queue from which it came, and the process at the front of the queue is selected to run.

The system adjusts the scheduling priority of a process dynamically to reflect resource requirements (being blocked) and the CPU utilization consumed by the process. Processes are moved between run queues based on changes in their scheduling priority (hence the word feedback in the name).

The process CPU utilization is incremented in hardclock() each time that the system clock ticks and the process is found to be executing. CPU utilization is adjusted once per second in schedcpu() via a digital decay filter. Each time a process accumulates four ticks in its CPU utilization, schedclock() invokes resetpriority() to recalculate the process's scheduling priority.

When a process other than the currently running process attains a higher scheduling priority, the system notices this in resetpriority() and schedules a context switch to the higher-priority process to occur on return to user mode.

A blocked process exists on the sleep queue and is not affected by the scheduling behaviour discussed above. However, each second schedcpu() updates the time each process has been on the sleep queue. The system recomputes a process's CPU utilization estimate in updatepri() when it is awakened and has been sleeping for longer than 1 second. When the process is placed on the run queue by setrunnable(), resetpriority() is invoked to recalculate the process's scheduling priority.

The system tailors this short-term scheduling algorithm to favor interactive jobs by raising the scheduling priority of processes that are blocked waiting for I/O for one or more seconds, and by lowering the priority of processes that accumulate significant amounts of CPU time.

CODE REFERENCES
This section describes places within the NetBSD source tree where actual code implementing the scheduler can be found. All pathnames are relative to /usr/src.

The scheduler subsystem is implemented within the file sys/kern/kern_synch.c.

SEE ALSO
SCHED_LOCK(9), hardclock(9), mi_switch(9), setrunqueue(9), userret(9)