51
CSE 331 Operating Systems Design 1 Scheduling • Scheduling is divided into various levels. • These levels are defined by the location of the processes • A process can be – available to be executed by the processor – partially or fully in main memory – in secondary memory – is not started yet

CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

Embed Size (px)

Citation preview

Page 1: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 1

Scheduling• Scheduling is divided into various levels.

• These levels are defined by the location of the processes

• A process can be – available to be executed by the processor– partially or fully in main memory– in secondary memory– is not started yet

Page 2: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 2

Types of Scheduling• Long Term Scheduling (batch processing)

– The decision to add to the pool of processes to be executed.

• Medium Term Scheduling (swapping)– The decision to add to the process in main memory.

• Short Term Scheduling(CPU Scheduling)– The decision as to which process will gain the processor.

• I/O Scheduling– The decision as to which process's I/O request shall be

handled by a device.

Page 3: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 3

CPU Scheduling

• Select process(es) to run on processor(s)

• Process state is changed from “ready” to “running”

• The component of the OS which does the scheduling is called the scheduler

Page 4: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 4

Basic Concepts (CPU Scheduling)

• Maximum CPU utilization obtained with multiprogramming

• CPU–I/O Burst Cycle – Process execution consists of a cycle of CPU execution and I/O wait.

• CPU burst distribution

Page 5: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 5

Alternating Sequence of CPU And I/O Bursts

Page 6: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 6

Histogram of CPU-burst Times

Page 7: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 7

Types of CPU Scheduling

• A scheduling algorithm is NON-PREEMPTIVE (run to completion) if the CPU cannot be taken away by the OS.

• A scheduling algorithm is PREEMPTIVE if the CPU can be taken away by the OS.

Page 8: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 8

CPU 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.

• Scheduling under 1 and 4 is nonpreemptive.

• All other scheduling is preemptive.

Page 9: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 9

Dispatcher

Dispatcher module gives control of the CPU to the process selected 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 one process and start another running. (context switch overhead)

Page 10: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 10

The Interrupting Clock- Timer

• The OS sets the interrupting clock to generate an interrupt at some specified future time.

• This interrupt time is the process quantum(time slice-ts, time quantum-tq).

• Provides reasonable response times and prevents the system being held up by processes in infinite loops.

Page 11: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 11

Scheduling Criteria• CPU utilization – keep the CPU as busy as possible

• Throughput – # of processes that complete their execution per time unit

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

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

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

Page 12: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 12

Optimization Criteria

• Fairness : each process should get a fair share of the CPU

• Efficiency: keep CPU 100% utilized• Response time : should be minimized for

interactive users• Turnaround : minimize batch turnaround

times• Throughput : maximize number of jobs

processed per hour

Page 13: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 13

User-Oriented, Performance Criteria

Criteria AimResponse Time low response time,

maximum number of interactive users

Turnaround Time time between submission and completion

Deadlines maximize deadlines met

Page 14: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 14

System-oriented, Performance Criteria

Criteria Aim•Throughput allow maximum number of jobs to complete

•Processor maximize percentage of time processor is busy

utilization

•Overhead minimize time processor busy executing OS

Page 15: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 15

System oriented, other criteria

Criteria Aim

Fairness treat processes the same avoid starvation

Enforcing Priorities give preference to higher priority processes

Balancing Resources keep the system resources busy

Page 16: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 16

Important Factors• I/O / CPU boundedness of a process• Is the process interactive or batch?• Process priority• Page fault frequency (Virtual Memory )• Preemption frequency (time slice)• Execution time received• Execution time required to complete

Page 17: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 17

Popular research area in 1970’s..

Assumptions

• One program per user

• One thread per program

• Programs are independent

Page 18: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 18

Scheduling Algorithms

• FCFS -----------------------FCFS• Shortest Job First ------------------ SJF• Shortest Remaining Time• Highest Response Ratio Next• Round Robin -------------------------RR• Virtual Round Robin• Priority -------------------------------Priority• Priority Classes(Multilevel Queues)• Feedback Queues

Page 19: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 19

FCFS (First Come First Serve)

• Implementation:– As each process becomes ready, it joins the

ready queue. – When the current process finishes, the

oldest process is selected next.

• Characteristics:– Simple to implement– Non-premptive– Penalizes short and I/O-bound processes

Page 20: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 20

First-Come, First-Served (FCFS) Scheduling

Process Burst Time

P1 24

P2 3

P3 3

• Suppose that the processes arrive in the order: P1 , P2 , P3

The Gantt Chart for the schedule is:

• Waiting time for P1 = 0; P2 = 24; P3 = 27

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

P1 P2 P3

24 27 300

Page 21: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 21

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order

P2 , P3 , P1 .

• The Gantt chart for the schedule is:

• Waiting time for P1 = 6; P2 = 0; P3 = 3

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

• Much better than previous case.• Convoy effect short process behind long process

P1P3P2

63 300

Page 22: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 22

Approximating Load

• Let λ = mean arrival rate

• So 1/ λ = mean time between arrivals

• And µ = mean service rate

• So 1/ µ = mean service time (avg t(pi))

• CPU busy = ρ = λ * 1/ µ = λ / µ

• Notice must have λ / µ (i.e., ρ < 1)

• What if ρ approaches 1?

Page 23: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 23

Predicting Wait Time in FCFS

• In FCFS, when a process arrives, all in ready list will be processed before this job

• Let µ be the service rate

• Let L be the ready list length

Wavg(p) = L*1/ µ + 0.5* 1/ µ = L/ µ +1/(2 µ )

Compare predicted wait with actual in earlier examples

Page 24: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 24

Shortest-Job-First (SJF)• Sometimes known as Shortest Process Next

(SPN)

• Implementation:

–The process with the shortest expected execution time is given priority on the processor

Page 25: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 25

Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 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

Page 26: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 26

SJF-continued

• Characteristics:– Non-premptive– Reduces average waiting time over FIFO– Always produces the minimum average

turnaround time– Must know how long a process will run– Possible user abuse– Suitable for batch environments. Not useful in a

timesharing environment

Page 27: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 27

Shortest Remaining Time (SRT)

• Preemptive counterpart of SPN

• Implementation:–Process with the smallest estimated run-

time to completion is run next–A running process may be preempted by a

new process with a shorter estimate run-time

Page 28: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 28

Example of Preemptive SJFProcess Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 29: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 29

SRT-continued

• Characteristics:– Still requires estimates of the future– Higher overhead than SJF– No additional interrupts are generated as

in Round Robin– Elapsed service times must be recorded

Page 30: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 30

Determining Length of Next CPU Burst

• Can only estimate the length.

• Can be done by using the length of previous CPU bursts, using exponential averaging.

:Define 4.

10 , 3.

burst CPU next the for value predicted 2.

burst CPU of lenght actual 1.

1n

thn nt

.tnnn

+1

1

Page 31: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 31

Prediction of the Length of the Next CPU Burst

Page 32: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 32

Examples of Exponential Averaging

=0 n+1 = n

– Recent history does not count. =1

– n+1 = tn

– Only the actual last CPU burst counts.

• If we expand the formula, we get:

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

+(1 - )j tn-1 + …

+(1 - )n=1 tn 0

• Since both and (1 - ) are less than or equal to 1, each successive term has less weight than its predecessor.

Page 33: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 33

Highest Response Ratio Next (HRRN)

• How do you get around the problem of Indefinite postponement?

• Implementation:– Once a job gets the CPU, it runs to completion– The priority of a job is a function of the job's

service time and the time it has been waiting for service

priority = (time waiting + service time) / service time

Page 34: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 34

• Characteristics:

–Nonpremptive

–Shorter jobs still get preference over longer jobs

–However aging ensures long jobs will eventually gain the processor

–Estimation still involved

Page 35: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 35

Round Robin (RR)

• Implementation:– Processes are dispatched FIFO. But are given a

fixed time on the CPU (quantum - time slice).

• Characteristics:– Preemptive– Effective in time sharing environments– Penalizes I/O bound processes

Page 36: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 36

Quantum Size• Some Options:

– Large or small quantum– Fixed or variable quantum– Same for everyone or different– If quantum is to large RR degenerates into FCFS– If quantum is to small context switching becomes

the primary job being executed• A good guide is: quantum should be slightly larger

than the time required for a typical interaction (overhead 10%)

For 100ms ts - context switch time < 10ms

Page 37: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 37

Example of RR with Time Quantum = 20

Process Burst Time

P1 53

P2 17

P3 68

P4 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

Page 38: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 38

Time Quantum and Context Switch Time

Page 39: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 39

Turnaround Time Varies With The Time Quantum

Page 40: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 40

Virtual Round Robin (VRR)• A modification to the RR algorithm to remove

the bias towards CPU bound processes.

• Implementation:– Two “ready” queues, one called an AUX

queue for storing “completed” IO processes– AUX queue has priority over READY queue– IO processes only runs for remaining time

• Characteristics:– Performance studies indicate fairer than RR

Page 41: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 41

Priority• Implementation:

– Each process is assigned a priority and the scheduler always selects the highest priority process first

• Characteristics:– High priority processes may run indefinitely, so

decrease the priority of these processes at regular intervals

– Assign high priority to system processes with known characteristics such as being I/O bound

Page 42: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 42

Priority Classes

Priority Class 4

Priority Class 2

Priority Class 1

Priority Class 3

Highest

Lowest

Page 43: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 43

• Implementation:– Processes are grouped into priority classes– Round Robin is used within a class– When selecting process start with the highest

class. If the class is empty, use a lower class

• Characteristics:– If priorities are not adjusted from time to time,

lower classes may starve to death

Page 44: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 44

Multilevel Queue• Ready queue is partitioned into separate queues:

foreground (interactive)background (batch)

• Each queue has its own scheduling algorithm, foreground – RRbackground – FCFS

• Scheduling must be done between the queues.– Fixed priority scheduling; (i.e., serve all from

foreground then from 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% to foreground in RR 20% to background in FCFS

Page 45: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 45

Multilevel Queue Scheduling

Page 46: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 46

Multilevel Feedback Queue

• A process can move between the various queues; aging can be implemented this way.

• Multilevel-feedback-queue scheduler defined by the

following parameters:– 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 enter when that process needs service

Page 47: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 47

Example of Multilevel Feedback Queue

• Three queues:

– Q0 – time quantum 8 milliseconds

– Q1 – time quantum 16 milliseconds

– Q2 – FCFS

• Scheduling

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

– At Q1 job is again served FCFS and receives 16 additional milliseconds. If it still does not complete, it is preempted and moved to queue Q2.

Page 48: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 48

Multilevel Feedback Queues

Page 49: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 49

• I/O processes:– If the job requires I/O before quantum

expiration it leaves the network and comes back at the same level queue

• CPU bound processes:– If the quantum expires first, the process is

placed on the next lower queue– This continues until it reaches the bottom

queue

Page 50: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 50

• Dispatching:– A process is only placed on the CPU if all

higher level queues are empty– A running process is preempted by a

process arriving in a higher queue– Processes from lower level queues receive a

larger quantum

Page 51: CSE 331 Operating Systems Design 1 Scheduling Scheduling is divided into various levels. These levels are defined by the location of the processes A process

CSE 331 Operating Systems Design 51

Summary:

A CPU Scheduling Mechanism Should

• Favour short jobs

• Favour I/O bound jobs to get good I/O device utilization

• Determine the nature of a job and schedule accordingly