Chapter 2: Process Management and Scheduling
provided to establish whether a given task is a real-time process or not by inspecting its priority, and
task_has_rt_policychecks if the process is associated with a real-time scheduling policy.
2.7.1 Properties
Real-time processes differ from normal processes in one essential way: If a real-time process exists in the
system and is runnable, it will always be selected by the scheduler — unless there is another real-time
process with a higher priority.
The two available real-time classes differ as follows:
❑ Round robinprocesses (SCHED_RR) have a time slice whose value is reduced when they run if they
are normal processes. Once all time quantums have expired, the value is reset to the initial value,
but the process is placed at the end of the queue. This ensures that if there are severalSCHED_RR
processes with the same priority, they are always executed in turn.
❑ First-in, first-outprocesses (SCHED_FIFO) do not have a time slice and are permitted to run as long
as they want once they have been selected.
It is evident that the system can be rendered unusable by badly programmed real-time processes — all
that is needed is an endless loop whose loop body never sleeps. Extreme care should therefore be taken
when writing real-time applications.^32
2.7.2 Data Structures
The scheduling class for real-time tasks is defined as follows:
kernel/sched-rt.c
const struct sched_class rt_sched_class = {
.next = &fair_sched_class,
.enqueue_task = enqueue_task_rt,
.dequeue_task = dequeue_task_rt,
.yield_task = yield_task_rt,
.check_preempt_curr = check_preempt_curr_rt,
.pick_next_task = pick_next_task_rt,
.put_prev_task = put_prev_task_rt,
.set_curr_task = set_curr_task_rt,
.task_tick = task_tick_rt,
};
The implementation of the real-time scheduler class is simpler than the completely fair scheduler. Only
roughly 250 lines of code compared to 1,100 for CFS are required!
The core run queue also contains a sub-run queue for real-time tasks as embedded instance ofstruct
rt_rq:
(^32) Notice that this situation will be eased with the introduction of real-time group scheduling in kernel 2.6.25, which was still under
development when this book was written.