module 6: cpu scheduling - icpak€¦ · • cpu scheduling is the ... • preemptive sjf...

61
| CPU Scheduling

Upload: lecong

Post on 18-May-2018

229 views

Category:

Documents


3 download

TRANSCRIPT

|

CPU Scheduling

|

Objectives

• Describe various CPU-scheduling algorithms

• Describe various multi- processor CPU-scheduling

algorithms

• To discuss evaluation criteria for selecting a CPU-

scheduling algorithm for a particular system

CPU scheduling 2

|

Basic Concepts

• Almost all computer resources are scheduled before use.

The CPU is one of the primary computer resources.

• CPU scheduling is the basis of multi-programmed operating

systems.

• What are multi-programmed operating systems?

• What are the benefits of multiprogramming?

CPU scheduling 3

|

Basic Concepts

• In a single-processor system, only one process can run

at a time; any others must wait until the CPU is free

and can be rescheduled.

• The objective of multiprogramming is to have some

process running at all times, to maximize CPU

utilization. How is multiprogramming achieved?

CPU scheduling 4

|

Cycle of CPU execution and I/O wait

• The success of CPU scheduling depends on an observed

property of processes:

» Process execution consists of a cycle of

CPU execution and I/O wait. Processes

alternate between these two states.

CPU scheduling 5

|

Cycle of CPU execution and I/O wait

CPU scheduling 6

|

Cycle of CPU execution and I/O wait

• Process execution begins with a CPU burst. That is

followed by an I/O burst, which is followed by another

CPU burst, then another I/O burst, and so on.

Eventually, the final CPU burst ends with a system

request to terminate execution.

CPU scheduling 7

|

Short term Scheduler

• Selects from among the

processes in ready queue,

and allocates the CPU to one

of them

– Queue may be ordered in

various ways. What are

this ways? (think real life

queues)

CPU scheduling 8

|

CPU scheduling

• 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

CPU scheduling 9

|

Non-preemptive & preemptive

• CPU Scheduling algorithms can be categorized into two;

» Non-Preemptive

» Preemptive

CPU scheduling 10

|

Non-preemptive & preemptive

• Under non preemptive scheduling, once the CPU has been

allocated to a process, the process keeps the CPU until it

releases the CPU either by terminating or by switching to

the waiting state……The process cannot be interrupted

• With Preemptive a process can be interrupted during its

course of execution

CPU scheduling 11

|

Scheduling Algorithms

|

CPU scheduling deals with the

problem of deciding which of

the processes in the ready

queue is to be allocated the

processor.

There are many different CPU

scheduling algorithms. These

are;

CPU scheduling 13

|

First-Come, First-Served Scheduling

Shortest-Job-First Scheduling

Priority Scheduling

Round-Robin Scheduling

Multilevel Queue Scheduling

Multilevel Feedback-Queue Scheduling

CPU scheduling 14

|

First-Come, First-Served (FCFS) Scheduling

• The simplest CPU-scheduling algorithm

• With this scheme, the process that requests the CPU

first is allocated the CPU first. The implementation of

the FCFS policy is easily managed with a FIFO queue.

• When a process enters the ready queue, its PCB is

linked onto the tail of the queue.

• When the CPU is free, it is allocated to the process at

the head of the queue. The running process is then

removed from the queue.

CPU scheduling 15

|

First come First Served (FCFS)

|

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

CPU scheduling 17

|

First-Come, First-Served (FCFS) Scheduling

The Gantt Chart for the schedule is:

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

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

P1 P2 P3

24 27 30 0

CPU scheduling 18

|

FCFS Scheduling (Cont.)

Suppose that the processes arrive in the order:

P2 , P3 , P1

Will there be a difference in the average waiting time?

CPU scheduling 19

|

FCFS Scheduling (summary)

The FCFS scheduling algorithm is non-preemptive. What

does this mean?

• FCFS algorithm is troublesome for time-sharing systems

because Once the CPU has been allocated to a process, that

process keeps the CPU until it releases the CPU, either by

terminating or by requesting I/O.

CPU scheduling 20

|

Class Exercise 1

Process l Time Burst Time

P1 0.0 6

P2 2.0 8

P3 4.0 7

P4 5.0 3

Determine the average waiting time if FCFS scheduling algorithm

was in use

CPU scheduling 21

|

Shortest Job First (SJF)

|

Shortest-Job-First (SJF) Scheduling

• This algorithm associates with each process the length of

the process's next CPU burst.

• When the CPU is available, it is assigned to the process

that has the smallest next CPU burst.

• What if two processes have the same CPU burst?

• If the next CPU bursts of two processes are the same,

FCFS scheduling is used to break the tie.

CPU scheduling 23

|

Example of SJF

Process l Time Burst Time

P1 0.0 6

P2 2.0 8

P3 4.0 7

P4 5.0 3

• Average waiting time = (0 + 3 + 9 + 16) / 4 = 7 miliseconds

P4 P3 P1

3 16 0 9

P2

24

CPU scheduling 24

|

Class Exercise 2

Process Burst Time

P1 24

P2 3

P3 3

Assume STF scheduling algorithm is used, what would the

order of the processes be?

Compute average waiting time if STF scheduling algorithm

was used

CPU scheduling 25

|

Difficulties with SJF

• The real difficulty with the SJF algorithm is knowing the length

of the next CPU request.

• However For long-term (job) scheduling, we can use the length

of the process specified when the process is submited.

• SJF is much difficult to implement at the level of short-term

CPU scheduling. There is no way to know the exact length of

the next CPU burst.

• What are the possible solutions to address this issue?

CPU scheduling 26

|

SJF cont…

• The SJF algorithm can be either preemptive or non-

preemptive.

• In Preemtive, The choice arises when a new process arrives at

the ready queue while a previous process is still executing.

The next CPU burst of the newly arrived process may be

shorter than what is left of the currently executing process

CPU scheduling 27

|

SJF cont…

• A preemptive SJF algorithm will preempt the currently

executing process, whereas a non-preemptive SJF algorithm

will allow the currently running process to finish its CPU burst.

CPU scheduling 28

|

SJF cont…

• Preemptive SJF scheduling is sometimes called shortest-

remaining-time-first scheduling.

CPU scheduling 29

|

Shortest Remaining time First

(SRTF)

|

Example of Shortest-remaining-time-first

ProcessAarrArrival TimeT Burst Time

P1 0 8

P2 1 4

P3 2 9

P4 3 5

Class Discussion Questions

• What will be the order of process execution?

• Compute the average waiting time

• Compute the average waiting time if the algorithm is non

preemptive

CPU scheduling 31

|

Example of Shortest-remaining-time-first

ProcessAarri Arrival TimeTBurst Time

P1 0 8

P2 1 4

P3 2 9

P4 3 5

• Preemptive SJF Gantt Chart

• Average waiting time = 6.5 msec

P1 P1 P2

1 17 0 10

P3

26 5

P4

CPU scheduling 32

|

Non-preemptive

ProcessAarri Arrival TimeTBurst Time

P1 0 8

P2 1 4

P3 2 9

P4 3 5

• Order of execution will be P1-P2-P4-P3

• P1 0

• P2 7

• P3 9

• P4 15

Average waiting time = 7.75 msec

CPU scheduling 33

|

Class Exercise

ProcessAarri Arrival TimeTBurst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4

i. Draw a gantt chart to represent the following processes

ii. Compute the average waiting time of the following set of

processes using

» SJF (preemptive)-3m/s

» SJF (non-Preemptive)-4m/s

CPU scheduling 34

|

Priority Scheduling

|

Priority Scheduling

• A priority is associated with each process, and the CPU is

allocated to the process with the highest priority.

• Would we be right to say that the SJF algorithm is a special

case of the general priority scheduling algorithm?

• What would happen if two processes have the same priority?

• When two processes have the same priority, First come first

served algorithm is used to schedule

CPU scheduling 36

|

Priority Scheduling

• Note that we discuss scheduling in terms of high priority and

low priority. Priorities are generally indicated by some fixed

range of numbers, such as 0 to 7 or 0 to 4,095. However, there

is no general agreement on whether 0 is the highest or lowest

priority.

• Some systems use low numbers to represent low priority;

others use low numbers for high priority. This difference can

lead to confusion. In our class, we assume that low numbers

represent high priority.

CPU scheduling 37

|

Example of Priority Scheduling – assumption is that all processes are in the ready queue

ProcessAarriBurst TimeT Priority

P1 10 3

P2 1 1

P3 2 4

P4 1 5

P5 5 2

Class discussion questions

Draw the Gantt chart for the processes above

What will the average waiting time be?

CPU scheduling 38

|

Example of Priority Scheduling –Solution assumption is that all processes are in the ready queue

• Priority scheduling Gantt Chart

P2 P3 P5

1 18 0 16

P4

19 6

P1

CPU scheduling 39

|

Priority Scheduling

• Priorities can be defined either internally or externally.

• Internally defined priorities use some measurable quantity or

quantities to compute the priority of a process. For example,

time limits, memory requirements, the number of open files,

and the ratio of average I/O burst to average CPU burst have

been used in computing priorities.

CPU scheduling 40

|

Priority Scheduling

• External priorities are set by criteria outside the operating

system, such as the importance of the process. Examples?

• Priority scheduling can be either preemptive or non-

preemptive. Explain

CPU scheduling 41

|

Problems with Priority Scheduling

• A major problem with priority scheduling algorithms is

indefinite blocking, or starvation.

• A process that is ready to run but waiting for the CPU can be

considered blocked.

• A priority scheduling algorithm can leave some low priority

processes waiting indefinitely. In a heavily loaded computer

system, a steady stream of higher-priority processes can

prevent a low-priority process from ever getting the CPU.

CPU scheduling 42

|

Indefinite blocking solution

• A solution to the problem of indefinite blockage of low-priority

processes is aging.

• Aging is a technique of gradually increasing the priority of

processes that wait in the system for a long time.

• For example, if priorities range from 127 (low) to 0 (high), we

could increase the priority of a waiting process by 1 every 15

minutes.

CPU scheduling 43

|

Class Exercise

Process Duration Priority Arrival

Time

P1 6 4 0.0

P2 8 1 2.0

P3 7 3 3.0

P4 3 2 5.0

• Draw a Gantt chart to

represent the following

processes

• Compute the average

waiting time of the

following set of processes

using preemptive priority

scheduling algorithm

CPU scheduling 44

|

Round Robin (RR)

|

Round Robin (RR)

• The round-robin (RR) scheduling algorithm is designed

especially for time sharing systems.

• It is similar to FCFS scheduling, but preemption is added to

switch between processes. A small unit of time, called a time

quantum or time slice, is defined.

• The CPU scheduler goes around the ready queue, allocating

the CPU to each process for a time interval of up to 1 time

quantum.

CPU scheduling 46

|

Example of RR with Time Quantum = 4

Process Burst Time

P1 24

P2 3

P3 3

• The Gantt chart is:

• What will the average waiting time be?

• What will the average Turn-around time be?

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

CPU scheduling 47

|

Class Exercise : Time Quantum = 4 Process Burst Time Arrival time P1 8 P2 4 P3 9 P4 5

• Draw the Gantt chart to represent the execution for a time quantum of 4

and 5

• Compute the average waiting time for a time quantum of 4 and 5

• Compute the average turn-around time for a time quantum of 4 and 5

(comment on the turn around time)

CPU scheduling 48

|

Turnaround Time Vs Time Quantum

• Would we be right to conclude that turnaround time of a set of

processes improves as the time-quantum size increases?

CPU scheduling 49

|

Turnaround Time Vs Time Quantum

• The average turnaround time of a set of processes does not

necessarily improve as the time-quantum size increases.

• In general, the average turnaround time can be improved if

most processes finish their next CPU burst in a single time

quantum.

CPU scheduling 50

|

Multilevel Queue

|

Multilevel Queue

• Multilevel queues are used for situations where processes

are easily classified into different groups.

• A common division is made between foreground

(interactive) processes and background processes. These

two types of processes have different response-time

requirements and so may have different scheduling needs

CPU scheduling 52

|

Multilevel Queue

• A multilevel queue scheduling algorithm partitions the

ready queue into several separate queues.

• The processes are permanently assigned to one queue,

generally based on some property of the process, such as

memory size, process priority, or process type. Each queue

has its own scheduling algorithm.

CPU scheduling 53

|

Multilevel Queue Scheduling

CPU scheduling 54

|

Multilevel Feedback Queue

• In contrast to Multilevel queue, this algorithm allows a

process to move between queues. The idea is to

separate processes according to the characteristics of

their CPU bursts.

• If a process uses too much CPU time, it will be moved

to a lower-priority queue. This scheme leaves I/O-

bound and interactive processes in the higher-priority

queues.

CPU scheduling 55

|

Multilevel Feedback Queues

CPU scheduling 56

|

Scheduling Criteria

• Different CPU scheduling algorithms have different

properties, and the choice of a particular algorithm may

favor one class of processes over another.

• Many criteria have been suggested for comparing CPU

scheduling algorithms. Which characteristics are used for

comparison can make a substantial difference in which

algorithm is judged to be best. The criteria include the

following:

CPU scheduling 57

|

Scheduling Criteria

• CPU utilization – keep the CPU as busy as possible

• Throughput – # of processes that complete their execution per

time unit

• Turnaround time – From the point of view of a particular

process 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)

CPU scheduling 58

|

Scheduling Algorithm Optimization Criteria

• Max CPU utilization

• Max throughput

• Min turnaround time

• Min waiting time

• Min response time

CPU scheduling 59

|

Multilevel Queue Scheduling-Example

• ML queue, 2 levels

– RR @ 10 units

– FCFS

RR gets priority over FCFS

• Process Arrival Burst Queue

P1 0 12 FCFS

P2 4 12 RR

P3 8 8 FCFS

P4 20 10 RR

• Non-preemptive and preemptive

• Draw a Gantt charts to represent this algorithm both for

preemptive and non-preemptive

• Compute average waiting and turn around time.

CPU scheduling 60

|

Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498

Email: [email protected] Website: www.strathmore.edu