• Keine Ergebnisse gefunden

CPU Scheduling

N/A
N/A
Protected

Academic year: 2022

Aktie "CPU Scheduling"

Copied!
59
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

CPU Scheduling

(2)

Outlook

Motivation and Basic Concepts p

Scheduling Algorithms

R lti S h d li Al ith

Realtime Scheduling Algorithms

Examples a p es

(3)

Motivation and Basic Concepts

(4)

Motivation

A processor can run only one process at a time

CPU is available when a running process is

CPU is available when a running process is blocked, yields the CPU, or is preemptively removed

removed

S h d l i ibl f l ti

Scheduler is responsible for selecting one process from the ready queue

Dispatcher is responsible for switching the

context from ready to running y g

(5)

CPU-I/O Burst Cycles y

Process execution is a l f lt ti CPU cycle of alternating CPU and I/O waits

Referred as CPU and I/O b t

burst

CPU burst tend to be

small

(6)

Histogram of CPU-Burst Times g

Histogram of CPU-burst

durations [Silberschatz et al.], typically exponentially or

typically exponentially or

hyperexponentially distributed

(7)

Preemptive and Nonpreemptive Scheduling p p p g

Scheduling is required when a process

1 Switches from running to waiting

1. Switches from running to waiting

2. Switches from running to ready

3. Switches from waiting to ready

4. Terminates

Nonpreemptive (or cooperative) scheduling includes case 1. and 4.

onlyy

Preemptive scheduling includes case 2. and 3. as well

Cooperative scheduling does not need special hardware required for

Cooperative scheduling does not need special hardware required for preemptive scheduling

Preemptive scheduling requires mechanisms to coordinate access to shared data

(8)

Criteria for Choosing the Right Scheduling g g g

CPU utilization – keep the CPU as busy as possible

Throughput number of processes that complete their

Throughput – number of processes that complete their execution per time unit

Turnaround time – amount of time to execute a particular p process

Waiting time – amount of time a process has been waiting in the ready queue

in the ready queue

Response time – amount of time it takes from when a

request was submitted until the first response is produced

Measures: maximum/minimum, average, variance

Example: optimize for maximum/minimum to provide each user a

Example: optimize for maximum/minimum to provide each user a good service

Example: in an interactive system one should optimize for low

(9)

Scheduling Algorithms

(10)

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

The process that requests the CPU first is p q allocated the CPU first

Process keeps the CPU until it is terminated

Process keeps the CPU until it is terminated or blocked

Implemented by a FIFO queue

Average waiting time under FCFS is often

quite long

(11)

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

Process Burst Time

P1 24

P22 3

P3 3

Suppose that the processes arrive at time 0 in the

d P P P

order: P1 , P2 , P3

The Gantt Chart for the schedule is:

P1 P2 P3

24 27 30

0 24 27 30

0

(12)

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

Suppose that the processes arrive in the order P

2

, P

3

, P

1

The Gantt chart for the schedule is:

P1 P3

P2

6

3 30

0

Waiting time for P

1

= 6 ; P

2

= 0

;

P

3

= 3

Much better than previous case

Much better than previous case

Average waiting time: (6 + 0 + 3)/3 = 3

(13)

Shortest-Job-First Scheduling (SJF) g ( )

Assign the CPU to the process with the g p smallest next CPU burst

FCFS scheduling to break ties

FCFS scheduling to break ties

SJF provably gives the minimum average waiting time for a given set of processes waiting time for a given set of processes

“Proof”: moving short process before a long

one decreases average waiting time (see

(14)

Proving the Optimality of SJF – Proof Idea g p y

(15)

Proving the Optimality of SJF g p y

Given processes p1,…,pn, we define the set P of all possible schedules as follows:

Let the burst time of a process p be b(p). Given a schedule

( ) h ( ) f

(q1,…,qn) ∈ P, the waiting time w(qk) for any qk is:

The average waiting time w(q1,…,qn) of a schedule

( ) P i

q=(q ,…,q ) ∈ P is:

(16)

Proving the Optimality of SJF g p y

A schedule (q1,…,qn) ∈ P is a SJF schedule iff:

Remark 1: there could be more than one SJF schedule.

Consider b(p1) = … = b(pn) = 1, for example. Here each permutation of p1,…,pn is obviously a valid SJF schedule.

Remark 2: If both q P and r P are SJF schedules, they

“obviously” satisfy w(q) = w(r).

[P f E i A ( ) ( ) f h k f

[Proof Exercise: Assume w(q) < w(r) for the sake of contradiction…]

(17)

Proving the Optimality of SJF g p y

Theorem: A SJF schedule q=(q1,…,qn) ∈ P is is optimal

with respect to the average waiting time, i.e. for each other non-SJF schedule r=(r1,…,rn) ∈ P :

Proof: Assume for the sake of contradiction that there exists a non SJF schedule r (r r ) ∈ P such that

a non-SJF schedule r=(r1,…,rn) ∈ P such that

Si ll SJF h d l h th i ht thi i li

Since all SJF schedules have the same weight, this implies that a non-SJF schedule r=(r1,…,rn) ∈ P has the lowest weight i e :

weight, i.e.:

(18)

Proving the Optimality of SJF g p y

Since r=(r1,…,rn) ∈ P is a non-SJF schedule there exists a k such that

(otherwise r would obviously be an SJF schedule) Consider the schedule r’ = (r’1 , …, r’n)

:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (i.e. schedule r where rk are rk+1 are interchanged)

(19)

Proving the Optimality of SJF g p y

Consider the schedule r’ = (r’1, …, r’n)

:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (A) for i · k we have:

(B) for i ≥ k+2 we have:

( )

(20)

Proving the Optimality of SJF g p y

Consider the schedule r’ = (r’1, …, r’n)

:= (r1, …, rk-1, rk+1, rk , rk+2 , …, rn) (C) for i = k+1 we have:

(21)

Proving the Optimality of SJF g p y

With (A), (B) and (C) follows:

This contradicts the assumption at the beginning that schedule r has the lowest weight.

(22)

SJF: Estimating the Length of a CPU-Burst g g

Problem: CPU-burst length can not exactly be predicted

Idea: Assume that CPU-burst length is similar to the length of the previous ones

Compute an exponential average …

n n

n

t  

1

  ( 1  )

Why exponential?

)

1

1 ( )

1 ( )

1

(

1 j n1 0

1

 ( 1  )  ... ( 1  )  ... ( 1  ) 

n

t

n

  t

n

  

j

t

n j

  

n

(23)

Notes on the Exponential Average p g

(24)

Notes on the Exponential Average p g

(25)

SJF: Estimating the Length of a CPU-Burst g g

(26)

SJF: Preemptive and Nonpreemptive Variant p p p

CPU burst of a new arriving process may be g p y shorter than what is left for the currently

executing process executing process

Preemptive: switch CPU to the new arriving process

process

Nonpreemptive: allow the currently executing t fi i h

process to finish

Example …

(27)

Example of Non-Preemptive SJF

Process Arrival Time Burst Time

P 0 0 7

P

1

0.0 7

P

2

2.0 4

P 4 0 1

P

3

4.0 1

P

4

5.0 4

SJF ( ti )

SJF (non-preemptive)

P P P P

P1 P3 P2

7 16

0

P4

8 12

(28)

Example of Preemptive SJF p p

Process Arrival Time Burst Time

P 0 0 7

P

1

0.0 7

P

2

2.0 4

P 4 0 1

P

3

4.0 1

P

4

5.0 4

SJF ( ti )

SJF (preemptive)

P P P P P P

P1 P2 P3

4

2 11

0

P4

5 7

P2 P1

16

Average waiting time = (9 + 1 + 0 +2)/4 = 3

4

2 5 7

(29)

Priority Scheduling y g

Each process has an assigned priority

CPU is allocated to the process with the highest priority

CPU is allocated to the process with the highest priority

Ties are broken by FCFS

SJF is a possible instance of priority scheduling

SJF is a possible instance of priority scheduling

Internal priorities – based on some measurable quantity

External priorities – set outside the operating system Preemptive and nonpreemptive variant

Preemptive and nonpreemptive variant

Starvation problem: steady stream of higher-priorityStarvation problem: steady stream of higher priority

(30)

Round Robin Scheduling (RR) g ( )

FCFS + preemption p p

Process gets CPU for maximal one time quantum  (e.g. 100 ms)

quantum  (e.g. 100 ms)

After this time has elapsed, the process is

preempted and added to the end of the ready preempted and added to the end of the ready queue

O CPU b t l th  th CPU i l d

On CPU-burst less than  the CPU is released

previously

(31)

Round Robin Scheduling (RR) ou d ob du g ( )

Process Burst Time

P 53

P1 53

P2 17

P3 68

P3 68

P4 24

The Gantt chart is:

P P P P P P P P P P

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3 0 20 37 57 77 97 117 121 134 154 162

(32)

RR: Properties p

Observation for n queued processes

Each process gets at least 1/n of the CPU time

Each process must wait no longer than (n-1)*

If  is large then RR gets FCFS

If  is large then RR gets FCFS

If  is extremely small

RR i ll d h i

RR is called processor sharing

In theory each process has its own processor running with 1/n of the original processor speed

with 1/n of the original processor speed

Control Data Corporation (CDC)

(33)

RR: Time Quantum and Context-Switch Time Q

Context switch requires time

(34)

RR: Turnaround Time Varies with 

Turnaround time is improved if most processes finish processes finish their next CPU burst in a single time quantum

If time quantum is too large RR

degenerates to FCFS

FCFS

If time quantum is too small

turnaround time turnaround time increases due to context switch time

Rule of thumb: 80 percent of CPU-

(35)

Multilevel Queue Scheduling Q g

Partition the ready queue into several separate queues

queues

Processes are permanently assigned to one queue

Each queue has its own scheduling algorithm

Example FCFS queue for background processes and RR

Example FCFS queue for background processes and RR queue for foreground processes

Scheduling among queues commonly implemented as

Scheduling among queues commonly implemented as fixed-priority preemptive scheduling

Alternative: Time slicing among queues

(36)

Example p

(37)

Multilevel Feedback-Queue Scheduling Q g

Multilevel Queue scheduling but process is allowed to move between queues

to move between queues

The idea is to separate processes with different CPU- burst characteristics

Process using too much CPU time is moved to lower- priority queue

Aging allows processes to move to upper priority

Aging allows processes to move to upper-priority queues. Used to prevent starvation.

General parameters: number of queues, scheduling algorithms, methods to move

th d t l t th t

processes, method to select the queue on entry

(38)

Example p

if quantum is exceededq

if quantum is exceeded

(39)

Multiple-Processor Scheduling p g

Scheduling in a homogeneous system

Each processor maintaining its own queue?

It is reasonable to use a single ready queue and selecting a free processor

Asymmetric multiprocessing

One master processor

One master processor

Other processors execute user code only

Symmetric multiprocessing (SMP)

Symmetric multiprocessing (SMP)

(40)

Realtime Scheduling Algorithms

(41)

Real-Time Schedulingg

Hard real-time – accomplish a task within a given t f ti

amount of time

Each operation must be guaranteed to take a maximum amount of time

amount of time

Possible for special hardware which lacks from full operating system functionality

operating system functionality

Soft real-time – critical processes receive priority

Soft real time critical processes receive priority over less fortunate ones

Requires priority scheduling

Requires priority scheduling

(42)

Real-Time Scheduling Issues g

Systems waiting for completion of system call have long dispatch latency

dispatch latency

Solution: Preemptible system calls

Preemption points

Preemption points

Preemptible kernel with synchronized data structures

Priority inversion – Higher priority process waits for resources used by lower priority process

Example: Processes L M H with priorities L < M< H; H waits for

Example: Processes L, M, H with priorities L < M< H; H waits for ressource occupied by L; CPU is however allocated to M; the CPU should be allocated to H before it is allocated to M!

S l ti i it i h it l i it

Solution: priority-inheritance – lower priority process

inherits the higher priority until deallocating the resource

(43)

Hard-Real-Time CPU Schedulingg

Periodic processes require the CPU at specified intervals (periods)

(periods)

p is the duration of the period

d is the deadline by when the process must be serviced

d is the deadline by when the process must be serviced

t is the processing time

(44)

Rate Montonic Schedulingg

Motivation

P1

P1

Period: 50

Processing time: 20

Deadline: 50

Deadline: 50

CPU utilization: 20/50 = 0.4

P2

Period: 100

Period: 100

Processing time: 35

Deadline: 100

CPU utilization: 35/100 = 0.35

CPU utilization: 35/100 0.35

Total CPU utilization: 0.4 + 0.35 = 0.75

However, P2 with higher priority than P1?

(45)

Rate Montonic Schedulingg

Priority is assigned based on the inverse of period

Shorter periods = higher priority

Longer periods = lower priority

Scheduling is preemptive

Scheduling is preemptive

Previous example

Previous example

P1: Period = 50, Processing time = 20, Deadline = 50

P2: Period = 100, Processing time = 35, Deadline = 100

P1 is assigned a higher priority than P2

(46)

Optimality of RMS p y

RMS is optimal in the sense that if a set of processes can

b h d l d b RMS i b h d l d b

not be scheduled by RMS it can not be scheduled by any other algorithm that utilizes static priorities

For processes with CPU utilization above 100% obviously real time scheduling is not possible

real time scheduling is not possible

For processes with CPU utilization below 100% RMS (and

For processes with CPU utilization below 100% RMS (and thus any of the before mentioned strategies) might not find a valid schedule; see next example

find a valid schedule; see next example

(47)

Missed Deadlines with RMS

P1

P i d 50

Period: 50

CPU burst: 25

CPU utilization: 25/50 = 0.5/

P2

Period: 80 b

CPU burst: 35

CPU utilization: 35/80 = 0.44

Total CPU utilization: 0 5 + 0 44 = 0 94

Total CPU utilization: 0.5 + 0.44 = 0.94

However, deadline is missed

(48)

Earliest Deadline First Schedulingg

Priorities assigned according to deadlines

earlier deadline, higher priority

later deadline, lower priority

Previous Example

P1: Period = deadline = 50; CPU burst = 25 P2: Period deadline 80; CPU burst 35

P2: Period = deadline = 80; CPU burst = 35

EDF is optimal in theory

EDF is optimal in theory

Schedule for 100% CPU utilization

(49)

Proportional Share Scheduling p g

T shares are allocated among all processes in g p the system.

An application receives N shares where N < T .

This ensures each application will receive N / T

of the total processor time.

(50)

Examples

(51)

Example: Pthread Scheduling p g

Recall: user-level threads are mapped to kernel- l l th d

level-threads

Process-contention scope (PCS)

User-level threads belonging to the same process

Scheduling on LWPs

System-contention scope (SCS)

Deciding which kernel level thread to schedule onto a CPU

(52)

Example: Pthread Scheduling p g

pthread_attr_setscope(&attr, . )

PTHREAD_SCOPE_PROCESS – PCS scheduling

PTHREAD SCOPE SYSTEM _ _ – SCS scheduling g

th d tt t h d li (& tt )

pthread_attr_setschedpolicy(&attr, . )

SCHED_FIFO – FCFS

SCHED_RR – Round Robin

SCHED OTHER _ – not specified; system specific p ; y p

(53)

Example: Solaris Scheduling p g

Priority-based thread scheduling

Priority Time

Quantum Time

Quantum Return

From scheduling

CPU assigned to highest priority thread

Quantum Quantum

Expired From Blocking IO

0 200 0 50

RR in case of multiple threads with the highest priority

0 200 0 50

5 200 0 50

10 160 0 51

Example: dispatch table

f ti h i

15 160 5 51

20 120 10 52

25 120 15 52

for time sharing processes

Interactive processes have good response time

5 0 5 5

30 80 20 53

35 80 25 54

0 0 30 g p

40 40 30 55

(54)

Example: Windows XP Scheduling p g

Priority-based preemptive scheduling Idle thread if no other thread is ready

Idle thread if no other thread is ready

Priority is determined by priority class and relative priority

Variable class: 1 to 15, real-time class: 16 to 31Variable class: 1 to 15, real time class: 16 to 31

Variable priorities are lowered on time quantum exceeded Priority never lowered below a base priority

(55)

Example: Linux Scheduling p g

Preemtive priority-based scheduling (lower value = higher priority)

real-time range: 0 to 99

nice: 100 to 140

Unlike other systems: higher priority gets larger time quanta

Example: priority 0 – time quantum 200ms

Example: priority 0 – time quantum 200ms

Example: priority 140 – time quantum 10ms

Processes remain active as long time quantum is not exceededg q

Processes become expired otherwise

Only active processes get the CPU

Expired process becomes active again when all processes became

pi d p g p

expired

Real-time

Soft real time Soft real-time

(56)

Example: Java Scheduling p g

Loosely defined scheduling policy

Higher priority threads run in preference

Higher-priority threads run in preference

Not necessarily preemptive

Lower-priority threads may get an opportunity to run on expense of higher priority threads

of higher-priority threads

Not necessarily time sliced

C ti ltit ki A th d i ld t l f

Cooperative multitasking: A thread may yield control of CPU by invoking the method Thread.yield()

Thread priorities are not altered by the JVM

Priorities can be set by

Th d tTh d() tP i it (i t)

Thread.currentThread().setPriority(int)

Thread priority is often mapped to the priority of the

(57)

Summary and References

(58)

Summaryy

Scheduler selects the next process; dispatcher allocates the CPU to the selected process

the CPU to the selected process

The simplest scheduling strategy: FCFS

SJF is optimal wrt. to average waiting time; but how to p g g ; predict the expected job length?

RR useful for interactive systems

Multilevel scheduling useful for systems with a mix of IO

Multilevel scheduling useful for systems with a mix of IO and CPU bound process types

Real time systems: EDF, SJF

System performance depends on the scheduling strategy

There is not the one scheduling strategy: The right choice depends on the system demands

depends on the system demands

Methods for evaluating a scheduling strategy: Gantt charts, queuing-network analysis; simulation

(59)

References

Silberschatz, Galvin, Gagne, „Operating , , g , „ p g

System Concepts“, Seventh Edition, Wiley, 2005

2005

Chapter 5 „CPU Scheduling“

Ch 19 5 R l Ti CPU S h d li “

Chapter 19.5 „Real-Time CPU Scheduling“

Referenzen

ÄHNLICHE DOKUMENTE

Rahel Brunschwiler

Der ÄKBV München etabliert eine konsequente Rettungskette vom ersten außerklinischen Kontakt mit dem an Sepsis Erkrankten bis zu seiner defi- nitiven Versorgung in der Klinik. Es

An important part of this thesis is the construction of a Hilbert space describing the possi- ble measurements compatible with a continuous-time evolution as an inductive limit..

Die Trugschlüsse in den Formteilen B und B'', die anstatt zu einem Refrain überleiten, harmonisch wieder zum Formteil A der Strophen zurückkehren, könnten semantisch als

We introduced Time-sensitive Software-dened Network (TSSDN), an SDN based architecture, for handling time-triggered trac using commodity SDN switches, i.e., the transmission

jedoch nicht unterscheiden kann, ob eine TIA oder ein Schlaganfall vorliegt, sollten Anwesende niemals zögern und stets schnell handeln. Alarmzeichen erkennen Raten Sie

Use the following OBP command to manually synchronize a new or replacement I/O Board to an existing Clock Board:.

[r]