Scheduling in Multiprocessor Systems
Systems with multiple CPUs are reasonably rare in the Linux world but a lot of work
has already gone into making Linux an SMP (Symmetric Multi-Processing) operating system.
That is, one that is capable of evenly balancing work between the CPUs in the system.
Nowhere is this balancing of work more apparent than in the scheduler.
In a multiprocessor system, hopefully, all of the processors are busily running processes.
Each will run the scheduler separately as its current process exhausts its time-slice
or has to wait for a system resource.
The first thing to notice about an SMP system is that there is not just one idle process
in the system.
In a single processor system the idle process is the first task in the task vector,
in an SMP system there is one idle process per CPU, and you could have more than one idle CPU.
Additionally there is one current process per CPU, so SMP systems must keep track of
the current and idle processes for each processor.
In an SMP system each process's task_struct contains the number of the
processor that it is currently running on
(processor) and its processor number of the last processor that
it ran on (last_processor).
There is no reason why a process should not run on a different CPU each time it is
selected to run but Linux can restrict a process to one or more processors in the
system using the processor_mask.
If bit N is set, then this process can run on processor N.
When the scheduler is choosing a new process to run it will not consider one that
does not have the appropriate bit set for the current processor's number in its processor_mask.
The scheduler also gives a slight advantage to a process that last ran on the
current processor because there is often a performance overhead when moving a
process to a different processor.