Você está na página 1de 26

National Institute of Science & Technology

CPU
Scheduling
(Chap-6)

Operating System Concept Ashish Kumar Dass


6.1
Topics
Basic Concepts
National Institute of Science & Technology

Scheduling Criteria
Scheduling Algorithms
First-Come-First-Serve (FCFS) Scheduling
Shortest Job First (SJF) Scheduling
Priority Scheduling
Round-Robin (RR) Scheduling
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling

Operating System Concept Ashish Kumar Dass


6.2
Basic Concepts
Scheduling is a fundamental operating-system function.
National Institute of Science & Technology

Almost all computer resources are scheduled before use.


CPU scheduling is the basis of multi-programmed
operating system.
The objective of multiprogramming is to Maximize the
CPU utilization.
As CPU is one of the primary resources, its scheduling is
central to the operating-system design.
The success of CPU scheduling depends on the process
execution cycle.
The execution cycles of process are called as CPU burst
and I/O burst cycle.

Operating System Concept Ashish Kumar Dass


6.3
CPU - I/O Bursts Cycle
National Institute of Science & Technology

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.
The last CPU burst will end up
with a system request to
terminate execution, rather
than with another I/O burst.

Operating System Concept Ashish Kumar Dass


6.4
CPU Scheduler
Selects from among the processes in memory that are
National Institute of Science & Technology

ready to execute, and allocates the CPU to one of them.


CPU scheduling decisions may take place when a
process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
Scheduling under 1 and 4 is nonpreemptive.
All other scheduling is preemptive.
Under nonpreemptive 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.

Operating System Concept Ashish Kumar Dass


6.5
Dispatcher
National Institute of Science & Technology

 Dispatcher module gives control of the CPU to the


process selected by the short-term scheduler. This
involves:
switching context
switching to user mode
jumping to the proper location in the user program to
restart that program
Dispatch latency – time it takes for the dispatcher
to stop one process and start another running.

Operating System Concept Ashish Kumar Dass


6.6
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible.
National Institute of Science & Technology

Throughput – the number of processes that complete


their execution per time unit.
Turnaround time – the interval from the time of
submission to the time of completion of a process. [OR]
 Turnaround time (TAT) = Finishing time – Arrival time
Waiting time – the sum of the time periods spends
waiting in the ready queue. [OR]
 Waiting time (WT) = Starting time – Arrival time
Response time – amount of time it takes from when a
request was submitted until the first response is
produced. [OR]
 Response time (RT) = First response – Arrival time

Operating System Concept Ashish Kumar Dass


6.7
Optimization Criteria
National Institute of Science & Technology

Maximum CPU utilization


Maximum throughput
Minimum turnaround time
Minimum waiting time
Minimum response time

Note: The algorithm which gives the minimum


waiting time, turn around time and response
is the best one.

Operating System Concept Ashish Kumar Dass


6.8
First-Come, First-Served (FCFS) Scheduling

FCFS is the simplest CPU scheduling algorithm.


National Institute of Science & Technology

The process that requests the CPU first is allocated the


CPU first.
The implementation of the FCFS policy is easily managed
with a FIFO queue.
When the process enters the ready queue, its PCB is
linked onto the tail of the FIFO queue.
When 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.
The average waiting time under FCFS policy is quite long.
FCFS scheduling algorithm is nonpreemtive.

Operating System Concept Ashish Kumar Dass


6.9
FCFS Scheduling (Cont.)
Example : Process Burst Time (in mili second)
National Institute of Science & Technology

P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30

Turnaround time for P1 = 24; P2 = 27; P3 = 30


Average TAT time : (24 + 27 + 30 )/3 = 27 ms
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average WT: (0 + 24 + 27)/3 = 17 ms
Response time for P1 = 0; P2 = 24; P3 = 27 (Same as WT in Case
Non-PS)
Operating System Concept Ashish Kumar Dass
6.10
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order P2 , P3 , P1 .
National Institute of Science & Technology

 The Gantt chart for the schedule is:

P2 P3 P1

0 3 6 30

 Turnaround time for P1 = 6; P2 = 0; P3 = 3


 Average TAT: (3 + 6 + 30)/3 = 13 ms
 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average WT: (6 + 0 + 3)/3 = 3 ms
 Much better than previous case.
 Convoy effect - short process behind long process.
 This effect results in lower CPU and device utilization.

Operating System Concept Ashish Kumar Dass


6.11
Shortest-Job-First (SJF) Scheduling
 Associate with each process the length of its next CPU
National Institute of Science & Technology

burst. Use these lengths to schedule the process with


the shortest time.
 Two schemes:
 nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst.
 preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining-Time-First (SRTF).
 SJF is optimal – gives minimum average waiting time for
a given set of processes.

Operating System Concept Ashish Kumar Dass


6.12
Example of Non-Preemptive SJF
Example: Process Arrival Time Burst Time (in ms)
National Institute of Science & Technology

P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

Waiting time for P1= 0; P2 = 8-2 = 6; P3 = 7-3 = 4; P4 = 12-5 = 7


Average waiting time = (0 + 6 + 3 + 7)/4 = 4 ms

Operating System Concept Ashish Kumar Dass


6.13
Example of Preemptive SJF
Example: Process Arrival Time Burst Time
National Institute of Science & Technology

P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
 SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 Waiting Time for: P1 = [{0+(11-2)}-0] = 9; P2 = [{2+(5-4)}-2] = 1;


P3 = (4-4) = 0; P4 = (7-5) = 2 ;
Average waiting time = (9 + 1 + 0 +2)/4 = 3

Operating System Concept Ashish Kumar Dass


6.14
Priority Scheduling
A priority number (integer) is associated with each process.
National Institute of Science & Technology

The CPU is allocated to the process with the highest priority


(smallest integer  highest priority).
SJF is a special case of priority scheduling algorithm where
priority (p) is the predicted next CPU burst time.
Equal priority processes are scheduled in FCFS order.
Priority can be defined either internally or externally.
 Internal priorities are set by taking some measurable quantities
like, time limits, memory requirements, the number of open
files, and the ratio of avg. I/O burst to avg. CPU burst time.
 External priorities are set by taking the external factors (to O.S)
such as the importance of the process, the type and the amount
of funds being paid for computer use, even the political factors.

Operating System Concept Ashish Kumar Dass


6.15
Priority Scheduling (cont.)
National Institute of Science & Technology

Priority scheduling is either preemptive or nonpreemptive.


 A preemptive priority-scheduling algorithm will preempt the
CPU if the priority of the newly arrived process is higher than
the priority of the current running process.
 A nonpreemptive priority-scheduling algorithm will simply put
the new process at the head of the ready queue.
Problem  Starvation – low priority processes may never
execute.
 Also called as indefinite blocking.
Solution  Aging – as time progresses increase the
priority of the process.

Operating System Concept Ashish Kumar Dass


6.16
Round Robin Scheduling (RR)
It is designed especially for the time-sharing systems.
National Institute of Science & Technology

It is similar to FCFS scheduling but preemption is added


to switch between processes.
Each process gets a small unit of CPU time (time slice or
time quantum), usually 10-100 milliseconds. After this
time has elapsed, the process is preempted and added to
the end/tail of the ready queue.
The ready queue is treated as a circular queue.
Performance
 If q is very large  similar to FCFS policy.
 If q is very small  many switching will occur and it may
cause overhead to the system.

Operating System Concept Ashish Kumar Dass


6.17
Example of RR with Time Quantum = 5 ms
Process Burst Time
National Institute of Science & Technology

P1 30
P2 6
P3 8
The Gantt chart is:

P1 P2 P3 P1 P2 P3 P1 P1 P1 P1

0 5 10 15 20 21 24 29 34 39 44

Waiting time for P1 = {0+(15-5)+(24-20)} = 14;


P2 = {5+(20-10)} = 15; P3 = {10+(21-15)} = 16
Average WT: (14 + 15 + 16)/3 = 15 ms
Average TAT = 29.66 ms and Average RT = 5 ms.

Operating System Concept Ashish Kumar Dass


6.18
Multilevel Queue Scheduling
Ready queue is partitioned into separate queues:
National Institute of Science & Technology

 foreground (interactive) processes


 background (batch) processes
The processes are permanently assigned to one queue,
generally based on some properties of process (memory
size, process priority, or process type.
Each queue has its own scheduling algorithm.
 foreground – Round-Robin
 background – FCFS
In addition, there must be scheduling among the queues,
which is commonly implemented as fixed-priority
preempted scheduling.

Operating System Concept Ashish Kumar Dass


6.19
Multilevel Queue Scheduling (cont.)
For example, the foreground queue may have absolute priority
National Institute of Science & Technology

over the background queue that means each queue has


absolute priority over lower-priority queues.
So, no process in the batch queue, for example, could run
unless the queues for system processes, interactive processes
were all empty.
If an interactive editing process entered the ready queue while
a batch process was running, the batch process would be
preempted. (e.g. Solaris 2)
Another possibility is to time slice between queues.
Each queue gets a certain portion of CPU time, which it can
then schedule among the various processes in its queue.
 Foreground queue ~ 80% of CPU time for RR scheduling
 Background queue ~ 20% of CPU time for FCFS scheduling

Operating System Concept Ashish Kumar Dass


6.20
Multilevel Queue Scheduling (cont.)
National Institute of Science & Technology

Fig: Example of a multilevel queue scheduling

Operating System Concept Ashish Kumar Dass


6.21
Multilevel Feedback Queue
In multilevel queue-scheduling processes doesn’t move
National Institute of Science & Technology

from one queue to another as processes are permanently


assigned to a queue on entry to the system.
However, multilevel feedback queue scheduling allows a
process to move between queues.
The idea is to separate processes with different CPU-burst
characteristics.
If a process uses too much of CPU time, it will be moved
to a lower-priority queue.
Similarly, a process that waits too long in a lower-priority
queue may be moved to a higher-priority queue.
This form of aging prevents starvation.

Operating System Concept Ashish Kumar Dass


6.22
Example of Multilevel Feedback Queue
Consider a multilevel feedback queue scheduler with
National Institute of Science & Technology

three queues:
 Q0 – time quantum 8 milliseconds
 Q1 – time quantum 16 milliseconds
 Q2 – FCFS
Scheduling:
 The scheduler first executes all processes in Q0. Only when
Q0 will empty, it will executes the processes in Q1.
 Similarly, processes in Q2 will be executed only if Q0 and Q1
are empty.
 A process that arrives for Q1 will preempt a process in Q2.
 A process that arrives for Q0 will preempt a process in Q1.

Operating System Concept Ashish Kumar Dass


6.23
Multilevel Feedback Queues
National Institute of Science & Technology

A process entering the


ready queue is put in Q0.A
process in Q0 is given a time
quantum of 8 ms.
If it doesn’t finish with this
time, it is moved to the tail of
Q1.
If Q0 is empty, the process
at the head of Q1 is given a
quantum 16 ms.
 If it does not complete, it is
preempted and is put into Q2.

Operating System Concept Ashish Kumar Dass


6.24
Multilevel Feedback Queue (cont.)
Multilevel-feedback-queue scheduler defined by the
National Institute of Science & Technology

following parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will
enter when that process needs service.
From the properties and definition of a multilevel
feedback queue scheduler makes it the most general
CPU-scheduling algorithm, but it also the most complex.
It can be configured to match a specific system under
design.

Operating System Concept Ashish Kumar Dass


6.25
National Institute of Science & Technology

Thank You
Operating System Concept Ashish Kumar Dass
6.26

Você também pode gostar