chapter 5 processor scheduling

28
Chapter 5 Processor Scheduling

Upload: wattan

Post on 09-Jan-2016

51 views

Category:

Documents


1 download

DESCRIPTION

Chapter 5 Processor Scheduling. 5.1 Introduction. Processor (CPU) scheduling is the sharing of the processor(s) among the processes in the ready queue The critical activities are: the ordering of the allocation and de-allocation of the CPU to the various processes and threads, one at a time - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 5 Processor Scheduling

Chapter 5Processor

Scheduling

Page 2: Chapter 5 Processor Scheduling

2

5.1 Introduction

• Processor (CPU) scheduling is the sharing of the processor(s) among the processes in the ready queue

• The critical activities are: – the ordering of the allocation and de-allocation of

the CPU to the various processes and threads, one at a time

– deciding when to de-allocate and allocate the CPU from a process to another process

Page 3: Chapter 5 Processor Scheduling

Scheduling and Performance

These activities must be carried out in such a way as to meet the performance objectives of the system

• Scheduling algorithms: FCFS, SJF, RR, SRT

3

Page 4: Chapter 5 Processor Scheduling

4

5.2 Types of Schedulers• Long-term scheduler (memory allocation)

– Determines which processes are loaded into memory

– Controls the degree of multiprogramming

• Medium-term scheduler

– Suspends (swaps out) and resumes (swaps in) processes

• Short-term scheduler (processor scheduling)

– Selects one of the processes that are ready and allocates the CPU to it.

Page 5: Chapter 5 Processor Scheduling

5

Medium-Term Scheduler

The basic idea is that some of the processes can be removed from memory (to disk) and thus reduce the degree of multiprogramming. this is called swapping

Page 6: Chapter 5 Processor Scheduling

6

5.3 Processor Scheduling

• A CPU scheduling policy defines the order in which processes are selected from the ready queue for CPU processing.

• The scheduling mechanism decides when and how to carry out the context switch to the selected process, i.e., the de-allocation of the CPU from the current process and allocation of the CPU to the selected process.

Page 7: Chapter 5 Processor Scheduling

7

Processor Scheduling (2)

• The scheduler selects the next process to execute from among several processes waiting in the ready queue.

• The dispatcher allocates the CPU to the selected process at the appropriate time.

Page 8: Chapter 5 Processor Scheduling

OS AddressSpace

OS AddressSpace

Implementing the Process Abstraction

ControlUnit

OS interface

Mac

hine

Exe

cuta

ble

Mem

ory

ALU

CPU

Pi AddressSpace

Pi AddressSpace

Pi CPU

Pi ExecutableMemory

Pi ExecutableMemory

Pk AddressSpace

Pk AddressSpace

Pk CPU

Pk ExecutableMemory

Pk ExecutableMemory

Pj AddressSpace

Pj AddressSpace

Pj CPU

Pj ExecutableMemory

Pj ExecutableMemory

Page 9: Chapter 5 Processor Scheduling

9

Process ActivitiesEvery process that request CPU service, carries out the

following sequence of actions:1. Join the ready queue and wait for CPU service. 2. Execute (receive CPU) for the duration of the current

CPU burst or for the duration of the time slice (timeout).

3. Join the I/O queue to wait for I/O service or return to the ready queue to wait for more CPU service.

4. Terminate and exit if service is completed, i.e., there are no more CPU or I/O bursts. If more service is required, return to the ready queue to wait for more CPU service.

Page 10: Chapter 5 Processor Scheduling

10

Simple Model for processor Scheduling

Page 11: Chapter 5 Processor Scheduling

11

5.3.1 CPU Scheduler• Insertion of processes that request CPU service into the

ready queue. This queue is usually a data structure that represents a simple first-in-first-out (FIFO) list, a set of simple lists, or as a priority list. This function is provided by the enqueuer, a component of the scheduler.

Page 12: Chapter 5 Processor Scheduling

12

Scheduler (2)

• The occurrence of a context switch, carried by the context switcher that saves the context of the current process and de-allocates the CPU from that process.

• The selection of the next process from the ready queue and loading its context. This can be carried out by the dispatcher, which then allocates the CPU to the newly selected process.

Page 13: Chapter 5 Processor Scheduling

13

Occurrence of a Context Switch

A context switch can occur at any of the following possible times:

• The executing process has completed its current CPU burst. This is the normal case in simple batch systems.

• The executing process is interrupted by the operating system because its allocated time (time slice) has expired. This is a normal case in time-sharing systems.

• The executing process is interrupted by the operating system because a higher priority process has arrived requesting CPU service.

Page 14: Chapter 5 Processor Scheduling

14

Process Queues

The processes that are ready and waiting to execute are kept on a list called the ready queue.

A similar list exists for each I/O device, called the device queue.

Page 15: Chapter 5 Processor Scheduling

15

5.3.2 Multiple Classes of ProcessesSingle-class system: If the OS treats all processes in the same

manner, it is referred as to a single-class system. fair

Multiclass system: A system with different groups of processes, each group is assigned a priority depending on some criteria.

Page 16: Chapter 5 Processor Scheduling

16

Scheduling with Multiple Queues

Page 17: Chapter 5 Processor Scheduling

17

Scheduling of Multi-Class Systems

• Not Fair – Processes are not treated alike, as preference is given to higher-priority processes

• A major problem in multi-class systems is STARVATION

– indefinite waiting, one or more low-priority processes may never execute

– solution is AGING

Page 18: Chapter 5 Processor Scheduling

18

5.4 CPU Scheduling Policies

Categories of scheduling policies:

• Non-Preemptive -- no interruptions are allowed. A process completes execution of its CPU burst

• Preemptive – a process can be interrupted before the process completes its CPU burst

Page 19: Chapter 5 Processor Scheduling

19

Priorities and Scheduling

• Priorities can be used with either preemptive or non-preemptive scheduling.

• Depending on the goals of an operating system, one or more of various scheduling policies can be used; each will result in a different system performance.

Page 20: Chapter 5 Processor Scheduling

20

Criteria for Scheduling Algorithms

• CPU utilization

• Throughput

• Turnaround time

• Waiting time

• Response time

• Fairness

Page 21: Chapter 5 Processor Scheduling

21

CPU-IO Bursts of Processes

An important property of a process is its CPU-IO burst

• An I/O bound process has many short CPU burst

• A CPU bound process has few long CPU bursts

• The OS tries to main maintain a balance of these two types of processes

Page 22: Chapter 5 Processor Scheduling

22

CPU Scheduling Policies

• First-come-first-served (FCFS)• Shortest job first (Shortest process next)• Longest job first • Priority scheduling • Round robin (RR)• Shortest remaining time (SRT) also known as

shortest remaining time first (SRTF)

Page 23: Chapter 5 Processor Scheduling

23

5.4.1 FCFS Scheduling

First come first served (FCFS) scheduling algorithm, a non-preemptive policy– The order of service is the same order of arrivals

– Managed with FIFO queue

– Simple to understand and implement

– Scheduling is FAIR

– The performance of this scheme is relatively poor

Page 24: Chapter 5 Processor Scheduling

24

FCFS Scheduling (cont’d)Example: Five processes arrive at time 0, in the order: P1, P2, P3, P4, P5. Their CPU burst time are shown in the following table. Using FCFS algorithm, find the average turnaround time, average waiting time, throughput, and CPU utilization rate.

Process CPU burst (Unit: ms)

P1 135

P2 102

P3 56

P4 148

P5 125

Page 25: Chapter 5 Processor Scheduling

25

FCFS Scheduling (cont’d)Solution:

Turnaround Time (unit: msec): T(p1)=135, T(p2)=135+102=237, T(p3)=135+102+56=293,T(p4)=135+102+56+148=441, T(p5)=135+102+56+148+125=566 The average turnaround time: (135 + 237 + 293 + 441+566) / 5 = 334.4 msec

The average waiting time is: (0 + 135 + 237 + 293 + 441)/ 5 = 221.2 msecThroughput: 5CPU utilization: 100%

Throughput: 5CPU utilization: 100%

The Gantt chart for FCFS

Page 26: Chapter 5 Processor Scheduling

26

5.4.2 SJF (SPN) Scheduling

• The scheduler selects the next the process with the shortest CPU burst

• Basically a non-preemptive policy• SJF is optimal - gives the minimum average

waiting time for a given set of processes.

Page 27: Chapter 5 Processor Scheduling

27

SJF Scheduling (cont’d)Example: Five processes arrive at time 0, in the order: P1, P2, P3, P4, P5. Their CPU burst time are shown in the following table. Using SJF algorithm, find the average turnaround time, and average waiting time.

Process CPU burst (Unit: ms)

P1 135

P2 102

P3 56

P4 148

P5 125

Page 28: Chapter 5 Processor Scheduling

28

P3 P4 P1 P5 P2

Solution:

Gantt chart for SJF:

Waiting Time:Tw(p3)=0, Tw (p2)=56, Tw (p5) =158, Tw (p1)=283, Tw (p4)=418

Average Waiting Time = (0+56+158+283+418)/5=183 msec

Turnaround Time:Ta(p3)=56, Ta (p2)=56+102=158, Ta (p5) =56+102+125=283Ta (p1)=56+102+125+135=418Ta (p4)=56+102+125+135+148=566

Average Turnaround Time = (56+158+283+418+566)/5=296 msec