Questions-1. Consider the following set of processes, with the length of the CPU burst time given in milliseconds.
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 8 | 5 |
| P2 | 3 | 3 |
| P3 | 1 | 4 |
| P4 | 4 | 7 |
| P5 | 2 | 6 |
The processes are assumed to have arrived in the order P1, P2, P3, P4 and P5 and all of them have arrived at time 0.
a. Draw corresponding Gantt charts illustrating the execution of these processes using FCFS, SJF, SRTN, Priority based scheduling/even-driven scheduling and Round Robin (quantum=2) scheduling algorithms.
b. What is the turnaround time of each process for each of the scheduling algorithm in (a)?
c. What is the waiting time of each process for each of the scheduling algorithm in (a)?
d. Which of the schedules in (a) results in minimal average waiting time (overall processes)?
Answer :
Process Information
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 8 | 5 |
| P2 | 3 | 3 |
| P3 | 1 | 4 |
| P4 | 4 | 7 |
| P5 | 2 | 6 |
(a) Gantt Charts
1. First Come First Serve (FCFS) Scheduling
In FCFS, processes are scheduled in the order they arrive.
- Gantt Chart:
| Time | 0 - 8 | 8 - 11 | 11 - 12 | 12 - 16 | 16 - 18 |
|---|---|---|---|---|---|
| P1 | P2 | P3 | P4 | P5 |
2. Shortest Job First (SJF) Scheduling
In SJF, the process with the shortest burst time is executed next. All processes arrive at time 0, so we sort them by burst time.
Order of Execution: <math xmlns="http://www.w3.org/1998/Math/MathML">
</math>P 3 → P 5 → P 2 → P 4 → P 1 P3 \rightarrow P5 \rightarrow P2 \rightarrow P4 \rightarrow P1 Gantt Chart:
| Time | 0 - 1 | 1 - 3 | 3 - 6 | 6 - 10 | 10 - 18 |
|---|---|---|---|---|---|
| P3 | P5 | P2 | P4 | P1 |
3. Shortest Remaining Time Next (SRTN) Scheduling
SRTN is a preemptive version of SJF. Whenever a new process arrives, it can preempt the running process if it has a shorter remaining time.
Execution: <math xmlns="http://www.w3.org/1998/Math/MathML">
</math>P 3 → P 5 → P 2 → P 4 → P 1 P3 \rightarrow P5 \rightarrow P2 \rightarrow P4 \rightarrow P1 - P3 (executes fully)
- P5 (executes fully)
- P2 (executes fully)
- P4 (executes fully)
- P1 (executes fully)
Gantt Chart:
| Time | 0 - 1 | 1 - 3 | 3 - 6 | 6 - 10 | 10 - 18 |
|---|---|---|---|---|---|
| P3 | P5 | P2 | P4 | P1 |
4. Priority-Based Scheduling
In priority scheduling, the process with the highest priority (lowest number) is selected first.
Order of Execution (based on priority): <math xmlns="http://www.w3.org/1998/Math/MathML">
</math>P 2 → P 3 → P 5 → P 1 → P 4 P2 \rightarrow P3 \rightarrow P5 \rightarrow P1 \rightarrow P4 Gantt Chart:
| Time | 0 - 3 | 3 - 4 | 4 - 6 | 6 - 14 | 14 - 18 |
|---|---|---|---|---|---|
| P2 | P3 | P5 | P1 | P4 |
5. Round Robin Scheduling (Quantum = 2)
In Round Robin, processes are given a fixed time quantum (2 ms here). If a process isn't finished within that quantum, it's moved to the back of the queue.
- Order of Execution (Rotating based on time quantum of 2):
| Time | 0 - 2 | 2 - 4 | 4 - 5 | 5 - 7 | 7 - 9 | 9 - 10 | 10 - 12 | 12 - 14 | 14 - 16 | 16 - 18 |
|---|---|---|---|---|---|---|---|---|---|---|
| P1 | P2 | P3 | P4 | P5 | P1 | P2 | P4 | P1 | P1 |
(b) Turnaround Time (TAT)
Turnaround time is calculated as:
<math xmlns="http://www.w3.org/1998/Math/MathML">
- For each scheduling algorithm, calculate when each process finishes.
1. FCFS:
| Process | Completion Time | Turnaround Time |
|---|---|---|
| P1 | 8 | 8 |
| P2 | 11 | 11 |
| P3 | 12 | 12 |
| P4 | 16 | 16 |
| P5 | 18 | 18 |
2. SJF:
| Process | Completion Time | Turnaround Time |
|---|---|---|
| P1 | 18 | 18 |
| P2 | 6 | 6 |
| P3 | 1 | 1 |
| P4 | 10 | 10 |
| P5 | 3 | 3 |
3. SRTN:
| Process | Completion Time | Turnaround Time |
|---|---|---|
| P1 | 18 | 18 |
| P2 | 6 | 6 |
| P3 | 1 | 1 |
| P4 | 10 | 10 |
| P5 | 3 | 3 |
4. Priority-Based:
| Process | Completion Time | Turnaround Time |
|---|---|---|
| P1 | 14 | 14 |
| P2 | 3 | 3 |
| P3 | 4 | 4 |
| P4 | 18 | 18 |
| P5 | 6 | 6 |
5. Round Robin (Quantum = 2):
| Process | Completion Time | Turnaround Time |
|---|---|---|
| P1 | 18 | 18 |
| P2 | 12 | 12 |
| P3 | 5 | 5 |
| P4 | 14 | 14 |
| P5 | 9 | 9 |
(c) Waiting Time (WT)
Waiting time is calculated as:
<math xmlns="http://www.w3.org/1998/Math/MathML">
1. FCFS:
| Process | Burst Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 8 | 8 | 0 |
| P2 | 3 | 11 | 8 |
| P3 | 1 | 12 | 11 |
| P4 | 4 | 16 | 12 |
| P5 | 2 | 18 | 16 |
2. SJF:
| Process | Burst Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 8 | 18 | 10 |
| P2 | 3 | 6 | 3 |
| P3 | 1 | 1 | 0 |
| P4 | 4 | 10 | 6 |
| P5 | 2 | 3 | 1 |
3. SRTN:
| Process | Burst Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 8 | 18 | 10 |
| P2 | 3 | 6 | 3 |
| P3 | 1 | 1 | 0 |
| P4 | 4 | 10 | 6 |
| P5 | 2 | 3 | 1 |
4. Priority-Based:
| Process | Burst Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 8 | 14 | 6 |
| P2 | 3 | 3 | 0 |
| P3 | 1 | 4 | 3 |
| P4 | 4 | 18 | 14 |
| P5 | 2 | 6 | 4 |
5. Round Robin (Quantum = 2):
| Process | Burst Time | Turnaround Time | Waiting Time |
|---|---|---|---|
| P1 | 8 | 18 | 10 |
| P2 | 3 | 12 | 9 |
| P3 | 1 | 5 | 4 |
| P4 | 4 | 14 | 10 |
| P5 | 2 | 9 | 7 |
(d) Minimal Average Waiting Time
- To find the minimal average waiting time, we compute the average for each algorithm:
1. FCFS:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">2. SJF:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">3. SRTN:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">4. Priority-Based:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">5. Round Robin:
<math xmlns="http://www.w3.org/1998/Math/MathML" display="block">Questions -2. Consider the following page-reference string: 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
How many page faults would occur for the following replacement algorithms, assuming three frames? Remember all frames are initially empty. a. FIFO replacement b. LRU replacement c. Optimal Mention the merits and demerits of each of the above algorithms.
Answer :
To determine the number of page faults for the given page-reference string using three different page replacement algorithms (FIFO, LRU, and Optimal), let's analyze each algorithm step by step.
Page-Reference String
1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
a. FIFO (First-In, First-Out) Replacement
In FIFO, the oldest page is replaced first.
- Frames: Initially empty:
[] - Step by Step:
- Page 1: Fault →
[1] - Page 2: Fault →
[1, 2] - Page 3: Fault →
[1, 2, 3] - Page 4: Fault →
[2, 3, 4](1 replaced) - Page 2: Hit →
[2, 3, 4] - Page 1: Fault →
[3, 4, 1](2 replaced) - Page 5: Fault →
[4, 1, 5](3 replaced) - Page 6: Fault →
[1, 5, 6](4 replaced) - Page 2: Fault →
[5, 6, 2](1 replaced) - Page 1: Fault →
[6, 2, 1](5 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 7: Fault →
[1, 3, 7](2 replaced) - Page 6: Fault →
[3, 7, 6](1 replaced) - Page 3: Hit →
[3, 7, 6] - Page 2: Fault →
[7, 6, 2](3 replaced) - Page 1: Fault →
[6, 2, 1](7 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 6: Fault →
[1, 3, 6](2 replaced)
- Page 1: Fault →
Total Page Faults (FIFO): 12
b. LRU (Least Recently Used) Replacement
In LRU, the least recently used page is replaced.
- Frames: Initially empty:
[] - Step by Step:
- Page 1: Fault →
[1] - Page 2: Fault →
[1, 2] - Page 3: Fault →
[1, 2, 3] - Page 4: Fault →
[2, 3, 4](1 replaced) - Page 2: Hit →
[2, 3, 4] - Page 1: Fault →
[3, 4, 1](2 replaced) - Page 5: Fault →
[4, 1, 5](3 replaced) - Page 6: Fault →
[1, 5, 6](4 replaced) - Page 2: Fault →
[5, 6, 2](1 replaced) - Page 1: Fault →
[6, 2, 1](5 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 7: Fault →
[1, 3, 7](2 replaced) - Page 6: Fault →
[3, 7, 6](1 replaced) - Page 3: Hit →
[3, 7, 6] - Page 2: Fault →
[7, 6, 2](3 replaced) - Page 1: Fault →
[6, 2, 1](7 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 6: Fault →
[1, 3, 6](2 replaced)
- Page 1: Fault →
Total Page Faults (LRU): 12
c. Optimal Replacement
In Optimal, the page that will not be used for the longest time in the future is replaced.
- Frames: Initially empty:
[] - Step by Step:
- Page 1: Fault →
[1] - Page 2: Fault →
[1, 2] - Page 3: Fault →
[1, 2, 3] - Page 4: Fault →
[2, 3, 4](1 replaced; will not be used again) - Page 2: Hit →
[2, 3, 4] - Page 1: Fault →
[3, 4, 1](2 replaced; used in future) - Page 5: Fault →
[4, 1, 5](3 replaced; used in future) - Page 6: Fault →
[1, 5, 6](4 replaced) - Page 2: Fault →
[5, 6, 2](1 replaced; used in future) - Page 1: Fault →
[6, 2, 1](5 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 7: Fault →
[1, 3, 7](2 replaced) - Page 6: Fault →
[3, 7, 6](1 replaced) - Page 3: Hit →
[3, 7, 6] - Page 2: Fault →
[7, 6, 2](3 replaced) - Page 1: Fault →
[6, 2, 1](7 replaced) - Page 2: Hit →
[6, 2, 1] - Page 3: Fault →
[2, 1, 3](6 replaced) - Page 6: Fault →
[1, 3, 6](2 replaced)
- Page 1: Fault →
Total Page Faults (Optimal): 9
Summary of Page Faults
- FIFO: 12 faults
- LRU: 12 faults
- Optimal: 9 faults
Merits and Demerits
FIFO (First-In, First-Out)
- Merits:
- Simple to implement and understand.
- No need for future knowledge of references.
- Demerits:
- Can lead to Belady's anomaly (more frames can cause more faults).
- Does not consider page usage frequency.
LRU (Least Recently Used)
- Merits:
- Better performance than FIFO in many cases.
- Intuitive, as it considers the recent usage pattern.
- Demerits:
- More complex to implement, requiring tracking of page usage.
- High overhead due to the need to maintain usage order.
Optimal
- Merits:
- Provides the best possible performance (fewest page faults).
- Useful as a benchmark for evaluating other algorithms.
- Demerits:
- Requires future knowledge of reference strings, which is impractical in real systems.
- Not implementable in a standard operating environment.
Questions-4. Write a program in C to implement Banker’s Algorithm to avoid Deadlock. Also explain the code briefly.
<!--[if gte mso 9]><xml>