0% found this document useful (0 votes)
15 views44 pages

Problem Unit 1, 2 3

The document discusses CPU scheduling algorithms, focusing on First Come First Served (FCFS) and Shortest Job First (SJF) methods. It provides examples illustrating how different arrival orders and burst times affect average waiting and turnaround times, highlighting issues like the convoy effect. Additionally, it covers both non-preemptive and preemptive versions of SJF, showing their impact on scheduling efficiency.

Uploaded by

nctitacademic
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)
15 views44 pages

Problem Unit 1, 2 3

The document discusses CPU scheduling algorithms, focusing on First Come First Served (FCFS) and Shortest Job First (SJF) methods. It provides examples illustrating how different arrival orders and burst times affect average waiting and turnaround times, highlighting issues like the convoy effect. Additionally, it covers both non-preemptive and preemptive versions of SJF, showing their impact on scheduling efficiency.

Uploaded by

nctitacademic
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/ 44

CPU Scheduling:

First Come First Served (FCFS)


Example: 1

Process Burst Time


P1 24
P2 3
P3 3
Total burst time=30

If the processes arrive in the order PI, P2, P3, and are served in FCFS order,
we get the result shown in the following

Gantt chart:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 24 24 0
2 27 27 24
3 30 30 27

The average waiting time is = (0 + 24 + 27)/3 = 17 milliseconds.


The turn around time is = (24+27+30)/3 = 27 milliseconds.
If the processes arrive in the order P2, P3, P1, however,
the results will be as shown in the following

Gantt chart:

The average waiting time is now (6 + 0 + 3)/3 = 3 milliseconds.


This reduction is substantial. Thus, the average waiting time under a FCFS policy is generally
not minimal, and may vary substantially if the process CPU-burst times vary greatly.

Page 1 of 44
First Come First Served (FCFS)
Example: 2

Process No. Arrival Time Burst Time


1 0 2
2 1 3
3 2 5
4 3 4
5 4 1
Total Burst time=15
Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 2 (2 -0) = 2 (2 – 2) = 0
2 5 (5 – 1) = 4 (4 – 3) = 1
3 10 (10 – 2) =8 (8 – 5) = 3
4 14 (14 – 3) = 11 (11 – 4) = 7
5 15 (15 – 4) =11 (11 – 1) = 10

Average waiting time: =( 0+1+3+7+10)/5 =4.2 milliseconds.

Average Turnaround time: =(2+4+8+11+11)/5 =7.2 milliseconds.

Schedule length = maximum completion time – minimum arrival time


=15-0 =15

Page 2 of 44
What is Convey Effect? When one process with big burst time will scheduled first, then all
other process have to wait or get blocked until big process get off the CPU is called convoy
effect. It produce huge average waiting time, which means utilization of CPU gets lower.
Lets have an example,
Process Arrival Time Burst time
P1 0 30
P2 1 3
P3 1 1
Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 30 (30-0) = 30 (30 – 30) =0
2 33 (33-1)= 32 (32 – 3) = 29
3 34 (34-1) = 33 (33 – 1) =32
Average waiting time: =( 0+29+32)/3 =20.33 milliseconds.
Average Turnaround time: =(30+32+33)/3 =31.66 milliseconds.
From above example we can see that, the rest process have to wait for long time.
Suppose if we change arrival time off process P2, P3 at 0 and P1 at 1, then we will have less
average waiting time.

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 34 (34-1) = 33 (33 – 30) =3
2 3 (3-0)= 3 (3 – 3) = 0
3 4 (4 - 0) = 4 (4 – 1) =3
Average waiting time: =( 3+0+3)/3 =2
Average Turnaround time: =(33+3+4)/3 =13.33
Page 3 of 44
Shortest Job First (SJF):
Example:
Process No. Burst Time
1 4
2 7
3 3
4 3
5 5
Total Burst time=16
Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 0 (0-0) = 0 (0 – 4) =4
2 3 (3-0)= 3 (3 – 7) = 10
3 6 (6 - 0) = 6 (6 – 3) =9
4 15 (15 - 0) = 15 (15 – 3) =18
5 22 (22 - 0) = 22 (22 – 5) =27

Average waiting time = ( 0+3+6+15+22)/5 = 6.8ms

Average Turnaround time = (4+10+4+18+27)/5 = 11.2ms

Page 4 of 44
Shortest Job First (SJF):
(SJF NON preemptive):

Example 1-

Process No. Arrival Time Burst Time


1 0 6
2 0 8
3 0 7
4 0 3
Total Burst time=24

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 9 (9-0) = 9 (9 – 6) =3
2 24 (24-0)= 24 (24 – 8) = 16
3 16 (16 - 0) = 16 (16 – 7) =9
4 3 (3 - 0) = 3 (3 – 3) =0

Average waiting time: =(3+16+9+0)/4 =7

Average Turnaround time =(9+24+16+3)/4 =13

Schedule length: = maximum completion time – minimum arrival time


=24-0 =24

Page 5 of 44
Shortest Job First (SJF):
(SJF NON preemptive):

Example 2-
Process No. Arrival Time Burst Time
1 0 7
2 2 4
3 4 1
4 5 4
Total Burst time=16
Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 7 (8-0) = 7 (7 – 7) =0
2 12 (12-2)= 10 (10 – 4) = 6
3 8 (8 - 4) = 4 (4 – 1) =3
4 16 (16 - 5) = 11 (11 – 4) =7

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

Average Turnaround time = (7+10+4+11)/4 =8

Schedule length = maximum completion time – minimum arrival time


= 16-0 = 16

Page 6 of 44
Shortest Job First (SJF):
(SJF NON preemptive):

Example 3-

Process No. Arrival Time Burst Time


1 3 1
2 1 4
3 4 2
4 0 6
5 2 3
Total Burst time=16

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 7 (7-3) = 4 (4 – 1) =3
2 16 (16-1)= 15 (15 – 4) = 11
3 9 (9 - 4) = 5 (5 – 2) =3
4 6 (6 - 0) = 6 (6 – 6) =0
5 12 (12-2)=10 (10 - 3)=7

Average waiting time: =(4+15+5+5+10)/5 =8ms


Average Turnaround time =(3+11+3+0+7)/5 =4.8ms

Schedule length: = maximum completion time – minimum arrival time


=16-0 =16

Page 7 of 44
Shortest Job First (SJF):
(SJF NON preemptive):

Example 4-

Process No. Arrival Time Burst Time


1 0 10
2 1 6
3 2 12
4 3 15
Total Burst time=43

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 10 (10-0) = 10 (10 – 10) =0
2 16 (16-1)= 15 (15 – 6) = 9
3 28 (28 - 2) = 26 (26 – 12) =14
4 43 (43 - 3) = 40 (43 – 15) =28

Average waiting time: =(0+9+14+28)/4 =12.75ms

Average Turnaround time =(10+15+26+40)/4 = 22.75ms

Page 8 of 44
(SJF preemptive): or Shortest Remaining Time First (SRTF)

Preemptive mode of Shortest Job First is called as Shortest Remaining Time First (SRTF)

Example 1-

Process No. Arrival Time Burst Time


1 3 1
2 1 4
3 4 2
4 0 6
5 2 3
Total Burst time=16

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 4 (4-3) = 1 (1 – 1) =0
2 6 (6-1)= 5 (5 – 4) = 1
3 8 (8 - 4) = 4 (4 – 2) =2
4 16 (16 - 0) = 16 (16 – 6) =0
5 11 (11-2)=9 (9 - 3)=6

Average waiting time: =(1+5+4+16+9)/5 =7ms

Average Turnaround time =(0+1+2+10+6)/5 =3.8ms

Schedule length: = maximum completion time – minimum arrival time


=16-0 =16

Page 9 of 44
Shortest Job First (SJF):
(SJF Preemptive):

Example 2-

Process No. Arrival Time Burst Time


1 0 10
2 1 6
3 2 12
4 3 15
Total Burst time=43

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 16 (16-0) = 16 (16 – 10) =6
2 7 (7-1)= 6 (6 – 6) = 0
3 28 (28 - 2) = 26 (26 – 12) =14
4 43 (43 - 3) = 40 (43 – 15) =28

Average waiting time: =(6+0+14+25)/4 =11.25ms

Average Turnaround time =(16+6+26+40)/4 = 22ms

Page 10 of 44
Shortest Job First (SJF):
(SJF Preemptive):

Example 3:
Process No. Arrival Time Burst Time
1 0 5
2 1 2
3 2 8
4 3 3
Total Burst time=18

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 10 (10-0) = 10 (10 – 5) =5
2 3 (3-1)= 2 (2 – 2) = 0
3 18 (18 - 2) = 16 (16 – 8) =8
4 6 (6 - 3) = 3 (3 – 3) =0

Average waiting time = (5+0+8+0)/4 = 3.25

Average Turnaround time = (10+2+16+3)/4 = 7.75

Schedule length = maximum completion time – minimum arrival time


= 18-0 = 18

Page 11 of 44
Shortest Job First (SJF):
(SJF Preemptive):

Example 4:
Process No. Arrival Time Burst Time
1 0 7
2 1 5
3 2 3
4 3 1
5 4 2
6 5 1
Total Burst time=19

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 19 (19-0) = 19 (19 – 7) =12
2 13 (13 - 1)= 12 (12 – 5) = 7
3 6 (6 - 2) = 4 (4 – 3) =1
4 4 (4 - 3) = 1 (1 – 1) =0
5 9 (9 - 4)=5 (5 - 2)=3
6 7 (7 - 5)=2 (2 - 1)=1

Average waiting time: =(19+12+4+1+5+2)/6 =7.17ms


Average Turnaround time =(12+7+1+0+3+1)/6 =4 ms

Schedule length: = maximum completion time – minimum arrival time


=19 - 0 =19

Page 12 of 44
Shortest Job First (SJF):
(SJF Preemptive):

Example 5:
Process No. Arrival Time Burst Time
1 0 7
2 1 5
3 2 3
Total Burst time=15

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 13 (13-0) = 13 (13 – 9) =4
2 5 (5 - 1)= 4 (4 – 4) = 0
3 22 (22 - 2) = 20 (20 – 9) =11

Average waiting time: =(13+4+20)3 = 12.33ms

Average Turnaround time =(4+0+11)/3 = 5ms

Schedule length: = maximum completion time – minimum arrival time


=15- 0 =15

Page 13 of 44
Round Robin (RR)

Example 1:

Process No. Burst Time


1 8
2 4
3 5
4 2
5 1
Total Burst time=20

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 19 (19 - 0) = 19 (19 – 8) =11
2 8 (8 - 0)= 8 (8 – 4) = 4
3 20 (20 - 0) = 20 (20 – 5) =15
4 14 (14 - 0) = 14 (14 – 2) =12
5 15 (15 - 0) = 15 (15 – 1) =14

Average waiting time = (11+4+15+12+14)/5 = 11.20ms

Average Turnaround time = (19+8+20+14+15)/5 = 15.20ms

Page 14 of 44
Round Robin (RR) Example 2:
Method One:
Process No. Arrival Time Burst Time
1 0 5
2 1 3
3 2 1
4 3 2
5 4 3
Total Burst time=14
Solution:
Ready Queue:
Table Shows scheduled and waiting to scheduled in ready queue
Process get scheduled Process will waiting to get schedule
P1 P2, P3
P2 P3, P1
P3 P1, P4, P5, P2
P1 P4, P5, P2
P4 P5, P2, P1
P5 P2, P1
P2 P1, P5
P1 P5
P5

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 13 (13-0) = 13 (13 – 5) =8
2 12 (12-1)= 11 (11 – 3) = 8
3 5 (5 - 2) = 3 (3 – 1) =2
4 9 (9 - 3) = 6 (6 – 2) =4
5 14 (14 - 4) = 10 (10 – 3) =7

Average waiting time = (8+8+2+4+7)/5 = 5.8ms


Average Turnaround time = (13+11+3+6+10)/5 = 8.6ms

Page 15 of 44
Round Robin (RR) Example 3:
Process No. Arrival Time Burst Time
1 0 4
2 1 5
3 2 2
4 3 1
5 4 6
6 6 3
Total Burst time=21
Solution: Ready Queue:
Table Shows scheduled and waiting to scheduled in ready queue
Process get scheduled Process will waiting to get schedule
P1 P2, P3
P2 P3, P1
P3 P1, P4, P5, P2
P1 P4, P5. P2, P6
P4 P5, P2, P6
P5 P2. P6
P2 P6, P5
P6 P5, P2
P5 P2, P6
P2 P6, P5
P6 P5
P5

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 8 (8-0) = 8 (8 – 4) = 4
2 18 (18-1)= 17 (17 – 5) = 12
3 6 (6 - 2) = 4 (4 – 2) = 2
4 9 (9 - 3) = 6 (6 – 1) = 5
5 21 (21 - 4) = 17 (17 – 6) = 11
6 19 (19 - 6) = 13 (12 – 3) = 10
Average waiting time = (4+12+2+5+11+10)/6 = 7.33ms
Average Turnaround time = (8+17+4+6+17+13)/6 = 10.84ms
Page 16 of 44
Round Robin (RR)
Alternative Method:
Example: 1

Process No. Arrival Time Burst Time


1 0 6
2 1 2
3 1 3
4 2 1
5 2 2
Total Burst time=14

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 14 (14-0) = 14 (14 – 6) = 8
2 7 (7-1)= 6 (6 – 2) = 4
3 11 (11 - 1) = 10 (10 – 3) = 7
4 4 (4 - 2) = 2 (2 – 1) = 1
5 9 (9 - 2) = 7 (7 – 2) = 5

Average waiting time = (8+4+7+1+5)/5 = 25/5 = 5 ms

Average Turnaround time = (14+6+10+2+7)/5 = 39/5 =7.8 ms

Page 17 of 44
Round Robin (RR)
Alternative Method:
Example: 2

Process No. Arrival Time Burst Time


1 0 10
2 0 6
3 1 7
4 1 4
5 2 5
Total Burst time=32

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 29 (29-0) = 29 (29 – 10) = 19
2 30 (30-1)= 29 (29 – 6) = 23
3 32 (32 - 1) = 31 (31 – 7) = 24
4 19 (19 - 2) = 17 (19 – 4) = 15
5 24 (24 - 2) = 22 (22 – 5) = 17

Average waiting time = (19+23+24+15+17)/5 = 98/5 = 19.6 ms

Average Turnaround time = (29+29+31+17+22)/5 = 130/5 =26 ms

Page 18 of 44
Priority Scheduling
(Non Preemptive)

Example 1:
Process No. Arrival Time Burst Time Priority
1 0 4 2
2 1 3 3
3 2 1 4
4 3 5 5
5 4 2 5
Total Burst time=19
Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 4 (4-0) = 4 (4 – 4) =0
2 15 (15 - 1)= 14 (14 – 3) = 11
3 12 (12 - 2) = 10 (10 – 1) =9
4 9 (9 - 3) = 6 (6 – 5) =1
5 11 (11 - 4)=7 (7 - 2)=5

Average waiting time: =(14+14+10+5+7)/5 =8.2ms

Average Turnaround time =(0+11+9+1+5)/5 =5.2 ms

Page 19 of 44
Priority Scheduling
(Non Preemptive)
Example 2:

Process No. Arrival Time Burst Time Priority


1 0 6 2
2 1 2 2
3 1 3 4
4 2 1 1
5 2 2 3
Total Burst time=12

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 6 (6-0) = 6 (6 – 6) =0
2 9 (9 - 1)= 8 (8 – 2) = 6
3 14 (14 - 1) = 13 (13 – 3) =10
4 7 (7 - 2) = 5 (5 – 1) =4
5 11 (11 - 2)=9 (9 - 2)=7

Average waiting time: =(0+6+10+4+7)/5 =5.2ms

Average Turnaround time =(6+8+13+5+9)/5 =8.2 ms

Page 20 of 44
Priority Scheduling
(Non Preemptive)
Example 3:

Process No. Arrival Time Burst Time Priority


1 0 10 5
2 0 6 2
3 1 7 4
4 1 4 1
5 2 5 3
Total Burst time=32

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 32 (32 - 0) = 32 (32 – 10) =22
2 6 (6 - 0)= 6 (6 – 6) = 0
3 22 (22 - 1) = 21 (21 – 7) =14
4 10 (10 - 1) = 9 (9 – 4) =5
5 15 (15 - 2) = 13 (13 - 5)=8

Average waiting time: =(22+0+14+5+8)/5 =9.8 ms

Average Turnaround time =(32+6+21+9+13)/5 =16.2 ms

Page 21 of 44
Priority Scheduling
(Preemptive)
Example 1:
Process No. Arrival Time Burst Time Priority
1 0 4 2
2 1 3 3
3 2 1 4
4 3 5 5
5 4 2 5
Total Burst time=19

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 15 (15-0) = 15 (15 – 4) =11
2 12 (12 - 1)= 11 (11 – 3) = 8
3 3 (3 - 2) = 1 (1 – 1) =0
4 8 (8 - 3) = 5 (5 – 5) =0
5 10 (10 - 4)=6 (6 - 2)=4

Average waiting time: =(15+11+1+5+6)/5 =7.6 ms

Average Turnaround time =(11+8+0+0+4)/5 =4.6 ms

Page 22 of 44
Priority Scheduling
(Preemptive)
Example 2:
Process No. Arrival Time Burst Time Priority
1 0 6 2
2 1 2 2
3 1 3 4
4 2 1 1
5 2 2 3
Total Burst time=12

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 7 (7-0) = 7 (7 – 6) =1
2 9 (9 - 1)= 8 (8 – 2) = 6
3 14 (14 - 1) = 13 (13 – 3) =10
4 3 (3 - 2) = 1 (1 – 1) =0
5 11 (11 - 2)=9 (9 - 2)=7

Average waiting time: =(1+6+10+0+7)/5 =4.8ms

Average Turnaround time =(7+8+13+1+9)/5 =7.6 ms

Page 23 of 44
Priority Scheduling

(Preemptive)

Example 3:

Process No. Arrival Time Burst Time Priority


1 0 10 5
2 0 6 2
3 1 7 4
4 1 4 1
5 2 5 3
Total Burst time=32

Solution:

Turnaround time Waiting time


Process
Exit time (completion time – (Turnaround time - burst
No
arrival time) time)
1 32 (32 - 0) = 32 (32 – 10) =22
2 10 (10 - 0)= 10 (10 – 6) = 4
3 22 (22 - 1) = 21 (21 – 7) =14
4 5 (5 - 1) = 4 (4 – 4) =0
5 15 (15 - 2) = 13 (13 - 5)=8

Average waiting time: =(22+4+14+0+8)/5 =9.6 ms

Average Turnaround time =(32+10+21+4+13)/5 =16 ms

Page 24 of 44
Bankers Algorithm:
Example 1:
Process Allocation MAX Available
A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3

a) Need = MAX - Allocation

Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

b) Currently the system is safe sequence


Safe sequence: Safe sequence is calculated as follows:
1. Need of each process is compared with available.
If Needi  Availablei, then the resources are allocated to that process and
process will releases the resource.
2. If Need >= Available, next process need is taken for comparison.
In the above example,
Process P0
Need  Available
(7, 4, 3)  (3, 3, 2)
P0 is not granted
So the system will move for next process
Page 25 of 44
Process P1
Need  Available
(1, 2, 2)  (3, 3, 2),
P1 is granted
Current Available = old Available + Allocation
= (3, 3, 2) + (2, 0, 0) = (5, 3, 2)

Process P2
Need  Available
(6, 0, 0)  (5, 3, 2),
P2 is not granted

Process P3
Need  Available
(0, 1, 1)  (5, 3, 2)
P3 is granted
Current Available = old Available + Allocation
= (5, 3, 2) + (2, 1, 1):= (7, 4, 3)

Process P4
Need  Available
(4, 3, 1)  (7, 4, 3)
P4 is granted
Current Available = old Available + Allocation
= (7, 4, 3) + (0, 0, 2):= (7, 4, 5)

Process P0
Need  Available
(7, 4, 3)  (7, 4, 5)
P0 is granted
Current Available = old Available + Allocation
= (7, 4, 5) + (0, 1, 0)= (7, 5, 5)
Page 26 of 44
Process P2
Need  Available
(6, 0, 0)  (7, 4, 5)
P2 is granted
Current Available = old Available + Allocation
= (7,5, 5) + (3, 0, 2) = (10, 5, 7)

The system is in Safe State in the sequence


Safe sequence is <P1, P3, P4, P0, and P2>

c) Request  Need
(1, 0, 2)  (1,2,2)
Request  Available
(1, 0, 2)  (3,3,2)
Request will be granted and satisfied
Available = Available – Request
=3,3,2 – 1,0,2 = 2,3,0
Need = Need – Request
=1,2,2 – 1,0,2 = 0,2,0
Allocation = Allocation + Request
=2,0,0 + 1,0,2 = 3,0,2

Process Allocation MAX Available


A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0
P1 3 0 2 3 2 2

P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3

Page 27 of 44
a) Need = MAX - Allocation

Process Need
A B C
P0 7 4 3
P1 0 2 0
P2 6 0 0
P3 0 1 1
P4 4 3 1

b) Currently the system is safe sequence


Process P0
Need  Available
(7, 4, 3)  (2, 3, 0)
P0 is not granted
So the system will move for next process
Process P1
Need  Available
(0, 2, 0)  (2, 3, 0),
P1 is granted
Current Available = old Available + Allocation
= (2, 3, 0) + (3, 0, 2) = (5, 3, 2)
Process P2
Need  Available
(6, 0, 0)  (5, 3, 2),
P2 is not granted
Process P3
Need  Available
(0, 1, 1)  (5, 3, 2)
P3 is granted
Current Available = old Available + Allocation
= (5, 3, 2) + (2, 1, 1):= (7, 4, 3)
Page 28 of 44
Process P4
Need  Available
(0, 0, 2)  (7, 4, 3)
P4 is granted
Current Available = old Available + Allocation
= (7, 4, 3) + (0, 0, 2):= (7, 4, 5)
Process P0
Need  Available
(7, 4, 3)  (7, 4, 5)
P0 is granted
Current Available = old Available + Allocation
= (7, 4, 5) + (0, 1, 0)= (7, 5, 5)
Process P2
Need  Available
(6, 0, 0)  (7, 5, 5)
P2 is granted
Current Available = old Available + Allocation
= (7,5, 5) + (3, 0, 2) = (10, 5, 7)
<P1, P3, P4, P0, and P2>
The Safe sequence is exist. So request P1(1,0,2) is granted immediately

_________________________________________________________________________________________________________

Page 29 of 44
Example 2:
Process Allocation MAX Available
A B C D A B C D A B C D
P0 0 0 1 1 0 0 1 1 1 5 2 2
P1 0 1 0 1 1 7 5 1
P2 1 3 5 1 2 3 5 2
P3 0 5 3 1 1 6 5 2
P4 0 0 1 1 5 6 5 1
Answer the following question using bankers algorithm
a) What is the content of the Matrix
b) Is the system in a safe state
c) If the request from process P1, arrives for (0,3, 2, 0) can the request bbe
granted immediately
Solution:
c) Need = MAX - Allocation

Process Need
A B C D
P0 0 0 0 0
P1 1 6 5 0
P2 1 0 0 1
P3 1 1 2 1
P4 5 6 4 0

d) Currently the system is safe sequence


Process P0
Need  Available
(0,0,0,0)  (1,5,2,2)
P0 is granted
Current Available = old Available + Allocation
= (1,5,2,2) + (0,0,1,1) = (1,5,3,3)

Page 30 of 44
Process P1
Need  Available
(1,6,5,0)  (1,5,3,3)
P1 is not granted
Process P2
Need  Available
(1, 0, 0,1)  (1,5,3,3)
P2 is granted
Current Available = old Available + Allocation
= (1,5.3,3) + (1,3,5,1) = (2,8,8,4)
Process P3
Need  Available
(1, 1, 2,1)  (2,8,8,4)
P3 is granted
Current Available = old Available + Allocation
= (2,8.8,4) + (0,5,3,1) = (2,13,11,5)
Process P4
Need  Available
(5,6,4,0)  (2,13,11,5)
P4 is not granted
Process P1
Need  Available
(1,6,5,0)  (2,13,11,5)
P1 is granted
Current Available = old Available + Allocation
= (2,13.11,5) + (0,1,0,1) = (2,14,11,6)
Process P4
Need  Available
(5,6,4,0)  (2,14,11,6)
P4 is not granted
The System is in Un-Safe State.

Page 31 of 44
e) Request  Need
(0, 3, 2,0)  (1,6,5,0)
Request  Available
(0, 3, 2,0)  (1,5,2,2)
Request will be granted and satisfied
Available = Available – Request
=1,5,2,2 – 0,3,2,0 = 1,2,0,2
Need = Need – Request
=1,6,5,0 – 0,3,2,0 = 1,3,3,0
Allocation = Allocation + Request
=0,1,0,1 + 0,3,2,0 = 0,4,2,1

Process Allocation MAX Available


A B C D A B C D A B C D
P0 0 0 1 1 0 0 1 1 1 2 0 2
P1 0 4 2 1 1 7 5 1

P2 1 3 5 1 2 3 5 2
P3 0 5 3 1 6 5 2
P4 0 0 1 1 5 6 5 1
Answer the following question using banker’s algorithm
Solution:
a) Need = MAX - Allocation

Process Need
A B C D
P0 0 0 0 0
P1 1 3 3 0
P2 1 0 0 1
P3 1 1 2 1
P4 5 6 4 0

Page 32 of 44
b) Currently the system is safe sequence

Process P0
Need  Available
(0,0,0,0)  (1,2,0,2)
P0 is granted
Current Available = old Available + Allocation
= (1,2,0,2) + (0,0,1,1) = (1,2,1,3)

Process P1
Need  Available
(1,3,3,0)  (1,2,1,3)
P1 is not granted

Process P2
Need  Available
(1, 0, 0,1)  (1,2,1,3)
P2 is granted
Current Available = old Available + Allocation
= (1,2.1,3) + (1,3,5,1) = (2,5,6,4)

Process P3
Need  Available
(1, 1, 2,1)  (2,5,6,4)
P3 is granted
Current Available = old Available + Allocation
= (2,5.6,4) + (0,5,3,1) = (2,10,9,5)

Process P4
Need  Available
(5,6,4,0)  (2,10,9,5)
P4 is not granted
Page 33 of 44
Process P1
Need  Available
(1,3,3,0)  (2,10,9,5)
P1 is granted
Current Available = old Available + Allocation
= (2,10.9,5) + (0,4,2,1) = (2,14,11,6)

Process P4
Need  Available
(5,6,4,0)  (2,14,11,6)
P4 is not granted
Not granted, because the process is deadlocked
The request A(0,3,2,0) is not granted immediately

_________________________________________________________________________________________________________

Page 34 of 44
Example 3:
Process Allocation MAX Available
A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 5 2 0
P1 1 0 0 0 1 7 5 0
P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6
Answer the following question using bankers algorithm
a) What is the content of the Matrix
b) Is the system in a safe state
c) If the request from process P1, arrives for (0,3, 2, 0) can the request bbe
granted immediately

Solution:
a) Need = MAX - Allocation

Process Need
A B C D
P0 0 0 0 0
P1 0 7 5 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2

b) Currently the system is safe sequence


Process P0
Need  Available
(0,0,0,0)  (1,5,2,2)
P0 is granted
Current Available = old Available + Allocation
= (1,5,2,0) + (0,0,1,2) = (1,5,3,2)
Page 35 of 44
Process P1
Need  Available
(0,7,5,0)  (1,5,3,2)
P1 is not granted

Process P2
Need  Available
(1,0,0,2)  (1,5,3,2)
P2 is granted
Current Available = old Available + Allocation
= (1,5.3,2) + (1,3,5,4) = (2,8,8,6)
Process P3
Need  Available
(0, 0, 2,0)  (2,8,8,6)
P3 is granted
Current Available = old Available + Allocation
= (2,8.8,6) + (0,6,3,2) = (2,14,11,8)

Process P4
Need  Available
(0,6,4,2)  (2,14,11,8)
P4 is granted
Current Available = old Available + Allocation
= (2,14.11,8) + (0,0,1,4) = (2,14,12,12)

Process P1
Need  Available
(0,7,5,0)  (2,14,12,12)
P1 is granted
Current Available = old Available + Allocation
= (2,14.12,12) + (1,0,0,0) = (3,14,12,12)
The System is in the Safe State in the Sequence <P0,P2,P3,P4,P1>
Page 36 of 44
c) Request  Need
(0, 4, 2,0)  (0,7,5,0)
Request  Available
(0, 4, 2,0)  (1,5,2,0)
Request will be granted and satisfied

Available = Available – Request


=1,5,2,0 – 0,4,2,0 = 1,1,0,0
Need = Need – Request
=0,7,5,0 – 0,4,2,0 = 0,3,3,0
Allocation = Allocation + Request
= 0,7,5,0 + 0,4,2,0 = 1,4,2,0

Process Allocation MAX Available


A B C D A B C D A B C D
P0 0 0 1 2 0 0 1 2 1 1 0 0
P1 1 4 2 0 1 7 5 0

P2 1 3 5 4 2 3 5 6
P3 0 6 3 2 0 6 5 2
P4 0 0 1 4 0 6 5 6

a) Need = MAX - Allocation

Process Need
A B C D
P0 0 0 0 0
P1 0 3 3 0
P2 1 0 0 2
P3 0 0 2 0
P4 0 6 4 2

Page 37 of 44
b) Currently the system is safe sequence

Process P0
Need  Available
(0,0,0,0)  (1,1,0,0)
P0 is granted
Current Available = old Available + Allocation
= (1,1,0,0) + (0,0,1,2) = (1,1,1,2)

Process P1
Need  Available
(0,3,3,0)  (1,1,1,2)
P1 is not granted

Process P2
Need  Available
(1,0,0,2)  (1,1,1,2)
P2 is granted
Current Available = old Available + Allocation
= (1,1.1,2) + (1,3,5,4) = (2,4,6,6)

Process P3
Need  Available
(0, 0, 2,0)  (2,4,6,6)
P3 is granted
Current Available = old Available + Allocation
= (2,4.4,6) + (0,6,3,2) = (2,10,9,8)

Process P4
Need  Available
(0,6,4,2)  (2,10,9,8)
P4 is granted
Page 38 of 44
Current Available = old Available + Allocation
= (2,10.9,8) + (0,0,1,4) = (2,10,10,12)
Process P1
Need  Available
(0,3,3,0)  (2,10,10,12)
P1 is granted
Current Available = old Available + Allocation
= (2,10.10,12) + (1,4,2,0) = (3,14,12,12)

<P0,P2,P3,P4,P1>
The Safe Sequence is exist so request P1(0,4,2,0) is granted immediately

_______________________________________________________________________________________________________

Example of context switch

 Assumes the process is of size 1MB and the backing store is a standard hard disk
with a transfer rate of 5 MB per second.
 The actual transfer of 1 MB process to or from memory takes:

1000 KB / 5000 KB per second = 1 / 5 second = 200 milliseconds.

_________________________________________________________________________________________________________

Page 39 of 44
Contiguous Memory Allocation
Describe the following allocation algorithms: a. First fit b. Best fit c. Worst fit
a. First-fit:
Search the list of available memory and allocate the first block that is big enough.
b. Best-fit:
Search the entire list of available memory and allocate the smallest block that is big enough.
c. Worst-fit:
Search the entire list of available memory and allocate the largest block.
(The justification for this scheme is that the leftover block produced would be larger and
potentially more useful than that produced by the best-fit approach.)

Problem: 1
Given six memory partitions of 300KB, 600KB, 350KB, 200KB, 750KB and 125KB (in
order), how would each of the First-fit, Best-fit, and Worst-fit algorithms place
processes of 115KB, 500KB, 358KB, 200KB and 375KB (in order)? Which algorithm
makes the most efficient use of memory?
Solution:
First-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
P1 300KB- 115KB = 185KB
P5: 375KB
P2 600KB- 500KB = 100KB

P4 350KB- 200KB = 150KB

200KB

P3 750KB - 358KB = 392KB

125KB

Fragmentation:= 185 + 100 + 150 + 200 + 92 + 125


Fragmentation = 1152 KB

Page 40 of 44
Best-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
P5: 375KB 300KB

P2 600KB- 500KB = 100KB

350KB

P4 200KB- 200KB = 0KB

P3 750KB- 358KB = 392KB

P1 125KB- 115KB = 10KB

Fragmentation = 300 + 100 + 350 + 392 + 10


Fragmentation = 1152 KB

Worst-fit:
P1: 115KB
P2: 500KB
P3: 358KB
P4: 200KB
300KB
P5: 375KB
P2 600KB- 500KB = 100KB

P4 350KB- 200KB = 150KB

200KB

P1 750KB- 115KB = 635KB

125KB

Fragmentation = 300 + 100 + 150 + 200 +635 + 125


Fragmentation = 1510 KB
Note: In this example, Best-fit turns out to be the best.

Page 41 of 44
Problem: 2
Given memory partitions of 100K, 500K, 200K, 300K, and 600K (in order), how
would each of the First-fit, Best-fit, and Worst-fit algorithms place processes of 212K,
417K, 112K, and 426K (in order)? Which algorithm makes the most efficient use of
memory?
Solution:
First-fit:

P1: 212K
P2: 417K 100K
P3: 112K P1 500K – 212K =288K
P4: 426K
P3 200K - 112K = 88K

300K

P2 600K- 426K = 174K

Fragmentation = 100 + 288 + 88 + 300 + 174


Fragmentation = 950 K
Best-fit:
P1: 212K
P2: 417K
P3: 112K
P4: 426K

100K

P2 500K – 417K = 83K

P3 200K - 112K = 88K

P1 300K – 212K = 88k

P4 600K- 426K = 174K

Fragmentation = 100 + 83 + 88 + 88 + 174


Fragmentation = 533 K

Page 42 of 44
Worst-fit:
P1: 212K
P2: 417K
P3: 112K 100K
P4: 426K
P2 500K – 426K =74K

200K

P3 300K – 112K = 188k

P1 600K- 417K = 183K

Fragmentation = 100 + 74 + 200 + 188 + 183


Fragmentation = 745K
Note: In this example, Best-fit turns out to be the best.

Page 43 of 44
Alternative Method: Problem: 3 Given memory partitions of 100K, 500K, 200K,
300K and 600K (in order) how would each of the first fit, best fit and worst fit
algorithms place processes of 212K, 417K, 112K and 426K (in order) ? Which
algorithm makes the most efficient use of memory.
Solution:
1. First fit:

Memory 100K 500K 200K 300K 600K


Partitions

Processes 212K 417K 112K 426K

No memory
partitions

2.Worst fit:

Memory 100K 500K 200K 300K 600K


Partitions

Processes 212K 417K 112K 426K

No memory
partitions

3. Best fit:

Memory 100K 500K 200K 300K 600K


Partitions

Processes 212K 417K 112K 426K

Page 44 of 44

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy