34
Operating Systems WT 2019/20 Scheduling

Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems WT 2019/20Scheduling

Page 2: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 2

Process Concept● Process – a program in execution● A process includes:

● CPU state (one or multiple threads)● memory segments (.text, .data, .bss, ...)● resources (open files, sockets, etc)

● Operating System manages multiprogramming● Batch system – jobs● Time-shared systems – user programs or tasks

Page 3: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 3

Thread Concept● Thread – unit of scheduling

● assigned time slices (quantum)● Belongs to only one process

● All threads in a process share memory segments and resources

● A thread includes:● CPU context (registers)● Thread-local data

Page 4: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 4

Scheduling

● Long-term scheduler (or Job scheduler) ● Selects which processes with their threads

should be enabled to run

● Short-term scheduler (or CPU scheduler)● Selects which thread should be executed next

and allocates CPU

Page 5: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 5

Long-Term Scheduler

● Controls degree of multiprogramming● by suspending entire processes and their threads● can reduce resource contention

● Invoked infrequently, if at all, can be slow

Page 6: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 6

Short-Term Scheduler

● Also referred to as “CPU Scheduler” or just “Scheduler”● selects among the threads ready for execution the one to

execute next, and allocates a CPU● Invokes dispatcher to give control of the CPU to scheduled

thread

● Invoked very frequently, must be fast and scalable

Page 7: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 7

Dispatcher● gives control of the CPU to the next active thread

● saving and restoring CPU contexts – Program Counter– Stack Register– Data Registers– Processor Status Word

● prepares address space of next active process, if different● dispatch latency: ca. 1–2 µs

Page 8: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 8

CPU Switch from Thread to ThreadThread T1

Save state into TCB2

Reload state from TCB1

Save state into TCB1

Reload state from TCB2

timer interrupt or blocking system call Thread T2

timer interrupt or blocking system call

Page 9: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 9

Process / Thread Control Block (PCB / TCB)

Program Counter

Parent PID

Handle Table

Process ID (PID)

Data Registers

Next Process Block

Image File Name

PCB

List of ThreadControl Blocks

List of open files

Next TCB

Thread Control Block (TCB)

Page 10: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 10

Context Switch● When CPU switches to another thread, the system must save the

state of the old thread and load the saved state for the new thread.

● Interaction with memory management (MMU) is required when switching between threads in different processes.

● Context-switch time is overhead; the system does no useful work while switching.

Page 11: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 11

Short-Term Scheduler

● Also referred to as “CPU Scheduler” or just “Scheduler”● selects among the threads ready for execution the one to

execute next, and allocates a CPU● Invokes dispatcher to give control of the CPU to scheduled

thread

● Invoked very frequently, must be fast and scalable

Page 12: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 12

Thread Queues● Scheduler maintains threads in queues● Enqueue and dequeue operations very efficient● Threads migrate between queues

● “ready” queue● Contains threads ready to execute

● “device” queues● Contains threads waiting on an I/O device

Page 13: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 13

Ready Queue and I/O Device Queues

CPUReady queue

I/O 1 wait

I/O 2 wait

I/O n wait

I/O n queue

I/O 1 queue

I/O complete

Time-out

ReleaseDispatch

Page 14: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 14

Thread States● Five-state diagram for thread scheduling:

– init: The thread is being created– ready: The thread is waiting to be assigned to a CPU– running: The threads instructions are being executed– waiting: The thread is waiting for some event to occur– terminated: The thread has finished execution

init

ready

waiting

running

terminated

Page 15: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 15

Basic Scheduling Considerations● What invokes the scheduler?● What assumptions should a scheduler rely on?● What are its optimization goals?

● Considering:● Multiprogramming designed to increase CPU utilization● Thread execution subject to cycles of I/O

and compute bursts

Page 16: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 16

Basic Scheduling Considerations● What invokes the scheduler?● What assumptions should a scheduler rely on?● What are its optimization goals?

● Considering:● Multiprogramming designed to increase CPU utilization● Thread execution subject to cycles of I/O

and compute bursts

Page 17: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 17

Sequence of CPU and I/O BurstsThreads can be described as either:

● I/O-bound – spends more time doing I/O than computations, many short CPU bursts

● CPU-bound – spends more time doing computations; few very long CPU bursts

…load valinc valread file

wait for I/O

inc countadd data, valwrite file

wait for I/Oload valinc valread from file

wait for I/O…

CPU burst

CPU burst

CPU burst

I/O burst

I/O burst

I/O burst

Page 18: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 18

Basic Scheduling Considerations● What invokes the scheduler?● What assumptions should a scheduler rely on?● What are its optimization goals?

● Considering:● Multiprogramming designed to increase CPU utilization● Thread execution subject to cycles of I/O

and compute bursts

Page 19: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 19

Short-Term Scheduler Invocation

● Scheduling decisions take place when:● a thread switches from running to waiting state● a thread switches from running to ready state● a thread switches from waiting to ready● a thread terminates

Page 20: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 20

Basic Scheduling Considerations● What invokes the scheduler?● What assumptions should a scheduler rely on?● What are its optimization goals?

● Considering:● Multiprogramming designed to increase CPU utilization● Thread execution subject to cycles of I/O

and compute bursts

Page 21: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 21

Optimization Criteria● CPU scheduling uses heuristics to manage contradicting optimization criteria.

● e.g. throughput vs. interactivity; fairness vs. starvation-avoidance● Schedulers are optimized for certain workloads

● interactive vs. batch processing; I/O-intense vs. compute-intense

● Common optimization criteria:● Maximize CPU utilization● Maximize throughput● Minimize turnaround time ● Minimize waiting time ● Minimize response time

Page 22: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 22

First-In, First-Out (FIFO)● Also known as First-Come, First-Served (FCFS)● Suppose that the threads arrive in the order: T1 , T2 , T3

● The Gantt Chart for the schedule is:

● Waiting time for T1 = 0; T2 = 20; T3 = 25● Average waiting time: (0 + 20 + 25) / 3 = 15● convoy effect: thread behind long threads experience long waiting time

T1 T2 T3

20 25 290

Thread Burst Time

T1 20

T2 5

T3 4

Page 23: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 23

First-In, First-Out (FIFO)Suppose that the threads arrive in the order

T2 , T3 , T1 .● The Gantt chart for the schedule is:

● Waiting time for T1 = 9; T2 = 0; T3 = 5● Average waiting time: (9 + 0 + 5) / 3 = 4.66● reordering affects wait time

T1T3T2

95 290

Thread Burst Time

T1 20

T2 5

T3 4

Page 24: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 24

First-In, First-Out (FIFO)

● Draw a Gantt chart!● Assume context switch

takes time of 1

Thread Arrival Time Burst Time

T1 0 8

T2 2 6

T3 2 6

T4 4 10

Page 25: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 25

Round Robin (RR)● Preemptive version of FIFO scheduling algorithm

● Each thread gets a small unit of CPU time (time quantum), usually 10-100 milliseconds

● After this time has elapsed, the thread is preempted and added to the end of the ready queue

● Each of n ready thread gets 1/n of the CPU time in chunks of at most quantum q time units at once

● Of n ready threads, no one 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

Page 26: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 26

Round Robin (RR)● Assuming all threads have same priority, and

terminating threads yield control immediately:

● Round-Robin favors CPU-intense over I/O-intense threads ● I/O bursts cause thread to yield control goes to the back of the →

ready queue after completion

T1 T2 T3 T4 T1 T3 T4 T1 T3 T3

0 10 17 27 37 47 57 61 64 74 82

Thread Burst Time

T1 23

T2 7

T3 38

T4 14

Page 27: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 27

Quantum Length Consideration

Thread execution time: 15

0 15

15

15

0

0

10

10

quantumcontext

switches20

10

1

0

1

14

● Longer quantum yields shorter average turnaround times

Page 28: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 28

Round Robin (RR)

● Draw a Gantt chart!● for quantum length 4● for quantum length 8

● assuming context switch takes time of 1

Thread Arrival Time Burst Time

T1 0 8

T2 2 6

T3 2 6

T4 4 + 0 10

Page 29: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 29

Priority Scheduling

● A priority number (integer) is associated with each thread● implemented through one ready queue per priority

● The CPU is allocated to the thread with the highest priority● Preemptive● Non-preemptive

Page 30: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 30

RR with priorities

● Round-Robin favors CPU-intense over I/O-intense threads ● Priority-elevation after I/O completion / Priority-aging after long

CPU bursts can provide a compensation● Windows uses Round-Robin with a priority-elevation scheme

T2

T1 T3 T1 T3 T1 T3 T3

T4 T4

0 7 17 27 37 47 50 60 68 78 82

4321

Thread Burst Time

Priority

T1 23 2

T2 7 4

T3 38 2

T4 14 1

Page 31: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 31

Round Robin (RR) with priorities● Draw a Gantt chart!

● for quantum length 4● for quantum length 8

● assuming context switch takes time of 1

● high-priority threads can preempt low priority threads

● a thread loses 1 priority after every full quantum

● unless interrupted early

Thread Arrival Time

Burst Time

Priority

T1 0 8 9

T2 2 6 11

T3 2 6 10

T4 4 10 11

Page 32: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 32

StarvationStarvation is a problem:

● low priority threads may never executeSolutions1) Decreasing priority & aging: the Unix approach

● Decrease priority of CPU-intense threads● Exponential averaging of CPU usage to slowly increase priority of

blocked threads2) Priority Elevation: the Windows/VMS approach

● Increase priority of a thread on I/O completion● System gives starved threads an extra burst

Page 33: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 33

Multilevel Queue● Ready queue is partitioned into separate queues:

● Real-time (system, multimedia)● Interactive

● Queues may have different scheduling algorithm,● Real-Time – RR● Interactive – RR + priority-elevation + quantum stretching

● Scheduling must be done between the queues● Fixed priority scheduling (i.e., serve all from real-time threads then from

interactive)– Possibility of starvation

● Time slice – each queue gets a certain amount of CPU time which it can schedule amongst its threads;

– CPU reserves

Page 34: Operating Systems WT 2019/20 - uni-potsdam.de · Operating Systems 25 Round Robin (RR) Preemptive version of FIFO scheduling algorithm Each thread gets a small unit of CPU time (time

Operating Systems 34

Multilevel Queue Scheduling

Real-time system threads

Real-time user threads

System threads

Interactive user threads

background threads

High priority

Low priority

● Windows uses strict Round-Robin for real-time threads● Priority-elevation can be disabled for non-RT threads