Lecture 5, 6 and 7 cpu scheduling

32
Rushdi Shams, Dept of CSE, KUET 1 Operating Systems CPU Scheduling Version 1.0

Transcript of Lecture 5, 6 and 7 cpu scheduling

Page 1: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 1

Operating SystemsCPU SchedulingVersion 1.0

Page 2: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 2

Basic ConceptsThe objective of multiprogramming is to run

processes all time, keeping the CPU busy.Uniprocessing system is different. Only one

process can run at a time- means processes have to wait until the CPU is free.

Page 3: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 3

Basic ConceptsA process is executed until the completion of

an IO requestThe CPU then sits idle and all this waiting

time is wastedWith multiprogramming, we keep several

processes in memory at one time. When one process has to wait, the OS takes

the CPU away from that process and gives the CPU to another process

Page 4: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 4

Basic ConceptsAlmost all computer resources are scheduled

before use. The CPU is one of the primary resources. Thus, its scheduling is central to operating systems

Page 5: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 5

CPU burst and IO burst

Page 6: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 6

CPU Scheduler Selects from the processes in memory that are ready

to execute, and allocates the CPU to one of them 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

Scheduling under 1 and 4 is nonpreemptive All other scheduling is preemptive

Page 7: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 7

CPU SchedulerIn case of Non-preemptive scheduling, the

process does not release the CPU unless it is finished (or terminated) or going to the waiting state.

Page 8: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 8

DispatcherDispatcher module gives control of the CPU

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

program to restart that programDispatch latency – time it takes for the

dispatcher to stop one process and start another running

Page 9: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 9

Scheduling CriteriaCPU utilization

How much of the CPU is in operation, how much of it is utilized to serve processes.Measured in PercentageShould be maximized

Page 10: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 10

Scheduling CriteriaThroughput

Number of processes executed and served per unit time. Measured in process per unit timeShould be maximized

Page 11: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 11

Scheduling CriteriaTurnaround time

Amount of time to execute a particular processInterval between the submission of the process and its completionShould be minimized

Page 12: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 12

Scheduling CriteriaWaiting time

amount of time a process has been waiting in the ready queueShould be minimized

Page 13: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 13

Scheduling CriteriaResponse time

Amount of time it takes from when a request was submitted until the first response is produced, not output (for time-sharing environment)Should be minimized

Page 14: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 14

First Come, First Served (FCFS)The simplest CPU Scheduling algorithmThe process that requests CPU first is

allocated the CPU firstEasily managed with a FIFO queue

Page 15: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 15

FCFS Proces Burst Time

P1 24P2 3

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

The Gantt Chart for the schedule is:

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

P1 P2 P3

24 27 300

Page 16: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 16

FCFS Proces Burst Time

P1 24P2 3

P3 3

Suppose that the 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 Much better than previous case Convoy effect short process behind long process

P1P3P2

63 300

Page 17: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 17

Shortest Job First (SJF) Associate with each process the length of its next

CPU burst. Use these lengths to schedule the process with the shortest time

Two schemes: non-preemptive – once CPU given to the process it

cannot be preempted until completes its CPU burst preemptive – if a new process arrives with CPU burst

length less than remaining time of current executing process, preempt. This scheme is also called Shortest-Remaining-Time-First (SRTF)

SJF is optimal – gives minimum average waiting time for a given set of processes

Page 18: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 18

SJFProcess Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (non-preemptive)

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

P1 P3 P2

73 160

P4

8 12

Page 19: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 19

SRTFProcess Arrival Time Burst Time

P1 0.0 7

P2 2.0 4

P3 4.0 1

P4 5.0 4 SJF (preemptive)

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

P1 P3P2

42 110

P4

5 7

P2 P1

16

Page 20: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 20

Priority SchedulingThe SJF Algorithm is special case of Priority

SchedulingA priority is associated with each process and the

CPU is given to the process with higher priorityIf processes have equal priority, FCFS is followedSJF is priority algorithm where

priority p = inverse of CPU burstPriorities are generally some fixed range of

numbers such as 0 to 7 or 0 to 4,095

Page 21: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 21

Priority SchedulingHowever, there is no agreement on the

highest priority- if it would be 0 or 4,095Priorities can be external or internalInternal priorities are number of files

required to execute the process, its memory usage, resources required, time to compute, etc.

External priorities are set by outside computer- mainly by human based on the choice of process, economical factors, etc.

Page 22: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 22

Priority Scheduling

The Average Waiting Time is (6+0+16+18+1)/5=8.2 ms

Page 23: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 23

Priority Scheduling: Starvation Higher priority schedules prohibits lower priority

schedules for getting the CPU There are two possibilities-1. If no mechanism is applied, lower priority

schedules will get CPU on Friday morning (that’s the holiday)

2. If mechanism applied, lower priority schedules will be swapped away by the CPU- those that did not get a chance for a specific period

IBM 7094 was shut down in 1973… during IBM 7094 was shut down in 1973… during the shut down, MIT found a lower prioritized the shut down, MIT found a lower prioritized process still ignored that was submitted in process still ignored that was submitted in 1967!1967!

Page 24: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 24

Priority Scheduling: AgingTechnique to increase the priority of the

processesIf priorities are ranged from 0 to 127 (127 is

the highest priority), then we can increase the priority of a process for every 15 minutes.

The lowest prioritized process will now take no more than 32 hours to get the CPU!

Page 25: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 25

Round-Robin (RR) SchedulingSimilar to FCFS but pre-emption is added to

switch between processesA smaller unit of time called time quantum

(or time slice) is defined generally from 10 to 100 ms.

The ready queue is treated as circular queueScheduler goes around the ready queue,

allots CPU to each process for a time interval of up to 1 time quantum

Page 26: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 26

Round-Robin (RR) SchedulingWe need to treat the ready queue as FIFONew processes are added at the end of the

queue.The CPU scheduler picks the first process

from the queue, sets a timer to interrupt the process after 1 time quantum and dispatches the process

Page 27: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 27

RR Scheduling2 things may happen-The process may have a CPU burst less than

time quantum- the process will release the CPU voluntarily.

CPU burst of the process is longer than time quantum-the process will be put at the end of the queue.

Page 28: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 28

RR Scheduling

Time quantum is of 4 msAverage waiting time: ((10-4)+4+7)/3 = 5.66 msTypically, higher average turnaround than SJF,

but better response

Page 29: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 29

RR SchedulingThe performance heavily depends on the size

of time quantumIf the time quantum is very large, at one

extreme, it would be similar to FCFSIf the time quantum is very small, the RR

approach is called processor sharing- as it seems every process has its own CPU

Smaller time quantum requires more context switching.

Page 30: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 30

Context switch with Quantum

Page 31: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 31

Turnaround time with Quantum

Page 32: Lecture 5, 6 and 7  cpu scheduling

Rushdi Shams, Dept of CSE, KUET 32

80% of the CPU bursts should be shorter than

the time quantum