130
◾
Linux with
Operating System Concepts
memory. To the CPU, it is merely performing its fetch–execute cycle but on a different set
of instructions. But to us, and the operating system, the computer has switched contexts
from one process to another.
As the context switch requires moving values between the CPU and memory, it causes
the CPU to pause. This is not nearly as time-consuming as pausing the CPU to wait for
I/O, but it is a pause. High-performance computers use multiple sets
of registers so that the
switching of status values can be done solely within the CPU. This comes with a greater
expense since such CPUs require more registers.
The idea behind multiprogramming is that a process will voluntarily surrender its access
to the CPU because it has to wait for some other event. Above, we referred solely to some
input or output, but it could also occur because it is waiting on data from another running
process. We refer to the idea that a process voluntarily giving up the CPU as
cooperative
multitasking
. In essence, the computer is multitasking (doing several things at once) by
switching off between the tasks whenever the current task requires some waiting period.
Now the CPU switches to another task and works on it in the interim.
In multiprogramming, once the waiting process has completed its wait (whether for I/O
or for communication with another process), a context switch
occurs whereby this process
resumes and the executing process again waits. The operating system maintains a ready
queue to handle the switching of processes. This queue consists of those processes that the
CPU is actively switching between.
The next step in our evolution of process management is to force a context switch so
that no single process can monopolize the CPU. We add a piece of hardware called a timer,
which counts clock cycles. The timer is initialized to some value, say 10,000. Then, after
each clock cycle, the timer is decremented. When it reaches 0,
it alerts the operating sys-
tem which performs a context switch with the next process waiting in the ready queue.
The amount of time awarded to a process is known as its
time slice
. This form of process
management goes beyond multiprogramming (cooperative multitasking) and is known as
preemptive multitasking
, although we could also refer to it as multitasking as long as it is
clear that this differs from multiprogramming. So we now see two reasons for a context
switch, a process voluntarily gives up the CPU or the timer causes the switch.
Cooperative and preemptive multitasking both require scheduling. In this case, the
selection of the next process to execute when the CPU becomes free (either because the
current process terminates or is moved to a waiting situation) will be made by the operat-
ing system. For multiprogramming, scheduling may be accomplished via first come first
serve,
priority, or a round robin scheduling algorithm.
In round robin scheduling, processes are handled in the order that they were submitted
(first come first serve) except that the OS loops around to repeat the earliest processes after
the last processes in the ready queue have been moved into a waiting situation.
Preemptive multitasking usually uses round robin scheduling. However, we could add to
this strategy a priority for each process. If process priorities differ, then the time slices allot-
ted to the processes can also differ. Imagine each process has a priority value from 1 to 10.
For those with the highest priority (10), they are given 10,000 cycles of CPU time. Those given
the lowest priority (1) might be initialized with only 1000 cycles of CPU time.
We can also age
Managing Processes
◾
131
processes so that, as time goes on, their priority lessens so that the CPU can give more time
to newer processes.
Figure 4.1 illustrates the concepts of the context switch in multiprogramming (coopera-
tive multitasking) and (preemptive) multitasking. In the top portion of the figure, process
P3 is executing and the CPU’s registers reference information about this process (its cur-
rent instruction in the IR, the next instruction’s location in the PC, etc.). At some point,
P3 needs to perform I/O so the operating system, saves its process status information to
memory, moves P3 to an I/O queue (not shown in the figure), and starts or restores the
next process, P7.
In the bottom of the figure, P3 is executing and with each instruction,
the timer decrements. Upon reaching 0, the CPU is interrupted and the operating system
performs the same context switch except that P3 moves to the back of the ready queue.
The main detractor of cooperative and preemptive multitasking is the time it takes to
perform the context switch. Threads are similar to processes but they represent different
instances of the same running program. Because they are of the same program, they can
share some of the information that processes would not share.
For instance, two threads
of the Mozilla Firefox web browser would use the same executable code and some of the
same data. They would differ in that they would have different register values and their
own unique sets of data as well. The advantage of using threads is that context switching
Ready queue
P3 P7 P0 P9 P1
Ready queue
P3 P7 P0 P9 P1
Memory
Bus
Bus
CPU
PC
P3’s code
P7’s code
Process status info
Memory
P3’s code
P7’s code
Process status info
IR
SP
Flags
PC
IR
SP
Flags
Interrupt
signal to CPU
when timer reaches 0
Timer
0
CPU
Data
registers
Data
registers
FIGURE 4.1
Context switch in multiprogramming (top) and multitasking (bottom). (From Richard
Fox, 2013,
Do'stlaringiz bilan baham: