31
1 Operating Systems CPU Scheduling Version 1.0 

Lecture 5 and 6- CPU Scheduling

Embed Size (px)

Citation preview

Page 1: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 1/31

1

Operating Systems

CPU SchedulingVersion 1.0

Page 2: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 2/31

2

Basic ConceptsThe objective of multiprogramming is to runprocesses all time, keeping the CPU busy.

Uniprocessing system is different. Only one processcan run at a time- means processes have to wait untilthe CPU is free.

Page 3: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 3/31

3

Basic Concepts A process is executed until the completion of an IOrequestThe CPU then sits idle and all this waiting time is wasted With multiprogramming, we keep several processes inmemory at one time. When one process has to wait, the OS takes the CPUaway from that process and gives the CPU to anotherprocess

Page 4: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 4/31

4

Basic Concepts Almost all computer resources are scheduled beforeuse. The CPU is one of the primary resources. Thus,its scheduling is central to operating systems

Page 5: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 5/31

5

CPU burst and IO burst

Page 6: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 6/31

6

CPU SchedulerSelects from among the processes in memory that are ready toexecute, and allocates the CPU to one of themCPU 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 ready 4. Terminates

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

Page 7: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 7/317

CPU SchedulerIn case of Non-preemptive scheduling, the processdoes not release the CPU unless it is finished (orterminated) or going to the waiting state.

Page 8: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 8/318

DispatcherDispatcher module gives control of the CPU to theprocess selected by the short-term scheduler; thisinvolves:

switching context jumping to the proper location in the user program torestart that program

Dispatch latency – time it takes for the dispatcher tostop one process and start another running

Page 9: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 9/319

Scheduling CriteriaCPU utilizationHow 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 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 10/3110

Scheduling CriteriaThroughputNumber of processes executed and served per unit

time.Measured in process per unit timeShould be maximized

Page 11: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 11/31

11

Scheduling CriteriaTurnaround time Amount of time to execute a particular process

Interval between the submission of the process and itscompletionShould be minimized

Page 12: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 12/31

12

Scheduling Criteria Waiting timeamount of time a process has been waiting in the

ready queueShould be minimized

Page 13: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 13/31

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 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 14/31

Page 15: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 15/31

15

FCFS Proces Burst TimeP 1 24P 2 3P 3 3

Suppose that the processes arrive in the order: P 1 , P 2 , P 3The Gantt Chart for the schedule is:

Waiting time for P 1 = 0; P 2 = 24; P 3 = 27 Average waiting time: (0 + 24 + 27)/3 = 17

P 1 P 2 P 3

24 27 300

Page 16: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 16/31

16

FCFSSuppose that the processes arrive in the orderP 2 , P 3 , P 1

The Gantt chart for the schedule is:

Waiting time for P 1= 6; P 2 = 0; P 3 = 3

Average waiting time: (6 + 0 + 3)/3 = 3Much better than previous caseConvoy effect short process behind long process

P 1 P 3 P 2

63 300

Page 17: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 17/31

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 timeTwo schemes:

non-preemptive – once CPU given to the process it cannot bepreempted until completes its CPU burstpreemptive – if a new process arrives with CPU burst length lessthan remaining time of current executing process, preempt. Thisscheme is know as the Also called Shortest-Remaining-Time-First (SRTF)

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

Page 18: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 18/31

18

SJFProcess Arrival Time Burst TimeP 1 0.0 7P 2 2.0 4

P 3 4.0 1P 4 5.0 4SJF (non-preemptive)

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

P 1 P 3 P 2

73 160

P 4

8 12

Page 19: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 19/31

19

SRTFProcess Arrival Time Burst TimeP 1 0.0 7P 2 2.0 4

P 3 4.0 1P 4 5.0 4SJF (preemptive)

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

P 1 P 3 P 2

42 110

P 4

5 7

P 2 P 1

16

Page 20: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 20/31

20

Priority SchedulingThe SJF Algorithm is special case of Priority Scheduling A priority is associated with each process and the CPUis given to the process with higher priority If processes have equal priority, FCFS is followedSJF is priority algorithm where

priority p = inverse of CPU burstPriorities are generally some fixed range of numberssuch as 0 to 7 or 0 to 4,095

Page 21: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 21/31

21

Priority SchedulingHowever, there is no agreement on the highestpriority- if it would be 0 or 4,095Priorities can be external or internalInternal priorities are number of files required toexecute the process, its memory usage, resourcesrequired, time to compute, etc.

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

Page 22: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 22/31

22

Priority Scheduling

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

Page 23: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 23/31

23

Priority Scheduling: StarvationHigher priority schedules prohibits lower priority schedules for getting the CPUThere 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 willbe swapped away by the CPU- those that did not geta chance for a specific period

problem

Page 24: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 24/31

Page 25: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 25/31

25

Round-Robin (RR) SchedulingSimilar to FCFS but pre-emption is added to switchbetween processes A smaller unit of time called time quantum (or timeslice) 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 timequantum

Page 26: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 26/31

26

Round-Robin (RR) Scheduling We 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 thequeue, sets a timer to interrupt the process after 1 timequantum and dispatches the process

Page 27: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 27/31

27

RR Scheduling2 things may happen-The process may have a CPU burst less than timequantum- 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 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 28/31

28

RR Scheduling

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

Page 29: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 29/31

29

RR SchedulingThe performance heavily depends on the size of timequantumIf the time quantum is very large, at one extreme, it would be similar to FCFSIf the time quantum is very small, the RR approach iscalled processor sharing- as it seems every process has

its own CPUSmaller time quantum requires more contextswitching.

Page 30: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 30/31

30

Context switch with Quantum

Page 31: Lecture 5 and 6- CPU Scheduling

8/7/2019 Lecture 5 and 6- CPU Scheduling

http://slidepdf.com/reader/full/lecture-5-and-6-cpu-scheduling 31/31

80% of the CPU bursts

should be shorter than thetime quantum