0% found this document useful (0 votes)
19 views63 pages

Unit 4

Uploaded by

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

Unit 4

Uploaded by

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

UNIT-IV 4.

1 SCHEDULING TYPES-
Scheduling Objectives,
CPU CPU and I/O Burst Cycles
SCHEDULING Pre-emptive, Non-preemptive
Scheduling, Scheduling Criteria
AND 4.2 TYPES of SCHEDULING Algorithms-
ALGORITHMS First come First Served (FCFS),
Shortest Job First (SJF)
(14 Marks) Shortest Remaining Time (SRTN)
Round Robin (RR)
Priority Scheduling ,
Multilevel Queue Scheduling
4.3 DEADLOCK-
System Models, Necessary Conditions
leading to Deadlocks, Deadlock
Handling-Preventions, Avoidance
SCHEDULING
SCHEDULING OBJECTIVES
All resources must be scheduled before they are used.
Scheduling is required for the CPU as well as input/output
devices.

Process of allocating CPU to process is known as CPU


scheduling.

Short term scheduler is used for allocating CPU.


Main objective is to keep CPU busy and it should be fully
utilized.
CPU SCHEDULING DECISION
CPU scheduling decisions take place under one of four conditions:
1. When a process switches from the running state to the
waiting state (for example, as the result of an I/0 request )

2. When a process switches from the running state to the


ready state (High priority process requested for CPU)

3. When a process switches from the waiting state to the


ready state (for example, at completion of I/0)

4. When a process terminates.


CPU AND I/O BURST CYCLE
CPU & I/ OBURST
• CPU Burst: Interval of time a process is being executed in the CPU.
• I/O Burst: Interval of time a process spends doing a single I/O Operation and CPU is
waiting for I/O for further execution.
• Burst times do not include waiting times.

• CPU bound process:


• Process having large number of CPU burst cycle and less I/O burst cycle called as CPU
bound process.
• Response time will be fast and waiting time, turnaround times will also be less.

• I/O bound process:


• Process having large number of I/O burst cycle and less CPU burst cycle called as I/O
bound process.
• Response time will be slow and waiting time, turnaround times will also be more.
CPU & I/ OBURST CYCLE
CPU BURST Process execution is a cycle of CPU
execution and I/O operation.
 Process execution begins with
I/O BURST
CPU burst followed by I/O burst
upto process termination
CPU BURST  At last it will use CPU burst

I/O BURST

CPU BURST
CPU & I/ OBURST CYCLE EXAMPLE

load store
add store CPU Burst
read from file

wait for I/O I/O Burst

store increment Alternate sequence of CPU and I/O burst


index CPU Burst
write to file

wait for I/O I/O Burst

load store
add store CPU Burst
read from file
wait for I/O I/O Burst
PRE-EMPTIVE & NON-PREMPTIVE
SCHEDULING
PRE-EMPTIVE SCHEDULING
Process is interrupted to stop its execution and allow
other process to start execution
Higher priority process get chance for execution early
 Less number of process get executed

Process Arrival Time CPU Burst Time Priority


(in ms)
P2 0 6 4 (LOW)
P3 1 4 1 (HIGH)
P1 2 4 3
P0 3 2 2
NON PREEMPTIVE SCHEDULING
 Process continue their execution priority
even if high process arrives.
 Process may stop because of I/O request or terminate or
time slice end.
 Not suitable for real time system
 Large number of process get executed
Process Arrival CPU Burst Priority
Time Time
(in ms)
P2 0 6 4 (LOW)
P3 1 4 1 (HIGH)
P1 2 4 3
P0 3 2 2
DIFFERENCES BETWEEN PREEMPTIVE & NONPREMPTIVE SCHEDULING
S.No. Preemptive Scheduling Non-Preemptive Scheduling
The type of scheduling in which once the
The CPU scheduling in which the resources
resources (CPU Cycle) have been allocated to a
(CPU Cycle) have been allocated to a
1 process, the process holds it until it completes its
process for a limited amount of time is
burst time or switches to the 'wait' state is known
known as preemptive scheduling.
as non-preemptive scheduling.
In the non-preemptive scheduling, a process
In the preemptive scheduling, a process can
2 cannot be interrupted until it terminates itself or its
be interrupted when it is being executed.
time is over.
If a process that has a high priority arrives If a process that has a long burst time is running
3 frequently in the 'ready' queue, the low the CPU, then the process that has less CPU burst
priority processes may starve. time would starve
4 It has overheads. It does not have overheads.
5 It is flexible in nature. It is not flexible in nature.
6 It is expensive. It is not expensive.
Examples − Round Robin scheduling, SRTN Examples − First Come First Serve and Shortest
7
scheduling Priority scheduling Job First scheduling
SCHEDULING CRITERIA
SCHEDULING CRITERIA

CPU UTILIZATION

THROUGHPUT

TURNAROUND TIME
WAITING TIME

RESPONSE TIME
SCHEDULING CRITERIA
1. CPU Utilization: CPU must be kept busy all the time so that it can be
utilized from 0 to 100%. The percentage of time during which CPU is busy
to execute processes is known as CPU utilization.

2. Throughput: Total No. of processes executed in particular time or


Amount of work done by CPU within particular unit of time is known as
throughput..
3. Turnaround Time:
Time difference between submission of process and completion of process.
Total time Required for complete execution of process
TA time= Time in Waiting queue + Time in I/O Request Queue
+ Time required for execution
SCHEDULING CRITERIA
4. Waiting time:
The sum of the waiting times of the process in the ready queue
during its life cycle is known as total waiting time..

5. Response time:
The time difference between submission of job and the first
response from job, is known as response time.
TYPES OF SCHEDULING
ALGORITHMS
TYPES OF SCHEDULING ALGORITHMS
1 First Come First Serve (FCFS)

2 Shortest Job First (SJF)

3 Shortest Remaining Time Next (SRTN)

4 Round Robin (RR)

5 Priority Scheduling

6 Multilevel Queue Scheduling


FIRST COME FIRST SERVED (FCFS)
FIRST COME FIRST SERVED (FCFS)

• It is simplest non pre-emptive scheduling


algorithm in which process which request CPU first
will get CPU first
• Jobs are scheduled on first come first basis.
• When process enters for processing, its PCB is
connected with the ready queue.
• FCFS performance is poor because CPU and I/O
are not effectively utilized in this method
FCFS Example -1 Calculate
Process Arrival Burst time average
(ms) waiting time
P1 0 8 and
P2 1 4 turnaround
P3 2 9 time of above
P4 3 5 processes
using FCFS
GANTT CHART
P1 P2 P3 P4
0 8 12 21 26
• Waiting Time for Process P1: (0-0)=0 Avg Waiting Time = (0+7+10+18) /4
• Waiting Time for Process P2: (8-1)=7 =35/4
• Waiting Time for Process P3: (12-2)=10 = 8.75 ms
• Waiting Time for Process P4: (21-3)=18
FCFS Example -1 Calculate
Process Arrival Burst time average
(ms) waiting time
P1 0 8 and
P2 1 4 turnaround
P3 2 9 time of above
P4 3 5 processes
using FCFS
GANTT CHART TURNAROUND TIME= Completion Time- Arrival Time

P1 P2 P3 P4
0 8 12 21 26
• Turnaround Time for Process P1: (8-0)=8 Avg Turnaround Time
• Turnaround Time for Process P2: (12-1)=11 = (8+11+19+23) /4
• Turnaround Time for Process P3: (21-2)=19 = 61/4
• Turnaround Time for Process P4: (26-3)=23 = 15.25 ms
FCFS Example -2 Calculate
Process Arrival Burst time average
(ms) waiting time
P1 0 5 and
P2 1 15 turnaround
P3 2 25 time of above
P4 3 5 processes
using FCFS
GANTT CHART
P1 P2 P3 P4
0 5 20 45 50
• Waiting Time for Process P1: (0-0)=0 Avg Waiting Time = (0+4+18+42) /4
• Waiting Time for Process P2: (5-1)=4 =64/4
• Waiting Time for Process P3: (20-2)=18 = 16 ms
• Waiting Time for Process P4: (45-3)=42
FCFS Example -2 Calculate
Process Arrival Burst time average
(ms) waiting time
P1 0 5 and
P2 1 15 turnaround
P3 2 25 time of above
P4 3 5 processes
using FCFS
GANTT CHART TURNAROUND TIME= Completion Time- Arrival Time

P1 P2 P3 P4
0 5 20 45 50
• Turnaround Time for Process P1: (5-0)=5 Avg Turnaround Time
• Turnaround Time for Process P2: (20-1)=19 = (5+19+43+47) /4
• Turnaround Time for Process P3: (45-2)=43 = 114/4
• Turnaround Time for Process P4: (50-3)=47 = 28.5 ms
FCFS Example -3 (Practical 14)
Process Burst time (ms)
P1 21
P2 6
P3 3
P4 2

Consider the processes P1, P2, P3, P4 given in the below table, all processes arrives for
execution in the same order, with Arrival Time 0 and given Burst Time. Find using the
FCFS scheduling algorithm.
1. Turn around Time for each process
2. Waiting Time for Each Process
3. Average Turnaround Time
4. Average Waiting Time
FCFS Example -4 (Practical 14)
The jobs are scheduled for execution as follows:
Solve the problem by using FCFS and preemptive SJF
Find the average waiting time using GANTT chart

Process Arrival Time Burst time


(ms)
P1 0 10
P2 1 4
P3 2 14
P4 3 8
SHORTEST JOB FIRST (SJF)

• In this scheduling algorithm, the process having


the shortest period of time for execution( smallest
CPU Burst) will be the first one to get the CPU.
• If the next CPU Bursts of two processes are same
then FCFS scheduling is used.
• This scheduling algorithm can be preemptive or
non-preemptive.
SHORTEST JOB FIRST (SJF)- Non preemptive Example
Process Arrival Burst time Calculate
(ms) average
P1 0 7 waiting time of
P2 1 4 above
P3 2 10 processes
GANTT CHART P4 3 6 using SJF
P5 4 8

P1 P2 P4 P5 P3
0 7 11 17 25 35

• Waiting Time for Process P1: (0-0)=0 Avg Waiting Time =


• Waiting Time for Process P2: (7-1)=6 (0+6+23+8+13) /5
• Waiting Time for Process P3: (25-2)=23 =50/5
• Waiting Time for Process P4: (11-3)=8 = 10 ms
• Waiting Time for Process P5:(17-4)=13
SHORTEST REMAINING TIME NEXT (SRTN)
• The Preemptive Shortest Job is called as Shortest
Remaining Time Next Scheduling.
• If newly arrived process has less CPU burst as compare
to the remaining CPU time of currently executing
process, then the CPU will be preempted from current
process & is given to newly arrived process.
• The current process is sent back to ready queue.
SHORTEST JOB FIRST (SJF)- Preemptive Example
Process Arrival Burst time
(ms) Calculate
P1 0 7 average
P2 1 4 waiting time of
above
P3 2 10
processes
P4 3 6 using SJF
GANTT CHART P5 4 8

P1 P2 P1 P4 P5 P3
0 1 5 11 17 25 35

• Waiting Time for Process P1: (0-0)+(5-1)=4 Avg Waiting Time =


• Waiting Time for Process P2: (1-1)=0 (4+0+23+8+13) /5
• Waiting Time for Process P3: (25-2)=23 =48/5
• Waiting Time for Process P4: (11-3)=8 = 9.6 ms
• Waiting Time for Process P5:(17-4)=13
SHORTEST REMAINING TIME NEXT(SRTN)- Example
Process Arrival Burst time Calculate
(ms) average
P1 0 10 waiting time
P2 1 5 and
P3 2 8 turnaround
GANTT CHART P4 3 15 time using
SRTN

P1 P2 P3 P1 P4
0 1 6 14 23 38

• Waiting Time for Process P1: (0-0)+(14-1)=13 Avg Waiting Time =


• Waiting Time for Process P2: (1-1)=0 (13+0+4+20) /4
• Waiting Time for Process P3: (6-2)=4 =37/4
• Waiting Time for Process P4: (23-3)=20 = 9.25 ms
SHORTEST REMAINING TIME NEXT(SRTN)- Example
Process Arrival Burst time Calculate
(ms) average
P1 0 10 waiting time
P2 1 5 and
P3 2 8 turnaround
GANTT CHART P4 3 15 time using
TURNAROUND TIME= Completion Time- Arrival Time SRTN

P1 P2 P3 P1 P4
0 1 6 14 23 38

• Turnaround Time for Process P1: (23-0)=23 Avg Turnaround Time =


• Turnaround Time for Process P2: (6-1)=5 (23+5+12+35) /4
• Turnaround Time for Process P3: (14-2)=12 =75/4
• Turnaround Time for Process P4: (38-3)=35 = 18.75 ms
ROUND ROBIN (RR)
• It is designed especially for time sharing systems
where many processes get CPU on time sharing
basis CPU Scheduler

• It is preemptive scheduling algorithm


• A small unit of time known as a time quantum or P1 P2
time slice is used for pre-emption of a currently P8
P3
running process.
• Ready queue is implemented as a circular queue P7 P4
• CPU is assigned to the entire processes one by
P6 P5
one, on first come first serve basis, for a specific
time period. READY QUEUE
• Every process executes for specified time period
and CPU is given to the next process when time
quantum expires.
ROUND ROBIN (RR)- Example
Process Burst time (ms) Calculate average
P1 11 waiting time and
P2 7 turnaround time using
P3 15 RR
P4 4
GANTT CHART TIME QUANTUM =4 ms

P1 P2 P3 P4 P1 P2 P3 P1 P3 P3
0 4 8 12 16 20 23 27 30 34 37

• Waiting Time for Process P1: 0+(16-4)+(27-20)=19 Avg Waiting Time =


• Waiting Time for Process P2: 4+(20-8)=16 (19+16+22+12) /4
• Waiting Time for Process P3: 8+(23-12)+(30-27)=22 =69/4
• Waiting Time for Process P4: 12 = 17.25 ms
ROUND ROBIN (RR)- Example
Process Burst time (ms) Calculate average
P1 11 waiting time and
P2 7 turnaround time using
P3 15 RR
P4 4
GANTT CHART TIME QUNATUM =4 ms
TURNAROUND TIME= Completion Time- Arrival Time

P1 P2 P3 P4 P1 P2 P3 P1 P3 P3
0 4 8 12 16 20 23 27 30 34 37

• Turnaround Time for Process P1: 30 Avg Turnaround Time =


• Turnaround Time for Process P2: 23 (30+23+37+16) /4
• Turnaround Time for Process P3: 37 =106/4
• Turnaround Time for Process P4: 16 = 26.5 ms
ROUND ROBIN (RR)- Example (Practical 14)
Process Burst time (ms) Calculate average
P1 12 waiting time using RR
P2 45 algorithm
P3 78
P4 90 TIME QUANTUM =20 ms
PRIORITY SCHEDULING
• In priority scheduling, each process has a priority which is a
integer value assigned to it.
• Smallest integer is considered as highest priority and largest
integer is considered as lowest priority.
• In this, highest priority process is given CPU time first and then
next highest priority process and so on.
• Equal priority process are scheduled in FCFS order
• SJF algorithm is special case of general priority algorithm
because smallest job gets first executed.
• This scheduling algorithm can be preemptive or non-preemptive
PRIORITY SCHEDULING Example
Process Burst time Priority
Calculate
(ms)
average
P1 12 4 waiting time of
P2 10 5 above
P3 5 2 processes
P4 4 1 using Priority
GANTT CHART P5 3 3

P4 P3 P5 P1 P2
0 4 9 12 24 34

• Waiting Time for Process P1: 12 Avg Waiting Time =


• Waiting Time for Process P2: 24 (12+24+4+0+9) /5
• Waiting Time for Process P3: 4 =49/5
• Waiting Time for Process P4: 0 =9.8 ms
• Waiting Time for Process P5: 9
PRIORITY SCHEDULING Example
Process Burst time Priority
Calculate
(ms)
average
P1 12 4 waiting time of
P2 10 5 above
P3 5 2 processes
P4 4 1 using Priority
GANTT CHART P5 3 3

P4 P3 P5 P1 P2
0 4 9 12 24 34
TURNAROUND TIME= Completion Time- Arrival Time
Avg Turnaround Time =
• Turnaround Time for Process P1: 24 (24+34+9+4+12) /5
• Turnaround Time for Process P2: 34 =83/5
• Turnaround Time for Process P3: 9 = 16.6 ms
• Turnaround Time for Process P4: 4
• Turnaround Time for Process P5: 12
PRIORITY SCHEDULING Example 2
Process Burst time Priority
Calculate
(ms)
average
P1 10 3 waiting time of
P2 1 1 above
P3 2 3 processes
P4 1 4 using
P5 5 2 i) Nonpreem
ptive
GANTT CHART Priority
ii) RR
(Quantum
=1)
PRIORITY Scheduling- Preemptive Example
Process Arrival Burst Priority
Calculate
time (ms) average
P1 0 8 3 waiting time of
P2 1 1 1 above
P3 2 3 2 processes
GANTT CHART P4 3 2 3 using
P5 4 6 4 preemptive
priority
P1 P2 P3 P1 P4 P5
0 1 2 5 12 14 20

• Waiting Time for Process P1: (0-0)+(5-1)=4 Avg Waiting Time =


• Waiting Time for Process P2: (1-1)=0 (4+0+0+9+10) /5
• Waiting Time for Process P3: (2-2)=0 =23/5
• Waiting Time for Process P4: (12-3)=9 = 4.6 ms
• Waiting Time for Process P5:(14-4)=10
MULTILEVEL QUEUE SCHEDULING

Example
• System,
• interactive,
• interactive editing,
Foreground Processes background Processes • Batch and
• Student processes queue.

P1 P2 P3 P4 P5 P7

READY QUEUE
MULTILEVEL QUEUE SCHEDULING WITH 5 QUEUES
AS PER PRIORITY
HIGHEST PRIORITY
SYSTEM PROCESSES

INTERACTIVE PROCESSES

INTERACTIVE EDITING PROCESSES

BATCH PROCESSES

STUDENT PROCESSES
LOWEST PRIORITY
MULTILEVEL QUEUE SCHEDULING

• In a system there are foreground processes and background


processes
• Multilevel queue scheduling classifies processes into different
groups.
• It partitions the ready queue into several separate queues.
• The processes are permanently assigned to one queue based
on some properties such as memory size, priority, process type,
etc.
• Each queue has its own scheduling algorithm.
• Scheduling is done for all the processes inside the queue as
well as for all separate queues.
MULTILEVEL QUEUE SCHEDULING
• Example
• Foreground queue can be scheduled with Round Robin
algorithm where as background queue can be scheduled by
First Come First Serve algorithm

• Consider all the processes in the system are divided into four
groups:
• System, Interactive, Interactive editing, Batch and Student
Processes Queue.
• Each queue contains processes.
• CPU is first scheduled for all queues on may be Priority, Total
burst time or Process type.
MULTILEVEL QUEUE SCHEDULING WITH 5 QUEUES
AS PER PRIORITY
HIGHEST PRIORITY
PR
SYSTEM PROCESSES
RR (Quantum: 8 ms)
INTERACTIVE PROCESSES
RR (Quantum: 10 ms)
INTERACTIVE EDITING PROCESSES
FCFS
BATCH PROCESSES
SJF
STUDENT PROCESSES
LOWEST PRIORITY
DEADLOCK
DEADLOCK
DEADLOCK
DEADLOCK
Allocated R1 REQUEST

P1
P2

REQUEST Allocated

R2
DEADLOCK
 It occurs in multiprogramming system
 Deadlock exists among a set of processes
if
 Every process is waiting for an event
 This event can be caused only by
another process in the set.
 A waiting process never changes its
state

Deadlock state: A set of blocked processes,


each holding resource and waiting to
acquire resources held by another process
in set. This state of processes is known as
Deadlock state.
SYSTEM MODEL
 System consists of resources
 Resource types R1, R2, . . ., Rm
 CPU , Memory, I/O devices , Printers
 Each resource type Ri has many instances.
 Each process utilizes a resource as follows:
– REQUEST
 If resource is available, access is granted
 If not available, the process is blocked
– USE
– RELEASE
 Undesirable scenario:
 Process P1 acquires resource R1, and is waiting for resource R2
 Process P2 acquires resource R2, and is waiting for resource R1
 Deadlock!
CONDITIONS FOR DEADLOCK

Deadlock can arise if four conditions hold simultaneously.

Coffman’s Conditions:
1. Mutual Exclusion

2. Hold and Wait

3. No Preemption

4. Circular Wait:
CONDITIONS FOR DEADLOCK
Coffman’s Conditions:
1. Mutual Exclusion: 2. Hold and Wait: A process holding at
least one resource & is waiting to acquire
Only one process at a time can use a additional resources held by other
resource & another process cannot processes
request for it.

P1 R1 X P2
CONDITIONS FOR DEADLOCK
Coffman’s Conditions: 4. Circular Wait: there exists a set {P0,
3. No Preemption: P1, …, Pn} of waiting processes such that
A resource can be released only P0 is waiting for a resource that is held by
voluntarily by the process P1, P1 is waiting for a resource that is held
holding it, after the process has by P2, …, Pn–1 is waiting for a resource
completed its task. It can not be that is held by Pn, and Pn is waiting for a
preempted. resource that is held by P0. This state of
the processes is circular wait state.
P2
Allocated R1 REQUEST
P1 P3

P1 P2
P4
DEADLOCK HANDLING METHODS

Ensure that the system will never enter in a deadlock state:


1. Deadlock Prevention
2. Deadlock Avoidance
3. Deadlock Detection & Recovery
• Allow the system to enter a deadlock state and then recover
4. Deadlock Ignorance
• Ignore the problem and pretend that deadlocks never occur in the
system
DEADLOCK PREVENTION
• We try to ensure that one of the four necessary conditions
cannot hold, then we can prevent it

• Mutual Exclusion:
• If it is shareable resource, then we can break the mutual
exclusion (such as: Read-only file)

• If it is not a shareable resource, then mutual exclusion must


hold (such as: Printer)
DEADLOCK PREVENTION
• Hold and wait: two methods

• must guarantee that whenever a process requests a


resource, it does not hold any other resources
– Require process to request and be allocated all its resources
before it begins execution, or allow process to request resources
only when the process has none allocated to it.
– Low resource utilization; starvation possible
DEADLOCK PREVENTION
• NO PREEMPTTION:

• If a process that is holding some resources requests another


resource that cannot be immediately to allocated it, then all
resources currently being held are released.

• Preempted resources are added to the list of resources for which


the process is waiting.

• Process will be restarted only when it can regain its old


resources, as well as the new ones that it is requesting.
DEADLOCK PREVENTION
• Circular wait:
• Impose a total ordering of all resource types, and require that
each process requests resources in an increasing order of
enumeration.
Allocated R1 REQUEST
• CPU -1
Printer -2
Memory -3 P1
P2

REQUEST Allocated

R3
DEADLOCK AVOIDANCE
 Additional prior information regarding resources of
process required
Process must give information regarding resources at
starting
Bankers - safety algorithm used to avoid deadlock
Deadlock Avoidance
• If a system is in safe state  no
deadlocks

• If a system is in unsafe state 


possibility of deadlock

• Avoidance  ensure that a system


will never enter an unsafe state.
Thank you

You might also like