CPU Scheduling Heechul Yun 1 Agenda Introduction to CPU - - PowerPoint PPT Presentation

cpu scheduling
SMART_READER_LITE
LIVE PREVIEW

CPU Scheduling Heechul Yun 1 Agenda Introduction to CPU - - PowerPoint PPT Presentation

CPU Scheduling Heechul Yun 1 Agenda Introduction to CPU scheduling Classical CPU scheduling algorithms 2 CPU Scheduling CPU scheduling is a policy to decide Which thread to run next? When to schedule the next thread? How


slide-1
SLIDE 1

CPU Scheduling

Heechul Yun

1

slide-2
SLIDE 2

Agenda

  • Introduction to CPU scheduling
  • Classical CPU scheduling algorithms

2

slide-3
SLIDE 3

CPU Scheduling

  • CPU scheduling is a policy to decide

– Which thread to run next? – When to schedule the next thread? – How long?

  • Context switching is a mechanism

– To change the running thread

3

slide-4
SLIDE 4

Assumption: CPU Bursts

  • Execution model

– Program uses the CPU for a while and the does some I/O, back to use CPU, …, keep alternating

4

slide-5
SLIDE 5

CPU Scheduler

  • An OS component that determines which

thread to run, at what time, and how long

– Among threads in the ready queue

5

slide-6
SLIDE 6

CPU Scheduler

  • When the scheduler runs?

– The running thread finishes – The running thread voluntarily gives up the CPU

  • yield, block on I/O, …

– The OS preempts the current running thread

  • quantum expire (timer interrupt)

6

slide-7
SLIDE 7

Performance Metrics for CPU Scheduling

  • CPU utilization

– % of time the CPU is busy doing something

  • Throughput

– #of jobs done / unit time

  • Response time (Turn-around time)

– Time to complete a task (ready -> complete)

  • Waiting time

– Time spent on waiting in the ready queue

  • Scheduling latency

– Time to schedule a task (ready -> first scheduled)

7

slide-8
SLIDE 8

Example

  • Assumption: A, B, C are released at time 0
  • The times of Process A

– Response time: 9 – Wait time: 5 – Sched. latency: 0

8

A B C A B C A C A C Time

  • sched. latency

+ + wait time response time

slide-9
SLIDE 9

Example

  • Assumption: A, B, C are released at time 0
  • The times of Process B

– Response time: 5 – Wait time: 3 – Latency: 1

9

A B C A B C A C A C Time

  • sched. latency

+ wait time response time

slide-10
SLIDE 10

Example

  • Assumption: A, B, C are released at time 0
  • The times of Process C

– Response time: 10 – Wait time: 6 – Latency: 2

10

A B C A B C A C A C Time

  • sched. latency

+ + + wait time response time

slide-11
SLIDE 11

Administrative

  • No office hour today

– If you need to meet me, plz wait until Thursday or schedule a separate appointment via email

  • Project 2

– Will be out Thursday – Due: 10/22 (Monday)

11

slide-12
SLIDE 12

Recap: CPU Scheduling

  • CPU scheduling is a policy to decide

– Which thread to run next? – When to schedule the next thread? – How long?

  • Context switching is a mechanism

– To change the running thread

12

slide-13
SLIDE 13

Recap: Example Metrics

  • Assumption: A, B, C are released at time 0
  • The times of Process C

– Response time: 10 – Wait time: 6 – Latency: 2

13

A B C A B C A C A C Time

  • sched. latency

+ + + wait time response time

slide-14
SLIDE 14

Workload Model and Gantt Chart

  • Workload model
  • Gantt chart

– bar chart to illustrate a particular schedule

14

Process Arrival Time Burst Time P1 8 P2 1 4 P3 1 10 P4 6 2

P1 P2 P3 P4

8 12 22 24

slide-15
SLIDE 15

Agenda

  • Basic scheduling policies

– First-come-first-serve (FCFS) – Shortest-job-first (SJF) – Shortest-remaining-time-first (SRTF) – Round-robin (RR)

15

slide-16
SLIDE 16

Scheduling Policy Goals

  • Maximize throughput (minimize avg. waiting time)

– High throughput (#of jobs done / time) is good

  • Minimize scheduling latency

– Important to interactive applications (games, editor, …)

  • Fairness

– Make all threads progress equally

  • Goals often conflicts

– Frequent context switching may be good for reducing response time, but not so much for maximizing throughput

16

slide-17
SLIDE 17

First-Come, First-Served (FCFS)

  • FCFS

– Assigns the CPU based on the order of the requests. – Implemented using a FIFO queue.

17

slide-18
SLIDE 18

FCFS

  • Example

– Suppose that the processes arrive in the order: P1 , P2 , P3 – Waiting time?

  • P1 = 0; P2 = 24; P3 = 27

– Average waiting time

  • (0 + 24 + 27)/3 = 17

18

Process Arrival Time Burst Time P1 24 P2 3 P3 3 P1 P2 P3 24 27 30

slide-19
SLIDE 19

FCFS

  • Example 2

– Suppose that the processes arrive in the order: P2 , P3, P1 – Waiting time?

  • P1 = 6; P2 = 0; P3 = 3

– Average waiting time

  • (6 + 0 + 3)/3 = 3

– Much better than previous case  performance varies greatly depending on the scheduling order

19

Process Arrival Time Burst Time P1 24 P2 3 P3 3 P1 P3 P2 6 3 30

slide-20
SLIDE 20

Shortest Job First (SJF)

  • Can we always do better than FIFO?

– Yes: if you know the tasks’ CPU burst times

  • Shortest Job First (SJF)

– Order jobs based on their burst lengths – Executes the job with the shortest CPU burst first – SJF is optimal

  • Achieves minimum average waiting time

20

slide-21
SLIDE 21

Shortest Job First (SJF)

  • Example

– Gantt chart – Average waiting time?

  • (3 + 16 + 9 + 0) / 4 = 7
  • How to know the CPU burst time in advance?

21

Process Arrival Time Burst Time P1 6 P2 8 P3 7 P4 3 P4 P3 P1 3 16 9 P2 24

slide-22
SLIDE 22

Determining CPU Burst Length

  • Can only estimate the length

– Next CPU burst similar to previous CPU bursts ? – Predict based on the past history

  • Exponential weighted moving average (EWMA)

– of past CPU bursts

22

slide-23
SLIDE 23

Shortest Job First (SJF)

  • What if jobs don’t arrive at the same time?

– Average waiting time

  • (0+7+15+9)/4 = 7.5

23

Process Arrival Time Burst Time P1 8 P2 1 4 P3 2 9 P4 3 5

P2 P4 P1 P3

8 12 17 26

slide-24
SLIDE 24

Shortest Remaining Time First (SRTF)

  • Preemptive version of SJF
  • New shorter job preempt longer running job
  • Average waiting time

– (9 + 0 + 15 + 2 ) / 4 = 6.5

24

Process Arrival Time Burst Time P1 8 P2 1 4 P3 2 9 P4 3 5

P

1

P2 P4 P1 P3

1 5 10 17 26

slide-25
SLIDE 25

Quiz: SRTF

  • Average waiting time?
  • (9 + 0 + 15 + 2 ) / 4 = 6.5

25

Process Arrival Time Burst Time P1 8 P2 1 4 P3 2 9 P4 3 5

P

1

P2 P4 P1 P3

1 5 10 17 26

P1 P2 P3 P4

slide-26
SLIDE 26

So Far…

  • FIFO

– In the order of arrival – Non-preemptive

  • SJF

– Shortest job first. – Non preemptive

  • SRTF

– Preemptive version of SJF

26

slide-27
SLIDE 27

Issues

  • FIFO

– Bad average response time

  • SJF/SRTF

– Good average waiting time – IF you know or can predict the future

  • Time-sharing systems

– Multiple users share a machine – Need high interactivity  low scheduling latency

27

slide-28
SLIDE 28

Round-Robin (RR)

  • FIFO with preemption
  • Simple, fair, and easy to implement
  • Algorithm

– Each job executes for a fixed time slice: quantum – When quantum expires, the scheduler preempts the task – Schedule the next job and continue...

28

slide-29
SLIDE 29

Round-Robin (RR)

  • Example

– Quantum size = 4 – Gantt chart – Sched. Latency (between ready to first schedule)

  • P1: 0, P2: 4, P3: 7. average response time = (0+4+7)/3 = 3.67

– Waiting time

  • P1: 6, P2: 4, P3: 7. average waiting time = (6+4+7)/3 = 5.67

29

Process Burst Times P1 24 P2 3 P3 3 P1 P2 P3 P1 P1 P1 P1 P1 4 7 10 14 18 22 26 30

slide-30
SLIDE 30

How To Choose Quantum Size?

  • Quantum length

– Too short  high overhead (why?) – Too long  bad scheduling latency

  • Very long quantum  FIFO

30

slide-31
SLIDE 31

Round-Robin (RR)

  • Example

– Quantum size = 2 – Gantt chart – Scheduling latency

  • P1: 0, P2: 2, P3: 4. average response time = (0+2+4)/3 = 2

– Waiting time

  • P1: 6, P2: 6, P3: 7. average waiting time = (6+6+7)/3 = 6.33

31

Process Burst Times P1 24 P2 3 P3 3 P1 P2 P3 P1 P2 P1 P1 P3 P1 2 4 6 8 9 12 14 10 30

slide-32
SLIDE 32

Discussion

  • Comparison between FCFS, SRTF(SJF), and RR

– What to choose for smallest average waiting time?

  • SRTF (SFJ) is the optimal

– What to choose for better interactivity?

  • RR with small time quantum (or SRTF)

– What to choose to minimize scheduling overhead?

  • FCFS

32

slide-33
SLIDE 33

Example

  • Task A and B

– CPU bound, run an hour

  • Task C

– I/O bound, repeat(1ms CPU, 9ms disk I/O)

  • FCFS?

– If A or B is scheduled first, C can begins an hour later

  • RR and SRTF?

33

Compute I/O I/O

A or B C

slide-34
SLIDE 34

Example Timeline

34

RR with 100ms time quantum RR with 1ms time quantum

C A B

I/O

C

A B … A B

I/O

C A B A B … C A B …

I/O

SRTF

C

A

I/O

C A C A …

I/O

slide-35
SLIDE 35

Summary

  • First-Come, First-Served (FCFS)

– Run to completion in order of arrival – Pros: simple, low overhead, good for batch jobs – Cons: short jobs can stuck behind the long ones

  • Round-Robin (RR)

– FCFS with preemption. Cycle after a fixed time quantum – Pros: better interactivity (low average scheduling latency) – Cons: performance is dependent on the quantum size

  • Shortest Job First (SJF)/ Shorted Remaining Time First (SRTF)

– Shorted job (or shortest remaining job) first – Pros: optimal average waiting time – Cons: you need to know the future, long jobs can be starved by short jobs

35

slide-36
SLIDE 36

Recap: Workload Model and Gantt Chart

  • Workload model
  • Gantt chart

– bar chart to illustrate a particular schedule

36

Process Arrival Time Burst Time P1 8 P2 1 4 P3 1 10 P4 6 2

P1 P2 P3 P4

8 12 22 24

slide-37
SLIDE 37

Recap: Scheduling Policies

  • First-Come, First-Served (FCFS)

– Run to completion in order of arrival – Pros: simple, low overhead, good for batch jobs – Cons: short jobs can stuck behind the long ones

  • Round-Robin (RR)

– FCFS with preemption. Cycle after a fixed time quantum – Pros: better interactivity (low average scheduling latency) – Cons: performance is dependent on the quantum size

  • Shortest Job First (SJF)/ Shorted Remaining Time First (SRTF)

– Shorted job (or shortest remaining job) first – Pros: optimal average waiting time – Cons: you need to know the future, long jobs can be starved by short jobs

37

slide-38
SLIDE 38

Agenda

  • Multi-level queue scheduling
  • Fair scheduling
  • Real-time scheduling
  • Multicore scheduling

38

slide-39
SLIDE 39

Multiple Scheduling Goals

  • Optimize for interactive applications

– Round-robin

  • Optimize for batch jobs

– FCFS

  • Can we do both?

39

slide-40
SLIDE 40

Multi-level Queue

  • Ready queue is partitioned into separate queues

– Foreground: interactive jobs – Background: batch jobs

  • Each queue has its own scheduling algorithm

– Foreground : RR – Background: FCFS

  • Between the queue?

40

slide-41
SLIDE 41

Multi-level Queue Scheduling

  • Scheduling between the queues

– Fixed priority

  • Foreground first; schedule background only when no

tasks in foreground

  • Possible starvation

– Time slicing

  • Assign fraction of CPU time for each queue
  • 80% time for foreground; 20% time for background

41

slide-42
SLIDE 42

Multi-level Feedback Queue

  • Each queue has a priority
  • Tasks migrate across queues

– Each job starts at the highest priority queue – If it uses up an entire quantum, drop one-level – If it finishes early, move up one-level (or stay at top)

  • Benefits

– Interactive jobs stay at high priority queues – Batch jobs will be at the low priority queue – Automatically!

42

slide-43
SLIDE 43

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 0 Time

A B C

2 5 9

43

slide-44
SLIDE 44

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 1 Time

A B C

3 7

A

1

44

slide-45
SLIDE 45

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 2 Time

A B C

4 3

A

1

B

45

slide-46
SLIDE 46

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 3 Time

A B C

6 3

A

1

B C

46

slide-47
SLIDE 47

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 3 Time

A B C

6 3

A

1

B C

Suppose A is blocked on I/O

47

slide-48
SLIDE 48

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 3 Time

B C

5 2

A B C

Suppose A is blocked on I/O

48

slide-49
SLIDE 49

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 5 Time

A B C

3

A

1

B C

A is returned from I/O

49

slide-50
SLIDE 50

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 6 Time

A B C

3

A B C

50

slide-51
SLIDE 51

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 8 Time

A B C

3

A B C

2

C

51

slide-52
SLIDE 52

Example of Multilevel Feedback Queues

 Priority 0 (time slice = 1):  Priority 1 (time slice = 2):  Priority 2 (time slice = 4):

time = 9 Time

A B C A B C C

52

slide-53
SLIDE 53

Completely Fair Scheduler (CFS)

53

  • Linux default scheduler, focusing on fairness
  • Each task owns a fraction of CPU time share

– E.g.,) A=10%, B=30%, C=60%

  • Scheduling algorithm

– Each task maintains its virtual runtime

  • Virtual runtime = executed time (x weight)

– Pick the task with the smallest virtual runtime

  • Tasks are sorted according to their virtual times
slide-54
SLIDE 54

CFS Example

  • Tasks are sorted according to their virtual times

54

5 6 8 10

Scheduled the “neediest” task

slide-55
SLIDE 55

CFS Example

  • Tasks are sorted according to their virtual times

55

9 6 8 10

On a next scheduler event re-sort the list But list is inefficient.

slide-56
SLIDE 56

Red-black Tree

– Self-balancing binary search tree – Insert: O(log N), Remove: O(1)

56 Figure source: M. Tim Jones, “Inside the Linux 2.6 Completely Fair Scheduler”, IBM developerWorks

slide-57
SLIDE 57

Weighed Fair Sharing: Example

Weights: gcc = 2/3, bigsim=1/3 X-axis: mcu (tick), Y-axis: virtual time Fair in the long run

57

slide-58
SLIDE 58

Some Edge Cases

  • How to set the virtual time of a new task?

– Can’t set as zero. Why? – System virtual time (SVT)

  • The minimum virtual time among all active tasks
  • cfs_rq->min_vruntime

– The new task can “catch-up” tasks by setting its virtual time with SVT

58

slide-59
SLIDE 59

Weighed Fair Sharing: Example 2

59

Weights: gcc = 2/3, bigsim=1/3 X-axis: mcu (tick), Y-axis: virtual time gcc slept 15 mcu