45
Course 5: CPU Scheduling

Ch05 CPU Scheduling

  • Upload
    mmmaya

  • View
    228

  • Download
    0

Embed Size (px)

Citation preview

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 1/45

Course 5: CPU Scheduling

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 2/45

5.2 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Course 4: Review

Thread – single, sequential stream of execution

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 3/45

5.3 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Course 4: Review (Cont.)

Responsiveness – e.g. web browser, word processor etc

Resource Sharing – e.g. IPC issues

Economy (vs. process model) – e.g. Solaris – thread creation – 30times faster, thread context switch 5 times faster

Scalability – e.g. utilization of MP architectures

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 4/45

5.4 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Course 5: CPU Scheduling

Basic Concepts

Scheduling Algorithms

Multiple-Processor Scheduling

Operating Systems Examples

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 5/45

5.5 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Scheduling Basic

Concepts

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 6/45

5.6 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Basic Concepts

Process scheduling aka kernel-

level thread scheduling inthreaded systems

Scheduling may apply to all thecomputer resources

CPU scheduling:

Maximum CPU utilizationobtained withmultiprogramming

CPU –I/O Burst Cycle –

Process execution consists ofa cycle of CPU execution andI/O wait

CPU burst distribution

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 7/455.7 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Histogram of CPU-burst Times

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 8/455.8 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

CPU Scheduler

Selects from among the processes in memory that are ready toexecute (PCB queues), 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 (interrupt)

3. Switches from waiting to ready

4. Terminates

Scheduling under 1 and 4 is nonpreemptive/cooperative

All other scheduling is preemptive

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 9/455.9 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Dispatcher

Dispatcher module gives control of the CPU to the processselected 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 oneprocess and start another running

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 10/455.10 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Scheduling Criteria

CPU utilization – keep the CPU as busy as possible [40lightly loaded, 90 heavily used]

Throughput – # of processes that complete their executionper time unit

Turnaround time – amount of time to execute a particularprocess

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

Response time – amount of time it takes from when arequest was submitted until the first response is produced,not output (for time-sharing environment)

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 11/455.11 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Optimization Criteria

General

Max CPU utilization

Max throughput

Min turnaround time

Min waiting time Min response time

Contextual:

Interactive systems – minimize variance

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 12/455.12 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Scheduling Algorithms

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 13/455.13 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

First-Come, First-Served (FCFS) Scheduling

Process Burst TimeP 1 24

P  2  3

P 3 3

Suppose that the processes arrive in the order: P 1 , P  2 , P 3

The Gantt Chart for the schedule is:

Waiting time for P 1 = 0; P  2  = 24; P 3 = 27

Average waiting time: (0 + 24 + 27)/3 = 17

P1 P2 P3

24 27 300

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 14/455.14 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order

P  2  , P 3 , P 1

The Gantt chart for the schedule is:

Waiting time for P 1 = 6; P  2 = 0; P 3 = 3

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

Much better than previous case

Convoy effect short process behind long process

FCFS – not optimal for time-sharing systems

P1P3P2

63 300

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 15/455.15 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Shortest-Job-First (SJF) Scheduling

Associate with each process the length of its next CPU burst. Usethese lengths to schedule the process with the shortest time

Two schemes:

nonpreemptive – once CPU given to the process it cannot bepreempted until completes its CPU burst

preemptive –

if a new process arrives with CPU burst lengthless than remaining time of current executing process,preempt. This scheme is know as theShortest-Remaining-Time-First (SRTF)

SJF is optimal – gives minimum average waiting time for a givenset of processes

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 16/455.16 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Process Arrival Time Burst Time

P 1 0.0 7

P  2  2.0 4

P 3 4.0 1

P 4 5.0 4

SJF (non-preemptive)

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

Example of Non-Preemptive SJF

P1 P3 P2

73 160

P4

8 12

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 17/455.17 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Example of Preemptive SJF

Process Arrival Time Burst Time

P 1 0.0 7

P  2  2.0 4

P 3 4.0 1

P 4 5.0 4

SJF (preemptive)

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

P1 P3P2

4211

0

P4

5 7

P2 P1

16

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 18/455.18 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Determining Length of Next CPU Burst

Can only estimate the length

Can be done by using the length of previous CPU bursts, usingexponential averaging

:Define 4.

10, 3.burst CPUnextthefor valuepredicted 2.

burst CPUof length actual 1.

  

  

1n

th

n   nt 

.11   nnn

  t            

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 19/455.19 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Prediction of the Length of the Next CPU Burst

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 20/455.20 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Examples of Exponential Averaging

  =0

  n+1 = n

Recent history does not count

  =1

  n+1 = t n

Only the actual last CPU burst counts If we expand the formula, we get:

n+1 = tn+(1 -   ) t n -1 + …

+( 1 -   ) j  t n - j + …

+( 1 -   )n +10

Since both and (1 -  ) are less than or equal to 1, eachsuccessive term has less weight than its predecessor

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 21/455.21 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Priority Scheduling

A priority number (integer) is associated with each process

Priority may be calculated considering: time limits, memoryrequirements, no. of open files, avg. I/O burst/avg. CPU burst

The CPU is allocated to the process with the highest priority(smallest integer highest priority)

Preemptive nonpreemptive

SJF is a priority scheduling where priority is the predicted next CPUburst time

Problem Starvation – low priority processes may never execute

Solution Aging – as time progresses increase the priority of theprocess

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 22/455.22 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Round Robin (RR)

Each process gets a small unit of CPU time (time quantum),usually 10-100 milliseconds. After this time has elapsed, theprocess is preempted and added to the end of the ready queue.

If there are n processes in the ready queue and the timequantum is q, then each process gets 1/ n of the CPU time inchunks of at most q time units at once. No process waits more

than (n-1)q time units.

Performance

q large FIFO

q small q must be large with respect to context switch,otherwise overhead is too high

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 23/45

5.23 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Example of RR with Time Quantum = 20

Process Burst Time

P 1 53

P  2  17

P 3 68

P 4 24

The Gantt chart is:

Typically, higher average turnaround than SJF, but better response

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 24/45

5.24 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Time Quantum and Context Switch Time

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 25/45

5.25 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Turnaround Time Varies With The Time Quantum

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 26/45

5.26 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multilevel Queue

Ready queue is partitioned into separate queues:- foreground (interactive)- background (batch)

Each queue has its own scheduling algorithm

foreground – RR

background –

FCFS Scheduling must be done between the queues

Fixed priority scheduling; (i.e., serve all from foreground thenfrom background). Possibility of starvation.

Time slice – each queue gets a certain amount of CPU time

which it can schedule amongst its processes; i.e., 80% toforeground in RR

20% to background in FCFS

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 27/45

5.27 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multilevel Queue Scheduling

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 28/45

5.28 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multilevel Feedback Queue

A process can move between the various queues; aging can beimplemented this way

Multilevel-feedback-queue scheduler defined by the followingparameters:

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 enterwhen that process needs service

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 29/45

5.29 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Example of Multilevel Feedback Queue

Three queues:

Q0 – RR with time quantum 8 milliseconds

Q1 – RR time quantum 16 milliseconds

Q2 – FCFS

Scheduling

A new job enters queue Q0 which is served RR. When it gainsCPU, job receives 8 milliseconds. If it does not finish in 8milliseconds, job is moved to queue Q1.

At Q1 job is again served RR and receives 16 additionalmilliseconds. If it still does not complete, it is preempted and

moved to queue Q2.

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 30/45

5.30 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multilevel Feedback Queues

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 31/45

5.31 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multiple-Processor Scheduling

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 32/45

5.32 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multiple-Processor Scheduling

CPU scheduling more complex when multiple CPUs areavailable

 Asymmetric multiprocessing – only one processoraccesses the system data structures, alleviating the needfor data sharing

Symmetric multiprocessing - each processor is self-scheduled

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 33/45

5.33 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Processor affinity

Most recent data in cache If a process is switched from one processor to

another, there is a cost of invalidating andrepopulating caches

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 34/45

5.34 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Load balancing

Workload evenly distributed across all processors

Necessary on systems with multiple queues

Affects processor affinity rule

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 35/45

5.35 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Multicore processors

Multiple processors on the same chip

Memory stall = time waiting for data to be available whenaccessing memory

In order to optimize memory stall => multithreaded cores,each hardware thread appearing as a logical processor

Two levels of scheduling – OS, hardware

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 36/45

5.36 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Operating System Examples

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 37/45

5.37 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Operating System Examples

Solaris scheduling Windows XP scheduling

Linux scheduling

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 38/45

5.38 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Solaris Scheduling

6 priority classes, default TS

Inverse relation btw priorities andtime slices

Interactive processes – higherpriority, CPU-bound processeslower priority

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 39/45

5.39 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Solaris Dispatch Table

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 40/45

5.40 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Windows XP Priorities

Preemptive

6 priority classes / relative priority in each class

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 41/45

5.41 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Linux Scheduling

Two algorithms: time-sharing and real-time Time-sharing

Prioritized credit-based – process with most credits isscheduled next

Credit subtracted when timer interrupt occurs

When credit = 0, another process chosen When all processes have credit = 0, recrediting occurs

Based on factors including priority and history

Real-time

Soft real-time

Posix.1b compliant – two classes FCFS and RR

Highest priority process always runs first

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 42/45

5.42 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

The Relationship Between Priorities and Time-slice length

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 43/45

5.43 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

Algorithm Evaluation

Deterministic modeling –

takes a particularpredetermined workload and defines the performance ofeach algorithm for that workload

Queueing models

Implementation

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 44/45

5.44 Silberschatz, Galvin and GagneOperating System Concepts  – 8th Edition

5.15

8/10/2019 Ch05 CPU Scheduling

http://slidepdf.com/reader/full/ch05-cpu-scheduling 45/45