Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
106 views34 pages

Module 1 Process Management

This document discusses processes and process management. It defines a process and describes the key elements that characterize a process, including its identifier, state, priority, program counter, memory pointers, and other information stored in a process control block. It then explains two common models for representing process states: a two-state model with running and not running states, and a five-state model that further splits the not running state into ready and blocked states. The document also covers process creation, termination, and the transitions between states in the five-state model.

Uploaded by

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

Module 1 Process Management

This document discusses processes and process management. It defines a process and describes the key elements that characterize a process, including its identifier, state, priority, program counter, memory pointers, and other information stored in a process control block. It then explains two common models for representing process states: a two-state model with running and not running states, and a five-state model that further splits the not running state into ready and blocked states. The document also covers process creation, termination, and the transitions between states in the five-state model.

Uploaded by

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

1

Module 1 PROCESS MANAGEMENT


2.1 PROCESS
A process can be defined in several ways:
 A program in execution
 An instance of a program running on a computer
 The entity that can be assigned to and executed on a processor
 A unit of activity characterized by the execution of a sequence of instructions,
a current state, and an associated set of system resources.

Two essential elements of a process are:


 program code: which may be shared with other processes that are executing the
same program
 Set of data: associated with that code.

Figure 2.1 Process control block

At any given point in time, while the program is executing, this process can be
uniquely characterized by a number of elements, including the following:
 Identifier: A unique identifier associated with this process, to distinguish it from all
other processes.
 State: If the process is currently executing, it is in the running state.
 Priority: Priority level relative to other processes.
 Program counter: The address of the next instruction in the program to be
executed.
2

 Memory pointers: Includes pointers to the program code and data associated with
this process, plus any memory blocks shared with other processes.
 Context data: These are data that are present in registers in the processor while the
process is executing.
 I/O status information: Includes outstanding I/O requests, I/O devices (e.g.,
disk drives) assigned to this process, a list of files in use by the process, and so on.
 Accounting information: May include the amount of processor time and clock time
used, time limits, account numbers, and so on.

The above information is stored in a data structure known as process control block as
shown in Figure 2.1. It is created and managed by OS.

2.2 PROCESS STATES


Execution of individual program involves a sequence of instructions within that
program. This list of instructions is known as trace of the process. These instructions
characterize the behavior of an individual process. Dispatcher is a small program which
switches the processor from one process to another.

Major responsibility of an OS is to control the execution of processes. This task involves


determining the inner pattern for execution and allocating resources to processes. Here we
will consider two models of behavior of processes. These are known as process states.

2.2.1 A Two – State Process Model


This is a simplest model and assumes that any process can be in any two states at
any given point of time:
 Running
 Not Running

(a) State Transition Diagram

(b) Queuing Diagram


Figure 2.2 Two – State Process Model
3

W hen a job (or process) enters a job pool, it will be in a not running state. Once it is ready
to execute and gets a processor, it will be into running state. W hen it has to wait for
any resources or for I/O, it will be paused and sent into not running state again. During
this time, another process may start running. Once the previous process is ready again,
it will be switched from not running to running. This task is shown in Figure 2.2(a).

In this model, there must be some information relating to each process like current state,
location in the memory etc. This will help OS to keep track of the process and is known as
process control block. Processes that are not running must be kept in a queue, waiting for
their turn to execute as shown in Figure 2.2(b). In this single queue, each entry is a pointer
to the process control block of a particular process. In other words, each block represents
one process here.

2.2.2 The Creation and Termination of Processes


The life of a process is bounded by its creation and termination. Hence, here we
will discuss these aspects.

Process Creation: W hen a new process has to be added to the job pool, the OS builds
the data structures for managing this process and allocates address space in main
memory. These actions form the creation of a new process. The situations that will lead to
the process creation are listed in Table 2.1.

Table 2.1 Reasons for Process Creation


Situation Description
New batch job The OS is provided with a batch job control stream, usually
on tape or disk. W hen the OS is prepared to take on new
work, it will read the next sequence of job control commands.
Interactive logon A user at a terminal logs on to the system.
Created by OS to The OS can create a process to perform a function on behalf
provide a service of a user program, without the user having to wait (e.g., a
process to control printing)
Spawned by existing For purposes of modularity or to exploit parallelism, a user
process program can dictate the creation of a number of processes.

Table 2.2 Reasons for Process Termination

 Normal Completion  Time Limit exceeded  Memory unavailable


 Bounds violation  Protection Error  Arithmetic error
 Time overrun  I/O failure  Invalid instruction
 Privileged instruction  Data misuse  Operator/OS intervention
 Parent termination  Parent request
4

Most of the times, the OS creates the processes depending on various situations. But,
sometimes, it creates a process based on the explicit request of another process. This is
called as process spawning. W hen one process spawns another, the first process is
called as parent process and the spawned process is called as child process.

Process Termination: Every process that is created will be terminated at some moment
of time. There are several reasons for a process to get terminated as listed in Table 2.2.

2.2.3 A Five-State Model


If all the processes are always ready to execute, then the queue system showed in Figure
2.2(b) will be effective. This queue may work on first-in-first-out basis or round-robin basis.
But, we cannot say that all the processes in not running state are ready to run. Because
some of them may be waiting for some I/O, blocked by other process etc. So, the
dispatcher needs to look for a process which is really ready to execute.

To avoid this problem, it is better to split not running state into two parts viz. ready
and blocked. This model, known as five-state model is shown in Figure 2.3. The five
states of this model are explained below:

Figure 2.3 Five-state Process Model

 Running: The process that is currently being executed.


 Ready: A process that is ready to execute when given an opportunity.
 Blocked/Waiting: A process that cannot execute until some event occurs like I/O
operation.
 New: A process that has been created just now and not yet been put into job pool by
the OS. That is, the process which has not yet been loaded into main memory.
 Exit: A process that has been released from the job pool by OS, either because of
successful termination or due to abnormal termination.
5

(a) Single Blocked Queue

(b) Multiple Blocked queues


Figure 2.4 Queuing model

Any process has to undergo transition between these states. The possible transitions are
listed below:
 Null to New: A new process is created for execution. This event may occur due to
any reasons listed in Table 2.1.
 New to Ready: The OS will move a process from New state to Ready when it is
ready to take additional process. Generally, there will be a limit for a number of
processes that can be kept on a queue so as to maintain the efficiency.
 Ready to Running: A scheduler or dispatcher selects one ready process for
execution.
 Running to Exit: Currently running process is terminated by OS. (Refer Table 2.2
for reasons for termination).
 Running to Ready: Most of the OS have a limit for a time duration for which
a process can run continuously. After that, even if it is not completed, it has to
leave
the processor for other waiting processes and has to enter a job queue.
6

 Running to Blocked: If a process requests for some resources, then it will be kept
in a blocked state.
 Blocked to Ready: Once a process gets the requested resources and completes
the task using those resources, it can enter into ready state.
 Ready to Exit: Though this state is not shown in the diagram, it may occur.
Sometimes, a child process may be in ready state, and its parent may exit. Then, the
child process also will be exited. Or, the parent itself may terminate the child.
 Blocked to Exit: A process may not get the resources that it requested or it
exceeds the waiting time.

Figure 2.4 shows the queuing discipline maintained by processes with respect to the states
mentioned in Figure 2.3.

2.2.4 Suspended Processes


In the whole course of process execution, OS may suspend a process sometimes. The
reason is as explained below.

Assume an OS without virtual memory. Then all the processes to be executed must be
loaded into main memory. Most of the processes will require I/O and hence may get
blocked. As, I/O speed is much slower than that of processor, the processor would stay idle
again. Hence, when none of the processes in the main memory is in Ready state, the OS
can swap (move) one of the blocked processes into the disk. This will form a
suspend queue. Now, any other process can be loaded into the main memory and start
execution. As disk I/O is faster, the swapping will increase the performance. W ith
the usage of swapping, one more state will be added to the process behavior model as
shown in Figure
2.5 (a).

W hen OS performs a swapping-out operation, it can load either a newly created


process into main memory or it can load a previously suspended process. But, a
suspended process might have been actually a blocked process (waiting for I/O). Hence, the
OS has to determine: W hether a process in suspended queue is
 a process waiting on an event – that is, blocked or not
 a process has been swapped out of main memory – that is suspended or not.

Keeping above two conditions, we will have four states:


 Ready: The process is in main memory and ready for execution
 Blocked: The process is in main memory and waiting for an event
 Blocked/Suspend: The process is on disk (secondary memory) and waiting for an
event
 Ready/Suspend: The process is on disc, but ready for execution as soon as it gets
loaded into main memory

These states can be included in the state diagram as shown in Figure


2.5(b).
7

(a) W ith one suspend state

(b) W ith two suspend states


Figure 2.5 Process State Transition Diagram with Suspend States

2.2.5 Other Uses of Suspension


A suspended process has following characteristics:
 The process is not immediately available for execution
 The process may or may not be waiting for an event
 The process was kept in suspended queue by an agent –the process itself, or parent
or OS.
 The process may not be removed from this state until the agent explicitly orders to
remove.

Some of the reason for process suspension is listed in Table 2.3. It also describes
the usages by suspending the process.
8

Table 2.3 Reasons for Process Suspension


Reason Description
Swapping The OS needs to release sufficient main memory to bring
in a process that is ready to execute.
Other OS Reason OS suspects process of causing a problem.
Interactive User User may wish to suspend a process to debug or in
Request connection with the use of a resource
Timing A process may be executed periodically and may be
suspended while waiting for the next time slot.
Parent Process A parent process may wish to suspend execution of a child
Request to examine or modify the suspended process, or to
coordinate the activity of various children.
99


2.10 CPU SCHEDULER
CPU scheduling is a basis of multiprogrammed OS. By switching CPU among processes,
the OS makes the computer more productive. In multiprogrammed OS, some process has
to keep running all the time in CPU without keeping it idle. This will lead to maximum CPU
utilization.

W henever the CPU becomes idle, the OS must select one of the processes in the
ready queue to be executed. The selection process is carried out by the short-term
scheduler (or CPU scheduler). The processed picked from ready queue need not be first-
come-first- out queue. There are various types like shortest job first, round robin etc.

2.10.1 CPU – I/O Burst Cycle


The success of CPU scheduling depends on the following property of processes: Process
execution consists of a cycle of CPU execution and I/O wait. Processes alternate between
these two states. Process execution begins with a CPU burst. That is followed by an
I/O burst, then another CPU burst, then another I/O burst, and so on. Eventually, the last
CPU burst will end with a system request to terminate execution, rather than with
another I/O burst. This is illustrated in Figure 2.17(a). The duration of CPU bursts vary by
the process and by the computer. Still, they have exponential/hyper exponential frequency
curve with many short CPU bursts and few long CPU bursts as shown in Figure 2.17(b).

(a) (b)
Figure 2.17(a) Alternating sequence of CPU & I/O bursts (b) Histogram of CPU burst times
10
10

2.10.2 Preemptive Scheduling


CPU scheduling decisions may take place under the following four circumstances:
1. W hen a process switches from the running state to the waiting state
2. W hen a process switches from the running state to the ready state
3. W hen a process switches from the waiting state to the ready state
4. W hen a process terminates
In the situations (1) and (4) above, there is no choice for scheduling. A new process has to
be taken from ready queue. This is called as non-preemptive scheduling. But, in
the situations (2) and (3), there will be preemptive scheduling.

Under non-preemptive scheduling, once the CPU has been allocated to a process, the
process keeps the CPU until it releases the CPU either by terminating or by switching
to the waiting state. Preemptive scheduling incurs a cost. Consider the case of two
processes sharing data. One may be in the midst of updating the data when it is
preempted and the second process is run. The second process may try to read the data,
which are currently in an inconsistent state.

2.10.3 Dispatcher
The dispatcher is the module that gives control of the CPU to the process selected by the
short-term scheduler. This function involves:
 Switching context
 Switching to user mode
 Jumping to the proper location in the user program to resume that program
The dispatcher should be as fast as possible, because it will be invoked during
every process switch.

2.10.4 Scheduling Criteria


Different scheduling algorithms have different properties to support different types of
processes. There are many criteria to compare CPU scheduling algorithms as given below:
 CPU Utilization: CPU must be as busy as possible. CPU utilization may range from
0% to 100%. The real time systems have CPU utilization as 40% to 90%.
 Throughput: The number of processes completed per time unit is called
as throughput.
 Turnaround time: The interval from the time of submission of a process to the time
of completion is the turnaround time. Turnaround time is the sum of the
duration spent waiting to get into memory, waiting in the ready queue, executing on
the CPU, and doing I/O.
 Waiting Time: The CPU-scheduling algorithm does not affect the amount of time
during which a process executes or does I/O; it affects only the amount of time that a
process spends waiting in the ready queue. W aiting time is the sum of the
durations spent waiting in the ready queue.
 Response Time: The time duration from the submission of a request till the
first response received is known as response time.
11
11

The optimization criteria for CPU scheduling will be –


 Maximum CPU utilization
 Maximum throughput
 Minimum turnaround time
 Minimum waiting time
 Minimum response time

2.11 SCHEDULING ALGORITHMS


CPU scheduling deals with the problem of deciding which of the processes in the
ready queue is to be allocated the CPU. W e will discuss various algorithms used for
CPU scheduling.

2.11.1 First Come, First Serve Scheduling


FCFS is the simplest algorithm which is managed by a simple FIFO queue. W hen a
process enters the ready queue, its PCB is linked onto the tail of the queue. W hen the CPU
is free, it is allocated to the process at the head of the queue. The running process is then
removed from the queue. Usually, the average waiting time for FCFS will be more.

Example 1: The three processes P1 , P2 , and P3 arrive at a time 0 with the CPU burst time
given as below. Calculate average waiting time and average turnaround time.

Process Burst Time


P1 24
P2 3
P3 3

Solution: Suppose, the processes arrive in the order P1, P2 , P3, then the Gantt Chart for
the schedule is –
P1 P2 P3

0 24 27 30

W e can observe that, W


aiting time for P1 = 0
W aiting time for P2 = 24
W aiting time for P3 = 27
Thus, Average waiting time = (0 + 24 + 27)/3 = 17 milliseconds

Turnaround time is the duration from submission of the process till its completion. Hence,
turnaround time for P1= 24
Turnaround time for P2 = 27
Turnaround time for P3 = 30
Average turnaround time = (24+27+30)/3 = 27 milliseconds
12
12

Throughput is total number of processes completed per one unit of time. Here, 30
time units were for completing 3 processes. So, for 1 time unit, the number of
processes that can be completed is 3/30. That is,
Throughput = 3/30 = 0.1

Example 2:
Assume, in the above example, the processes arrive in the order P2 , P3 , P1 then the Gantt
Chart for the schedule is –

P2 P3 P1

0 3 6 30
Now,
W aiting time for P1 = 6
W aiting time for P2 = 0
W aiting time for P3 = 3
Thus, Average waiting time = (6 + 0 + 3)/3 = 3 milliseconds
Average turnaround time = (30 + 3 + 6)/3 = 13 milliseconds
Here also, throughput = 3/30=0.1

NOTE: W e can observe that average waiting time in FCFS vary substantially if there is
a much variation in CPU burst time of the processes.

Disadvantages:
 FCFS is non-preemptive, hence the average waiting time can be more.
 Troublesome in time-sharing systems, where each user needs to get a share of
CPU. But, FCFS scheme will keep CPU for a longer duration.

2.11.2 Shortest-Job-First Scheduling


As the name suggests, in SJF scheduling, the process with a shortest CPU-burst
is allocated the CPU first. If two processes have equal CPU-burst value, then FCFS is used
to break a tie. There are two schemes in SJF viz. non-preemptive and preemptive.

 Non-preemptive SJF Scheduling: Here, once the CPU is given to the process, it
cannot be preempted until completes its CPU burst.

Example 1: There are four processes P1 to P4 all arrived at the time 0 and burst time
is as given below. Compute average waiting time and average turnaround time.

Process Burst Time


P1 6
P2 8
P3 7
P4 3
The Gantt chart would be –
P4 P1 P3 P2

0 3 9 16 24

W aiting time for P1 = 3


W aiting time for P2 = 16
W aiting time for P3 = 9
W aiting time for P4 = 0
Average waiting time = (3+16+9+0)/4 = 7 milliseconds

Turnaround time for P1 = 9


Turnaround time for P2 =24
Turnaround time for P3 = 16
Turnaround time for P4 = 3
Average turnaround time = (9 + 24 + 16 + 3)/4 = 13 milliseconds
Throughput = 4/24 = 0.1667

Note that, if we would have used FCFS here, the average waiting time would have been
10.25 milliseconds.

Example 2: There are four processes P1 to P4 which arrived at different times as given
below. Compute average waiting time and average turnaround time.

Process Arrival Time Burst Time


P1 0 7
P2 2 4
P3 4 1
P4 5 4
Solution:
In SJF – non-preemptive algorithm when arrival time of processes is different,
one should be careful while drawing Gantt chart. Here, at the time 0, only one process
P1 is in a ready queue. Hence, it will take a CPU. At the time 2, the process P2
arrives, whose burst time is smaller than P1. But, as it is non-preemptive, P1 will
continue to execute. W hen P1 completes the execution and leaves the CPU at the
time 7, observe that all three processes P2, P3 and P4 are arrived. Now, take the
shorter one, that is P3. After completion of P3, there is a tie between P2 and P4 as
both have same burst time. Now, resolve the tie using FCFS. Hence, the Gantt chart
would be –

W aiting time for P1 =


0
W aiting time for P2 = 8 – 2 (arrival time) =
6
W aiting time for P3 = 7 – 4 (arrival time) =
3
W aiting time for P4 = 12 – 5 (arrival time) =
7
Average waiting time = (0+6+3+7)/4 = 4ms

Turnaround time for P1 = 7 (completion time) – 0 (arrival time) = 7


Turnaround time for P2 = 12 (completion time) –2 (arrival time) = 10
Turnaround time for P3 = 8 (completion time) – 4 (arrival time) = 4
Turnaround time for P4 = 16 (completion time) –5 (arrival time) = 11
Average turnaround time = (7+10+4+11)/4 = 8ms
Throughput = 4/16 = 0.25

SJF algorithms usually give minimum average-waiting time and hence an


optimal algorithm. But, the problem is – in a dynamic situation, knowing the length of
next CPU burst will be difficult.

 Preemptive SJF Scheduling: W hen a new process enters a ready queue


while another process is still executing, there will be a choice. If a new process
arrives with CPU burst length less than remaining time of current executing process,
preempt. This scheme is also known as the Shortest-Remaining-Time-First (SRTF).
In this scheme, the arrival time of every process plays a key role.

Example: There are four processes P1 to P4 arrived at different time as given


below. Compute average waiting time and average turnaround time.

Process Arrival Time Burst Time


P1 0 7
P2 2 4
P3 4 1
P4 5 4

Solution: Here, at the time 0, only process P1 is in a ready queue and hence will start
executing. But, after 2 milliseconds, the process P2 arrives with a burst time as 4.
The remaining time for P1 is 5, which is greater than time required for P2. Hence,
P1 is preempted and P2 gets CPU. After 2 more milliseconds (that is, at the time
4), P3 arrives. The burst time of P3 (1) is lesser than the remaining time of P2(2).
Hence, P2 is preempted and P3 is given a CPU. W hen P3 completes, the remaining
time of other three processes are –
P1 -- 5
P2 – 2
P4 – 4
Now, the shortest job will be getting the CPU. Hence, the Gantt chart would be –

P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Now, the waiting time for each process is calculated as below:
Waiting time for P1: P1 arrived at the time 0 and had been given CPU. After 2ms, it
has been preempted. Later, at the time 11, it was given a CPU again. Note that, it has
finished the task of 2ms already, hence, the waiting time would be
11 – 2 (completed portion) = 9
Waiting time for P2: P2 arrived at the time 2, and given CPU immediately. Then after 2
ms, it has been preempted. Again, CPU was given at the time 5. So, waiting time will
be
5 – 2 (arrival time) – 2 (completed time) = 1
Waiting time for P3: P3 arrived at the time 4 and was given CPU immediately. It was
not preempted during its execution. And hence, the waiting time is 0.
Waiting time for P4: P4 arrived at the time 5 and was given CPU at the time 7. Hence,
its waiting time is 2.

The average waiting time = (9+1+0+2)/4 = 3 ms.

Turnaround time is calculated as below –


Turnaround time for P1 = 16 (completion time) – 0 (arrival time) = 16
Turnaround time for P2 = 7 (completion time) – 2 (arrival time) = 5
Turnaround time for P3 = 5 (completion time) – 4 (arrival time) = 1
Turnaround time for P4 = 11 (completion time) – 5 (arrival time) = 6
Average turnaround time = (16+5+1+6)/4 = 7 ms

Throughput = 4/16 = 0.25

2.11.3 Priority Scheduling


Priority scheduling is a special case of general priority scheduling algorithm. A priority is
associated with each process, and the CPU is allocated to the process with the highest
priority. Equal-priority processes are scheduled in FCFS order. SJF is a priority algorithm
where priority is nothing but, its burst time – smaller the burst time, higher the
priority. Priority status is normally treated as – lower the number, higher the priority.

The priority scheduling also can be preemptive and non-preemptive. In non-preemptive,


normally, the priorities of all the processes are known before. That is, here, we assume that
all the processes have arrived at the same time and CPU is assigned based on
their priorities. But, sometimes, all the processes may not enter the ready queue at the
same time. W hile one process is running, another process with higher priority than the
currently executing process may enter the ready queue. Then currently executing process
has to be preempted. So, here, preemption will be based on the arrival of new job
with higher priority. This is somewhat similar to preemptive SJF, where new job with
lesser burst time will be given a CPU.

Example 1: There are five processes P1, P2, P3, P4 and P5 and have arrived at the time 0
in that order. The priority and burst time are as given below –
30
30

Process Burst Time Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Now, the Gantt chart would be –
P2 P5 P1 P3 P4

0 1 6 16 18 19
The average waiting time = (6+0+16+18+1)/5 = 8.2 milliseconds
The average turnaround time = (16+1+18+19+6)/5 = 12 ms
Throughput = 5/19 = 0.2632

Example 2: Assume there are four processes whose arrival time, burst time and
priority have been given as below. Compute average waiting time and turnaround time.

Process Arrival Burst Priority


Time Time
P1 0 8 3
P2 1 4 2
P3 2 9 4
P4 3 5 1
Solution:
Note that, the process P1 with priority 3 arrived at the time 0 and no other process is there
in a ready queue. Hence, it will be given a CPU. But, just after 1 millisecond, the
process P2 with higher priority than P1 arrives and so, P1 has to be preempted and P2
will get CPU. W hen P2 is under execution, P3 arrives at the time 2. But, its priority is
lower than P2. Hence, P2 will continue to execute. Later, P4 arrives at the time 3, which
has higher priority than the currently executing P2. So, P2 will be preempted at P4
gets the CPU. Thus, P4 will finish execution before all other processes. Now, the
remaining processes will be executed as per their priorities. Thus, the Gantt chart would be

P1 P2 P4 P2 P1 P3

0 1 3 8 10 17 26

W aiting time for P1 = 10 – 1 (completed duration) – 0 (arrival time) =


9
W aiting time for P2 = 8 – 2 (completed duration) – 1 (arrival time) =
5
W aiting time for P3 = 17 – 2 (arrival time) =
31
15 31
W aiting time for P4 =
0
Average waiting time = (9+5+15+0)/4 = 7.25ms
32
32

Turnaround time for P1 = 17 – 0 (arrival time) = 17


Turnaround time for P2 = 10 – 1 (arrival time) = 9
Turnaround time for P3 = 26 – 2 (arrival time) = 24
Turnaround time for P4 = 8 – 3 (arrival time) = 5
Average Turnaround time = (17+9+24+5)/4 = 13.75ms
Throughput = 4/26 = 0.1538

NOTE: The priority scheduling has one drawback: the lower priority processes may never
gets executed. This problem is known as starvation. As higher priority processes keeps
getting added to ready queue, the lower priority processes will find indefinite delay in
getting the CPU. In such a situation, either the process will run only when the system
becomes free, or eventually, it may crash with all un-finished processes. To solve this
problem, there is a remedy called – aging. Here, the priority of the process will
be increased as the time passes by. Hence, an initially lower priority process will
eventually becomes higher priority process and gets the CPU.

2.11.4 Round-Robin Scheduling


This algorithm is designed for time-sharing systems. It is similar to FCFS, but a
time quantum is introduced. CPU will be given to the process for one unit of time quantum.
After that, the process will be preempted and added back to ready queue. The ready
queue will behave as a circular queue. The scheduler will go around the ready queue and
allocates CPU for each process for a time interval of one time quantum. The value of time
quantum here can be 10ms to 100ms.

There are two possibilities:


o The process will not be completed in a one time quantum. Then, the context
switch will happen and the current process is kept back at the tail of the ready
queue. And, the next process in a ready queue is picked and allotted a CPU.
o The process will be completed within the duration of one time quantum. Then the
process will give up the CPU voluntarily. Then, next process in a ready queue will
be picked for execution.

Example 1: Consider the processes P1, P2 and P3 which are arrived at the time 0. Let the
time quantum be 4 milliseconds. The burst time is given as below:

Process Burst Time


P1 24
P2 3
P3 3
The Gantt chart would be –
P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30
33
33

Note that, the burst time of processes P2 and P3 are lesser than the actual time quantum
and hence, they will give up the CPU immediately after their completion.

W aiting time for P1 = 0 (first pickup) + {10 (next pickup) – 4 (previous CPU release )} = 6
W aiting time for P2 = 4
W aiting time for P3 = 7
Average waiting time = (6+4+7)/3 = 5. 67ms
Average turnaround time = (30 +7+10)/3 = 15.67ms
Throughput = 3/30 = 0.1

Example 2: Consider the processes P1, P2, P3 and P4 which are arrived at the time 0. Let
the time quantum be 20 milliseconds. The burst time is given as below:

Process Burst Time


P1 53
P2 17
P3 68
P4 24

The Gantt chart would be –


P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154


162

W aiting time for P1 = 0 (first pickup) + {77 (next pickup) – 20 (previous release)}
+ {121(next pick up) – 97 (previous release)}
= 81

W aiting time for P2 = 20 (first pickup)

W aiting time for P3 = 37 (first pickup) + {97 (next pickup) – 57 (previous release)} +
{134 (next pickup) – 117 (previous release)} +
{154 (next pickup) - 154 (previous release)}
= 94

W aiting time for P4 = 57 (first pickup) + {117 (next pickup) – 77 (previous release)}
= 97
Average waiting time = (81+20+94+97))/4 = 73 ms
Average turnaround time = (134+37+162+121)/4 =113.5 ms
Throughput = 4/162 = 0.0247

2.11.5 Multilevel Queue Scheduling


Sometimes, the processes can be easily classified into different groups like –
o Foreground (or interactive) processes
o Background (or batch) processes
34
34

These two types have different response – time requirements and different scheduling
needs. And, foreground processes may have higher priority over background processes.

A multilevel queue scheduling algorithm partitions the ready queue into several separate
queues as shown in Figure 2.18 . Each queue has its own scheduling algorithm. Normally,
foreground queue will have Round Robin algorithm whereas, the background queue has
FCFS algorithm.

Figure 2.18 Multilevel queue scheduling

2.11.6 Multilevel Feedback Queue Scheduling


In, multilevel queue scheduling algorithm, processes are permanently assigned to a queue
as soon as they enter a system. They do not move between the queues. This inflexibility is
solved using multilevel feedback queue scheduling algorithm, where each process can
move to any other queue. The idea is to separate processes with different CPU – burst
characteristics. If a process uses too much CPU time, it will be moved to a lower priority
queue. Similarly, if a process waits too long in a lower priority queue, it may be moved to a
higher-priority queue.

Multilevel-feedback-queue scheduler defined by the following parameters:


o number of queues
o scheduling algorithms for each queue
o method used to determine when to upgrade a process
o method used to determine when to demote a process
o method used to determine which queue a process will enter when that process
needs service
35
35

2.12 INTRODUCTION TO MUTUAL EXCLUSION AND


SYNCHRONIZATION

The OS design is concerned more about management of processes and threads with
respect to multiprogramming, multiprocessing and distributed processing. Concurrency is a
fundamental issue for all these areas. Concurrency includes design issues
like communication among processes, sharing of and competing for resources,
synchronization of the activities of multiple processes, and allocation of processor
time to processes. Concurrency arises in three different contexts:
 Multiple applications: Multiprogramming was invented to allow processing time to
be dynamically shared among a number of active applications.
 Structured applications: As an extension of the principles of modular design and
structured programming, some applications can be effectively programmed as a set
of concurrent processes.
 Operating system structure: The same structuring advantages apply to systems
programs, and we have seen that operating systems are themselves often
implemented as a set of processes or threads.

In this chapter, the importance of concurrency is discussed in detail. Some of the important
key words in the study of concurrency are listed below.
36
36

One can understand the meaning of concurrency with the definition: concurrency is the
property of program, algorithm, or problem decomposability into order-independent
or partially-ordered components or units.

2.13 PRINCIPLES OF CONCURRENCY


In a single – processor systems, the processes are switched (interleaved) among
themselves (Figure 2.19) and appear to be executed simultaneously. In multiprocessing
systems, the processes may be switched as well as overlapped (Figure 2.20).

Figure 2.19 Single – processing system: Interleaving of processes

Figure 2.20 Multi – processing system: Interleaving and overlapping of processes

Interleaving and overlapping cause certain problems as listed below:


 Sharing of global resources may be dangerous
 Optimal allocation of resources may be difficult for OS
 Difficult to locating programming errors

To understand these problems, an example is considered in the following section.

2.13.1 A Simple Example


37
37

Consider the following function:

void echo()
{
chin = getchar();
chout = chin;
putchar(chout);
}

It is easily observed that chin and chout are global variables. The function echo()
reads one character from the keyboard and stores into chin. It is then assigned to chout.
And then it is displayed. The echo() function is global and available to all applications.
Hence, only a single copy of echo() is loaded into main memory. Sharing of such global
variables will cause certain problems in both single-processor and multi-processor systems.

In a single-processor multiprogramming system, assume there is only one I/O


devices (keyboard and monitor). Assume, a process P1 invokes echo() function
and it is interrupted immediately (due to some reason) after reading a character, say, x
from the keyboard. Now, the global variable chin contains x. At this moment of time,
the process P2 invokes the function echo()and executes. Let the character read now be
y. Assume, the process P2 completes its execution and displays y. Now, the process
P1 is resumed and displays the character as y, because, the recent content of chin is
being y.

To solve this problem, only one process should be allowed to invoke echo() function for
a given moment of time. That is, when P1 has invoked the function and got interrupted,
P2 may try to invoke the same function. But, P2 must be suspended and should be
made to wait. Only after P1 comes out of the echo() function, P2 must be resumed.

Thus, it is necessary to control the code for protecting shared global variable (or any other
global resources).

In a multiprocessor system, similar problem occurs. Consider there are two


processes P1 and P2 executing on two different processors. The execution of instructions
of echo() function for both P1 and P2 are as shown below. Note that, the instructions
on same line executes in parallel.
Process P1 Process P2

1 ------------------------- ------------------------
2 chin = getchar(); ------------------------
3 ------------------------- chin = getchar();
4 chout=chin; chout=chin;
5 putchar(chout); -----------------------
6 ------------------------- putchar(chout);
7 ------------------------ -----------------------
38
38

One can observe that, the global variable chin, initially read through P1, is overwritten by
P2. Here also, to avoid the problem, control the access of shared resources.

2.13.2 Race Condition


A race condition occurs when multiple threads/processes read/write data items so that the
final result depends on order of execution of instructions. Consider two examples
to understand this concept.

Example 1: Assume that two processes P1 and P2 are sharing a global variable a.
The process P1 has an instruction to update the value of a as –
a= 1;
The process P2 also has an instruction –
a= 2;
Both P1 and P2 will be executing their own set of instructions. Now, the value of a depends
on the order of execution of above statements. In other words, the loser of the race (the
process which executes last) will decide the value of a.

Example 2: Let there are two processes P3 and P4 sharing two global variables b and c.
Initially,
b=1 and c = 2
The process P3 has a statement –
b=b+c;
whereas, the process P4 has a statement –
c=b+c;
One can easily make out that, the final value of b and c depends on order of execution of
both of these statements. If P3 executes first,
b =3 and
c=5
On the other hand, if P4 executes first,
b= 4 and
c=3

2.13.5 Requirements for Mutual Exclusion


The facility of mutual exclusion should meet the following requirements:
 Only one process at a time is allowed in the critical section for a resource
 A process that halts in its non-critical section must do so without interfering
with other processes
 No deadlock or starvation
 A process must not be delayed access to a critical section when there is no other
process using it
 No assumptions are made about relative process speeds or number of processes
 A process remains inside its critical section for a finite time only

These requirements of mutual exclusion can be satisfied by number of ways:


 Leaving the responsibility of concurrent execution to processes themselves
39
 Usage of special-purpose machine instructions 39
 Providing the support within the OS or programming language

2.15 SEMAPHORES
As discussed earlier, apart from hardware-enabled mechanism for concurrency, there
are OS and programming language mechanisms as well. Semaphore is one such
mechanism for providing concurrency. Semaphore is an integer value used for
signaling among processes. Only three operations may be performed on a
semaphore, all of which are atomic: initialize, decrement, and increment. The
decrement operation is for blocking of a process, and the increment operation is for
unblocking of a process.

The fundamental principle is: Two or more processes can cooperate by means of simple
signals, such that a process can be forced to stop at a specified place until it has received a
specific signal. For signaling, special variables called semaphores are used. To transmit a
signal via semaphore s, a process executes the primitive semSignal(s). To receive a signal
via semaphore s, a process executes the primitive semWait(s). If the corresponding
signal has not yet been transmitted, the process is suspended until the transmission takes
place.

To achieve the desired effect, we can view the semaphore as a variable that has an
integer value upon which only three operations are defined:
1. A semaphore may be initialized to a nonnegative integer value.
2. The semWait operation decrements the semaphore value. If the value becomes
negative, then the process executing the semWait is blocked. Otherwise, the
process continues execution.
3. The semSignal operation increments the semaphore value. If the resulting value is
less than or equal to zero, then a process blocked by a semWait operation, if any, is
unblocked.
40
40

The non-binary semaphore is also known as counting semaphore or


general
semaphore.

The semaphore is initialized to 1. Thus, the first process that executes a semWait will be
able to enter the critical section immediately, setting the value of s to 0. Any other process
attempting to enter the critical section will find it busy and will be blocked, setting the value
of s to –1.

2.15.2 The Producer/Consumer Problem


The producer/consumer problem is one of the most commonly faced problems
in concurrent processing. The general statement is this: There are one or more
producers generating some type of data (records, characters) and placing these in a buffer.
There is a single consumer that is taking items out of the buffer one at a time. The
system is to be constrained to prevent the overlap of buffer operations. That is, only one
agent (producer or consumer) may access the buffer at any one time. The problem is to
make sure that the producer won’t try to add data into the buffer if it’s full and that
the consumer won’t try to remove data from an empty buffer.

Let us assume that the buffer is of infinite size. Consider the code segments for producer
and consumer as given below:

Producer Consumer
while (true) while (true)
{ {
/* produce item v */; while (in <= out)
b[in] = v; /* do nothing */;
in++;
} w = b[out];
out++;
/* consume item w */;
}

The Figure 2.24 illustrates the structure of buffer b. The producer can generate items and
store them in the buffer at its own speed. Each time, an index in is incremented.
The consumer proceeds in a similar fashion but must make sure that it does not attempt to
read from an empty buffer. Hence, the consumer makes sure that the producer is ahead
of consumer (in > out).
41
41

Figure 2.24 Infinite buffer for produce

r/consumer problem
42
42

If we add a realistic restriction on this problem, it can be solved easily using


semaphore. The restriction is: instead of taking infinite buffer, consider a finite buffer of size
n having a circular storage as shown in Figure 2.25.

Figure 2.25 Finite circular buffer for producer/consumer problem

Since, it will work in circular manner, the pointers in and out will be considered with modulo
n. Now, the situations would be –

Block on Unblock on
Producer: insert in full buffer Consumer: item inserted
Consumer: remove from empty buffer Producer: item removed

Now, the producer and consumer functions can be given as –

Producer Consumer
while (true) while (true)
{ {
/*produce item v */ while(in==out)
while((in+1)%n==out) //do nothing
//do nothing w=b[out]; b[in]=v;
out=(out+1)%n; in=(in+1)%n;
/*consume item w */
} }

2.16 MONITORS
The monitor is a programming-language construct that provides equivalent functionality to
that of semaphores and that is easier to control. The monitor construct has been
implemented many programming languages like Concurrent Pascal, Pascal-Plus, Java etc.
It has also been implemented as a program library. This allows programmers to put
a monitor lock on any object. For example, we can lock all linked list with one lock, or
one lock for each list or one lock for each element of every list.

2.16.1 Monitor with Signal


43
43

A monitor is a software module consisting of one or more procedures, an


initialization sequence, and local data. The chief characteristics of a monitor are the
following:
1. The local variables are accessible only by the monitor’s procedures and not by any
external procedure.
2. A process enters the monitor by invoking one of its procedures.
3. Only one process may be executing in the monitor at a time; any other processes
that have invoked the monitor are blocked, waiting for the monitor to become
available.

Monitor must include synchronization tools for helping concurrency. For example, suppose
a process invokes the monitor and, while in the monitor, it must be blocked until some
condition is satisfied. A facility is needed by which the process is not only blocked but
releases the monitor so that some other process may enter it. Later, when the condition is
satisfied and the monitor is again available, the process needs to be resumed and allowed
to reenter the monitor at the point of its suspension.

Figure 2.26 Structure of Monitor


50
50

A monitor supports synchronization by the use of condition variables that are contained
within the monitor and accessible only within the monitor. Condition variables are a
special data type in monitors, which are operated on by two functions:
 cwait(c) : Suspend execution of the calling process on condition c . The monitor is
now available for use by another process.
 csignal(c) : Resume execution of some process blocked after a cwait on the same
condition. If there are several such processes, choose one of them; if there is
no such process, do nothing.

2.18 READERS/WRITERS PROBLEM


The readers/writers problem is defined as follows:
There is a data area shared among a number of processes. The data area could be
a file, a block of main memory, or a set of processor registers. There are a number
of processes that only read the data area (readers) and a number that only write to
the data area (writers). The conditions that must be satisfied are as follows:
1. Any number of readers may simultaneously read the file.
2. Only one writer at a time may write to the file.
3. If a writer is writing to the file, no reader may read it.

Thus, we can make out that, a writer wants every other process to be blocked for its
execution; whereas, a reader need not block other processes.

The problem can be solved with the help of semaphores. Here, there are two ways: readers
having the priority and writers having priority.

2.18.1 Readers Have Priority


Here, once a single reader has begun to access the data area, it is possible for readers to
retain control of the data area as long as there is at least one reader in the act of reading.
The procedure is explained here –
 The first reader blocks if there is a writer; any other readers who try to enter block on
mutex.
 W hile exiting, the last reader gives signal to a waiting writer.
 W hen a writer exits, if there is both a reader and writer waiting, which goes
next depends on the scheduler.
 If a writer exits and a reader goes next, then all other waiting readers will get
a chance to access.
But, in this methodology, writers are subject to starvation.
51
51

2.18.2 Writers have Priority


Unlike the previous case, here writers are decision makers. No new readers are allowed
access to the data area once at least one writer has declared a desire to write. This is
accomplished by forcing every reader to lock and release the semaphore individually. The
writers on the other hand don't need to lock it individually. Only the first writer will lock the
semaphore and then all subsequent writers can simply use the resource as it gets freed by
the previous writer. The very last writer must release the semaphore, thus opening the gate
for readers to try reading.

In this methodology, the readers may starve.

As both solutions using semaphore will lead to starvation, another solution using message
passing can be adopted. In this case, there is a controller process that has access to the
shared data area. Other processes wishing to access the data area send a request
message to the controller, are granted access with an “OK” reply message, and indicate
completion of access with a “finished” message. The controller is equipped with three
mailboxes, one for each type of message (i.e. – a request, an OK, a finished) that it may
receive. The controller process services write request messages before read
request messages to give writers priority. In addition, mutual exclusion must be enforced.

Few Exercise Problems on CPU Scheduling:


1. Draw the Gantt Chart and compute average waiting time, average turnaround time
and throughput using the following algorithms:
(i) FCFS (ii) SJF (preemptive and non-preemptive)

Process Arrival Time Burst Time


P1 0 8
P2 1 4
P3 2 9
P4 3 5

2. Draw the Gantt Chart and compute average waiting time, average turnaround time
and throughput using the following algorithms:
(i) FCFS (ii) SJF (ii) RR (Quantum= 10ms)

Process Burst Time


P1 10
P2 29
P3 3
P4 7
P5 12
52
52

3. Draw the Gantt Chart and compute average waiting time, average turnaround time
and throughput using the following algorithms:
(i) FCFS (ii) SJF (preemptive) (ii) Priority(preemptive) (iv) RR (Q=4)

Process Arrival Time Burst Time Priority


P1 0 8 3
P2 1 4 2
P3 2 9 4
P4 3 5 1

4. Draw the Gantt Chart and compute average waiting time, average turnaround time
and throughput using the following algorithms:
(i) FCFS (ii) SJF (ii) Priority (iv) RR (Q=1)

Process Burst Time Priority


P1 8 4
P2 2 1
P3 2 3
P4 3 3
P5 5 2

You might also like