Page 79 - DCAP103_Principle of operating system
P. 79
Principles of Operating Systems
Notes by means of context switches that are occurring in rapid succession (tens or hundreds of times
per second). These context switches occur as a result of processes voluntarily relinquishing their
time in the CPU or as a result of the scheduler making the switch when a process has used up
its CPU time slice.
A context switch can also occur as a result of a hardware interrupt, which is a signal from a
hardware device (such as a keyboard, mouse, modem or system clock) to the kernel that an even
(e.g., a key press, mouse movement or arrival of data from a network connection) has occurred.
Intel 80386 and higher CPUs contain hardware support for context switches. However, most
modern operating systems perform software context switching, which can be used on any
CPU, rather than hardware context switching in an attempt to obtain improved performance.
Software context switching was first implemented in Linux for Intel-compatible processors with
the 2.4 kernel.
One major advantage claimed for software context switching is that, whereas the hardware
mechanism saves almost all of the CPU state, software can be more selective and save only
that portion that actually needs to be saved and reloaded. However, there is some question as
to how important this really is in increasing the efficiency of context switching. Its advocates
also claim that software context switching allows for the possibility of improving the switching
code, thereby further enhancing efficiency, and that it permits better control over the validity
of the data that is being loaded.
3.3.8 The Cost of Context Switching
Context switching is generally computationally intensive. That is, it requires considerable
processor time, which can be on the order of nanoseconds for each of the tens or hundreds of
switches per second. Thus, context switching represents a substantial cost to the system in terms
of CPU time and can, in fact, be the most costly operation on an operating system.
Consequently, a major focus in the design of operating systems has been to avoid unnecessary
context switching to the extent possible. However, this has not been easy to accomplish in
practice. In fact, although the cost of context switching has been declining when measured in
terms of the absolute amount of CPU time consumed, this appears to be due mainly to increases
in CPU clock speeds rather than to improvements in the efficiency of context switching itself.
One of the many advantages claimed for Linux as compared with other operating systems,
including some other Unix-like systems, is its extremely low cost of context switching and
mode switching.
3.3.9 Monitoring Context Switches
A context switch occurs when the kernel switches the processor from one thread to another—for
example, when a thread with a higher priority than the running thread becomes ready. Context
switching activity is important for several reasons. A program that monopolizes the processor
lowers the rate of context switches because it does not allow much processor time for the other
processes’ threads. A high rate of context switching means that the processor is being shared
repeatedly—for example, by many threads of equal priority. A high context-switch rate often
indicates that there are too many threads competing for the processors on the system.
The rate of context switches can also affect performance of multiprocessor
computers. For information about how to monitor and tune context-switch
activity on multiprocessor systems, see “Measuring Multiprocessor System
Activity” in this book.
72 LOVELY PROFESSIONAL UNIVERSITY