session 4 - cpu scheduling - finals
TRANSCRIPT
-
8/3/2019 Session 4 - CPU Scheduling - Finals
1/24
What is in this topic?
To find out how to get a process attached to a
processor.
It centers around efficient algorithms that performwell.
The design of a scheduler is concerned with makingsure all users get their fair share of the resources.
1
CPU Scheduling
-
8/3/2019 Session 4 - CPU Scheduling - Finals
2/24
5: CPU-Scheduling
2
CPU Scheduling
ConceptsMultiprogramming A number of programs can be inmemory at the same time. Allows
overlap of CPU and I/O.
Jobs are programs that run without
user interaction.
User are programs that may have user
interaction.
Process is the common name for both.
CPU - I/O burst cycle Characterizes process execution,
which alternates, between CPU
and I/O activity. CPU times are
generally much shorter than I/O
times.
Preemptive Scheduling An interrupt causes currently
running process to give up the
CPU and be replaced by anotherprocess.
-
8/3/2019 Session 4 - CPU Scheduling - Finals
3/24
CPU SCHEDULING
5: CPU-Scheduling
3
The Scheduler
Selects from among the processes in memory that are 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
-
8/3/2019 Session 4 - CPU Scheduling - Finals
4/24
SCHEDULERS
Process migrates among several queues
Device queue, job queue, ready queue
Scheduler selects a process to run from these queues
Long-term scheduler: load a job in memory
Runs infrequently
Short-term scheduler:
Select ready process to run on CPU
Should be fast
-
8/3/2019 Session 4 - CPU Scheduling - Finals
5/24
WHEN DOES SCHEDULER RUN?
Non-preemptive minimum
Process runs until voluntarily relinquish CPU
process blocks on an event
process terminates
process yields
Preemptive minimum
All of the above, plus:
Event completes: process moves from blocked to ready
Timer interrupts Implementation: process can be interrupted in favor of another
New Ready RunningExit
Waiting
-
8/3/2019 Session 4 - CPU Scheduling - Finals
6/24
Note usage of the words DEVICE, SYSTEM, REQUEST, JOB.
UTILIZATION The fraction of time a device is in use. ( ratio of in-use time / totalobservation time )
THROUGHPUT The number of job completions in a period of time. (jobs / second )
SERVICE TIME The time required by a device to handle a request. (seconds)
QUEUEING TIME Time on a queue waiting for service from the device. (seconds)
RESIDENCE TIME The time spent by a request at a device.RESIDENCE TIME = SERVICE TIME + QUEUEING TIME.
RESPONSE TIME Time used by a system to respond to a User Job. ( seconds )
THINK TIME The time spent by the user of an interactive system to figure out thenext request. (seconds)
The goal is to optimize both the average and the amount of variation.
CPU SCHEDULINGCriteria For
Performance
Evaluation
-
8/3/2019 Session 4 - CPU Scheduling - Finals
7/24
THE PERFECT CPU SCHEDULER
Minimize latency: response or job completion time
Maximize throughput: Maximize jobs / time.
Maximize utilization: keep I/O devices busy.
Fairness: everyone makes progress, no one starves
-
8/3/2019 Session 4 - CPU Scheduling - Finals
8/24
SCHEDULING ALGORITHMS FCFS
First-come First-served (FCFS) (FIFO)
Jobs are scheduled in order of arrival
Non-preemptive
Problem: Average waiting time depends on arrival order
Advantage: really simple!
time
P1 P2 P3
0 16 20 24
Process Service Time
P1 16
P2 4
P3 4
-
8/3/2019 Session 4 - CPU Scheduling - Finals
9/24
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
0 8 12 21 26
P1 P2 P3 P4
FCFS
Average Turnaround Time= ( (8-0) + (12-1) + (21-2) + (26-3) )/4 = 61/4 = 15.25 s
CPU SCHEDULING Scheduling
Algorithms
Average Waiting Time= ( (0-0) + (8-1) + (12-2) + (21-3) )/4 = 36/4 = 9 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
10/24
-
8/3/2019 Session 4 - CPU Scheduling - Finals
11/24
SHORTEST JOB FIRST SCHEDULING - NON-
PREEMPTIVE
Example: Process Arrival Time Burst Time
P1
0 7
P2
2 4
P3
4 1
P4 5 4 Non preemptive SJF
P1 P3 P2
7
P1(7)
160
P4
8 122 4 5
P2(4)
P3(1)
P4(4)
P1s wating time = 0
P2s wating time = 6
P3s wating time = 3
P4s wating time = 7
Ave. Turnaround Time= ( (7 - 0) + (12 - 2) + (8 - 4) + (16 - 5) )/4 = 32/4 = 8.0 s
Ave. Waiting Time= ( (0 - 0) + (8 - 2) + (7 - 4) + (12 - 5) )/4 = 16/4 = 4.0 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
12/24
SHORTEST JOB FIRST SCHEDULING -
PREEMPTIVE
Example: Process Arrival Time Burst Time
P1
0 7
P2
2 4
P3
4 1
P4
5 4
Preemptive SJF
P1 P3P2
42 110
P4
5 7
P2 P1
16
P1(7)
P2(4)
P3(1)
P4(4)
P1s wating time = 9
P2s wating time = 1
P3s wating time = 0
P4s wating time = 2
P1(5)
P2(2)
Ave. Waiting Time= ( (11 - 2 - 0) + (5 - 4 - 2) + (4 - 4) + (7 - 5) )/4 = 12/4 = 3.0 s
Ave. Turnaround Time= ( (16 - 0) + (7 - 2) + (5 - 4) + (11 - 5) )/4 = 28/4 = 7.0 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
13/24
CPU SCHEDULING
5: CPU-Scheduling
13
EXAMPLE DATA:
Process Arrival Service
Time Time (s)
1 0 8
2 1 4
3 2 9
4 3 5
0 5 10 17 26
P2 P4 P1 P3
Preemptive Shortest Job First
Ave. Turnaround Time= ( (17-0) + (5-1) + (26-2) + (10-3) )/4 =52/4 = 13.0 s
P1
1
Scheduling
Algorithms
Ave. Waiting Time= ( (10-1-0) + (1-1) + (17-2) + (5-3) )/4 = 26/4 = 6.5 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
14/24
CPU SCHEDULING
5: CPU-Scheduling
14
PRIORITY BASED SCHEDULING:
y Assign each process a priority. Schedule highest priority first. All processes
within same priority are FCFS.
y Priority may be determined by user or by some default mechanism. The
system may determine the priority based on memory requirements, time
limits, or other resource usage.
y Suitable if some jobs are absolutely more important than the other, and
must be serviced rst.
y Starvation occurs if a low priority process never runs.
Scheduling
Algorithms
-
8/3/2019 Session 4 - CPU Scheduling - Finals
15/24
PRIORITY SCHEDULING
Priority Scheduling
Choose next job based on priority
For SJF, priority = expected CPU burst
Can be either preemptive or non-preemptive
Problem:
Starvation: jobs can wait indefinitely
Solution to starvation
Age processes: increase priority as a function of waiting time
-
8/3/2019 Session 4 - CPU Scheduling - Finals
16/24
PRIORITY SCHEDULING
The example assumed that all processes arrived
at 0 second.
Ave. Turnaround Time= ( (16 - 0) + (1 - 0) + (18 - 0) + (19 - 0) + (6 - 0) )/5=52/4 = 12.0 s
Ave. Waiting Time= ( (6 - 0) + (0 - 0) + (16 - 0) + (18 - 0) + (1 - 0) )/5= 41/5= 8.2 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
17/24
PRIORITY SCHEDULING (NON-
PREEMPTIVE)
Ave. Waiting Time NP= ( (1 - 0) + (0 - 0) + (16 - 0) + (18 - 0) + (11 - 3) )/5= 43/5= 8.6 s
Ave. Turnaround Time NP= ( (11 - 0) + (1 - 0) + (18 - 0) + (19 - 0) + (16 - 3) )/5= 62/5= 12.4 s
0 11 16 18
P2 P5 P3P1
1
P4
19
-
8/3/2019 Session 4 - CPU Scheduling - Finals
18/24
PRIORITY SCHEDULING
(PREEMPTIVE)
0 3 8 16 18
P1 P5 P1 P3P2
1 19
P4
Ave. Waiting Time NP= ( (8 3 - 0) + (0 - 0) + (16 - 0) + (18 - 0) + (3 - 3) )/5= 39/5= 7.8 s
Ave. Turnaround Time NP= ( (16 - 0) + (1 - 0) + (18 - 0) + (19 - 0) + (8 - 3) )/5=59/5= 11.8 s
-
8/3/2019 Session 4 - CPU Scheduling - Finals
19/24
ROUND ROBIN:
y Use a timer to cause an interrupt after a predeterminedtime. Preempts if task exceeds its quantum.
y Train of eventsDispatch
Time slice occurs OR process suspends on event
Put process on some queue and dispatch next
y Use numbers in last example to find queueing andresidence times. (Use quantum = 4 sec.)
19
CPU SCHEDULING SchedulingAlgorithms
-
8/3/2019 Session 4 - CPU Scheduling - Finals
20/24
CPU SCHEDULING
5: CPU-Scheduling
20
EXAMPLE DATA:
Process Arrival Service
Time Time
1 0 8
2 1 4
3 2 9
4 3 5
0 8 12 16 26
P2 P3 P4 P1
Round Robin, quantum = 4, no priority-based preemption
Ave. Turnaround Time= ( (20-0) + (8-1) + (26-2) + (25-3) )/4 = 74/4 = 18.5
P1
4
P3 P4
20 24 25
P3
Scheduling
Algorithms
Note:
Example violates rules for
quantum size since most
processes dont finish in
one quantum.
-
8/3/2019 Session 4 - CPU Scheduling - Finals
21/24
ROUND ROBIN
Round Robin (RR)
Often used for timesharing
Ready queue is treated as a circular queue (FIFO)
Each process is given a time slice called a quantum It is run for the quantum or until it blocks
RR allocates the CPU uniformly (fairly) across participants.
If average queue length is n, each participant gets 1/n
-
8/3/2019 Session 4 - CPU Scheduling - Finals
22/24
CSS430 CPU Scheduling
22
Each process is given CPU time in turn, (i.e. time quantum: usually 10-100milliseconds), and thus waits no longer than ( n 1 ) * time quantum
time quantum = 20
Process Burst Time Wait Time
P1
53 57 +24 = 81
P2
17 20
P3
68 37 + 40 + 17= 94
P4
24 57 + 40 = 97
ROUND ROBIN SCHEDULING
P1 P2 P3 P4 P1 P3 P4 P1 P3 P30 20 37 57 77 97 117 121 134 154 162
Average wait time = (81+20+94+97)/4 = 73
57
20
37
57
24
40
40
17
P1(53)
P2(17)
P3(68)
P4(24)
P1(33) P1(13)
P3(48) P3(28) P3(8)
P4(4)
-
8/3/2019 Session 4 - CPU Scheduling - Finals
23/24
RR WITH TIME QUANTUM = 20
Process Burst Time
P1 53
P2
17
P3 68P4 24
The Gantt chart is:
Higher average turnaround than SJF,
But better response time
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
0 20 37 57 77 97 117 121 134 154 162
-
8/3/2019 Session 4 - CPU Scheduling - Finals
24/24
RR: CHOICE OF TIME QUANTUM
Performance depends on length of the timeslice If timeslice time is set too high
attempting to amortize context switch cost, you get FCFS.
i.e. processes will finish or block before their slice is up anyway
If its set too low youre spending all of your time context switching between threads.
Timeslice frequently set to ~100 milliseconds
Moral:
Context switch is usually negligible (< 1% per timeslice)otherwise you context switch too frequently and lose allproductivity