Chapter 4 - Scheduling-1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Chapter 4

Scheduling
12 Hours
22 Marks

4.1 Scheduling

Scheduling is also referred as CPU scheduling. i.e. Switching the CPU


among various processes. It is also called as process scheduling. Most often the
terms process scheduling and thread scheduling are also used interchangeably.
It forms basis for multi-programmed operating systems. When the CPU is
switched between multiple processes or threads, the productivity of the computer
is increased.
In a single-processor system, only one process can be executed by CPU at
one instance. If the system is multi-programmed, when multiple processes or
threads are in ready state simultaneously, they compete for CPU at the same
time. As only one CPU is available, a choice is to be made for selecting the next
process or thread to run on the processor.
The part of operating system that makes choice of next process or thread to
run is called as scheduler. The algorithm used for making this choice is called as
scheduling algorithm.
Computers have got so much faster nowadays with great speed of CPU.
Also, on a personal computer, most of the time there is only one active process.
So, scheduling does not matter much on simple PCs.
But in case of high-end networked workstations and servers, multiple
processes often compete for CPU. Therefore scheduling matters a lot.

4.1.1 Objectives
Objectives of scheduling are as follows:
1. To have some process running at all times.
2. To maximize CPU utilization.
3. To minimize idle time of CPU.
4. To overlap the processes for getting multi-programmed environment.
5. To be fair with all processes (Fairness).
6. To have good throughput.
7. To have low turnaround time.
8. To have low waiting time.
9. To have good response time.

4.1.2 CPU and I/O Burst Cycle


Generally, a process is executed until it requests for some I/O operation
(where it has to wait for completion of that I/O operation). After completion of the
I/O operation CPU resumes the execution of the process. In short, the process
execution consists of a cycle of CPU execution and I/O wait. Process switches
in between these two states.

4-1
When a process is executed by a CPU, it is called as CPU burst whereas
the waiting period for completion of I/O operation(s) required by the process is
called I/O burst. A process execution begins with a CPU burst followed by I/O
burst, which is followed by another CPU burst, and then another I/O burst and so
on as shown in figure 4.1. Generally process execution ends with a CPU burst.

main( )
{
int a,b,c; CPU Burst
float d;

scanf(“%d%d”,&a,&b); I/O Burst

c = a + b;
CPU Burst
d = c / 2.0;

printf(“Sum = %d, Average = %f”,c,d);


I/O Burst
getch( );

} CPU Burst

Figure 4.1: Cycle of CPU burst and I/O burst

Almost all processes switch between CPU burst and I/O burst as shown in
figure 4.1. Some processes spend most of their time with CPU as shown in figure
4.2. Such processes have longer CPU burst. These processes are called CPU
bound.

Long CPU burst Wait for I/O

Figure 4.2: CPU bound Process

But, some processes spend most of their time waiting for I/O as shown in
figure 4.3. Such processes have small CPU burst. These processes are called I/O
bound.

Short CPU burst Wait for I/O


Figure 4.3: I/O bound Process
4-2
4.1.3 Concept
Any process switches in between CPU burst and I/O burst. When a
running process enters in I/O burst, CPU becomes idle. CPU will remain idle
until the I/O operation of the process gets completed. This is clearly wastage of
precious CPU time. This time can be used productively with a multiprogramming
concept. In this concept, several processes are kept in memory at a time. When
one process goes in I/O burst, operating system takes away CPU from that
process and gives the CPU to another process which is ready for execution. This
is the very basic approach of scheduling.
But, this is not the only situation for scheduling. There are variety of
situations in which scheduling is needed. They are as follows
- When a new process is created, a decision is to be made whether to run
parent process or child process.
- When a process exits (i.e. process execution ends), some other process must
be chosen for execution.
- When a process gets blocked on an I/O or for some other reason, another
process has to be selected.
- When an I/O interrupt occurs, other process may be selected.
- Some operating systems make scheduling decision at each clock interrupt
or at each kth clock interrupt.
- When a process switches from waiting state to ready state (e.g at
completion of I/O), scheduling decision may be required to be taken.

Operating system can schedule processes at three different levels as,

- Long term scheduling


- Medium term scheduling
- Short term scheduling

Medium term
Scheduler Blocked & Ready
Swapped-out
Processes Queue

Long term
Scheduler
New Jobs Ready
(Batch) Queue CPU Exit
Short term
Scheduler

New Jobs Blocked


(Interactive) Processes
Queue

Figure 4.4: Scheduling levels

4-3
An operating system may use one or all of these levels.

4.1.3.1 Long term Scheduler:


If number of ready processes in the ready queue becomes very high, it
increases overhead on the operating system. So, it is better to have only limited
processes in the ready queue. Long term scheduler limits the number of
processes entering in the ready queue. New jobs trying to enter in ready queue
may be batch jobs or interactive jobs. Long term scheduler first disallows the
batch jobs and then interactive jobs which are crossing the limit of ready queue.
This is achieved by spooling the incoming jobs to a mass storage. Long
term scheduler selects processes from this pool and loads them into memory for
execution.
Frequency of execution of long-term scheduler is less.
Long-term scheduler controls the degree of multiprogramming (i.e. number
of processes in the memory).

4.1.3.2 Medium term Scheduler:


Main memory of a computer is limited and it can hold limited number of
processes. If less memory is available and a process is blocked, then it is swapped
out to the disk. Such processes are in Swapped out and blocked queue. When
I/O operation of such process is completed, the process enters in Swapped out
and ready queue. When some memory gets freed, operating system checks the
swapped out and ready queue for deciding which process is to be swapped in. This
is done by Medium term Scheduler.
It should work in coordination with long term scheduler.

4.1.3.3 Short term Scheduler:


Short term scheduler selects one of the processes from the ready queue
and allocates CPU to the process for its execution. The ready queue may be
implemented as First-In-First-Out (FIFO) queue, priority queue, a tree or simply
an unordered linked list.
One more component involved in the scheduling is dispatcher. It gives
control of the CPU to the selected process. It is responsible for context switching
and switching to user mode. The dispatcher should be fast as it is involved in
every process switch. The time taken by dispatcher for stopping one process and
starting another one is known as dispatch latency.
Short-term scheduler is executed most frequently.

4.1.4 Criteria
CPU scheduling algorithms can be compared with various criteria. These
criteria may conflict with each other. So, even if all the criteria are desirable,
operating system designer should select some of the criteria while designing
scheduling algorithms (sometimes called as scheduling objectives or scheduling
goals). The criteria include the following,
- CPU utilization
- Throughput
- Turnaround time
- Waiting time
- Response Time
4-4
- Fairness
- Proportionality

4.1.4.1 CPU Utilization


It is the fraction of time that the CPU is busy either on user processes or
the operating system. It is always desirable to keep CPU as busy as possible.
Ideally CPU utilization can range from 0 to 100. But practically, it ranges from 40
(for lightly loaded system) to 90 (for heavily loaded system).

4.1.4.2 Throughput
It is the total productive work done by all the users put together. In other
words, it is number of processes that are completed per unit time. Throughput of
a system should be good.

4.1.4.3 Turnaround time


The interval from time of submission of a process to the time of completion
of the process is called turnaround time. Turnaround time is the time elapsed
between the time when job is submitted and the time when it is completed. It
should be less.

4.1.4.4 Waiting time


It is the time that job spends waiting in ready queue. It is sum of all the
periods spent waiting in the ready queue. It should be less.

4.1.4.5 Response time


Response time is time of submission of a request to the time when first
response is produced.

4.1.4.6 Fairness
It refers to being fair to every user in terms of CPU time that he/she gets.

4.1.4.7 Proportionality
This criteria is somewhat related to response time. Users have idea of how
long things should take. When a request that is supposed to be complex takes a
long time, user accepts that. But, when simple request takes long time, user gets
irritated.

4.1.4.8 Environment-wise criteria


Different application areas have different scheduling goals (criteria). The
criteria those should be considered will not be same for all systems. The systems
may be distinguished in three major environments as
- Batch
- Interactive
- Real-time
Even though these environments are distinguished, there are some criteria
which must be satisfied by all systems. They are: Fairness and Balance
(keeping all parts of system busy when possible).
In batch systems, scheduler has control of which jobs are brought into
memory to run. So, all CPU bound processes are loaded first and then after
completion of their execution I/O bound processes are loaded. The criteria those

4-5
should be considered in batch system are: Throughput, Turnaround time and
CPU utilization.
For interactive systems, the important objectives those should be taken
in consideration are: Response Time and Proportionality.
Real-time systems have different scheduling goals than the interactive
system. They are: Meeting deadlines without losing data and Predictability
(avoiding quality degradation in multimedia systems).

4.2 Types of Scheduling

There are basically two types of scheduling which are also referred as
scheduling philosophies. They are
- Non-preemptive
- Pre-emptive

4.2.1 Non-preemptive
Non-preemptive scheduling is sometimes called as cooperative scheduling.
In this type of scheduling once the CPU is allocated to a process, the process
keeps the CPU until it give up control of CPU by its own (voluntarily). Even if a
higher priority process enters in system, running process cannot be forced to give
up the control of CPU. The running process gives up the control of CPU only
when it terminates or switches to waiting state due to some I/O request.
This type of scheduling is better suited for getting higher throughput (as
less overheads of context switching).

This type of scheduling was used in Microsoft 3.x. But subsequent versions
of Microsoft operating systems use preemptive scheduling.

4.2.2 Pre-emptive
This type of scheduling allows higher priority process to replace a running
process even if it has not been terminated or requested for any I/O. This requires
context switching more frequently due to which additional cost is incurred. This
reduces the throughput. Even then, it is suitable for online, real-time processing
where interactive users and high priority processes require immediate attention.

4.3 Gantt Chart:


It is a bar chart that illustrates a schedule by showing start time and
finish time of all the participating processes in the system.
e.g.:
Process Burst Time
P1 10
P2 2
P3 7

Gantt chart of above participating processes can be shown as follows,

P1 P2 P3
0 10 12 19

4-6
4.4 Scheduling Algorithms

CPU scheduling deals with the problem of deciding which of the processes
in the ready queue is to be allocated the CPU. There are many CPU scheduling
algorithms.
Some of them are:
- First Come First Served Scheduling
- Shortest Job First Scheduling
- Shortest Remaining Time Next Scheduling
- Priority Scheduling
- Round Robin Scheduling

4.4.1 First Come First Served (FCFS) Scheduling


The simplest CPU scheduling algorithm is First Come First Served (FCFS)
scheduling.
In this scheme the process that requests CPU first is allocated the CPU
first. FCFS scheduling can be easily implemented using FIFO queue. Whenever
any process enters the ready queue its Process Control Block (PCB) is linked to
the tail of the FIFO queue. When CPU becomes free it is allocated to the process
which is at the head of queue by removing it from the queue.
(Process Control Block contains many pieces of information associated with
specific process as process state, program counter, CPU registers, scheduling
information, memory management information, accounting information, I/O
status information etc.)
FCFS scheduling algorithm is non-preemptive. Once the CPU is
allocated to a process, it keeps the process until it terminates or it requests for
I/O.

Examples:
1. Consider following set of processes that arrive at time 0 , with length of
CPU burst given in milliseconds,
Process Burst Time
P1 16
P2 4
P3 6
If the processes arrive in the order P1, P2, P3, then result of FCFS
scheduling is shown in following Gantt chart.

P1 P2 P3
0 16 20 26

Waiting time for Process P1 : 0 milliseconds


Waiting time for Process P2 : 16 milliseconds
Waiting time for Process P3 : 20 milliseconds
Therefore average waiting time = (0+16+20)/3=12 milliseconds

2. Consider following set of processes that arrive at time 0 , with length of


CPU burst given in milliseconds,
Process Burst Time
P1 4
4-7
P2 6
P3 16
If the processes arrive in the order P1, P2, P3, then result of FCFS
scheduling is shown in following Gantt chart.

P1 P2 P3
0 4 10 26

Waiting time for Process P1 : 0 milliseconds


Waiting time for Process P2 : 4 milliseconds
Waiting time for Process P3 : 14 milliseconds
Therefore average waiting time = (0+4+14)/3=6 milliseconds

By observing the above examples, it is seen that the average waiting time
in FCFS policy varies when the CPU burst time varies and the order of entering
process varies. Also, the average waiting time is not minimal.
Advantages:
- FCFS scheme is simple to implement and understand.
Disadvantages:
- Average waiting time is quiet long.
- If a very long process enters first then other processes have to wait more.
This effect is called as convoy effect.
- As FCFS scheduling is non-preemptive, it is not suitable for time-sharing
system.

4.4.2 Shortest Job First (SJF) Scheduling


This scheme tries to execute shorter jobs first.
It is a special case of general priority scheduling algorithm where priority
of a process is decided on the basis of its next CPU burst.
This algorithm associates next CPU burst with each process. When CPU
becomes free, it is allocated to the process whose next CPU burst is smallest. If
the next CPU burst of two processes is same, FCFS scheduling is used for them.
This algorithm is optimal. But, the real difficulty is to know the length of
the process entering in the system. In long-term scheduling, whenever user
submits a job, he/she may be demanded to specify estimated time limit for the job.
Therefore, this algorithm is mainly used by long-term scheduler. As there is no
way to know the length of next CPU burst in short-term scheduling, this
algorithm is rarely used by short term scheduler.
SJF scheduling may be either non-preemptive or preemptive. Next
CPU burst of newly arrived process may be shorter than the remaining time of
the currently running process. Non-preemptive SJF algorithm will allow the
currently running process to finish its CPU burst whereas preemptive SJF
algorithm will preempt the currently running process. Preemptive SJF is also
called as Shortest Remaining Time Next (SRTN) scheduling.

Examples:
1. Consider following set of processes that arrive at time 0 , with length of
CPU burst given in milliseconds,
Process Burst Time
P1 10

4-8
P2 4
P3 7
P4 5
The result of SJF scheduling is shown in following Gantt chart.

P2 P4 P3 P1
0 4 9 16 26

Waiting time for Process P1 : 16 milliseconds


Waiting time for Process P2 : 0 milliseconds
Waiting time for Process P3 : 9 milliseconds
Waiting time for Process P4 : 4 milliseconds
Therefore average waiting time = (16+0+9+4)/4=7.25 milliseconds
(For this example if we use FCFS average waiting time will be
(0+10+14+21)/4 = 11.25 milliseconds)

2. Consider following set of processes that arrive at time 0 , with length of


CPU burst given in milliseconds,
Process Burst Time
P1 4
P2 16
P3 6
The result of SJF scheduling is shown in following Gantt chart.

P1 P3 P2
0 4 10 26

Waiting time for Process P1 : 0 milliseconds


Waiting time for Process P2 : 10 milliseconds
Waiting time for Process P3 : 4 milliseconds
Therefore average waiting time = (0+10+4)/3=4.67 milliseconds

Advantages:
- SJF scheme is optimal for average waiting time.
- As shortest jobs are finished faster, number of processes competing for
CPU is decreased.
- As smaller processes are finished faster, number of satisfied users is
increased.
Disadvantages:
- Knowing the length of next CPU burst for a process is difficult.
- It cannot be implemented at the level of short-term scheduling.

4.4.3 Shortest Remaining Time Next (SRTN) Scheduling


It is preemptive Shortest Job First Scheduling. When a new process
arrives in ready queue a choice is to be made whether to continue running the
currently running process or preempt it and allocate CPU to newly arrived
process. If the CPU burst of newly arrived process is shorter than remaining CPU
burst of the currently running process, the currently running process is
preempted and CPU is allocated to newly arrived process. Otherwise the
currently running process is allowed to continue.
Examples:
4-9
1. Consider following set of processes with length of CPU burst given in
milliseconds,
Process Arrival Time Burst Time
P1 0 12
P2 1 4
P3 2 2
P4 3 8
The result of SRTN scheduling is shown in following Gantt chart.

P1 P2 P3 P2 P4 P1
0 1 2 4 7 15 26

At time 0, only one process is in ready queue. i.e. P1. Therefore, CPU is
allocated to P1.
At time 1, P2 is arrived in ready queue whose CPU burst is 4 and
remaining CPU burst for process P1 is 11 milliseconds. So, process P1 is
preempted and CPU is allocated to process P2.
At time 2, P3 is arrived in ready queue whose CPU burst is 2 and
remaining CPU burst of process P2 is 3. So, process P2 is preempted and
CPU is allocated to process P3.
At time 3, P4 is arrived in ready queue whose CPU burst is 8 and
remaining CPU burst of process P3 is 1. So, process P3 is continued.
Then, shortest remaining time next job is selected.

Waiting time for Process P1 : 0+(15-1)=14 milliseconds


Waiting time for Process P2 : 0+(4-2)=2 milliseconds
Waiting time for Process P3 : 0 milliseconds
Waiting time for Process P4 : (7-3)=4 milliseconds
Therefore average waiting time = (14+2+0+4)/4=5 milliseconds

4.4.4Priority Scheduling
In this scheduling algorithm a priority is associated with each process and
CPU is allocated to the process with highest priority. If the two processes are
with equal priority, they are scheduled with FCFS scheduling.
SJF is a special case of priority scheduling where the priority is inverse of
next CPU burst. Larger the next CPU burst, less the priority.
Priorities are generally indicated by some fixed range of numbers (e.g. 0 to
127, 0 to 1023 etc). Some systems use low numbers to represent low priority and
others use low numbers to represent high priority. Let us assume that low
numbers represent high priority.
Priorities can be defined either internally or externally. Internally
computed priorities use some measurable quantities like time limits, memory
requirements, number of open files, ratio of average I/O burst to average CPU
burst. Externally, priorities are set by criteria outside the operating system such
as importance of process, type and amount of funds being paid for computer use
etc.
Priority scheduling can be either preemptive or non-preemptive.
When a new process arrives in ready queue, its priority is compared with priority
of currently running process. In preemptive case, if the priority of newly arrived
process is higher than priority of currently running process, it preempts the

4-10
currently running process and allocates CPU to the newly arrived process. In
non-preemptive case, the newly arrived process (which is having higher priority
that the currently running process) is kept at the head of ready queue.
Examples:
1. Consider following set of processes assumed to have arrived at time 0 in
the order P1, P2, P3, P4, P5, with length of CPU burst given in milliseconds,
Process Burst Time Priority
P1 9 3
P2 4 2
P3 2 4
P4 6 1
P5 5 5
Using priority scheduling, these processes will be scheduled as shown in
following Gantt chart.

P4 P2 P1 P3 P5
0 6 10 19 21 26

Waiting time for Process P1 : 10 milliseconds


Waiting time for Process P2 : 6 milliseconds
Waiting time for Process P3 : 19 milliseconds
Waiting time for Process P4 : 0 milliseconds
Waiting time for Process P5 : 21 milliseconds
Therefore average waiting time = (10+6+19+0+21)/5=11.2 milliseconds

Advantages:
- Higher priority gets executed first.
- Good for real-time system events.
Disadvantages:
- Unfair to low priority processes.
- The major problem with this scheduling is indefinite blocking or
starvation. It means low priority processes may not get CPU at all if the
system is heavily loaded with high-priority processes. i.e. low priority
processes has to wait (processes are considered to be blocked) in ready
queue for getting the CPU. Solution to this concept is aging. It is a
technique of gradually increasing the priority of processes that wait in the
system for long time. (e.g. increasing priority of such processes by 1 after
every 10 minutes).

4.4.5 Round Robin (RR) Scheduling


This algorithm is designed especially for time-sharing systems. It is similar
to FCFS scheduling with addition of preemption for enabling system to switch
between processes.
In this scheme, a small unit of time called time quantum or time slice is
defined which is generally in between 10 to 100 milliseconds.
Ready queue is implemented as a circular FIFO queue. Newly arriving
processes are kept at tail of the queue. At end of each time quantum, scheduler
performs two tasks
1. If the next CPU burst of currently running process is zero, it terminates
the process. Otherwise it adds that process at the tail of the queue.

4-11
2. It then removes process from the head of the queue and allocates CPU to it.
It may happen sometimes that the next CPU burst of currently running
process is less than the time quantum. In such case, the process voluntarily (by
its own) releases the CPU and then scheduler picks the next process from ready
queue.
Performance of RR algorithm depends heavily on the size of time quantum.
If the time quantum is extremely large, RR works just as FCFS scheduling. If the
time quantum is extremely small, RR approach is called processor sharing.
This creates appearance that each of n processes has its own processor running at
1/n speed of the real processor
Examples:
1. Consider following set of processes assumed to have arrived at time
0 in the order P1, P2, P3, P4, P5, with length of CPU burst given in
milliseconds,
Process Burst Time
P1 9
P2 4
P3 2
P4 6
P5 5
If a time quantum of 4 milliseconds is used, then the these processes will
be scheduled using Round Robin Scheduling as shown in following Gantt
chart.

P1 P2 P3 P4 P5 P1 P4 P5 P1
0 4 8 10 14 18 22 24 25 26

Waiting time for Process P1 : 0+(18-4)+(25-22)=17 milliseconds


Waiting time for Process P2 : 4 milliseconds
Waiting time for Process P3 : 8 milliseconds
Waiting time for Process P4 : 10+(22-14)=18 milliseconds
Waiting time for Process P5 : 14+(24-18)=20 milliseconds
Therefore average waiting time = (17+4+8+18+20)/5=13.4 milliseconds

Advantages:
- It is fair to all the processes.
Disadvantages:
- Average waiting time is often long.
- As the algorithm switches between various processes very often, there is
large number of context switches which incurs a large cost.
- If number of (users) processes is very high, response time may go down.

4.5 Other Scheduling

The algorithms discussed above are normally used in short-term


scheduling. There are so many scheduling methods apart from them. They are
discussed below.

4-12
4.5.1 Multilevel Queue Scheduling
This kind of scheduling can be used in the situations where processes can
be classified into different groups. One example may be considered as division
between foreground processes (interactive) and background processes (batch).
Both these groups have different response time requirements. Also, foreground
processes may have priority over background processes.
Multilevel queue scheduling algorithm partitions the ready queue in
multiple queues and processes are permanently assigned to any queue based on
some properties of the process as memory size, priority, process type etc. Every
queue has its own scheduling algorithm. (e.g. RR algorithm for foreground
processes and FCFS for background processes). In addition to this, foreground
queue may have absolute priority over background queue.
One example of multilevel scheduling is shown in figure 4.5.

Highest Priority
System Processes

Interactive Processes

Interactive Editing Processes

Batch Processes

Student Processes
Lowest Priority

Figure 4.5: Multilevel Queue Scheduling

Multilevel queue scheduling algorithm in the above figure has partitioned


the ready queue in multiple queues as,
- System Processes
- Interactive Processes
- Interactive Editing Processes
- Batch Processes
- Student Processes
Each queue has absolute priority over lower priority queue. e.g. If a system
process is entered in ready queue while a batch process is running, then batch
process is preempted.

4.5.2 Multilevel Feedback Queue Scheduling


In Multilevel Queue Scheduling algorithm, processes are not allowed to
move from one queue to another. So it is inflexible. In Multilevel Feedback
Queue Scheduling processes are allowed to move from one queue to another.

4-13
In this algorithm ready processes are separated into multiple queues on
the basis of their CPU bursts. If a process requires more CPU time, it is moved to
lower priority queue. Priority decreases from upper layer to lower layer. Time
slice of upper layer is always lesser than the lower one.
Figure 5.7 shows one example of multilevel feedback queues. A process
which has become ready is put in queue 0. Processes in queue 0 are given a time
quantum of 8 milliseconds. If the process does not finish within this quantum it is
added at the tail of queue 1.
If queue 0 is empty, process from head of queue 1 is given time quantum of
16 milliseconds. Even after this time quantum if the process is not finished, it is
moved to queue 2 which uses FCFS scheduling when queue 0 and queue 1 are
empty. i.e. each time, the time slice is increased but priority is decreased for the
process.

Queue 0 (time quantum = 8 milliseconds)

Queue 1 (time quantum = 16 milliseconds)

Queue 2 (FCFS)

Figure 5.7: Multilevel Feedback Queues

If the process request for an I/O before the time slice is over, the process is
blocked. When I/O is completed the process is moved to higher priority queue.
Also, if a process waits too long in a lower priority queue, it may be moved to a
higher priority queue (i.e. aging) for avoiding starvation.

4.5.3 Multiprocessor
All the scheduling algorithms discussed till now are for the systems with
single CPU. If multiple CPUs are available load sharing can be done. But,
scheduling problem becomes more complex in such situation. Here the discussion
is limited to the systems with homogeneous processors. i.e. all the processors
in the system are identical in terms of their functionality so that any process can
run on any processor.
There are two approaches for multiprocessor scheduling. They are:
1. Asymmetric Multiprocessing:
All the scheduling decisions, I/O processing and other system related
activities are handled by a single dedicated processor called master
server. All other processors execute user processes only. This approach is
easy to implement as only one processor accesses system data structures.
2. Symmetric Multiprocessing (SMP):
In this approach every processor is self-scheduling. All the processes
may be in a single common ready queue or each processor may have its
own private ready queue.
4-14
Scheduler for each processor examines the ready queue and selects a
process to execute. It should be ensured that two processors should not
choose same process.

When a process is running on a specific processor, the data accessed by


that process resides in the cache of that processor. Therefore, when the process is
again allocated to the same processor, the cache satisfies further memory
accesses. But, if the process is allocated to other processor, its cache will not
satisfy the memory access and it has to repopulate its cache which is time
consuming. So, SMP systems avoid migration of a process from one process to
another. This is called as processor affinity. Processor affinity can be either
soft affinity (attempting to keep a process running on the same processor but not
guaranteeing the same) or hard affinity (guaranteeing that process is not
migrated to another processor).
In SMP systems workload must be balanced amongst all the processors.
Otherwise, one or more processors may sit idle while other processors may have
high workloads. Load balancing tries to keep the workload evenly distributed
across all the processors. Two general approaches to load balancing are push
migration and pull migration. In push migration, a specific task periodically
checks the load on each processor and if imbalance is observed it pushes
processes from overloaded processor to idle or less-busy processor. Pull
migration occurs when an idle processor pull a waiting process from a busy
processor.

4.5.4 Scheduling in Real-time system


In real-time systems time plays essential role. One or more physical
devices external to the computer generate stimuli and the computer has to react
to them within stipulated time period. Some examples of real-time systems are as
follows:
1. Computer in a compact disc player:
2. Patient monitoring in hospital Intensive Care Unit.
3. Autopilot mode in an aircraft.
4. Robot control in automated industry.

The real time system may be either hard real-time (absolute deadlines
must be met) or soft real-time (missing occasional deadline is undesirable but
tolerable).
When an external event is detected, scheduler must schedule the processes
in such a way that all the deadlines are met.
Events in the real time system may be either periodic (occurring at
regular intervals) or non-periodic (not occurring regularly). It may happen that
a real-time system has to respond to multiple periodic events. In such situation,
the important factor is the time required for handling those events. The real-time
system which can handle all the periodic events is said to be schedulable.
Real time scheduling algorithm can be either static or dynamic. In static
case, scheduling decisions are made before the system starts running. It works
where perfect information (about work to be done and deadlines that have to be
met) is available in advance. In dynamic case, scheduling decisions are made at
run time. So, no any scheduling information is needed in advance.

4-15
4.6 Deadlock

Computer system is full of resources. Some examples of resources are:


printers, slots in system’s internal tables, CD recorder and scanner. Normally,
each such resource can be used by only one process at any instance of time.
For several applications, process needs exclusive control of multiple
resources at a time. e.g. A process that wants to record a scanned document on a
CD, requires exclusive control of both – scanner and CD recorder.
Let us consider one example as, two processes want to record scanned
document on a CD. Process P1 requests permission to use scanner which is
granted. Process P2 works in some different way and it requests permission to use
CD recorder first which is also granted. Now P1 asks for CD recorder, but request
is denied until P2 releases it. As a result, P1 gets blocked. Instead of releasing CD
recorder P2 requests for scanner. So, P2 also gets blocked. Here, both the
processes are blocked. They will remain blocked forever. Such situation is called
deadlock.
Example 2: Assuming that operating system allows maximum of 64
processes. (i.e. OS has allocated memory area which can accommodate only 64
PCBs). When a process creates a child process, a new PCB is to be created. But if
memory is not available for doing so, the parent process has to wait and has to try
after some time. After some time, if some other process is killed its PCB is deleted
and attempt of creating child will succeed. Imagine that 16 processes are running
and each needs to create 6 sub-processes (children). After creation of 3 child
processes by each process, total processes in the memory will be
16 parent processes + (16X3=) 48 child processes = 64 processes
Now, when these processes try to create 4th child process, they have to wait.
This waiting will be indefinite. Such situation is also called as deadlock.

Figure 5.8: Analogy of deadlock

A set of processes is deadlocked if each process in the set is waiting for an


event that only another process in the set can cause.

4.6.1 Resource
A resource can be a hardware device (e.g. a printer) or a piece of
information (e.g. a record in a database).
A non-sharable resource is anything that can be used by a single process at
any instance of time.
Resources can be of two types – pre-emptible and non-pre-emptible.

4-16
Pre-emptible resource is a resource which can be taken away from its
current owner without any side effects. e.g. Memory
Non-pre-emptible resource is a resource which cannot be taken away
from its current owner without causing the computation to fail. e.g. If a process
has started to burn a CD, the CD recorder allocated to it cannot be pre-empted. If
it is done so, it will result in a corrupted CD.
Generally, deadlocks involve non-pre-emptible resources.

4.6.2 System model


In a normal mode of operation, a process utilizes a resource in following
sequence.
- Request
o Process requests the resource. If the request cannot be granted,
requesting process must wait until it gets the resource.
- Use
o Process operates on the resource.

- Release
o Process releases the resource.

Request and release of resources are system calls. A system table


maintains whether each resource is free or allocated. This table also records the
process to which it is allocated. If a process requests for a resource which is
allocated to another process, the requesting process is added to queue of processes
waiting for the resource.

4.6.3 Principle Necessary Conditions


A deadlock situation can arise if and only if all of the following four
conditions hold simultaneously in the system. If any one of them is absent,
deadlock is not possible.
1. Mutual Exclusion
Resource must be allocated exclusively to a single process at a
time. If other process requests for the same resource it is delayed until
the resource has been released by first process.
This condition must be hold for non-sharable resources. e.g.
Printer is a non-sharable resource which cannot be shared by multiple
processes simultaneously.
In case of sharable resources, mutual exclusive access of the
resources is not required. Normally, sharable resources are not involved
in deadlock. e.g. Multiple processes may open a read-only file
simultaneously.

2. Hold and wait


A process which is holding one resource requests for other
resources which are currently being held by other processes.

3. No preemption
If a process holds certain resources, no other process should be
able to take them away forcibly. Resource can be released only
voluntarily by the process which was holding it.
4-17
4. Circular wait
A set { P0, P1, …, Pn } of waiting processes must exit such that P0
is waiting for a resource held by P1, P1 is waiting for a resource held by
P2, …, Pn-1 is waiting for a resource held by P n, and Pn is waiting for a
resource held by P0.

4.6.4 Resource Allocation Graph


Deadlocks can be represented or modeled more precisely by using graphical
representation in terms of a directed graph. This representation is called as
Resource Allocation Graph or Directed Resource Allocation Graph (DRAG).
The graph contains set of vertices which can be of two different kinds as
Processes (represented using circles) and Resources (represented using
rectangle).
A directed edge from process Pi to resource Rj ( Pi → Rj) indicates that Pi
has requested for resource Rj and is waiting for that resource. A directed edge
from a resource Rj to process Pi ( Rj → Pi) indicates that resource Rj is allocated to
the process Pi.
Some examples of DRAG are shown in figure 5.9 a, b and c.

A B C

R3 R4

R1 R2 D

Figure 5.9 ( a ) (b) (c)

Figure 5.9 ( a ): Holding a resource


Figure 5.9 ( b ): Requesting a resource
Figure 5.9 ( c ): Deadlock

Cycle in a DRAG indicates that there is a deadlock.


Resource graphs let us know if a given request/ release sequence leads to a
deadlock situation or not. Requests and releases can be carried out step by step
and the graph is checked at each step for cycles. If a graph contains cycle, it is
leading to deadlock. Operating System can take decisions here like suspending
the processes.

4.6.5 Critical Region


A situation in which multiple processes try to use same resource (e.g.
processes trying to write to same file) is called as race condition. Race condition
can be avoided by using concept of mutual exclusion.

4-18
Each process has a segment of code in which process may change common
variables, update a shared table or write a shared file and so on. Such region is
called as Critical Region. When one process is executing in its critical region, no
any process is allowed to execute in its critical region. i.e. Two processes are not
permitted to execute in their critical regions at the same time. For achieving this,
each process must request permission to enter its critical region. The code which
implements this request is called as entry section. The code following is the
critical region is called as exit section.

Entry section

Critical Region

Exit section

Figure 5.10: Critical Region


4.7 Deadlock Handling

We can deal with deadlock problem by using one of the following ways.

- Use a protocol to prevent deadlocks, ensuring that system will never enter
a deadlocked state. (Deadlock prevention)
- Use a protocol to avoid deadlocks (deadlock avoidance), trying to avoid the
deadlock to occur as far as possible.
- Allow the system to enter in deadlocked state, detect it and recover.
- Ignore the problem.

4.7.1 Deadlock Prevention


For arising deadlock all the four necessary conditions must hold. One can
prevent the occurrence of deadlock by ensuring that at least any one principle
necessary condition cannot hold.
1. Mutual Exclusion
For deadlock to occur mutual exclusion condition must hold
for non-sharable resources (e.g. Printer). For sharable resources (e.g.
Read-only files) mutual exclusive access is not required. So,
generally deadlock cannot be prevented by denying mutual
exclusion condition as some of the resources are essentially non-
sharable.
2. Hold and Wait
If we can prevent processes that hold resources from waiting
for more resources, deadlock can be eliminated. This can be achieved
by allocating all the resources to each process before it starts
execution. This can be implemented by having a provision of calling
system calls those request resources before all other system calls.
Alternatively, this can be implemented by allowing a process to
request resources only when it is not holding any other resources.
Disadvantages:
4-19
- Resource utilization may be low as allocated resources may
remain unused for long period.
- Starvation may occur for a process that requires many
resources and therefore may have to wait indefinitely.
3. No preemption
Third necessary condition for deadlock to occur is already
allocated resources cannot be preempted. For preventing this
condition, following strategy can be implemented.
If a process requests for some resources, it first checks
whether they are available. If available they are allocated. If not it
checks whether they are allocated to some other process that is
waiting for some additional resource. If so, it preempts the desired
resources from the waiting process and allocated them to requesting
process.
4. Circular Wait
For ensuring that this condition never holds, ordering of all
the resources types can be enforced and requesting of resources is
done strictly in increasing order.
After requesting and getting resource Ri, process can request
instance of Rj if and only if f(Rj) > f(Ri)

4.7.2 Deadlock Avoidance

For preventing deadlock, some restrictions are imposed either on the


environment or on the processes. But, as discussed above it is difficult. Also, if the
prevention is enforced, it may lead to some drawbacks like low device utilization
and reduced system throughput.
So as a compromise, operating system may aim to avoid deadlock to occur
rather than preventing it. For this, additional information about resources will be
required as how they are to be requested.
e.g. Two processes A and B requires scanner and CD recorder. Process A
first requests for scanner and then for CD reorder. Process B on the other hand,
first requests for CD recorder and then for scanner.
If system knows complete sequence of requests and releases of the devices
for all the processes, system can decide on each request whether to allocate the
device or not for avoiding future deadlock. While making such decision, system
should consider following
- resources currently available
- resources currently allocated to each process
- future requests and releases of each process
Different types of deadlock avoidance algorithm may require different
amount of information or different type of information for avoiding deadlock.

4.7.2.1 Safe State


System is in safe state, if it can allocate resources to each process (up to
its maximum requirement) in some order and still avoid deadlock. The system is
in safe state if and only if a safe sequence exists.
A safe state of a system will never lead to deadlock. i.e. an unsafe state
may lead to deadlock. However, all unsafe states are not deadlocks. This concept
is elaborated in figure 5.11.
4-20
unsafe
deadlock

safe

Figure 5.11: Safe, unsafe and deadlock state of system

Example: Consider a system with three processes and ten resources of a specific resource
type. Process P0 requires maximum of eight resources, P1 requires maximum of 3 resources
and P2 requires maximum of 7 resources.
Process Maximum Need Current Need
P0 8 4
P1 3 2
P2 7 2
At time t0, process P0 is holding four resources, P1 is holding two resources and
P2 is holding two resources. So, two resources are free.
System is in safe state at time t0. Also there exists a safe sequence <P1, P0, P2 >.
i.e. Process P1 may immediately get allotment of all its required resources. When process P1
will return all its resources, P0 may get allotment of all its required resources and finally P 2
may get allotment of all its required resources when P0 will release the resources.
System may go in unsafe state. e.g. at time t1, process P2 requests and get
allocated one more resource. Then, only process P1 can be satisfied as it can get all its
required resources. But, after completion of P1, when P1 releases all its resources, needs of no
any process can be satisfied which leads to a deadlock.

Avoidance algorithm should be defined such that system will not enter in
deadlock. Initially system is in safe state. When a process requests for a resource
which is available, the resource is allocated only if the system will be in safe sate
even after allocating the resource.

4.7.2.2 Resource Allocation Graph Algorithm

This is a deadlock avoidance algorithm which is used in a system where


there is only one instance of each resource type. In addition to the request edge
and allocation edge, a new type of edge called claim edge is introduced. It is
denoted by a dashed line.
A directed dashed edge from process Pi to resource Rj indicates that Pi may
request the resource Rj at some time in future. When process Pi requests resource
Rj, the claim edge is converted to request edge. After usage when resource R j is
released by Pi, the assignment edge is reconverted to claim edge.
All the claim edges for a process must be added in the resource allocation
graph before it starts executing.

4-21
B

R2

Figure 5.12: Claim edge

When a process Pi requests for a resource Rj, the request can be permitted
only if converting the request edge into assignment edge does not form a cycle. i.e.
safety is checked by using a cycle detection algorithm. If cycle does not exist, the
system will remain in safe state; otherwise not. For checking a cycle n 2 operations
are performed.

4.7.2.3 Banker’s Algorithm

Resource allocation graph algorithm is not applicable if there are multiple


instances of any resource type in the system. Banker’s algorithm is applicable to
such systems. This algorithm can be used in banking system to ensure that bank
never allocates its available cash in such a way that it could no longer satisfy
needs of all its customers.
When a new process enters in system it should declare maximum number
of instances of each resource type it may need. This should not exceed the total
number of resources. For implementing Banker’s algorithm following data
structures are maintained for n processes and m resources.
- Available
This is a vector of length m. It indicates number of available
resources.
{8, 4, 3} indicates that 8 resources of type 0, 4 resources of type 1 and
3 resources of type 2 are available.

- Max
This is n x m matrix. It indicates maximum demand of each process.
0 1 2
Process P0 needs maximum 3 instances of type 0, 2
P0 3 2 1 instances of type 1 and 1 instance of type 2

Process P1 needs maximum 2 instances of type 0, 2


P1 2 2 2 instances of type 1 and 2 instance of type 2
Process P2 needs maximum 2 instances of type 0, 1
P2 2 1 1 instance of type 1 and 1 instance of type 2
Process P3 needs maximum 3 instances of type 0, 2
P3 3 2 2
instances of type 1 and 2 instance of type 2

4-22
- Allocation
This is n x m matrix. It indicates number of resources of each type
currently allocated to each process.
0 1 2
Process P0 is allocated with 2 instances of type 0, 1
P0 2 1 1 instance of type 1 and 1 instance of type 2

Process P1 is allocated with 1 instance of type 0, 2


P1 1 2 1 instances of type 1 and 1 instance of type 2
Process P2 is allocated with 1 instance of all the
P2 1 1 1 resource types

- Need
This is n x m matrix. It indicates remaining resource need of each
resource type. It can be computed as Max – Allocation
0 1 2
Process P0 needs 1 more resource of type 0 and 1 more
P0 1 1 0 resource of type 1

P1 0 0 1 Process P1 needs 1 more resource of type 2


Process P2 needs 2 resources of type 0, 1 resources of
P2 2 1 1 type 1 and 1 resource of type 2

Size and values of the above data structures vary as time progresses.
Comparison of two vectors is done as per following discussion.

Let X and Y are two vectors of size n.


X ≤ Y if and only if X[i] ≤ Y[i] for all i
Also,
X < Y if and only if X ≠ Y (i.e. X[i] ≠ Y[i] for all i)

A row in the matrices Allocation and Need can be treated as vectors and
can be refereed as Allocationi and Needi (for the row index i). Here, Allocationi
indicates the resources currently allocated to process Pi and Needi indicates
additional resources those are required by process Pi.

4.7.2.3.1 Safety Algorithm

This algorithm checks whether system is in safe state or not.

1. Let Work and Finish be vectors of length m and n respectively.


Initialize Work = Available and Finish[i] = False for i = 0, 1, …, n–1
2. Find an index i such that Finish[i] = False AND Needi ≤ Work
If no such i exists go to step 4.
3. Work = Work + Allocation
Finish[i] = True
Go to step 2.
4. If Finish[i] = True for all i, then the system is in safe state.

4-23
Number of operations required = m x n2

4.7.2.3.2 Resource Request Algorithm

This algorithm determines whether requests can be safely granted or not.


The algorithm assumes a vector Requesti for a process Pi which indicates the
number of resource instances required by the process Pi.
Following steps are followed when request for resources is made by a
process Pi
1. If Requesti ≤ Needi, go to step 2.
Otherwise, raise an error condition as process has exceeded the maximum
claim.
2. If Requesti ≤ Availablei, go to step 3.
Otherwise, Pi must wait as resources are not available.
3. Pretend that the resources are allocated to the requesting process Pi by
modifying the state as,

Available = Avilable – Requesti


Allocationi = Allocationi + Requesti
Needi = Needi - Requesti

If the resulting resource allocation state is safe, transaction is completed


and process Pi gets the allocation of the requested resources. But if the new
state is unsafe, Pi must wait for Requesti and old resource allocation state
is restored.

4-24

You might also like