OS (2 CHP)
OS (2 CHP)
OS (2 CHP)
CHAPTER 2
PROCESS
MANAGEMENT
PROCESS CONCEP
STATES OF APROCESS
HREADS
OPERATIONS ON PROCESS
CHEDULING OF PROCE
CHIEDUHNG OUEUES
CHIEDUL
CONTEXTISWITCEN
SCHEDULING ALGORITHM
WWWN
DATIONE
ALGORITHMIEVALUATION
34
Process Management
2. PROCESS
cONCEPT
Computer systems of
COnpared to earlier systems. the present generation have greater capabilitie
ties when
With earlier systems it was possibe ior us tO execute
A4One program at a time. The executing program was allocated all he resources
avallable in the system.
However today it is possible for us to execute multiple
POBranns at any instant. This creates a requlrement to have Breater control over
progranm execution.
Thus the concept of a process is introduced.
A process is a of work in a modern computer system. It is basically
program in the state unit
of execution, which includes the executable program, the
programs data and related data structures
such as stack and all
needed to run the program. The operating system is responsible other iniormation
tor managing the
process it should provide it with a secure and fair space for execution.
This means
that the program has to run
free of any modification or interference irom any other
program or hardware device on the computer. All work done by the CPU
to the execution of processes. contributes
We have discussed how the Operating System deals with one process. This
process spends its time doing two things. computing and processing within its
program state, and waiting for the Operating System to fulAll its requests. While
the process is waiting or working on a device and the Operating System is not
dolng anything and the CPU Is idle. If there is only one process on the machine,
this is not a problem. But, if you have more than one process to run there is no
point in the system being idle waiting for a device to service one process request.
What the Operating System can do is, while one program waits on a device, another
process can bé run on the CPU.
The operating System keeps track of the processes under its control by
classifying them in terms of their state. We can see processes can be in three diiferent
states. Running on the CPU, ready to run and waiting.
During its existence, a process goes through a serles of discrete states.
Various events can cause a process to change states. The state of the process is
defined in part by the current activity of that process. Each process may be inn o
of the following states.
Exit
Running Terminated
Interrupt
1-O wait
Accepted;
STATE ACTIVITY
NEW
RUNNINC
ntisistateie procesiscrent
in theatethe pispertoming its totubes te
nstrietions orthe proceSS arelbeing exeguted
WAIT He prOCcsSwalting fo:the ocouirence of someteven
such ashecomplionofand-O.0peraton
READ heprocesSS Vaitin: tobeassgneda processor
TERMINATEDHThe Procesasiferilnatdekecuuln
36 Process Manageme
ne activity
NEW state indicates that a passive program has just beeri converted t
amic called a process. In the RUNNING state the process possesses al
ources required for its execution including the CPU. Tne running procese
a sequence of instructions and may go to the WAIT state when it calls on
Cutes
e opeating system to perform an input-output a
operation. Irn normal sequcnc
o operations
only one process may be using the resources and the CPU, howecve
there may be any number of processcs, which are ready for execution they are tn
the READY state. Once the I-O operation is complete a control signal is sent and
the process is transferred to the READY state. The READY list is maintained
priority and the first process in that list receives the processor. Once the execution
of the process is completed it goes to the TERMINATED state. It is important to
realize that only one process can be unning on any processor at any instant,
iowevr many processes may be ready and waiting.
Each process. which has been created. is identified with the help of a number
of infomation. The information identifies the current state of the process. The
Tequired information is stored in a data structure called as the Process Control
Block or the PCB. The Operating System uses the PCB to keep track of the status of
the process. Some of the information. which may be included in the PCB are:
The owner field can be used to determine the processes perTmissions such
as which files and devices it has access to, what processes it has control over and
so on. Scheduling informatian seems like it could be important for the operating
system in determining when a program gets run. Accounting information is just
an administrative tool for keeping track for how a process is been behaving. and is
mostly useful for charging users for resource usage and helping administrators to
configure the Operating System for the processes usage patterns. 1-O information
is there so the Operating Sy'siem can tell at a gance if the processes has any open
files or devices. and is also used to make sure that when a processes exits, 1 1
hasn't dlosed them the operating system will do it
Basically. the PCB is used to keep track of any adminístrative detalls
tha
the operating system will want to know at a gance. This includes detalls that mus
be associated with the processes, as well as detalls that are diSficult to figure o
through other means. This allows the operating system to manage processe process
smoothly and ea51y. wnen tne operating system switches from one process
37
Process Management
another process, the data of the current process is saved in a separate area called
as the save area. The saved data is then used to restart the process when the process
next gets the CPU.
Process numbeT
Process state
Program counteer
Registers
Memory
limits
List of open aaerzaaeDh.
files
2.4THREADS
ke processes threads share CPU and only one thread activeirunning at a tme
2.
Lke processes, threads within processes, execute sequentially.
3. Like processes, thread can create children.
4. And like process, if one thread 1s blocked, another thread can run.
1. A process with multiple threads makes a great server for example printer
server.
2. Because threads can share common data, they do not need to use inter
process communication.
3. Because of the very nature, threads can take advantage of multiprocessors.
Threads are cheap in the sense that
1. They only need a stack and storage for registers therefore, threads are cheap
to create.
2. Threads use very little resources of an operating system in which they are
working. That is, threads do not need new address space, global data, prograiu
code or operating system resources.
3. Context swltching is fast when working with threàds. The reason is that we
only have to save and/or restore PC, SP and registers.
But this cheapness does not come free -the biggest drawback is that thereis
no protection between threads.
Process Management 39
t3.
4
2. Error exlt (voluntary)
Fatal error (involuntary) .
Killed by another process (involuntary).
And agaln, internally the situatlon is smpler. In Unix terminology, there
are two system calls kill and exlt that are used. Kill (poorly named in my view)
sends a signal to another process. If this signal is not caught (via the signal systemm
call) the process is terminated. There is also an "uncatchable" signal. Exdt ts used
r self termination and can indicate success or fallure.
40
Process Managemet
The buffer may elther be provided by the operating system through the use
of an interprocess-communication (UPC) facility or by explicitly coded by the
application programmer with the use of shared memory. Let us illustrate a
shared-memory solution to the bounded-buffer problem. The producer and
consumer processes share the following varlables:
#define BUFFER-SIZE 10
typedef struct
) tem: 1 t
Process Management
41
item buffer[BUFFER-SIZE]:
intin =0:
int out O;'
The shared buffer is implemented as a circular array with two 10glcal
pointers: in and out. The varlable in polnts to the next free position in the buller
out points to the first full position in the buffer. The buffer is empty when in == out;
the buffer is full when (in + 1) % BUFFER-SIZE) == out.
The code for the producer and consumer processes follows. Theproducer process
has a local vartable next Produced in which the new item to be produced is stored:
while (1)
The consumer process has a local variable next Consumed in which the
item to be consumed is stored:
while (1)
This scheme allows at most BUFFER-SIZE - 1 1items in the buffer at the same time.
In this type of communication the process should identify the sender and
the recipient. A communication of this type should have the following propertles.
1. A link established automatlcally between every pair of processes.
2. A link assoctated with exactly two processes.
3. Only one link exdsting between. two processes.
This system establishes symmetry in addressing 1.e., both the sender and
the receiver must name each other for communication. In Asymmetry addressing
only the sender names the reciplent while the reciplent need not name the sender.
In this type of communication the messages are sent and recelve througn
mailboxes or ports. A mailbox can be viewed as an object where messages'can be
stored. Each mailbox is uniquely identifiled. In this system two messages can
communicate only if they share a mal box. A communication of this type should
have the following propertles.
1. A link established automatcaly between every, palr of processes only "
they share a mallbox.
2. A link may be associated with more than two processes.
43
Process Management
3. A number of different links may exist between two processes. With eacn
lInk corresponding to the mallbox.
Other methods 11ke synchronlzatlon and buffering can also be used for
communication.
Concurrent execution of cooperating processes requires above mechanisms that
allows processes to communicate with one another and to synichronlzE, their actions.
All modern computers can do several things at the same time. While running
the user program, a computer can also be reading from the disk and printing onha
terminal or a printer. In a multprogramming environment. the processor swtches
from program to program, running each program for a smail duration of time.
However at any instant of time, the processor is executing only one program. The
switching between programs occurs at such a fast pace the user has an impression
that he is the only person using the system i.e., an ililusion of parallelism. The
processor switches back and forth between programs keeping track of multiple
activities. The entire process is shown in the Figure 2.3.
Process 1 Process 2
Executing
Idle
ReloadReg
Idle
Interrupt
Executing
Regser
Idle
ReloadkRepistar
Executing
Figure 2.3 CPU Switching from one process to another
44 Process Managemen
Time slice
completed
sub sub
processs process Createa
cnds Crecites Sub-process
As described in the first two situations a process switches from the walting
state to ready state, and 1s then put back n the ready queue. A process continues
this cycle until it terminates, at which tume it is removed form all queues and has
1ts PCB and resources de-allocated.
2.10 SCHEDULERS
Long-Term scheduler determines which jobs are admitted to the system for
rocessing. For example in a batch processing system there are often a number o
jobs submitted to the systemn than can be executed. All the processes are spooled to
a suitable storage device such as a disk, where they are kept for executlon at a
later stage. The Long-Term Scheduler selects jobs from the job pool and loads it
into the memory for execution. Short-Term cheduler selects from the pool of
processes that is already in the ready queue for execution. The Short-Term
Scheduler then allocates the CPU to the selected process or job.
Medium term,
scheduler
Suspended or
Swapped
process queuc
Long term| Short term
scheduler scheduler
Ready gueue
I-O waiting
I-O gueue
Flgure 2.5 Types of schedulers
46
Proces Managemen
If all the processes in a system are 1-O bound then the ready queue will be
almost empty and the Short-Term Scheduler will have lite work to do. If al1
processes are in the system are CPU bound then the I-0 queue will be emnpty
and
devices will go unused and again the system will be unbalanced. A System
with
best performance is one, which will have a good combination of CPU bound
and
I-O bound processes
Is uslng lor example batch system, Interactive system or real-time system, etc. but
there are also some goals that aredeslrable In all systems.
Polley Enforcement : The scheduler has to make sure that system's policy
Is enlorced. For example, if the local policy is safety then the salety contro
processes must be able to run whenever they want to, even if ft means delay
in payroll processes.
i. Efficiency: Scheduler should keep the system (or in particular CPU) busy
cent percent of the time when possible. If the CPU and all the Input/Output
devices can be kept running all the time, more work gets done per second
than if some components are idle.
iv. Response Time : A scheduler should minimize the response time for
interactive user.
V Turnaround: A scheduler should minimize the time batch users must wait
for an output.
.
class of Jobs,
Scheduling
Anumber
has its own set ofof scheduling algorithms are avallable. Each scheduling algorth
eiecang an algorithmproperties and favors one class of processes over anothener.
properties ofvarlous for use in a particular situation, we must I
In
consider
algorithms. Many criteria have been suggested 1or comparin.th
vanous CPU scheduling ring
algorithms. Some of the criterias are:
CPU Utilization : The CPU
time the CPU is busy utilization is the average fraction of the clock
executing the users programs and periorming
various operating system functions. the
busy. CPU utilization may It is always necessary to Keep the CPU
range from 0% to 100%. În a real time system
should vary from 40 to 90%. t
i. Throughput: It is the number of jobs completed per
processes this rate may be less unit time. For long
long
order to compare the performance
whereas for the short processes it ismore.In
of various algorithms on this basis
necessary to present the same set of jobs it is
to different scheduling algorithms,
ili. Turnaround Tine : It is the time interval from the
to its of submnission of job
time
completion. The turnaround time includes the sum
walting to get into the memory, of the times spent
waiting in the ready queue, executing
the CPU., and performing 1-o. with
This is the simplest of all scheduling schemes that are available. In this
scheme the process, which requests the CPU first 1s allocated the CPU afirst. The
implementation of this scheduling algorithm requires us to consider natural
data structure called a queue. The jobs are executed in the order of their arival.
When the process enters the ready queue, 1ts PCB is línked on to the tail of the queue.
When the CPU is free, it is allocated to the process at the head of the ready queue.
The main advantage of this policy is that it is very simple to understand and
implement and the main disadvantage is that the average waiting time is often
quite long. Consider the following processes arriving for execution at time 0 with
the length of the CPU burst given in microseconds.
PROCESS BXECUTION TIME
32
If the processes arrived in the order Pl. P2, P3 then the result of their
execution would be as shown in the Gantt chart below
Pl P2 P3
32 34 39
rThe waiting time for process P1 is O microseconds, for process P2 it 1s 32
microseconds and for process P3 It is 34 microseconds. Thus the average waiting
time is
-
(O+32+34) /3 22 microseconds.
r
execution
If the processesarrived in the order P2, P3, P1 then the result of their
would be as shown in the Gantt chart below.
P1
L
O
P2
2
P3
7
= 39
50 Process Managemen
The waiting time for process P1 is 7 microseconds, for process P2 itit o0
microseconds and for proce P3 is
average walting
it is 2 microseconds. Thus the
time is
(7+0+2)/3 =3 mlcroseconds.
m the
nus the sequence of arrival of the jobs the system plays an important
amount of for execuuon.
role
time the jobs has to spend waiting
Consider the following processes arrlving for execution at time O with the
length of the CPU burst given in microseconds.
PROCEsS EXECUTION TIME
P2
P3
Procees Management 51
If the processes arrlved In the order P1, P2, P3, P4 then the result of their
executlon wlth the SJF would be as shown in the Gantt chart below.
P4 P1 P2
L
2 5 10 17
Cn +1 at + (l- a) n
This formula defines an exponential average. tn contains our most recent
information; Cn stores the part history. The parameter a controls the relative weight
of recent and past history in our predictions.
2.13.4 Priorlty Scheduling
A
preemptive algorithm has the abililty to remove the currently. executing
process from executlon and allocate the CPU to another process, the next process
considered for execution will normally be chosen on some priority. SJF is a special
case of the general priority-scheduling
algorithm. A prlority is associated with
each process, allocated to the process with the highest prlority. If
and, the CPU is
there are some processes with equal priority then they are scheduled using FCFS.
An SJF algorithm can be a priority algorithm where the prlority (P) is the inverse of
the predicted next CPU execution time.
The larger the CPU.execution time the lower will be the priority. The
expressing of priorlty can be done using different methods. It is normally expressed
as a fixed range of numberS. Some of them use small numbers to, represent low
priority whlle others use small numbers for high prlority.
52 Process Managen
Interactive Process
Batch Process
User Process
Lowest Priority
Time slice = 12
FCFS queue
Figure 2.7 Multilevel feedback queue
scheduling
In general, a multilevel feedback queue schedule is
parameters. defined by the followe
Proceas Manngement 66
If the processors are different. the options are relatlvely limited. Each
processor has its own scheduling algorithm. Processes are intrinsically typed by
their structure; they must be run on a particular processor. Processes are self
segregating. and each process can schedule itself. If several identical processors
are available, then load sharing could occur. It would be possible to provide a
separate queue for each processor. Here, one could be idle, with an empty queue,
while another processor was very busy. To prevent this, we use a common ready
queue. Al processes go into one queue and are scheduled on to any available
processor. Appoint one processor as scheduler for other processors, thus creating a
master-slave structure. This is asymmetric multiprocessing.
. Maximlze CPU utilization under the constraint that the maximum response
time is 1 second.
2 Maximize throughput such that turmaround time is linearly proportional to
total execution time.
Once the selection criterla have been defined, we want to evaluate the
varlous algorlthms under consideration. Some of the evaluation methods are:
56
Process
Managem
2.14.1 Deterministic
Modelng
One major class
evaluationuses
evaluaton 1on. Ana
of evaluation methods is called analytic evaluation. AnalAy
the given algorithm and the system workload to produce a for
Der that evaluates the performance of the algorithm ior mu
that workload,
One type of analytic
takes a particular evaluation is deterministic modeling. This
rhis meth
predetermined
algorithm for that workload. workload and defines the ce of eeaq
performance
or
example, assume that we have the workload shoWn. All iive proceso
arrive at time 0, in the esse
order given. with the length of the CPU-burst time
dme
milliseconds: glven
glven
Pl P2 P3 P4 P5
0 10 39
39 42 49 61
The waiting time is O milliseconds for process P1, 10 proces
P2, 39 milliseconds for process P3. 42 milliseconds milliseconds for
for process P4.
and 49
milliseconds for process P5.
Thus the average walting timeis (0+ 10+39+ 42+ 49)/5=
28 milliseconds
With non-preenptive SJF scheduling, we execute the processes
as
P3 P4 P1 P5
P2
3 10 20 32 61
57
Process Management
The waltingtime is 10 milliseconds for process P1, 32 milliseconds for
process P2, 0 milliseconds for process P3, 3 milliseconds for process P4, and 20
milliseconds for process P5.
Thus, the average waiting time is (10+ 32 + 0+3+20)/5 = 13 milliseconds.
P1 P2 P3 P4 P5 P2
10 20 23 30 40 50
The waiting time is 0 milliseconds for process P1, 32milisecondsfor process
P2. 20 milliseconds for process P3, 23 milliseconds for process P4, and 40
milliseconds for process P5.
Thus, the average waiting time is (0 +32 + 20+ 23+40)/5 = 23 millisecondis.
We see that, in this case, the sJF policy results in less than one-half the
average waiting time obtained with FCFS scheduling: the RR algorithm gives us an
intermediate value.
Deterministic modeling is simple and fast. It gives exact numbers, allowing
the algorithms to be compared. However, it requires exact numbers for input, andi
its answers apply to only those cases. The main uses of deterministie modeling
are in describing scheduling algorithms and providing examples: In cases where
we may be running the same programs over and over again and can measure the
-programs processing requirements exactly, we may be able to use deterministic
modeling to select a scheduling algorlthm. Over a set of examples, deterministic
modeling may indicate trends that can then be analyzed and proved separately.
For examnple, it can be shown that, for the environment described (all processes
and their times available at time 0). the SJF policy will always result in the minimum
waiting time.
N=yxw
This equation is known as Littles formula. Littles forTmula is particularly
useful because 1t is valid for any
scheduling algorithm and arrival distribution.
We can use Littles formula to compute one
the other two. For. example, if of the three variables, if we know
we know that seven processes arrive every
(on average). and that second
there are normally 14 processes in the queue, then we can
Compute the average waiting time per process
as 2 seconds.
9ueutng analysis can be useful in comparing scheduling
also has its limitations. At the moment, the algorithms, but it
classes of algorithms and distributions
that can be handled are fairly limited. The mathematics
or distributions can be difficult to work with. Thus, of complicated algorithms
arrival and service distributions
are often defined in unrealistic, but mathematically
tractable, ways. It is also
generally necessary to.make a number of independent
be accurate. Thus, so that they will be able to compute assumptions that may not
an answer, queuing models
are often only an approximation of a real system. As a result,
computed results may be questionable. the accuracy of the
2.14.3 Simulations
To get a more accurate evaluation of scheduling algorithms,
we can use
simulations. Simulations involve programming a model of the computer system.
Software data structures represent the major components of the system. The
simulator has a varlable representing a clock; as this variables value is increased
the simulator modifles the system state to reflect the activitles of the devices, the
rocesses. and the scheduler. As the simulation executes, statistics that indicate
algorithm perfornmance are gathered and printed
Process Management 69
2.14.4 Implementation
The major difflculty is the cost of this approach. The expense is incurred
not only tn coding the algorithm and modifying the operating system to support it
as well as its required data structures, but also in the reaction of the users toa
constantly changing operating system. Most users are not interested In building
a
better operating system: they merely want to get thetr processes executed and to
use thelr results. A constantly changing operating system does not help the users
to get their work done. A form of this method is used commonly for new computer
installations. For instance, a new web facility may have stimulated user loads
generated agatnst it before t "goes ive", to determine any bottlenecks in the factlity
and to estimate how many users the system can support.
The other difflculty with any algorithm evaluation is that the environment
In which the algorithm is used wll change. The environment will change not
only
In the usual way, as new programs are written and the types
of problems change.
but also as a result of the performance of the scheduler. If
short processors are
60
Process Manageme
Bven priority, then users may
If interactive break larger processes Into sets of smaller proceso
may switch to processes are given priority over non interactive processes, then sen
interactive use.
or processes in DEC TOPS-20, the system classlied interactlve and non
interactive
example,
1/0.
amount of terminal 1/0.
process did not input automatically by looking at the1-minute interval, the 1f
or itput to the terminal in a th process
was classified as non interactive
policy resulted in a and was moved to a lowerpony queue. Thi
situation where one programmer modifled his programs
te
ne
an arbitrary character to the th
terminal at regular intervals of less than minute
sy'stem gave his programs a high priority. even though the terminal output w
completely meaningless.
QUESTIONS
1. What is a process?
2. What is the relationship between a program and a process?
3. Give the difference between a passive activity and a dynamic
activity?
What are the different states of a process?
5. What are the activities performed in each state of a process?
6. What is PCB? What is its functon?
7 What is a thread?
8. What do we mean by multithreading?
9. What are the similarities between processes and threads?
10. What are the differences between processes and threads?
11. Why should we use threads in designing operating systems?
12. Explain inter process communication in deta
13. What should processes do to communicate with each other?,
14. Explain message passing system in detall.
15. How can we implement message passing?
16. What is the information, which is included in the process control block?
17. what are the objectives ol scnedunngr
lu et L a 92i
Process Management 61
18. Explain how the processor switches back and forth between programs keeping
track of multiple activities, with suitable illustrations.
19. What is a scheduling queue? What is its use?
20. Explain the role of
a. Job queue
b. Ready queue
C Device queu1e
21. With the help of a queuing diagram show the concept of scheduling.
22. What 1s a scheduler?
23. What are the different types of schedulers?
24. What is the difference between a long-term scheduler and a short-term
scheduler?
25. What is the role of a medium-term scheduler?
26. Explain the role of swapping in a medium-term scheduler.
27. What are the levels of scheduling? Descritbe the role of the three schedulers
in a multi-programming environment.
28. What are the general goals of scheduling?
29. What 1s context switch?
30. Explain some of the scheduling criterias.
31. Explain a. Throughput
b. Turnaround time
C. Response time
d. Walting time
32. What are the functions of process scheduling? Describe the policies used
for process scheduling.
33. Explain First-come-first-served scheduling with a suitable example.
34. Explain the importance of the order, of arrival of processes in FCFSS
scheduling.
35. Explain Shortest-job-flrst scheduling with a suitable example.
36. What does it mean for a process to be preemptive and non-preemptive?
37. Explain priority scheduling
38. What is starvation or blocking?
39. How does the concept of aging effect the execution of a process?
40. Explain the round robin scheduling with a sultable example.
41. Explain multilevel queue scheduling.
42. How doesi multilevel feedback queue differ from multilevel queue
scheduling?
62 Process Manage
EXAMINATION QUESTIONS
Miliseconds
Processs burst time
P1 10
P2
P3 2
P4
P5 5
11. caleulate averagewalting time. Constder the following set of processes with
the length of the CPU burst tume in milliseconds.
P1 10
P2 1
P3 2
P4 5
13. What is a process? Describe the process state with the help of a process
transition diagram. Januany 2004
22. What is a process? Describe the process states with the help of a process
transition diagram. Nov-Dec 2004
30. What are some scheduling decislons which must be taken above the Leve
of CPU scheduling? Nov-Dec 2006