0% found this document useful (0 votes)
82 views

Cpu Scheduling

The document discusses concepts related to CPU scheduling in operating systems, including: - Multiprogramming allows multiple processes to reside in memory simultaneously and the CPU switches between processes when one is waiting for I/O. - Processes alternate between CPU bursts and I/O bursts in an execution cycle. - The CPU scheduler selects the next process to run from the ready queue when the CPU becomes idle. Preemptive scheduling can interrupt the running process. - Short-term scheduling decides which process runs next while long and medium-term scheduling manage process admission and I/O waits.

Uploaded by

aishwarya saji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Cpu Scheduling

The document discusses concepts related to CPU scheduling in operating systems, including: - Multiprogramming allows multiple processes to reside in memory simultaneously and the CPU switches between processes when one is waiting for I/O. - Processes alternate between CPU bursts and I/O bursts in an execution cycle. - The CPU scheduler selects the next process to run from the ready queue when the CPU becomes idle. Preemptive scheduling can interrupt the running process. - Short-term scheduling decides which process runs next while long and medium-term scheduling manage process admission and I/O waits.

Uploaded by

aishwarya saji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

 Scheduling concept:

 In multiprogramming, several process are kept in memory at time.


 When one process is in the state of wait operating system switches the CPU
from one process to another process.
 This patten continues.

 CPU-I/O Burst Cycle:


 Process execution consists of a cycle of CPU execution and I/O wait. Processes
alternate between these two states. Process execution begins with a CPU burst.
That is followed by an I/O burst, then another CPU burst, then another I/O
burst, and so on. Eventually, the last CPU burst will end with a system request
to terminate execution, rather than with another I/O burst.
 CPU Burst:
 A period of uninterrupted CPU activity.
 I/O Burst:
 A period of uninterrupted input/output activity.
 Process execution consist of a cycle of CPU execution and I/O execution.
 Processes alternate between two states.
 Process execution beings with CPU burst time.
 That is followed by an I/O burst which is followed by another CPU burst time.
 CPU scheduler:
 Whenever the CPU becomes idle, it is the job of the CPU Scheduler ( a.k.a. the short-term scheduler ) to select
another process from the ready queue to run next..
 There are two types of CPU scheduler:
1) Preemptive scheduling
2) Non- preemptive scheduling

Non- preemptive scheduling


Once a process is in the running state, it continues to execute until it terminates or
block itself to wait for I/O
 Preemptive scheduling:

 The currently running process may be interrupted and moved to the ready state by
the operating system.
 This is known as preemptive scheduling.
 The decision to preempt the process may be performed due to
1) When a new process arrives
2) When an interrupt occurs that blocked process in the ready state
3) Periodically base on a clock interrupt
 Dispatcher:
 A dispatcher is a module, it connects the CPU to the process selected by short term scheduler.

 Functions of Dispatcher:
1) Function of dispatcher is switching, it means switching the CPU from one process to another
process.
2) Jumping to the proper location in the user program to restart the program.

The dispatcher should be fast because it is invoked during each and every process switch.
The time takes by dispatcher to stop one process and start another running process is known as
dispatcher latency.
 Types of processor scheduling:

 The scheduling activity is broken down into three functions:


1) Long term scheduling
2) Media term scheduling
3) Short term scheduling
 Long term scheduling:
 Long term scheduling is performed when a new process is created.
 The long term scheduler determines which programmes are admitted to the system for
processing.
 It controls the degree of multiprogramming.
 The long term scheduler create processes and it involves in 2 decisions
1) The scheduler must decide that the operating system can take one or more additional
processes
2) The scheduler must decide which join to accept and turn into processes.

 Each time a job terminates, the scheduler may take the decision to add one or more
new jobs.
 Medium term scheduling:

 If a process request I/o in the middle of the execution the process will removed
from main memory and loaded into waiting queue.

 When the I/O operation is completed then job is moved from waiting queue to
ready queue.

 These two operations are performed by medium term scheduling


 Short term scheduling:

 Short term scheduler is also known as dispatcher.


 Makes decision of which process is execute next.
 Scheduling criteria:

1) CPU utilisation:
our main aim is to keep the CPU as busy as possible.
The utilisation of CPU may range from 0 to 100 percent.
In real time loaded system range is from 40% to 90%.

2) Throughput:
Number of process completed per time unit is called throughput.
For long process this rate be one process per hour.
For short transaction might be the ten process per sec.
 Turnaround Time:
 For a process the important criteria is how long it takes to execute process.
 The interval from time of submission of a process to the time completion is
turn around time.
 It is the sum of period spends waiting to get into memory, waiting in the ready
queue, executing.

 Waiting time:
 The amount of time that process spends waiting in the ready queue.
 Waiting time is the sum of period spent waiting in the ready queue.
 FCFS (First Come First Serve)
 Consider the following set of processes. All processes arrived at time 0.
 Calculate average waiting time and average turn around time.

Process Burst Time

P1 24

P2 3

P3 3
P1 p2 p3
0 24 27 30

Process Arrival Burst Start Waiting Finishing Turn


Time Time time time time Around
time
p1 0 24 0 0 24 24
P2 0 3 24 24 27 27
p3 0 3 27 27 30 30
 How to calculate waiting time?

 Waiting time= start time – arrival time


 P1=0 - 0 = 0
 P2= 24 - 0 = 24
 P3= 27 - 0 = 27

 How to calculate Turn around time?


 TA = Finishing time – Arrival Time
 P1=24-0 = 0
 P2= 27 – 0 =0
 P3 = 30 – 0 = 0
 Calculate Average Waiting time:

 ( p1+ p2+ p3) / 3


 0+24+27 / 3
 17ms

 Calculate Average Turn around time:

 (p1 + p2 + p3) /3
 24 + 27 + 30/3
 27ms
 Consider the following set of processes. All arrived at time o.
 Calculate average waiting time and average turn around time

Process Burst time


P1 5
P2 24
P3 16
P4 10
P5 3
 Gantt Chart as follows

P1 p2 p3 p4 p5
0 5 29 45 55 58
Process Arrival Burst Start Time Waiting Finishing Turn
Time Time time Time around
time
P1 0 5 0 0 5 5
P2 0 24 5 5 29 29
P3 0 16 29 29 45 45
P4 0 10 45 45 55 55
P5 0 3 55 55 58 58
 Waiting time: start time – arrival time
 P1 = 0 – 0 = 0
 P2= 5 – 0 = 5
 P3= 29 – 0 =29
 P4 = 45 – 0 = 45
 P5 = 55 – 0 =55
 Average waiting time: (0 +5+29+45+55)/5 = 26.8 milisec
 Turn around time = finishing time – arrival time
 P1 = 5 – 0 = 5
 P2 = 29 – 0 =29
 P3 = 45 – 0 =25
 P4 = 55 – 0 = 55
 P5 = 58 – 0= 58
 Consider the following set of processes.
 Calculate average waiting time and average turn around time

Process Burst Time Arrival TIme


P1 3 0
P2 6 2
P3 4 4
P4 5 6
P5 2 8
 Gantt Chart

P1 P2 P3 P4 P5
0 3 9 13 18 20
Process Arrival time Burst time start time waiting time Finishing time turn A ti
P1 0 3 0 0 3 3
P2 2 6 3 1 9 7
P3 4 4 9 5 13 9
P4 6 5 13 7 18 12
P5 8 2 18 10 20 12
 Waiting time= start time – Arrival time
 P1 = 0 – 0 =0
 P2= 3 – 2 = 1
 P3 = 9 – 4 = 5
 P4 = 13 – 6 = 7
 P5 = 18 – 8 =10
 Average Waiting time
 (p1+p2+p3+p4+p5)/5
 4.6.ms
 Turn around time = finishing time – arrival time
 P1 = 3 – 0 = 3
 P2 = 9 -2 =7
 P3 = 13 – 4 = 9
 P4 = 18 – 6 =12
 P5 = 20 – 8 = 12

 Average Turn around time


 (p1+p2=p3+p4+p5)/5
 8.6 ms
 Shortest Job First:

 SJF algorithm allocates CPU to a process having smallest next cpu burst time.
 When CPU is available it is assigned to the process that has the smallest next CPU burst time.
 If 2 process having same CPU burst time then processes will be scheduled according to FCFS

 There are two types of SJF algorithm


 Pre-emptive
 Non Pre-emptive

 SJF is also called as optimal algorithm


 Non Pre-emptive SJF algorithm:

 Consider the following set of processes. All processes arrived at time 0.


 Calculate average waiting time and average turn around time.
 Gantt Chart:

P2 P3 P1
0 3 6 30

Process Arrival time Burst Time Start time Waiting time FT TRT
P1 0 24 6 6 30 30
P2 0 3 0 0 3 3
P3 0 3 0 3 6 6
 Waiting time = start time – arrival time
 P1= 6 – 0 = 6
 P2 = 0 – 0 = 0
 P3 = 3- 0 = 3
Average waiting time = (p1+p2+p3) /3
= 3ms
Turn around time = F.T – A.T
P1= 30 – 0 =30
P2 = 3 – 0 =3
P3 = 6 – 0 =6
Average turn around time = (p1+p2+p3)/3 = 13ms
 Non Pre-emptive algorithm:

 Consider the following set of processes.


 Calculate average waiting time and average turn around time.

Process Burst time Arrival time


P1 7 0
P2 4 2
P3 1 4
P4 4 5
 Gantt Chart
P1 P3 P2 P4
0 7 8 12 16
Process A.T B.T S.T W.T F.T TRTime
P1 0 7 0 0 7 7
P2 2 4 8 6 12 10
P3 4 1 7 3 8 4
P4 5 4 12 7 16 11
 Waiting time = Start time – Arrival time

 P1= 0 – 0 = 0
 P2 = 8 – 2 = 6
 P3 = 7 – 4 = 3
 P4 = 12 – 5 = 7
 Average waiting time = (p1+p2+p3+p4)/4
 4ms
 Turn Around time = finishing time – arrival time
 Average turn around time = 8ms
 Non Pre-emptive algorithm:

 Consider the following set of processes.


 Calculate average waiting time and average turn around time.

Process Burst Time Arrival time


p1 3 0
p2 6 2
p3 4 4
p4 5 6
p5 2 8

 Gantt Chart

P1 P2 P5 P3 P4
0 3 9 11 15 20
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 3 0 3 0 3
P2 2 6 3 9 1 7
P3 4 11 15 7 11
4
P4 6 20 9 14
5 15
P5 8 2 9 11 1 3
 SJF (Pre-emptive)

 Consider the following set of processes. Calculate average waiting time and average turn around time.

Proces Burst Arrival Time


s Time
P1 4 0
P2 1 1
P3 2 2
P4 1 3
 Gantt chart

P1 P2 P3 P3 P4 P1
0 1 2 3 4 5 8
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 4 0 8 4 8
P2 1 1 1 2 0 1
P3 2 2 4 0 2
2
P4 3 5 1 2
1 4
 Waiting Time = Arrival time – start time
 P1 = (0-0) + (5-1) =4
 P2=1-1 =0
 P3= 2 – 2 =0
 P4 = 4 - 3 = 1
 Average waiting time : (p1+p2+p3+p4)/4
 1.25ms
 Turn around time : Ft – AT
 P1= 8 – 0 = 8
 P2 = 2 – 1= 1
 P3 = 4 – 2 = 2
 P4 = 5 – 3 = 2
 SJF ( Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.
Process Arrival Burst Time
time
P1 0 7
P2 2 4
P3 4 1
p4 5 4

 Gantt Chart:

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16
Process Arrival time Burst Time Start FT Waiting Turn around
P1 0 7 0 16 9 16
P2 2 4 2 7 1 5
P3 4 4 5 0 1
1
P4 5 11 2 6
4 7
 Waiting time = start time – arrival time
 P1 = (0-0) + (11-2) = 9
 P2 = (2-2) + (5-4)= 1
 P3 = 4 – 4 =0
 P4= 7 – 5 = 2
 Avarage Waiting time = (p1+p2+p3+p4)/4
 Turn Around time = FT – At
 P1 = 16 – 0 = 16
 P2 = 7 – 2 = 5
 P3 = 5 – 4 = 1
 P4 = 11- 5 =6
 Priority scheduling:

 Priority is assigned to each process and CPU is allocated to the process with highest priority.
 Equal priority process are scheduled with FCFS algorithm.
 Priorities are the numbers ranging from 0 to 7 given to each process.

1) Starvation: A major problem with the priority scheduling is indefinite blocking.


The high priority process indefinitely blocks a low priority process.
As per algorithm high priority processes can prevent the low priority process for allocating CPU and
such a process will never get a chance to allocate a CPU.

2) Aging: A solution to this problem is to gradually increase the priority of a process that wait in the
system for ling time is called Aging.
 Priority scheduling are of two types:

 1) Pre-emptive scheduling
 2) Non Pre-emptive scheduling
Priority ( Non Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.
Process Burst Time Arrival Time Priority
P1 3 1 3(H)
P2 2 2 2
P3 5 0 1

 Gantt Chart

P3 P1 P2
0 5 8 10
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time

P1 3 1 5 4 8 7
P2 2 2 8 6 10 8
P3 5 0 0 0 5 5
 Waiting time = start time – arrival time
 P1= 5 – 1 = 4
 P2= 8 -2 = 6
 P3 = 0 – 0 =0
Average Waiting time = (p1+p2+p3)/3

Turn around time = finishing time – arrival time


P1= 8 – 1= 7
P2= 10 – 2 = 8
P3= 5 – 0 = 5
Average turn around time = (p1+p2+p3)/3
Priority ( Non Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.
Process Burst Time Arrival Time Priority
P1 10 0 3
P2 1 0 1(H)
P3 2 0 3
P4 1 0 4
P5 5 0 2

 Gantt Chart:
P2 P5 p1 p3 p4
0 1 6 16 18 19
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time

P1 10 0 6 6 16 16
P2 1 0 0 0 1 1
P3 2 0 16 16 18 18
P4 1 0 18 18 19 19
P5 5 0 1 1 6 6
Priority (Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.

Process Burst Time Arrival time Priority


P1 3 1 3(H)
P2 2 2 2
P3 5 0 1

 Gantt Chart

P3 P1 P2 P3
0 1 4 6 10
Process Burst Time Arrival Start time Waiting Finishing Turn
time time around time

P1 3 1 1 0 4 3
P2 2 2 4 2 6 4
P3 5 0 0 5 10 10
 Waiting time = start time – arrival time
 P1= 1 – 1 = 0
 P2= 4 -2 = 2
 P3 = (0 – 0) + (6-1) =5
Average Waiting time = (p1+p2+p3)/3
2.3 ms

Turn around time = finishing time – arrival time


P1= 4 – 1= 3
P2= 6 – 2 = 4
P3= 10 – 0 = 10
Average turn around time = (p1+p2+p3)/3
5.6ms
Priority (Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.

Process Burst Time Arrival Time Priority


P1 5 1 2
P2 3 0 3(L)
P3 2 2 2
P4 4 3 1
P5 2 13 1(H)

 Gantt Chart:

P2 P1 p4 p1 p3 P2 P5 P2
0 1 3 7 10 12 13 15 16
Process Arrival time Burst Time Start FT Waiting Turn around
P1 1 5 1 10 4 9
P2 0 3 0 16 2 16
P3 2 10 12 8 10
2
P4 3 7 0 4
4 3
P5 13 2 13 15 0 2
 Waiting time = start time – arrival time
 P1= (1 – 1) + ( 7 – 3) = 4
 P2= (0 -0) + (15-12)-1 = 3-1 =2
 P3 = 10-2 = 8
 P4= 3 – 3= 0
 P5 = 13 – 13= 0
Average Waiting time = (p1+p2+p3)/3
Turn around time = finishing time – arrival time
P1= 10 – 1= 9
P2= 16 – 0 = 16
P3= 12 – 2 = 10
P4= 7 – 3 = 4
P5 = 15 – 13 = 2
Average turn around time = (p1+p2+p3)/3
5.6ms
Priority (Pre-emptive)
 Consider the following set of processes.
 Calculate average waiting time and average turn around time.

Process Burst Time Arrival time Priority


P1 5 1 1
P2 6 0 2
P3 2 1 1
P4 4 0 3(L)

 Gantt Chart:

P2 P1 P3 P2 P4
0 1 6 8 13 17
Process Arrival time Burst Time Start FT Waiting Turn around
P1 1 5 1 6 0 5
P2 0 6 0 13 7 13
P3 1 6 8 5 7
2
P4 0 17 13 17
4 13
 Waiting time = start time – arrival time
 P1= (1 – 1) = 0
 P2= (0 -0) + (8-1)= 7
 P3 = 6-1 = 5
 P4= 13 – 3= 13
Average Waiting time = (p1+p2+p3)/3 = 6.25ms
Turn around time = finishing time – arrival time
P1= 6– 1= 5
P2= 13 – 0 = 13
P3= 8 – 1 = 7
P4= 17 – 0 = 17
Average turn around time = (p1+p2+p3)/3= 10.5ms
 Round Robin Method:
 Consider the following set of processes. (Time Slice 4)
 Calculate average waiting time and average turn around time.

Process Burst Time Arrival Time


P1 24 0
P2 3 0
P3 3 0

 Gantt Chart:

P1 P2 p3 p1 p1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Process Burst Time Arrival Start time Waiting Finishing Turn
time timr around time

P1 24 0 0 6 30 30
P2 3 0 4 4 7 7
P3 3 0 7 7 10 10
 Waiting time = start time – arrival time
 P1= (0-0) + (10-4) = 6
 P2= (4 – 0)
 P3 = (7 – 0) = 7
Average Waiting time = (p1+p2+p3)/3 = 5.6 ms
Turn around time = finishing time – arrival time
P1= 30 – 0 = 30
P2= 7 – 0 = 7
P3= 10 – 0 = 10
Average turn around time = (p1+p2+p3)/3 = 15.67ms
 Round Robin Method:
 Consider the following set of processes. (Time Slice= 3 )
 Calculate average waiting time and average turn around time.

Process Burst Time Arrival Time


P1 5 1
P2 6 0
P3 2 1
P4 4 0
 Gantt Chart:

P2 P4 p1 p3 p2 P4 P1
0 3 6 9 11 14 15 17
Process BT AT
P1 5 1
P2 3 0
P3 2 2
P4 4 3
P5 2 13

P2 P1 p3 p4 p2 P1 P4 P5 P1
0 2 4 6 8 9 11 13 15 16

You might also like