Chapter 4 - Scheduling-1
Chapter 4 - Scheduling-1
Chapter 4 - Scheduling-1
Scheduling
12 Hours
22 Marks
4.1 Scheduling
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
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;
c = a + b;
CPU Burst
d = c / 2.0;
} CPU 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.
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.
Medium term
Scheduler Blocked & Ready
Swapped-out
Processes Queue
Long term
Scheduler
New Jobs Ready
(Batch) Queue CPU Exit
Short term
Scheduler
4-3
An operating system may use one or all of these levels.
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.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.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-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).
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.
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
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
P1 P2 P3
0 4 10 26
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.
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
P1 P3 P2
0 4 10 26
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.
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.
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
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-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
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-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
Batch Processes
Student Processes
Lowest Priority
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 2 (FCFS)
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.
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
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.
- Release
o Process releases the resource.
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.
A B C
R3 R4
R1 R2 D
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
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.
safe
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-21
B
R2
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.
- 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
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
- 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
Size and values of the above data structures vary as time progresses.
Comparison of two vectors is done as per following discussion.
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-23
Number of operations required = m x n2
4-24