CPU’s Time Sharing
Timer interrupts are essential for
time-sharing the CPU among runnable processes (that is, those in the
TASK_RUNNING
state). As we shall see in Chapter 11, each process is usually allowed a
quantum
of time of limited duration: if the process is not terminated when
its quantum expires, the schedule( )
function
selects the new process to run.
The counter
field of the process descriptor
specifies how many ticks of CPU time are left to the process. The
quantum is always a multiple of a tick — a multiple of about 10
ms. The value of counter
is updated at every tick
by update_process_times( )
, which is invoked by
either the PIT’s timer interrupt handler on
uniprocessor systems or the local timer interrupt handler in
multiprocessor systems. The code is equivalent to the following:
if (current->pid) { --current->counter; if (current->counter <= 0) { current->counter = 0; current->need_resched = 1; } }
The snippet of code starts by making sure the kernel is not handling
a process with PID 0 — the
swapper
process associated with the executing
CPU. It must not be time-shared because it is the process that runs
on the CPU when no other TASK_RUNNING
processes
exist (see Section 3.2.2).
When counter
becomes smaller than 0, the
need_resched
field of the process descriptor is
set to 1. In this case, the schedule( )
function
is invoked before resuming User Mode execution, and other
TASK_RUNNING
processes will have a chance to
resume execution on the CPU.
Get Understanding the Linux Kernel, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.