0% found this document useful (0 votes)
56 views32 pages

Ilovepdf Merged

Uploaded by

2303A54054
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)
56 views32 pages

Ilovepdf Merged

Uploaded by

2303A54054
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/ 32

First Come First Serve | CPU

Scheduling
Operating System

FCFS Scheduling-

In FCFS Scheduling,
• The process which arrives first in the ready queue is firstly assigned the CPU.
• In case of a tie, process with smaller process id is executed first.
• It is always non-preemptive in nature.

Advantages-

• It is simple and easy to understand.


• It can be easily implemented using queue data structure.
• It does not lead to starvation.

Disadvantages-

• It does not consider the priority or burst time of the processes.


• It suffers from convoy effect.

Convoy Effect
In convoy effect,
• Consider processes with higher burst time arrived before the processes with smaller burst time.
• Then, smaller processes have to wait for a long time for longer processes to release the CPU.

PRACTICE PROBLEMS BASED ON FCFS


SCHEDULING-

Problem-01:

Consider the set of 5 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time

P1 3 4

P2 5 3

P3 0 2

P4 5 1

P5 4 3

If the CPU scheduling policy is FCFS, calculate the average waiting time and
average turn around time.

Solution-

Gantt Chart-

Here, black box represents the idle time of CPU.

Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time
Also read- Various Times of Process

Process Id Exit time Turn Around time Waiting time

P1 7 7–3=4 4–4=0

P2 13 13 – 5 = 8 8–3=5

P3 2 2–0=2 2–2=0

P4 14 14 – 5 = 9 9–1=8

P5 10 10 – 4 = 6 6–3=3

Now,
• Average Turn Around time = (4 + 8 + 2 + 9 + 6) / 5 = 29 / 5 = 5.8 unit
• Average waiting time = (0 + 5 + 0 + 8 + 3) / 5 = 16 / 5 = 3.2 unit

Problem-02:

Consider the set of 3 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time

P1 0 2

P2 3 1

P3 5 6
If the CPU scheduling policy is FCFS, calculate the average waiting time and
average turn around time.

Solution-

Gantt Chart-

Here, black box represents the idle time of CPU.

Now, we know-
• Turn Around time = Exit time – Arrival time
• Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time

P1 2 2–0=2 2–2=0

P2 4 4–3=1 1–1=0

P3 11 11- 5 = 6 6–6=0

Now,
• Average Turn Around time = (2 + 1 + 6) / 3 = 9 / 3 = 3 unit
• Average waiting time = (0 + 0 + 0) / 3 = 0 / 3 = 0 unit
Problem-03:

Consider the set of 6 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time

P1 0 3

P2 1 2

P3 2 1

P4 3 4

P5 4 5

P6 5 2

If the CPU scheduling policy is FCFS and there is 1 unit of overhead in scheduling
the processes, find the efficiency of the algorithm.

Solution-

Gantt Chart-
Here, δ denotes the context switching overhead.

Now,
• Useless time / Wasted time = 6 x δ = 6 x 1 = 6 unit
• Total time = 23 unit
• Useful time = 23 unit – 6 unit = 17 unit

Efficiency (η)
= Useful time / Total Total
= 17 unit / 23 unit
= 0.7391
= 73.91%
Contents
Practical Problems Based On FCFS Scheduling ........................................................................... 2
Problem-01:............................................................................................................................. 2
Problem-02: ............................................................................................................................ 3
Problem-03: ............................................................................................................................ 4
Practice Problems Based On SJF Scheduling ............................................................................... 5
Problem-01:............................................................................................................................. 5
Problem-02: ............................................................................................................................ 6
Problem-03: ............................................................................................................................ 7
Problem-04: ............................................................................................................................ 8
Problem-05: ............................................................................................................................ 9
Practice Problems Based On Round Robin Scheduling ............................................................. 10
Problem-01:........................................................................................................................... 10
Problem-02: .......................................................................................................................... 11
Problem-03: .......................................................................................................................... 12
Problem-04: .......................................................................................................................... 13
Practice Problems Based On Priority Scheduling ...................................................................... 14
Problem-01:........................................................................................................................... 14
Problem-02: .......................................................................................................................... 15
Practice Problems Based On CPU Scheduling Algorithms......................................................... 16
Problem-01:........................................................................................................................... 16
Problem-02: .......................................................................................................................... 17
Problem-03: .......................................................................................................................... 18

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 1
Practical Problems Based On FCFS Scheduling

Problem-01:

Consider the set of 5 processes whose arrival time and burst time are given below-

Process ID Arrival Time Burst Time


P1 3 4
P2 5 3
P3 0 2
P4 5 1
P5 4 3
If the CPU scheduling policy is FCFS, calculate the average waiting time and average
turnaround time.

Solution:

Gantt Chart-

Here, black box represents the idle time of CPU.

Now, we know

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 7 7–3=4 4–4=0
P2 13 13 – 5 = 8 8–3=5
P3 2 2–0=2 2–2=0
P4 14 14 – 5 = 9 9–1=8
P5 10 10 – 4 = 6 6–3=3

Now,

Average Turn Around time = (4 + 8 + 2 + 9 + 6) / 5 = 29 / 5 = 5.8 unit

Average waiting time = (0 + 5 + 0 + 8 + 3) / 5 = 16 / 5 = 3.2 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 2
Problem-02:
Consider the set of 3 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 2
P2 3 1
P3 5 6

If the CPU scheduling policy is FCFS, calculate the average waiting time and average
turnaround time.

Solution-

Gantt Chart-

Here, black box represents the idle time of CPU.

Now, we know

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 2 2–0=2 2–2=0
P2 4 4–3=1 1–1=0
P3 11 11- 5 = 6 6–6=0
Now,

Average Turn Around time = (2 + 1 + 6) / 3 = 9 / 3 = 3 unit

Average waiting time = (0 + 0 + 0) / 3 = 0 / 3 = 0 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 3
Problem-03:
Consider the set of 6 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 3
P2 1 2
P3 2 1
P4 3 4
P5 4 5
P6 5 2

If the CPU scheduling policy is FCFS and there is 1 unit of overhead in scheduling the
processes, find the efficiency of the algorithm.

Solution-
Gantt Chart-

Here, δ denotes the context switching overhead.


Now,
Useless time / Wasted time = 6 x δ = 6 x 1 = 6 unit
Total time = 23 unit
Useful time = 23 unit – 6 unit = 17 unit

Efficiency (η)
= Useful time / Total
= 17 unit / 23 unit
= 0.7391
= 73.91%

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 4
Practice Problems Based On SJF Scheduling

Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3

If the CPU scheduling policy is SJF non-pre-emptive, calculate the average waiting time and
average turnaround time.

Solution-

Gantt Chart-

Now, we know-

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 7 7–3=4 4–1=3
P2 16 16 – 1 = 15 15 – 4 = 11
P3 9 9–4=5 5–2=3
P4 6 6–0=6 6–6=0
P5 12 12 – 2 = 10 10 – 3 = 7

Now,

Average Turn Around time = (4 + 15 + 5 + 6 + 10) / 5 = 40 / 5 = 8 unit

Average waiting time = (3 + 11 + 3 + 0 + 7) / 5 = 24 / 5 = 4.8 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 5
Problem-02:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 3 1
P2 1 4
P3 4 2
P4 0 6
P5 2 3

If the CPU scheduling policy is SJF pre-emptive, calculate the average waiting time and
average turnaround time.

Solution-

Gantt Chart-

Now, we know-

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 4 4–3=1 1–1=0
P2 6 6–1=5 5–4=1
P3 8 8–4=4 4–2=2
P4 16 16 – 0 = 16 16 – 6 = 10
P5 11 11 – 2 = 9 9–3=6

Now,

Average Turn Around time = (1 + 5 + 4 + 16 + 9) / 5 = 35 / 5 = 7 unit

Average waiting time = (0 + 1 + 2 + 10 + 6) / 5 = 19 / 5 = 3.8 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 6
Problem-03:
Consider the set of 6 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 7
P2 1 5
P3 2 3
P4 3 1
P5 4 2
P6 5 1

If the CPU scheduling policy is shortest remaining time first, calculate the average waiting time
and average turnaround time.

Solution-

Gantt Chart-

Now, we know-

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 19 19 – 0 = 19 19 – 7 = 12
P2 13 13 – 1 = 12 12 – 5 = 7
P3 6 6–2=4 4–3=1
P4 4 4–3=1 1–1=0
P5 9 9–4=5 5–2=3
P6 7 7–5=2 2–1=1

Now,

Average Turn Around time = (19 + 12 + 4 + 1 + 5 + 2) / 6 = 43 / 6 = 7.17 unit

Average waiting time = (12 + 7 + 1 + 0 + 3 + 1) / 6 = 24 / 6 = 4 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 7
Problem-04:
Consider the set of 3 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 9
P2 1 4
P3 2 9

If the CPU scheduling policy is SRTF, calculate the average waiting time and average
turnaround time.

Solution-

Gantt Chart-

Now, we know-

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 13 13 – 0 = 13 13 – 9 = 4
P2 5 5–1=4 4–4=0
P3 22 22- 2 = 20 20 – 9 = 11

Now,

Average Turn Around time = (13 + 4 + 20) / 3 = 37 / 3 = 12.33 unit

Average waiting time = (4 + 0 + 11) / 3 = 15 / 3 = 5 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 8
Problem-05:
Consider the set of 4 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 20
P2 15 25
P3 30 10
P4 45 15

If the CPU scheduling policy is SRTF, calculate the waiting time of process P2.

Solution-

Gantt Chart-

Now, we know-

Turn Around time = Exit time – Arrival time

Waiting time = Turn Around time – Burst time

Thus,

Turn Around Time of process P2 = 55 – 15 = 40 unit

Waiting time of process P2 = 40 – 25 = 15 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 9
Practice Problems Based On Round Robin Scheduling

Problem-01:

Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time


P1 0 5
P2 1 3
P3 2 1
P4 3 2
P5 4 3

If the CPU scheduling policy is Round Robin with time quantum = 2 unit, calculate the average
waiting time and average turnaround time.

Solution-

Gantt Chart-

Ready Queue-
P5, P1, P2, P5, P4, P1, P3, P2, P1

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 13 13 – 0 = 13 13 – 5 = 8
P2 12 12 – 1 = 11 11 – 3 = 8
P3 5 5–2=3 3–1=2
P4 9 9–3=6 6–2=4
P5 14 14 – 4 = 10 10 – 3 = 7

Now,
Average Turn Around time = (13 + 11 + 3 + 6 + 10) / 5 = 43 / 5 = 8.6 unit
Average waiting time = (8 + 8 + 2 + 4 + 7) / 5 = 29 / 5 = 5.8 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 10
Problem-02:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 0 4
P2 1 5
P3 2 2
P4 3 1
P5 4 6
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 2, calculate the average
waiting time and average turnaround time.

Solution-
Gantt chart-
Ready Queue-
P5, P6, P2, P5, P6, P2, P5, P4, P1, P3, P2, P1

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 8 8–0=8 8–4=4
P2 18 18 – 1 = 17 17 – 5 = 12
P3 6 6–2=4 4–2=2
P4 9 9–3=6 6–1=5
P5 21 21 – 4 = 17 17 – 6 = 11
P6 19 19 – 6 = 13 13 – 3 = 10

Now,
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

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 11
Problem-03:
Consider the set of 6 processes whose arrival time and burst time are given below-
Process Id Arrival time Burst time
P1 5 5
P2 4 6
P3 3 7
P4 1 9
P5 2 2
P6 6 3
If the CPU scheduling policy is Round Robin with time quantum = 3, calculate the average
waiting time and average turnaround time.

Solution-
Gantt chart-
Ready Queue-
P3, P1, P4, P2, P3, P6, P1, P4, P2, P3, P5, P4

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 32 32 – 5 = 27 27 – 5 = 22
P2 27 27 – 4 = 23 23 – 6 = 17
P3 33 33 – 3 = 30 30 – 7 = 23
P4 30 30 – 1 = 29 29 – 9 = 20
P5 6 6–2=4 4–2=2
P6 21 21 – 6 = 15 15 – 3 = 12

Now,
Average Turn Around time = (27 + 23 + 30 + 29 + 4 + 15) / 6 = 128 / 6 = 21.33 unit
Average waiting time = (22 + 17 + 23 + 20 + 2 + 12) / 6 = 96 / 6 = 16 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 12
Problem-04:
Four jobs to be executed on a single processor system arrive at time 0 in the order A, B, C, D.
Their burst CPU time requirements are 4, 1, 8, 1 time units respectively. The completion time
of A under round robin scheduling with time slice of one-time unit is-
10
4
8
9

Solution-

Process Id Arrival time Burst time


A 0 4
B 0 1
C 0 8
D 0 1

Gantt chart-

Ready Queue-
C, A, C, A, C, A, D, C, B, A

Clearly, completion time of process A = 9 unit.


Thus, Option (D) is correct.

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 13
Practice Problems Based On Priority Scheduling

Problem-01:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5

If the CPU scheduling policy is priority non-pre-emptive, calculate the average waiting time and
average turnaround time. (Higher number represents higher priority)

Solution-

Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 4 4–0=4 4–4=0
P2 15 15 – 1 = 14 14 – 3 = 11
P3 12 12 – 2 = 10 10 – 1 = 9
P4 9 9–3=6 6–5=1
P5 11 11 – 4 = 7 7–2=5

Now,
Average Turn Around time = (4 + 14 + 10 + 6 + 7) / 5 = 41 / 5 = 8.2 unit
Average waiting time = (0 + 11 + 9 + 1 + 5) / 5 = 26 / 5 = 5.2 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 14
Problem-02:
Consider the set of 5 processes whose arrival time and burst time are given below-

Process Id Arrival time Burst time Priority


P1 0 4 2
P2 1 3 3
P3 2 1 4
P4 3 5 5
P5 4 2 5

If the CPU scheduling policy is priority pre-emptive, calculate the average waiting time and
average turnaround time. (Higher number represents higher priority)

Solution-

Gantt Chart-

Now, we know-
Turn Around time = Exit time – Arrival time
Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 15 15 – 0 = 15 15 – 4 = 11
P2 12 12 – 1 = 11 11 – 3 = 8
P3 3 3–2=1 1–1=0
P4 8 8–3=5 5–5=0
P5 10 10 – 4 = 6 6–2=4

Now,
Average Turn Around time = (15 + 11 + 1 + 5 + 6) / 5 = 38 / 5 = 7.6 unit
Average waiting time = (11 + 8 + 0 + 0 + 4) / 5 = 23 / 5 = 4.6 unit

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 15
Practice Problems Based On CPU Scheduling Algorithms

Problem-01:
Consider three process, all arriving at time zero, with total execution time of 10, 20 and 30
units respectively. Each process spends the first 20% of execution time doing I/O, the next 70%
of time doing computation, and the last 10% of time doing I/O again. The operating system
uses a shortest remaining compute time first scheduling algorithm and schedules a new process
either when the running process gets blocked on I/O or when the running process finishes its
compute burst. Assume that all I/O operations can be overlapped as much as possible. For what
percentage of does the CPU remain idle?

1. 0%
2. 10.6%
3. 30.0%
4. 89.4%
Solution-

According to question, we have-

Total Burst Time I/O Burst CPU Burst I/O Burst

Process P1 10 2 7 1

Process P2 20 4 14 2

Process P3 30 6 21 3

The scheduling algorithm used is Shortest Remaining Time First.

Percentage of time CPU remains idle

= (5 / 47) x 100

= 10.638%

Thus, Option (B) is correct.

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 16
Problem-02:
Consider the set of 4 processes whose arrival time and burst time are given below-

Process No. Arrival Time Burst Time


CPU Burst I/O Burst CPU Burst
P1 0 3 2 2
P2 0 2 4 1
P3 2 1 3 2
P4 5 2 2 1

If the CPU scheduling policy is Shortest Remaining Time First, calculate the average waiting time
and average turnaround time.

Solution-

Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 11 11 – 0 = 11 11 – (3+2) = 6
P2 7 7–0=7 7 – (2+1) = 4
P3 9 9–2=7 7 – (1+2) = 4
P4 16 16 – 5 = 11 11 – (2+1) = 8

Now,
 Average Turn Around time = (11 + 7 + 7 + 11) / 4 = 36 / 4 = 9 units
 Average waiting time = (6 + 4 + 4 + 8) / 4 = 22 / 5 = 4.4 units

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 17
Problem-03:
Consider the set of 4 processes whose arrival time and burst time are given below-

Process No. Arrival Priority Burst Time


Time CPU Burst I/O Burst CPU Burst
P1 0 2 1 5 3
P2 2 3 3 3 1
P3 3 1 2 3 1

If the CPU scheduling policy is Priority Scheduling, calculate the average waiting time and
average turnaround time. (Lower number means higher priority)

Solution-

The scheduling algorithm used is Priority Scheduling.

Gantt Chart-

Now, we know-
 Turn Around time = Exit time – Arrival time
 Waiting time = Turn Around time – Burst time

Process Id Exit time Turn Around time Waiting time


P1 10 10 – 0 = 10 10 – (1+3) = 6
P2 15 15 – 2 = 13 13 – (3+1) = 9
P3 9 9–3=6 6 – (2+1) = 3

Now,
 Average Turn Around time = (10 + 13 + 6) / 3 = 29 / 3 = 9.67 units
 Average waiting time = (6 + 9 + 3) / 3 = 18 / 3 = 6 units

MCA 304 and MSc IT 301 Operating System – CPU Scheduling Algorithm (Problem) 18
Operating System

PROBLEMS USING BANKERS ALGORTIHM

Q1.

Process Allocation Max


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

Available
A B C D
2 5 3 2

A) Write the need matrix


B) Is the system safe? If yes write the sequence.
Need = Max – Allocation

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

The System is in safe state. Sequence is < P0 P1 P2 P3 P4 >


need <= work
P0 --> 0110 <= 2532
w = 2532 + 0212 = 2744
P1 --> 1650 <= 2744
w = 2744 + 1102 = 3846
Operating System

P2 --> 0122 <= 3846


w = 3846 + 2254 = 510910
P3 --> 1330 <= 510910
w = 510910 + 0312 = 5131012
P4 --> 1244 <= 5131012
w = 5131012 + 2414 = 7171116
Q2.

Process Allocation Max


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

Available
A B C D
3 3 2 1

Given: A-12, B-12, C-8, D-10


A) What is the content of the need matrix
B) If a request from process P1 arrives for (1,1,0,0) can the request be granted?
C) If a request from process P4 (0,0,2,0) arrives, can it be granted?
Need = Max - Allocation

Process Need
A B C D
P0 2 2 1 1
P1 2 1 3 1
P2 0 2 1 3
P3 0 1 1 2
P4 2 2 3 3
Operating System

The system is in safe state. Safe Sequence is < P0 P3 P4 P1 P2 >


need <= work
P0 --> 2211 <= 3321
w = 3321 + 2001 = 5322
P1 --> 2131 <= 5322 ×
P2 -->0213 <= 5322 ×
P3 --> 0112 <= 5322
w = 5322 + 1312 = 6634
P4 --> 2233 <= 6634
w = 6634 + 1432 = 71066
P1 --> 2131 <= 71066
w = 71066 + 3121 = 101187
P4 --> 0213 <= 101187
w = 101187 + 2103 = 1212810
Request from P1 (1,1,0,0)
Request <= Need -> 1100 <= 2131
Request <= Available -> 1100 <= 3321
Allocation = Allocation + Request = 3121 + 1100 = 4221
Need = Need – Request = 2131 – 1100 = 1031
Available = Available – Request = 3321 – 1100 = 2221

Process Allocation Max Work Need


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

need <= work


P0 --> 2211 <= 2221
w = 2221 + 2001 = 4222
P1 --> 1031 <= 4222 ×
P2 --> 0213 <= 4222 ×
P3 --> 0112 <= 4222
w = 4222 + 1312 = 5534
P4 --> 2233 <= 5534
w = 5534 + 1432 = 6966
P1 --> 1031 <= 6966
w = 6966 + 4221 = 101187
P4 --> 0213 <= 101187
w = 101187 + 2103 = 1212810
Safe sequence is < P0 P3 P4 P1 P2 >
System continues to be in safe state. Request from P1 is granted.
Request from P4 (0,0,2,0)
Request <= Need -> 0020 <= 2233
Request <= Available -> 0020 <= 3321
Allocation = Allocation + Request = 1432 + 0020 = 1452
Need = Need – Request = 2233 – 0020 = 2213
Available = Available – Request = 3321 – 0020 = 3301

Process Allocation Max Work Need


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

need <= work


P0 --> 2211 <= 3301 ×
P1 --> 1031 <= 3301 ×
P2 -->0213 <= 3301 ×
P3 -->0112 <= 3301 ×
P4 --> 2213 <= 3301 ×
The request from P4 is not granted. The system is not in safe state.
Q3.

Process Allocation Max


A B C D A B C D
P0 0 1 1 2 0 0 1 2
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

Available
A B C D
1 5 2 0

A) What is the content of Need matrix?


B) Is the system in safe state?
C) If a request from process P1 (0,4,2,0) arrives, can it be granted?
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
Operating System

need <= work


P0 --> 0000 <= 1520
w = 1520 + 0012 = 1532
P1 --> 0750 <= 1532 ×
P2 --> 1002 <= 1532
w = 1532 + 1354 = 2886
P3 --> 0020 <= 2886
w = 2886 + 0632 = 214118
P4 --> 0642 <= 214118
w = 214118 + 0014 = 2141212
P1 --> 0750 <= 2141212
w = 2141212 + 1000 = 3141212
Request from Process P1 (0,4,2,0)
Request <= Need -> 0420 <= 0750
Request <= Work -> 0420 <= 1520
Work = Work – Request = 1520 – 0420 = 1100
Allocation = Allocation + Request = 1000 + 0420 = 1420
Need = Need – Request = 0750 – 0420 = 0330

Process Allocation Max Work Need


A B C D 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 0 0 0 0
P1 1 4 2 0 1 7 5 0 0 3 3 0
P2 1 3 5 4 2 3 5 6 1 0 0 2
P3 0 6 3 2 0 6 5 2 0 0 2 0
P4 0 0 1 4 0 6 5 6 0 6 4 2
need <= work
P0 --> 0000 <= 1100
w = 1100 + 0012 = 1112
Operating System

P1 --> 0330 <= 1112 ×


P2 --> 1002 <= 1112
w = 1112 + 1354 = 2466
P3 --> 0020 <= 2466
w = 2466 + 0632 = 21098
P4 --> 0642 <= 21098
w = 21098 + 0014 = 2101012
P1 --> 0750 <= 2101012
w = 2101012 + 1420 = 3141212
Sequence is < P0 P2 P3 P4 P1 >
The safe sequence is same, so the system continues to remain in a safe state.
The request P1 (0,4,2,0) can be granted.
Q5.

Process Allocation Max


A B C A B C
P0 0 1 0 7 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 0 4 3 3

Available
A B C
3 3 2

Need = Max – Allocation


Operating System

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

need <= work


P0 --> 743 <= 332 ×
P1 --> 122 <= 332
w = 332 + 200 = 532
P2 --> 600 <= 532 ×
P3 --> 011 <= 532
w = 532 + 211 = 743
P4 --> 433 <= 743
w = 743 + 000 = 743
P0 --> 743 <= 743
w = 743 + 010 = 753
P2 --> 600 <= 753
w = 753 + 302 = 1055
Safe sequence is < P1 P3 P4 P0 P2 >
The system is in safe state.

You might also like