Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


Again thanks to the modular structure of the scheduler, most work can be delegated to the scheduling
classes. If the current task was in an interruptible sleep but has received a signal now, it must be promoted
to a running task again. Otherwise, the task is deactivated with the scheduler-class-specific methods
(deactivate_taskessentially ends up in callingsched_class->dequeue_task):


kernel/sched.c
if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
unlikely(signal_pending(prev)))) {
prev->state = TASK_RUNNING;
} else {
deactivate_task(rq, prev, 1);
}
...

put_prev_taskfirst announces to the scheduler class that the currently running task is going to be
replaced by another one. Note that this isnotequivalent to taking the task off the run queue, but provides
the opportunity to perform some accounting and bring statistics up to date. The next task that is sup-
posed to be executed must also be selected by the scheduling class, andpick_next_taskis responsible
to do so:


prev->sched_class->put_prev_task(rq, prev);
next = pick_next_task(rq, prev);
...

It need not necessarily be the case that a new task hasbeen selected. If only one task is currently able to
run because all others are sleeping, it will naturally be left on the CPU. If, however, a new task has been
selected, then task switching at the hardware level must be prepared and executed.


kernel/sched.c
if (likely(prev != next)) {
rq->curr = next;
context_switch(rq, prev, next);
}
...

context_switchis the interface to the architecture-specific methods that perform a low-level context
switch.


The following code checks if the reschedule bit of the current task is set, and the scheduler jumps to the
label described above and the search for a new process recommences:


kernel/sched.c
if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
goto need_resched;
}

Notice that the above piece of code is executed in two different contexts: When no context switch has
been performed, it is run directly at the end of theschedulefunction. If, however, a context switch
has been performed, the current process will stop runningright beforethis point — the new task has
taken over the CPU. However, when the previous task is reselected to run later on, it will resume its
execution directly at this point. Sinceprevwill not point to the proper process in this case, the current
thread needs to be found viacurrentbytest_thread_flag.

Free download pdf