Você está na página 1de 28

CPU Scheduling

Chapter 4
CPU Scheduling
Maximize CPU utilization by swapping
processes in & out of CPU
Several processes in memory per time
When process A waits, another process B
must take over CPU
Scheduling
Deciding how to give processes a share/time
in CPU
Critical to OS design
CPU burst: time spent in CPU
I/O burst : time spent waiting for I/O
Four scenarios for scheduling decisions
Running to waiting switch (I/O request)
Running to Ready switch (interrupt occurs)
Waiting to Ready switch (I/O completion)
Process termination
Pre-emptive Scheduling
Process forcibly removed from CPU after
1. Critical data has been changed
2. An interrupt
3. Another process switches to ready state
Need to control access to shared data so
as to preserve integrity
Sections of code affected by interrupts
must be protected from simultaneous
use.
Non Pre-emptive Scheduling
No choice when it comes to scheduling
Process in CPU until
It releases the CPU voluntarily (gets into
waiting state)
It terminates
No special H/W required
User can be in total control of processes
they want in CPU e.g MS Windows
Dispatcher
Component involved in CPU scheduling
Give control of CPU to process selected
by short-term scheduler. Responsible for
Context switch
Switching to user mode
Jumping to right location when resuming
program
fast
Scheduling Criteria
Used to compare scheduling algorithms.
1. CPU utilization (%- busy or not)
2. Throughput (# of processes completed per
unit time)
3. Turnaround time (interval- time of
submission to time of completion = sum of
periods spent waiting to get into memory,
waiting in ready queue, executing in CPU +
doing I/O, affected by speed of devices)
4. Waiting time (CPU schedule algorithm
affects time a process spends waiting)
5. Response time (for interactive systems it is
the time it takes to start responding minus
time of submission of process, the time it
takes to output the response.)
CPU Scheduling Algorithms

1. First-Come, First-Served (FCFS)


2. Shortest-Job-First (SJF)
3. Priority
4. Round-Robin
5. Multi-level queue
6. Multi-level Feedback queue
FCFS
Non preemptive
Two variations
A. Given total CPU burst times only
B. Given CPU and I/O burst times plus arrival
times
Variation A
Consider CPU times only when
calculating average waiting times (awt)
FCFS

Variation B
Consider CPU and I/O times and process arrival times in
finding awt.
Process Arrival CPU/IO Total CPU
time
P1 0 5C 3I 4C 9C

P2 2 1C 2I 1C 2C

P3 4 4C 4I 6C 10C
Variation A
Gantt chart

P1 P2 P3

0 9 11 21
xC = x msec in CPU
yI = y msec doing I/O
AWT = (wtP1 + wtP2 +wtP3)/3
= (0ms +9ms + 11ms)
= 20ms/3 (ignoring arrival times)
AWT with arrival times
=(0ms + (9ms 2ms) + (11ms 4ms))/3
=14ms/3
Variation B FCFS
Process Arrival Time CPU/IO

P1 0 5C 3I 4C
P2 2 1C 1I 1C
P3 4 4C 4I 6C

P1 8 (5C + 3I) 4C

P2 8 (5C + 1C + 2I) 1C

P3 14 6C
(5C+1C+4C+4I)
Variation B Gantt chart
P1 P2 P3 P1 P2 P3

0 5 6 10 14 15 21
AWT =( wtP1 + wtP2 +wtP3)/3
WTP1 = 0ms + (10ms -7ms) = 3ms
WTP2 = (5ms 2ms)+(14ms 8ms)=9
ms
WTP3 = (6ms -4ms)+(15ms 14ms)=3ms
AWT=(3ms+9ms+3ms)/3 =5ms
SJF
Gives preference to job that requires
shortest CPU time
Both preemptive and non preemptive
approaches
Preemptive SJF
Let PCPU = process in CPU
Let PJA = process just arrived in ready
queue
Algorithm
If PCPU > PJA then
Remove PCPU from CPU &
Run PJA in CPU until first CPU requirement
completes
Repeat forever
Process Arrival Time Next CPU Time

P1 0 6
P2 1 3
P2 2 9
P4 3 4

P1 P2 P4 P1 P3

0 1 4 8 13 22
AWT =(WTP1+ WTP2 +WTP3 +WTP4)/4
WTP1= 0ms +(8ms- 1ms) =7ms
WTP2= (1ms -1ms) =0ms
WTP3 =(13ms 2ms) = 11ms
WTP4= (4ms -3ms) = 1ms
AWT = 19ms/4
Non Preemptive SJF
Do not evict while executing
Let shortest in CPU complete its CPU execution
first.
Gantt chart using similar graph to preemptive

P1 P2 P4 p3

0 6 9 13 22
WTP1= 0ms
WTP2 = (6ms 1ms) = 5ms
WTP3 = (13ms 2ms) = 11ms
WTP4 = (9ms 3ms) = 6ms
AWT = (0ms+5ms+11ms+6ms)/4 =22ms/4
Priority Scheduling
Assign priorities to processes
Preemptive (compare priority of arriving
processes with currently executing) & non
preemptive (if ariving process has highest
priority put at the head of the queue)
Give CPU to processes with highest priority
FCFS gives equal priorities
Priorities may be internally defined,
automatically generated or user/externally
defined.
Process CPU Time ms Priority
P1 5 2
P2 8 1
P3 3 3

P2 P1 P3

0 8 13 16
AWT = (0+8+13)ms/3 = 7ms
Dangers of Priority Scheduling
Starvation of lower priority processes
where there is a long stream of high
priority processes.
They wait for an indefinite time.
Solution of Starvation
Ageing (gradual increase of priority of low
priority processes as they wait) which means
they will eventually execute.
Round Robin
Defined for time sharing systems
Processes given equal share of time (quantum
small unit of time)
When a quantum expires it means context
switching.
Each process executes for one quantum at a
time. Small quantum means less progress &
increased context switching overhead. Large
quantum means RR turns into FCFS.
Ready queue of processes (FIFO)- add new to
tail.
Consider the following processes with a
quantum equal to 5ms
Process CPU Time ms
P1 9
P2 4
P3 4

P1 P2 P3 P1

0 5 9 13 17
AWT = (0+(13-5)+5+9)ms/3= 22ms/3

Você também pode gostar