Unit 2 - Os-Psna - Part 2
Unit 2 - Os-Psna - Part 2
Process AT BT CT TAT WT
P1 0 24 24 24 0
P2 0 3 27 27 24
P3 0 3 30 30 27
P2 0 3 3 3 0
P3 0 3 6 6 3
P1 0 24 30 30 6
P1 0 20 20 20 0
P2 1 2 22 21 19
P3 1 1 23 22 21
AVG 21 13.3
First-Come, First-Served (FCFS) Scheduling
• 3.
Process Arrival Burst
Time Time
P1 0 21
P2 0 3
P3 0 6
P4 0 2
FCFS
Proces AT BT CT TAT WT
s
P1 0 21 21 21 0
P2 0 3 24 24 21
P3 0 6 30 30 24
P4 0 2 32 32 30
Avg 26.75 18.75
4 . Example - FCFS
Answer - FCFS
Proces AT BT CT TAT WT
s
P1 0 2 2 2 0
P2 1 3 5 4 1
P3 2 5 10 8 3
P4 3 4 14 11 7
P5 4 6 20 16 10
8.2 4.2
Home work - FCFS
Process Arrival Time Burst Time
P1 0 80
P2 0 20
P3 0 10
P4 0 20
P5 0 50
FCFS -Home work 1 -Answer
• Average Waiting Time - 84 ms
• Average Turn Around time - 120 ms
FCFS -Home work 2
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
FCFS - Home work 2 -Answer
• Average Waiting Time – 8.75 ms
• Average Turn Around time – 15.25 ms
Shortest-Job-First (SJR) Scheduling
• Associate with each process the length of its next CPU
burst. Use these lengths to schedule the process with
the shortest time.
• Two schemes:
• nonpreemptive – 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 know as the
Shortest-Remaining-Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time
for a given set of processes.
Example of Non-Preemptive SJF
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (non-preemptive)
P1 P3 P2 P4
0 3 7 8 12 16
P2 0 3 3 3 0
P3 0 3 6 6 3
P1 0 24 30 30 6
AVG 13 3
Home work1 – SJF (Non Preemptive)
Process Arrival Time Burst Time
P1 0 80
P2 0 20
P3 0 10
P4 0 20
P5 0 50
SJF -Home work 1 -Answer
• Average Waiting Time - 38 ms
• Average Turn Around time - 74 ms
SJF -Home work 2
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
SJF - Home work 2 -Answer
• Average Waiting Time – 7.75 ms
• Average Turn Around time – 14.25 ms
Example of Preemptive SJF
(Shortest Remaining Time First)
Process Arrival Time Burst Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• SJF (preemptive)
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
P2 1 4 5 4 0
P3 2 9 26 24 15
P4 3 5 10 7 2
AVG 13 6.5
1 1 1 2 5 7 9
P1 P2 P2 P2 P4 P1 P3
Homework 1- SJF –(Preemptive)
Process Arrival Time Burst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
SJF (Pre emptive) - Home work 1 -Answer
• Average Waiting Time – 3 ms
• Average Turn Around time – 7 ms
Homework 2- SJF –(Preemptive)
Process Arrival Time Burst Time
P1 0 7
P2 2 6
P3 4 1
P4 5 2
SJF (Pre emptive) - Home work 2 -Answer
• Average Waiting Time – 3.75 ms
• Average Turn Around time – 7 ms
Round Robin (RR)
• Each process gets a small unit of CPU time (time quantum), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.
• If there are n processes in the ready queue and the time quantum is
q, then each process gets 1/n of the CPU time in chunks of at most q
time units at once. No process waits more than (n-1)q time units.
• Performance
• q large FIFO
• q small q must be large with respect to context switch, otherwise overhead
is too high.
Example 1: RR with Time Quantum = 20
Process Burst Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart is:
P1 P2 P3 P4 P1 P3 P4 P1 P3 P3
Process AT BT CT TAT WT
P1 0 3 7 7 4
P2 0 4 10 10 6
P3 0 3 9 9 6
P1 0 6
P2 0 5
P3 0 2
P4 0 3
P5 0 7
Homework 1- RR - Answer
Process AT BT CT TAT WT
P1 0 6 19 19 13
P2 0 5 20 20 15
P3 0 2 10 10 8
P4 0 3 13 13 10
P5 0 7 23 23 16
AVG 17 12.4
Homework 2- RR (complicated) -Q-3
Process AT BT
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
Homework 2- RR (complicated) -
Answer
Process AT BT CT TAT WT
P1 5 5 32 27 22
P2 4 6 27 23 17
P3 3 7 33 30 23
P4 1 9 30 29 20
P5 2 2 6 4 2
P6 6 3 21 15 12
AVG 21.3 16
Practice examples - 1
• If the CPU scheduling policy is Round Robin with time quantum = 2 unit,
calculate the average waiting time and average turn around time.
Practice examples - 1
Practice examples - 2
• Average Turn Around time = (8 + 17 + 4 + 6 + 17 + 13) / 6 = 65 /
6 = 10.84 unit
• Average waiting time = (4 + 12 + 2 + 5 + 11 + 10) / 6 = 44 / 6 =
7.33 unit
Practice examples - 3
• Average Turn Around time = 21.33 unit
• Average waiting time = 16 unit
Priority Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest priority (smallest
integer highest priority).
• Preemptive
• nonpreemptive
• SJF is a priority scheduling where priority is the predicted next CPU
burst time.
• Problem Starvation – low priority processes may never execute.
• Solution Aging – as time progresses increase the priority of the
process.
Priority Scheduling (Non Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 10 3
P2 0 1 1
P3 0 2 4
P4 0 1 5
P5 0 5 2
Priority Scheduling (Non Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 10 3 16 16 6
P2 0 1 1 1 1 0
P3 0 2 4 18 18 16
P4 0 1 5 19 19 18
P5 0 5 2 6 6 1
AVG 12 8.2
HW :Priority Scheduling (Non Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5
Priority Scheduling (Non Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 4 2 4 4 0
P2 1 3 3 15 14 11
P3 2 1 4 12 10 9
P4 3 5 5 9 6 1
P5 4 2 5 11 7 5
AVG 8.2 5.2
Priority Scheduling (Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 4 4
P2 1 3 3
P3 2 1 2
P4 3 5 1
P5 4 2 1
Priority Scheduling (Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 4 4 15 15 11
P2 1 3 3 12 11 8
P3 2 1 2 3 1 0
P4 3 5 1 8 5 0
P5 4 2 1 10 6 4
AVG 7.6 4.6
Hw : Priority Scheduling (Preemptive)
Process Arrival Burst Time Priority
Time
P1 0 1 2
P2 1 7 6
P3 2 3 3
P4 3 6 5
P5 4 5 4
P6 5 15 10
P7 15 8 9
Hw: Priority Scheduling (Preemptive)
Proce AT BT Priori CT TAT WT
ss ty
P1 0 1 2 1 1 0
P2 1 7 6 22 21 14
P3 2 3 3 5 3 0
P4 3 6 5 16 13 7
P5 4 5 4 10 6 1
P6 5 15 10 45 40 25
P7 15 8 9 30 24 16
Avg 9
Banker’s Algorithm
• Multiple instances.
• Each process must a priori claim maximum use.
• When a process requests a resource it may have to wait.
• When a process gets all its resources it must return them in a finite
amount of time.
Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types.