You are on page 1of 42

Uniprocessor Scheduling

•Types of Processor Scheduling

•Scheduling Algorithms
Scheduling
• An OS must allocate resources amongst competing processes.
• In the case of the processor, the resource to be allocated is
execution time on the processor and the means of allocation is
scheduling.

• The aim of processor scheduling is to assign processes to be


executed by the processor over time, in a way that meets system
objectives, such as response time, throughput, and processor
efficiency.
Scheduling Objectives
• The scheduling function must be designed to satisfy a number of
objectives, including:
• Share time fairly among processes
• Prevent starvation of a process
• Use the processor efficiently
• Have low overhead
• Prioritise processes when necessary (e.g. real time deadlines)
Recall

• Ready: The process is in main memory and available for execution.


• Blocked: The process is in main memory and awaiting an event.
• Blocked/Suspend: The process is in secondary memory and awaiting an event.
• Ready/Suspend: The process is in secondary memory but is available for execution
as soon as it is loaded into main memory.
Scheduling and Process State Transitions
Types of Scheduling
Long-Term Scheduling
• Determines which programs are admitted to the system for processing, May be
first-come-first-served Or according to criteria such as priority, I/O requirements
or expected execution time
• Thus it controls the degree of multiprogramming.

Medium-Term Scheduling
The medium-term scheduler is executed somewhat more
frequently.

Medium-term scheduling is part of the swapping function.


Short-Term Scheduling
• Also known as the dispatcher, executes most frequently and makes the
decision of which process to execute next.

• The short-term scheduler is invoked whenever an event occurs that may


lead to the blocking of the current process or that may provide an
opportunity to preempt a currently running process in favour of another.

• Examples of such events include


• Clock interrupts
• I/O interrupts
• Operating system calls
• Signals (e.g., semaphores)
Aim of Short Term Scheduling

Main objective is to allocate processor time to optimize certain aspects


of system behaviour.
• User-oriented performance criteria
• Response Time
Elapsed time between the submission of a request until there is
output.
• System-oriented performance criteria
• Effective and efficient utilization of the processor
Priorities

In many systems, each


process is assigned a
priority and the scheduler
will always choose a
process of higher priority
over one of lower priority.

• Common practice is to
have multiple ready
queues to represent each
level of priority
Starvation
The problem with a pure priority scheduling scheme is that lower-
priority processes may suffer starvation.
• This will happen if there is always a steady supply of higher-priority ready
processes.

• Solution
• Allow a process to change its priority based on its age or execution history
Selection Function
• Selection function determines which process, among
ready processes, is selected next for execution.

• May be based on priority, resource requirements, or


the execution characteristics of the process

• If based on execution characteristics then important


quantities are:
• w = time spent in system so far, waiting
• e = time spent in execution so far
• s = total service time required by the process, including e;
Decision Mode
• Decision mode Specifies the instants in time at which the selection
function is exercised.
• Two categories:
• Nonpreemptive
• Preemptive
Non-preemptive vs Preemptive
• Non-preemptive
• Once a process is in the running state, it will continue until it terminates or
blocks itself for I/O
• Preemptive
• Currently running process may be interrupted and moved to ready state by
the OS
• Preemption may occur when new process arrives, on an interrupt, or
periodically.
Process Scheduling Example

Example set of processes,


consider each a batch job

– Service time represents total execution time


First-Come-First-Served
• Each process joins the Ready queue
• When the current process ceases to
execute, the next process in the
Ready queue is selected
First-Come-First-Served

• FCFS performs much better for long processes than short ones.

• A short process may have to wait a very long time before it can
execute

• Favors CPU-bound processes


• I/O processes have to wait until CPU-bound process completes
Round Robin
• Uses preemption based on a clock (time quantum, q)
• also known as time slicing, because each process is given a slice of
time before being preempted.

A way to reduce the penalty that short jobs suffer with FCFS
Shortest Process Next
• Nonpreemptive
policy
• Process with
shortest expected
processing time is
selected next
• Short process
jumps ahead of
longer processes
Shortest Process Next
• Overall performance is significantly improved in terms of response
time.

• Variability of response times is increased, especially for longer


processes, and thus Predictability of longer processes is reduced

• If estimated time for process not correct, the operating system may
abort it
• Possibility of starvation for longer processes
Shortest Remaining Time
• Preemptive version of shortest process
next policy
• Must estimate processing time and choose
the shortest
• SRT does not have the bias in favor of long
processes found in FCFS.
• Unlike round robin, no additional interrupts
are generated, reducing overhead.
• On the other hand, elapsed service times
must be recorded, contributing to overhead.
Highest Response Ratio Next
• Choose next process with the greatest ratio

A smaller denominator yields a larger ratio so that shorter jobs are


favored, but aging without service increases the ratio so that a longer
process will eventually get past competing shorter jobs.

As with SRT and SPN, the expected service time must be estimated to use highest response
ratio next (HRRN).
Feedback Scheduling

• If we have no indication of the relative length of various


processes, then none of SPN, SRT, and HRRN can be used.

• Another way of establishing a preference for shorter jobs is


to penalize jobs that have been running longer.
• i.e if we cannot focus on the time remaining to execute, let’s focus
on the time spent in execution so far.

Scheduling is done on a preemptive (at time quantum) basis,


and a dynamic priority mechanism is used.

When a process first enters the system, it is placed in RQ0


• After its first preemption, when it returns to the Ready state, it is
placed in RQ1.
• Each subsequent time that it is preempted, it is demoted to the
next lower-priority queue.
Feedback Scheduling
A short process will complete quickly,
without migrating very far down the
hierarchy of ready queues.
• A longer process will gradually drift
downward.
• Thus, newer, shorter processes are favored
over older, longer processes.

Within each queue, except the lowest-


priority queue, a simple FCFS mechanism
is used.
• Once in the lowest-priority queue, a process
cannot go lower, but is returned to this queue
repeatedly until it completes execution.
• Thus, this queue is treated in round-robin
fashion.
Alternative Scheduling Policies
DISK SCHEDULING
Disks
• Disks comes into various types:
• Common ones are magnetic disks e.g hard disks
• Optical disks (CD-ROM, CD-R, DVDs)
• Magnetic disks are organized into cylinders
• Tracks are divided into sectors around the circumference
• On older disks, the number of sectors per track were the same for all cylinders.
• Modern disks are divided into zones, with more sectors on the outer zone than the inner ones.

27
Disks Scheduling
Disk scheduling also known as I/O scheduling is done by operating systems to schedule I/O
requests arriving for disk.

Disk scheduling is important because:


•Multiple I/O requests may arrive by different processes and only one I/O request can be
served at a time by disk controller. Thus other I/O requests need to wait in waiting queue and
need to be scheduled.

•Two or more request may be far from each other so can result in greater disk arm movement.

•Hard drives are one of the slowest parts of computer system and thus need to be accessed in
an efficient manner.

28
Disks Scheduling
Important terms in disk scheduling:
Seek Time: Seek time is the time taken to locate the disk arm to a specified track where the data is to be read or
write. So the disk scheduling algorithm that gives minimum average seek time is better.

Rotational Latency: Rotational Latency is the time taken by the desired sector of disk to rotate into a position so
that it can access the read/write heads. So the disk scheduling algorithm that gives minimum rotational latency
is better.
Transfer Time: Transfer time is the time to transfer the data. It depends on the rotating speed of the disk and
number of bytes to be transferred.

Disk Access Time: Disk Access Time = Seek Time + Rotational Latency + Transfer Time

Disk Response Time: Response Time is the average of time spent by a request waiting to perform its I/O
operation. So the disk scheduling algorithm that gives minimum variance response time is better.

29
Disk Scheduling Algorithms
1.FCFS: FCFS is the simplest of all the Disk Scheduling Algorithms. In
FCFS, the requests are addressed in the order they arrive in the disk queue.

Advantages:
•Every request gets a fair chance
•No indefinite postponement

Disadvantages:
•Does not try to optimize seek time
•May not provide the best possible service

30
Disk Scheduling Algorithms
2.SSTF: In SSTF (Shortest Seek Time First), requests having shortest seek time are
executed first. So, the seek time of every request is calculated in advance in queue and
then they are scheduled according to their calculated seek time. As a result, the request
near the disk arm will get executed first. SSTF is certainly an improvement over FCFS as
it decreases the average response time and increases the throughput of system.

Advantages:
•Average Response Time decreases
•Throughput increases

Disadvantages:
•Overhead to calculate seek time in advance
•Can cause Starvation for a request if it has higher seek time as compared to incoming
requests
•High variance of response time as SSTF favours only some requests with shorter31 seek
time
Disk Scheduling Algorithms

3.SCAN: In SCAN algorithm, the disk arm moves into a particular direction and services the
requests coming in its path and after reaching the end of disk, it reverses its direction and again
services the request arriving in its path. So, this algorithm works like an elevator and hence also
known as elevator algorithm. As a result, the requests at the midrange are serviced more and
those arriving behind the disk arm will have to wait.

Advantages:
•High throughput
•Low variance of response time
•Average response time

Disadvantages:
•Long waiting time for requests for locations just visited by disk arm
32
Disk Scheduling Algorithms

4.CSCAN: In SCAN algorithm, the disk arm again scans the path that has been scanned, after reversing its
direction. So, it may be possible that too many requests are waiting at the other end or there may be zero or few
requests pending at the scanned area.
These situations are avoided in CSAN algorithm in which the disk arm instead of reversing its direction goes to
the other end of the disk and starts servicing the requests from there. So, the disk arm moves in a circular
fashion and this algorithm is also similar to SCAN algorithm and hence it is known as C-SCAN (Circular SCAN).

Advantages:
•Provides more uniform wait time compared to SCAN

33
Disk Scheduling Algorithms

5.LOOK: It is similar to the SCAN disk scheduling algorithm except the difference that the
disk arm in spite of going to the end of the disk goes only to the last request to be serviced
in front of the head and then reverses its direction from there only. Thus it prevents the extra
delay which occurred due to unnecessary traversal to the end of the disk.

6.CLOOK: As LOOK is similar to SCAN algorithm, in similar way, CLOOK is similar to


CSCAN disk scheduling algorithm. In CLOOK, the disk arm inspite of going to the end
goes only to the last request to be serviced in front of the head and then from there goes to
the other end’s last request. Thus, it also prevents the extra delay which occurred due to
unnecessary traversal to the end of the disk.

34
Comparison of Disk Scheduling algorithms
• To compare various scheduling schemes, consider a disk head is
initially located at track 100.

• assume a disk with 200 tracks and that the disk request queue has random
requests in it.

• The requested tracks, in the order received by the disk scheduler, are
• 55, 58, 39, 18, 90, 160, 150, 38, 184.
First-Come, first-Served (FCFS)
• Process request sequentially
• Fair to all processes
• Approaches random scheduling in performance if
there are many processes

55, 58, 39, 18, 90, 160, 150, 38, 184.


Shortest Seek Time First
• Select the disk I/O request that requires the least
movement of the disk arm from its current position

55, 58, 39, 18, 90, 160, 150, 38, 184.


SCAN
• Arm moves in one direction only, satisfying all outstanding
requests until it reaches the last track in that direction then the
direction is reversed
• Also called elevator policy
• The SCAN policy favors jobs whose requests are for tracks nearest
to both innermost and outermost tracks and favors the latest-
arriving jobs.

55, 58, 39, 18, 90, 160, 150, 38, 184.


C-SCAN
• The C-SCAN (circular SCAN) policy restricts scanning
to one direction only.
• When the last track has been visited in one direction,
the arm is returned to the opposite end of the disk
and the scan begins again

55, 58, 39, 18, 90, 160, 150, 38, 184.


Performance Compared
Comparison of Disk Scheduling Algorithms
Priority
• Goal is not to optimize disk use but to meet other objectives
• Short batch jobs may have higher priority
• Provide good interactive response time
• Longer jobs may have to wait an excessively long time
• A poor policy for database systems
Last-in, first-out
• Good for transaction processing systems
• The device is given to the most recent user so there should be little arm
movement
• Possibility of starvation since a job may never regain the head of the
line

You might also like