34
Operating Systems CPU Scheduling

Operating Systems

  • Upload
    rowena

  • View
    23

  • Download
    0

Embed Size (px)

DESCRIPTION

Operating Systems. CPU Scheduling. Agenda for Today. What is Scheduler and its types Short-term scheduler Dispatcher Reasons for invoking scheduler Optimization criteria FCFS, SJF, SRTF, RR, Multi level Queues With Examples. CPU Scheduling. Scheduling processes in the ready queue - PowerPoint PPT Presentation

Citation preview

Page 1: Operating Systems

Operating Systems

CPU Scheduling

Page 2: Operating Systems

Agenda for Today

What is Scheduler and its typesShort-term schedulerDispatcherReasons for invoking schedulerOptimization criteriaFCFS, SJF, SRTF, RR, Multi

level Queues With Examples

Page 3: Operating Systems

CPU Scheduling

Scheduling processes in the ready queue

Short-term schedulerDifferent types of schedulers

Page 4: Operating Systems

Life of a Process

Page 5: Operating Systems

Histogram of CPU-burst Times

Page 6: Operating Systems

CPU SchedulerShort-term schedulerSelects a process from among

the processes in the ready queue

Invokes the dispatcher to have the CPU allocated to the selected process

Page 7: Operating Systems

Dispatcher Dispatcher gives control of the CPU

to the process selected by the short-term scheduler; this involves:switching contextswitching to user mode jumping to the proper location in

the user program to start (or restart) it

Page 8: Operating Systems

Dispatcher

Dispatch latency – time it takes for the dispatcher to stop one process and start another running.

Typically, a few microseconds

Page 9: Operating Systems

CPU Scheduler CPU scheduling decisions may

take place when a process:1.Switches from running to

waiting state2.Switches from running to

ready state3.Switches from waiting to ready4.Terminates

Page 10: Operating Systems

CPU Scheduler

Scheduling under 1 and 4 is nonpreemptive.

All other scheduling is preemptive.

Page 11: Operating Systems

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

Page 12: Operating Systems

Scheduling Criteria

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 13: Operating Systems

Optimization Criteria

Maximize CPU utilizationMaximize throughputMinimize turnaround time Minimize waiting time Minimize response time

Page 14: Operating Systems

FCFS Scheduling The process that enters the ready

queue first is scheduled first, regardless of the size of its next CPU burst

Example: Process Burst Time P1 24 P2 3 P3 3

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

Page 15: Operating Systems

FCFS Scheduling Processes are served in the order: P1,

P2, P3 The Gantt Chart for the schedule is:

Waiting times P1 = 0; P2 = 24; P3 = 27 Average waiting time: (0+24+27)/3 = 17

P1 P2 P3

24 27 300

Page 16: Operating Systems

Suppose that 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 Convoy effect short process behind long

process

P1P3P2

63 300

FCFS Scheduling

Page 17: Operating Systems

Shortest-Job-First (SJF) Scheduling

Process with the shortest CPU burst is scheduled first.

Non-preemptive – once CPU given to a process it cannot be preempted until completes its CPU burst.

Page 18: Operating Systems

Shortest-Job-First (SJF) Scheduling

Preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt it—Shortest-Remaining-Time-First (SRTF).

SJF is optimal non-preemptive scheduling algorithm – gives minimum average waiting time for a given set of processes.

Page 19: Operating Systems

Non-Preemptive SJF Process Arrival Time Burst

Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

Gantt chart

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

P1 P3 P2

7 160

P4

12

Page 20: Operating Systems

Preemptive SJF Process Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

Gantt chart

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

P3P2

42 110

P4

5 7

P2 P1

16

P1

Page 21: Operating Systems

Priority SchedulingA priority number (integer) is

associated with each processThe CPU is allocated to the

process with the highest priority (smallest integer highest priority).PreemptiveNon-preemptive

Page 22: Operating Systems

Priority SchedulingSJF is a priority scheduling

where priority is the predicted next CPU burst time.

Problem Starvation – low priority processes may never execute.

Solution Aging – as time progresses increase the priority of the process.

Page 23: Operating Systems

Round Robin (RR)Each process gets a small unit

of CPU time, called time slice or quantum, which is usually 10-100 milliseconds. After this time has elapsed, the process is preempted and added to the end of the ready queue.

Page 24: Operating Systems

Round Robin (RR) If there are n processes in the

ready queue, the time quantum is q, and context switch time is tcs, then no process waits more than (n-1)(q+tcs) time units

Used in time-sharing systems where response time is an important performance criteria

Page 25: Operating Systems

Performance q large FCFS q small q must be large with

respect to context switch, otherwise overhead is too high.

Round Robin (RR)

Page 26: Operating Systems

Round Robin ExampleProcess Burst Time

P1 53 — 33 — 13P2 17

P3 68 — 48 — 28 — 8

P4 24 — 4 The Gantt chart with quantum 20 is:

P1 P2 P3 P4 P1 P3 P4 P1 P3 P3

0 20 37 57 77 97 117 121 134 154 162

Page 27: Operating Systems

Round Robin ExampleProcess Turnaround Time Waiting Time

P1 134 134 – 53 = 81P2 37 37 – 17 = 20P3 162 162 – 68 = 94P4 121 121 – 24 = 97

Average waiting time = 73 Average waiting time for SJF = 38 Typically, higher average turnaround

than SJF, but better response.

Page 28: Operating Systems

Quantum vs Context Switch

Process Time = 10 Quantum Context Switches

12 0

6 1

1 9

Page 29: Operating Systems

Multilevel QueuesReady queue is partitioned into

separate queues:- foreground (interactive)- background (batch)

Each queue has its own priority and scheduling algorithm: - foreground – RR- background – FCFS

Page 30: Operating Systems

Multilevel Queues Scheduling must be done across

queues.Fixed priority scheduling; i.e.,

serve all from foreground then from background.

Time slice – each queue gets a certain percentage of CPU time, e.g., 80% to foreground in RR and 20% to background in FCFS

Page 31: Operating Systems

Multilevel Queues

Page 32: Operating Systems

Multilevel Feedback Queues

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 queuesScheduling algorithms for each

queue

Page 33: Operating Systems

Multilevel Feedback Queues

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 34: Operating Systems

Multilevel Feedback Queues