Linux with Operating System Concepts



Download 5,65 Mb.
Pdf ko'rish
bet53/254
Sana22.07.2022
Hajmi5,65 Mb.
#840170
1   ...   49   50   51   52   53   54   55   56   ...   254
Bog'liq
Linux-with-Operating-System-Concepts-Fox-Richard-CRC-Press-2014

context switch
. The 
context switch is the switching of processes from the one that the CPU was running to the 
next process. To perform a context switch, the operating system gets involved. First, the 
currently executing process’ status must be captured and saved. This is typically stored 
somewhere in the operating system’s area of main memory.
The process’ status, as explained earlier, includes the values stored in the PC, IR, SF, AC, 
and SP registers among others. That is, we are saving what the processor was doing (IR), 
where it was doing it (PC), and what the results of the latest operation were (SF, possibly 
AC, SP). Now the operating system starts or resumes another process by loading its status 
(IR, PC, SF, SP, AC) values from memory into the corresponding registers. Once done, 
the CPU starts up again but is now executing on a different process in a different area of 


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, 

Download 5,65 Mb.

Do'stlaringiz bilan baham:
1   ...   49   50   51   52   53   54   55   56   ...   254




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish