CPU Scheduling Section 2 5 CPU scheduling goals

  • Slides: 42
Download presentation
CPU Scheduling Section 2. 5

CPU Scheduling Section 2. 5

CPU scheduling goals

CPU scheduling goals

Competing performance objectives • CPU utilization. Keep the CPU as busy as possible •

Competing performance objectives • CPU utilization. Keep the CPU as busy as possible • Throughput. Maximize the number of processes completed in a unit of time • Turnaround Time. Minimize the time it takes a process to execute • Waiting Time. Minimize the total amount of time spent in the ready queue • Response Time. (for interactive jobs) Minimize the amount of time from the submission of a job to the first response.

CPU scheduling and process states new job sched interrupt ready cpu sched I/O or

CPU scheduling and process states new job sched interrupt ready cpu sched I/O or event complete waiting terminated running I/O or event wait

How the OS handles CPU allocation • When the CPU becomes idle the short-term

How the OS handles CPU allocation • When the CPU becomes idle the short-term scheduler is invoked. – It selects a process from the ready queue. • Then the dispatcher assigns the CPU to the chosen process. Its functions include: – context switching – switching from kernel to user mode – branching to the proper place in the user process

About that ready queue. . . • It contains the PCBs of all processes

About that ready queue. . . • It contains the PCBs of all processes ready to execute. • We refer to it as a queue but it is not necessarily FIFO. – It may be a priority queue, a tree, or an unordered linked list – The scheduling algorithm determines which of these data structures is used

When scheduling decisions are important • When a process is created – Should the

When scheduling decisions are important • When a process is created – Should the parent or the child process run? • When a process is terminated – What if no other process is ready to run? • When a process is blocked – Why is it blocked? What effect does the reason have on which process is chosen next? • When an I/O interrupt occurs – Should the process that was waiting on I/O be scheduled immediately?

Two kinds of scheduling algorithms • Non-Preemptive – Once a process is allocated the

Two kinds of scheduling algorithms • Non-Preemptive – Once a process is allocated the CPU a process, it keeps it until it voluntarily relinquishes it (by terminating or switching to another state). • Preemptive – The OS can ‘bump’ a process from the CPU and allocate it to another process. • Which has more overhead?

Scheduling algorithm categories • Batch scheduling – – First-Come, First-Served (FCFS) Shortest Job First

Scheduling algorithm categories • Batch scheduling – – First-Come, First-Served (FCFS) Shortest Job First (SJF) Shortest Remaining Time (SRT) Three-level scheduling • Interactive scheduling – Round-Robin – Priority Scheduling – Multilevel Feedback Queues (MLFQ) • Real-Time scheduling – Shortest Process Next – Guaranteed Scheduling – Fair-Share Scheduling

Analogy: Waiting to photocopy credit: John Estell, Bluffton College • In an office, we

Analogy: Waiting to photocopy credit: John Estell, Bluffton College • In an office, we have several people and one photocopier. • Each person has a variety of items to photocopy - some have one page, others a few pages out of several books, and there also those who want to copy an entire book. • How should we allocate access to the photocopier?

An individual represents a process • A variety of processes: – short processes ---

An individual represents a process • A variety of processes: – short processes --- copying one page – long processes --- copying an entire book – CPU-bound processes (performing many computations without interruption) • copying a sequence of pages from one book – I/O-bound processes (performing only a few computations before an interruption occurs) • copying a few pages each from several books, or single pages scattered throughout a single book

FCFS (Batch) Whoever arrived first gets to use the machine to make as many

FCFS (Batch) Whoever arrived first gets to use the machine to make as many copies as desired.

FCFS • Non-preemptive • Processes are assigned CPU in the order in which they

FCFS • Non-preemptive • Processes are assigned CPU in the order in which they request it. • Easy to implement. – The ready queue is FIFO. • What are its weaknesses?

Process CPU burst P 1 P 2 P 3 FCFS 24 3 3 P

Process CPU burst P 1 P 2 P 3 FCFS 24 3 3 P 1 P 2 0 24 P 3 27 30 Average waiting Time = (0 + 24 + 27)/3 = 17 msec If the processes arrive in order P 2, P 3, P 1 we have: P 2 P 3 P 1 0 3 6 Average waiting Time = (0 + 3 + 6)/3 = 3 msec 30

SJF (Batch) “ 3” goes next! dispatcher “ 5” “ 32” “ 28” “

SJF (Batch) “ 3” goes next! dispatcher “ 5” “ 32” “ 28” “ 31” “ 26” “ 11” “ 28”

SJF • Non-preemptive • Processes are assigned the CPU on the basis of the

SJF • Non-preemptive • Processes are assigned the CPU on the basis of the length of their next CPU bursts – This algorithm should really be called “shortest next burst” • Theoretically optimal when all processes are available at the same time – SRT is the preemptive version of SJF

Process SJF P 4 0 P 1 P 2 P 3 P 4 P

Process SJF P 4 0 P 1 P 2 P 3 P 4 P 1 3 Burst Time 6 8 7 3 P 3 9 P 2 16 24 Average waiting time = (0 + 3 + 9 + 16)/4 = 7 msec With FCFS scheduling the average waiting time would be 10. 25 milliseconds - try it out!

SJF Process P 1 P 2 P 3 P 4 Non-preemptive: P 1 Arrival

SJF Process P 1 P 2 P 3 P 4 Non-preemptive: P 1 Arrival 0 1 2 3 P 2 Burst Time 8 4 9 5 P 4 P 3 0 8 12 17 26 Average waiting time = (0 + 7 + 9 + 15)/4 = 7. 75 msec Preemptive: P 1 P 2 P 4 P 1 P 3 26 0 1 5 10 17 Average waiting time = (0 + 2 + 9 + 15)/4 = 6. 5 msec

Three level scheduling (Batch) Short-term Long-term Intermediate

Three level scheduling (Batch) Short-term Long-term Intermediate

N copies at a time! Dispatcher Round Robin (interactive)

N copies at a time! Dispatcher Round Robin (interactive)

Round-Robin • Preemptive FCFS – A time slice or quantum q, 20 q 50

Round-Robin • Preemptive FCFS – A time slice or quantum q, 20 q 50 msec – The next process in the ready queue gets up to q msec of CPU time. • If the CPU burst of the process < q, it voluntarily relinquishes the CPU • If it is > q, a timer goes off, the CPU is interrupted and the process is preempted and put at the end of the ready queue. The next process at the head of the queue is gets the CPU.

Process P 1 P 2 P 3 Round Robin Burst Time 24 3 3

Process P 1 P 2 P 3 Round Robin Burst Time 24 3 3 Time quantum is 4 P 1 0 P 3 P 2 4 7 P 1 10 P 1 14 P 1 18 P 1 22 P 1 26 Average waiting time = (0 + 4 + 7 + 6)/3 = 5. 66 msec Recall that for these same processes with no preemption the average waiting time was 17 msec 30

RR performance depends on the size of the time quantum • If it is

RR performance depends on the size of the time quantum • If it is very large, it is the same as having no preemption (i. e. FCFS) • If it is very small, there are many context switches and valuable CPU time is spent swapping processes in and out • A rule of thumb is that 80% of the CPU bursts should be shorter than the quantum

Priority (interactive) The“ 2” boss goes next! “ 200” Boss “ 5” “ 32”

Priority (interactive) The“ 2” boss goes next! “ 200” Boss “ 5” “ 32” “ 28” “ 31” “ 26” “ 11” “ 28”

Priority Scheduling • Some processes are deemed more important than others – So, processes

Priority Scheduling • Some processes are deemed more important than others – So, processes are assigned numbers indicating their relative priority • Preemptive or non-preemptive • Static or dynamic • SJF is a priority scheduling algorithm – the length of the job determines the priority level. • How can starvation be avoided?

MLFQ (interactive) “ 3” goes next! max = 10 max = 20 max =

MLFQ (interactive) “ 3” goes next! max = 10 max = 20 max = 30

MLFQ • Implemented as multiple level Round-Robin queues: – the highest level has smallest

MLFQ • Implemented as multiple level Round-Robin queues: – the highest level has smallest quantum size – a process enters the ready queue at the highest level; if it does not complete the first time it gets the CPU, it returns to the ready queue one level down • I/O bound and interactive processes usually complete execution after one time in the CPU. • CPU-bound processes that wait too long in lower priority queues may be promoted to prevent starvation

MLFQ with four classes q=1 q=2 q=4 q=8

MLFQ with four classes q=1 q=2 q=4 q=8

Multilevel Queue (hybrid) • Processes are placed in different queues depending on their processing

Multilevel Queue (hybrid) • Processes are placed in different queues depending on their processing requirements – E. g. , interactive jobs have different response time requirements from batch jobs • Each queue has its own scheduling algorithm. – E. g. , the foreground (interactive) queue might be RR, the background (batch) queue FCFS • There is a scheduling algorithm among queues. – The foreground queue has priority over the background queue • a background job is run only when the foreground queue is empty

A multilevel system of queues System processes Interactive editing processes Batch processes • Each

A multilevel system of queues System processes Interactive editing processes Batch processes • Each queue has absolute priority over lower queues • no batch process can run unless upper queues are empty • If a higher priority process enters a queue while a batch process is running, the batch process may be preempted.

Parameters of a multilevel scheduler – the number of queues – the scheduling algorithm

Parameters of a multilevel scheduler – the number of queues – the scheduling algorithm for each queue – the method used to determine when to upgrade a process to a higher priority queue – the method used to determine when to downgrade a process to a lower priority queue – the method used to determine which queue a process will enter initially

The lowest levels may starve • Instead of absolute priority, we can time slice

The lowest levels may starve • Instead of absolute priority, we can time slice between the queues. Each queue gets a certain amount of CPU time, which can then be scheduled among the processes – E. g. , with two queues we can give 80% of CPU time to the high priority queue, 20% to the secondary one • This ensures that background jobs run

Shortest Process Next • The interactive version of SJF. • Each command is considered

Shortest Process Next • The interactive version of SJF. • Each command is considered as a process • The necessary processing time for each command is estimated.

Miscellaneous algorithms • Guaranteed scheduling – With n users, each gets about 1/n of

Miscellaneous algorithms • Guaranteed scheduling – With n users, each gets about 1/n of CPU power • Lottery scheduling – Randomly distributed “lottery tickets” – Lottery may be held 50 times/second – Many variations • Fair-share scheduling

Real-Time Scheduling • Time is an important factor in real-time systems – Data must

Real-Time Scheduling • Time is an important factor in real-time systems – Data must be processed within a given time frame or the system is worthless • Program is divided into a number of processes whose behaviors are known in advance – They run to completion once they have the CPU

Separating scheduling mechanism from the scheduling policy • A process knows which of its

Separating scheduling mechanism from the scheduling policy • A process knows which of its children are important and need priority – So, provide the mechanism in the kernel but allow user processes to set policy among their child processes & threads • Scheduling algorithm is parameterized – mechanism is in the kernel • Parameters are filled in by user processes – policy is set by user process

User-level thread scheduling

User-level thread scheduling

Kernel-level thread scheduling

Kernel-level thread scheduling

Scheduling algorithm performance • The only accurate way to evaluate a scheduling algorithm is

Scheduling algorithm performance • The only accurate way to evaluate a scheduling algorithm is to code it and see how it works • This subjects the algorithm to the system’s actual conditions • However, it is costly to rewrite the code & modify the operating system, and users must deal with a changing environment

Windows NT CPU scheduling • The scheduler runs in the kernel. • It is

Windows NT CPU scheduling • The scheduler runs in the kernel. • It is time-sliced (ie, round robin) 20 msec <= quantum <= 200 msec Servers have 6 * quantum of workstations • It is priority based – real time, high, normal, idle priority classes – Threads inherit their process priority & also have relative priorities within their process. • It is preemptive • MLFQ – 32 queues, with absolute priority from top to bottom

Linux CPU scheduling • Threads are implemented at the kernel level – Scheduling is

Linux CPU scheduling • Threads are implemented at the kernel level – Scheduling is based on threads, not processes • Three classes of threads – Real time FIFO – Real time round robin – Timesharing • See pp. 708 -709 Priority decreases downward