Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 2: Process Management and Scheduling


method should be aware, however, that this isnotequivalent to an expiring time slice — they do not
exist anymore in the completely fair scheduler.

If the current task is supposed to be rescheduled, the scheduler class methods set theTIF_NEED_RESCHED
flag in the task structure to express this request, andthe kernel fulfills it at the next opportune moment.

The Main Scheduler


The main scheduler function (schedule) is invoked directly at many points in the kernel to allocate the
CPU to a process other than the currently active one. After returning from system calls, the kernel also
checks whether the reschedule flagTIF_NEED_RESCHEDof the current process is set — for example, the
flag is set byscheduler_tickas mentioned above. If it is, the kernel invokesschedule. The function then
assumes that the currently active task is definitely to be replaced with another task.

Before I discussschedulein detail, I need to make one remark that concerns the__schedprefix. This is
used for functions that can potentially call schedule, including theschedulefunction itself. The declara-
tion looks as follows:

void __sched some_function(...) {
...
schedule();
...
}

The purpose of the prefix is to put the compiled code of the function into a special section of the object file,
namely,.sched.text(see Appendix C for more information on ELF sections). This information enables
the kernel to ignore all scheduling-related calls when a stack dump or similar information needs to be
shown. Since the scheduler function calls are not part of the regular code flow, they are of no interest in
such cases.

Let’s come back to the implementation of the main schedulerschedule. The function first determines the
current run queue and saves a pointer to the task structure of the (still) active process inprev.

kernel/sched.c
asmlinkage void __sched schedule(void)
{
struct task_struct *prev, *next;
struct rq *rq;
int cpu;

need_resched:
cpu = smp_processor_id();
rq = cpu_rq(cpu);
prev = rq->curr;
...

As in the periodic scheduler, the kernel takes the opportunity to update the run queue clock and clears
the reschedule flagTIF_NEED_RESCHEDin the task structure of the currently running task.

kernel/sched.c
__update_rq_clock(rq);
clear_tsk_need_resched(prev);
...
Free download pdf