0% found this document useful (0 votes)
338 views2 pages

Context Switch Definition

A context switch is when the CPU switches from one process or thread to another by suspending the current process, storing its context in memory, retrieving the context of the next process from memory, and restoring that process's context to resume execution. Context switches allow an operating system to perform multitasking to make it seem like multiple processes are running simultaneously by rapidly switching between them. While context switches enable concurrency, they require significant CPU resources and are generally one of the most computationally expensive operations in an operating system.

Uploaded by

Lop Mang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
338 views2 pages

Context Switch Definition

A context switch is when the CPU switches from one process or thread to another by suspending the current process, storing its context in memory, retrieving the context of the next process from memory, and restoring that process's context to resume execution. Context switches allow an operating system to perform multitasking to make it seem like multiple processes are running simultaneously by rapidly switching between them. While context switches enable concurrency, they require significant CPU resources and are generally one of the most computationally expensive operations in an operating system.

Uploaded by

Lop Mang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

4/11/2017 Context Switch denition

Acontext switch(also sometimes referred to as aprocess switchor atask switch) is the switching of
theCPU(central processing unit) from oneprocessorthreadto another.

A process (also sometimes referred to as atask) is anexecuting(i.e., running)instanceof aprogram.


InLinux, threads are lightweight processes that can run in parallel and share anaddress space(i.e., a range
ofmemorylocations) and other resources with theirparentprocesses (i.e., the processes that created them).

Acontextis the contents of a CPU'sregistersandprogram counterat any point in time. A register is a small
amount of very fast memory inside of a CPU (as opposed to the slowerRAMmain memory outside of the
CPU) that is used to speed the execution ofcomputerprograms by providing quick access to commonly
used values, generally those in the midst of a calculation. A program counter is a specialized register that
indicates the position of the CPU in its instruction sequence and which holds either the address of the
instruction being executed or the address of the next instruction to be executed, depending on the specic
system.

Context switching can be described in slightly more detail as thekernel(i.e., the core of theoperating
system) performing the following activities with regard to processes (including threads) on the CPU: (1)
suspending the progression of one process and storing the CPU'sstate(i.e., the context) for that process
somewhere in memory, (2) retrieving the context of the next process from memory and restoring it in the
CPU's registers and (3) returning to the location indicated by the program counter (i.e., returning to the line
of code at which the process was interrupted) in order to resume the process.

A context switch is sometimes described as the kernel suspendingexecution of one processon the CPU and
resumingexecution of some other processthat had previously been suspended. Although this wording can
help clarify the concept, it can be confusing in itself because a processis, by denition, an executing
instance of a program. Thus the wordingsuspending progression of a processmight be preferable.

Context Switches and Mode Switches

Context switches can occur only inkernel mode. Kernel mode is a privileged mode of the CPU in which
only the kernel runs and which provides access to all memory locations and all other system resources.
Other programs, including applications, initially operate inuser mode, but they can run portions of the
kernel code viasystem calls. A system call is a request in aUnix-likeoperating system by anactive
process(i.e., a process currently progressing in the CPU) for a service performed by the kernel, such
asinput/output(I/O) orprocess creation(i.e., creation of a new process). I/O can be dened as any
movement of information to or from the combination of the CPU and main memory (i.e. RAM), that is,
communication between this combination and the computer's users (e.g., via the keyboard or mouse),
itsstoragedevices (e.g., disk or tape drives), or other computers.

The existence of these two modes in Unix-like operating systems means that a similar, but simpler,
operation is necessary when a system call causes the CPU to shift to kernel mode. This is referred to as
amode switchrather than a context switch, because it does not change the current process.

Context switching is an essential feature ofmultitaskingoperating systems. A multitasking operating system


is one in which multiple processes execute on a single CPU seemingly simultaneously and without
interfering with each other. This illusion ofconcurrencyis achieved 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 theschedulermaking the
switch when a process has used up its CPUtime slice.

A context switch can also occur as a result of ahardware interrupt, which is a signal from a hardware device
(such as a keyboard, mouse, modem or system clock) to the kernel that anevent(e.g., a key press, mouse
movement or arrival of data from anetworkconnection) has occurred.

http://webcache.googleusercontent.com/search?q=cache:-u_kTRjK0ZsJ:www.linfo.org/context_switch.html+&cd=14&hl=en&ct=clnk 1/2
4/11/2017 Context Switch denition

Intel 80386 and higher CPUs contain hardware support for context switches. However, most modern
operating systems performsoftware context switching, which can be used on any CPU, rather thanhardware
context switchingin an attempt to obtain improved performance. Software context switching was rst
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
efciency of context switching. Its advocates also claim that software context switching allows for the
possibility of improving the switching code, thereby further enhancing efciency, and that it permits better
control over the validity of the data that is being loaded.

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 substantialcostto 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 efciency 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.

http://webcache.googleusercontent.com/search?q=cache:-u_kTRjK0ZsJ:www.linfo.org/context_switch.html+&cd=14&hl=en&ct=clnk 2/2

You might also like