0% found this document useful (0 votes)
27 views10 pages

Numerical Concepts - Mid Sem (Scheduling, Etc)

Uploaded by

Rohit Padile
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)
27 views10 pages

Numerical Concepts - Mid Sem (Scheduling, Etc)

Uploaded by

Rohit Padile
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/ 10

Notes - FOS

19 September 2024 19:37

Sir, short-term scheduling in Operating Systems refers to the scheduling of processes by the CPU scheduler, which
decides which process will be executed next by the CPU. It's also known as CPU scheduling. The goal is to maximize
CPU utilization and system efficiency.
Key Algorithms in Short-Term Scheduling:
1. First-Come, First-Served (FCFS): Processes are executed in the order they arrive. It's non-preemptive,
meaning once a process starts, it runs to completion.
2. Shortest Job Next (SJN) or Shortest Job First (SJF): The process with the smallest execution time is selected
next. It can be preemptive or non-preemptive.
○ Preemptive SJF (or Shortest Remaining Time First) allows interrupting a running process if a new
process with a shorter burst time arrives.
3. Round Robin (RR): Each process gets a small unit of CPU time (called a time quantum). After this time
expires, the process is preempted, and the next one is scheduled.
4. Priority Scheduling: Each process is assigned a priority. The CPU is assigned to the process with the highest
priority (or lowest number depending on priority numbering). It can also be preemptive or non-preemptive.
5. Multilevel Queue Scheduling: Processes are divided into queues based on priority, and each queue can have
its own scheduling algorithm.
6. Multilevel Feedback Queue Scheduling: This is more flexible than multilevel queue scheduling, as it allows
processes to move between queues based on their behavior.

Numerical Example (FCFS, SJF, RR)


Let’s say you have the following processes:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
1. First-Come, First-Served (FCFS)
• In FCFS, processes are executed in the order of their arrival time.
Gantt Chart:

Copy code
| P1 | P2 | P3 | P4 |
0 8 12 21 26
• Completion times:
P1 = 8, P2 = 12, P3 = 21, P4 = 26
• Turnaround Time (Completion Time - Arrival Time):
P1 = 8 - 0 = 8
P2 = 12 - 1 = 11
P3 = 21 - 2 = 19
P4 = 26 - 3 = 23
• Average Turnaround Time = (8 + 11 + 19 + 23) / 4 = 15.25
2. Shortest Job First (SJF) - Non-preemptive
• The process with the smallest burst time is executed next.
Order of execution:
• P1 arrives first, so it starts.
• After P1, the remaining processes are P2 (4), P3 (9), P4 (5). P2 has the smallest burst time, so it's next.

OS Page 1
• After P1, the remaining processes are P2 (4), P3 (9), P4 (5). P2 has the smallest burst time, so it's next.
• Then, P4 runs, and finally, P3.
Gantt Chart:

Copy code
| P1 | P2 | P4 | P3 |
0 8 12 17 26
• Turnaround times:
P1 = 8 - 0 = 8
P2 = 12 - 1 = 11
P3 = 26 - 2 = 24
P4 = 17 - 3 = 14
• Average Turnaround Time = (8 + 11 + 24 + 14) / 4 = 14.25
3. Round Robin (RR) - Time Quantum = 4
• Each process gets 4 units of CPU time in a round-robin fashion.
Gantt Chart:

Copy code
| P1 | P2 | P3 | P4 | P1 | P3 | P4 |
0 4 8 12 16 20 24 26
• Turnaround times:
P1 = 20 - 0 = 20
P2 = 8 - 1 = 7
P3 = 26 - 2 = 24
P4 = 26 - 3 = 23
• Average Turnaround Time = (20 + 7 + 24 + 23) / 4 = 18.5

Concepts to Focus On:


• Turnaround Time (TAT): Total time taken by a process from submission to completion.
○ TAT = Completion Time - Arrival Time.
• Waiting Time (WT): Time a process spends waiting in the ready queue.
○ WT = Turnaround Time - Burst Time.
• Response Time (RT): Time from when a process enters the ready queue until its first execution.
Understanding how to calculate these for different scheduling algorithms is crucial for numerical problems.
Would you like more examples or clarifications on any specific points, Sir?

No problem, Sir! Let me walk you through the Shortest Job First (SJF) - Non-Preemptive example step-
by-step.

Given Processes:
Process Arrival Time Burst Time
P1 0 7
P2 1 4
P3 2 1

OS Page 2
P3 2 1
P4 3 4
SJF (Non-Preemptive) means that once a process starts, it will run until completion. The CPU will always
choose the process with the shortest burst time from the available processes.

Step 1: Select the First Process


At time = 0, only P1 has arrived (arrival time = 0). Since it's the only process available, we start executing
P1.
Process Start Time End Time Burst Time
P1 0 7 7
P1 finishes at time 7.

Step 2: Select the Next Process (After P1 finishes)


At time = 7, all processes P2, P3, and P4 have arrived by now. We need to choose the process with the
shortest burst time among the available processes:
• P2 (Arrival Time = 1, Burst Time = 4)
• P3 (Arrival Time = 2, Burst Time = 1)
• P4 (Arrival Time = 3, Burst Time = 4)
P3 has the shortest burst time of 1 unit, so we select P3 next.
Process Start Time End Time Burst Time
P3 7 8 1
P3 finishes at time 8.

Step 3: Select the Next Process (After P3 finishes)


At time = 8, we have P2 and P4 left. Both have the same burst time of 4, but since P2 arrived earlier, we
choose P2 (in case of a tie, the one that arrived first is selected).
Process Start Time End Time Burst Time
P2 8 12 4
P2 finishes at time 12.

Step 4: Select the Last Process (After P2 finishes)


At time = 12, only P4 remains, so we select P4.
Process Start Time End Time Burst Time
P4 12 16 4
P4 finishes at time 16.

Completion Table:
Now, let’s summarize the completion times, turnaround times, and waiting times:
Process Arrival Time Burst Time Completion Time Turnaround Time (TAT) Waiting Time (WT)
P1 0 7 7 7-0=7 7-7=0
P2 1 4 12 12 - 1 = 11 11 - 4 = 7
P3 2 1 8 8-2=6 6-1=5
P4 3 4 16 16 - 3 = 13 13 - 4 = 9

Step 5: Calculate Average Waiting Time


OS Page 3
Step 5: Calculate Average Waiting Time
Average Waiting Time=0+7+5+94=214=5.25units\text{Average Waiting Time} = \frac{0 + 7 + 5 + 9}{4} =
\frac{21}{4} = 5.25 \, \text{units}Average Waiting Time=40+7+5+9=421=5.25units
So the average waiting time is 5.25 units.

Summary:
• Process P1 runs first.
• Process P3 is selected after P1 finishes due to its shorter burst time.
• Process P2 is selected after P3 finishes because it arrived earlier than P4 (both had the same burst
time).
• Process P4 runs last.
Does that clarify things, Sir?

Let's solve the given processes using both Shortest Job First (SJF) with and without preemption.
Given Processes:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

1. SJF Non-Preemptive
Step 1: Execution Order
1. At time 0, only P1 has arrived, so it starts executing.
○ Completion of P1: 0 + 8 = 8
2. At time 8, the following processes have arrived:
○ P2 (Arrival Time = 1, Burst Time = 4)
○ P3 (Arrival Time = 2, Burst Time = 9)
○ P4 (Arrival Time = 3, Burst Time = 5)
The remaining processes to choose from are P2, P3, and P4. The shortest burst time is P2
(4).
○ Completion of P2: 8 + 4 = 12
3. At time 12, the remaining processes are:
○ P3 (Burst Time = 9)
○ P4 (Burst Time = 5)
The shortest burst time is P4 (5).
○ Completion of P4: 12 + 5 = 17
4. At time 17, only P3 remains.
○ Completion of P3: 17 + 9 = 26
Completion Table (Non-Preemptive):

OS Page 4
Completion Table (Non-Preemptive):
Process Arrival Time Burst Time Completion Time Turnaround Time (TAT) Waiting Time (WT)
P1 0 8 8 8-0=8 8-8=0
P2 1 4 12 12 - 1 = 11 11 - 4 = 7
P3 2 9 26 26 - 2 = 24 24 - 9 = 15
P4 3 5 17 17 - 3 = 14 14 - 5 = 9

Average Waiting Time (Non-Preemptive):


Average WT=0+7+15+94=314=7.75 units\text{Average WT} = \frac{0 + 7 + 15 + 9}{4} = \frac{31}{4} = 7.75
\text{ units}Average WT=40+7+15+9=431=7.75 units

//////////////////////

Let’s visualize the Shortest Job First (SJF) Preemptive scheduling using a Gantt chart to clarify the
execution order.
Visual Explanation of SJF Preemptive
Given Processes:
Process Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5

Gantt Chart Steps


1. At time 0:
○ Only P1 has arrived, so it starts executing.

Copy code
0
P1
2. At time 1:
○ P2 arrives.
○ Remaining time for P1 = 7 (8 - 1).
○ Since P2 (burst time = 4) is shorter than remaining P1, switch to P2.

Copy code
0 1
P1 P2
3. At time 5:
○ P2 finishes.
○ Remaining time for P1 = 7.
○ P3 (arrival at 2) and P4 (arrival at 3) are still available.
○ P1 (remaining 7) is longer than P4 (burst time = 5), so execute P4 next.

Copy code
0 1 5
P1 P2 P4
4. At time 12:
○ P4 finishes.
Remaining times are: P1 (remaining 7) and P3 (burst time = 9).

OS Page 5
○ Remaining times are: P1 (remaining 7) and P3 (burst time = 9).
○ Execute P1 next since it is shorter than P3.

Copy code
0 1 5 12
P1 P2 P4 P1
5. At time 19:
○ P1 finishes (remaining time was 2).
○ Now, only P3 is left.
○ Start executing P3.

Copy code
0 1 5 12 19
P1 P2 P4 P1 P3
6. At time 26:
○ P3 finishes.

Copy code
0 1 5 12 19 26
P1 P2 P4 P1 P3
Final Gantt Chart:
css
Copy code
|---P1---|---P2---|---P4---|---P1---|---P3---|
0 1 5 12 19 26
Summary of Execution:
• P1 runs first until P2 arrives.
• When P2 arrives, it preempts P1 because it has a shorter burst time.
• After P2 finishes, P4 (the next shortest) executes.
• Finally, P1 resumes and finishes, followed by P3.
Completion Times:
• P1: 19
• P2: 5
• P4: 12
• P3: 26

OS Page 6
Certainly, Sir! Let's solve the problem using the Round Robin (RR) scheduling policy with a time quantum of 20.
Given Processes:
Process Burst Time
P1 53
P2 17
P3 68
P4 24

Round Robin Execution


1. Time Quantum: 20
Step-by-Step Execution:
Initial Setup:
• Remaining Burst Times:
○ P1: 53
○ P2: 17
○ P3: 68
○ P4: 24
Execution Order:
1. At time 0-20: Execute P1 for 20 units (remaining = 33).
2. At time 20-37: Execute P2 for 17 units (remaining = 0).
3. At time 37-57: Execute P3 for 20 units (remaining = 48).
4. At time 57-77: Execute P4 for 20 units (remaining = 4).
5. At time 77-81: Execute P4 for the remaining 4 units (completes).
6. At time 81-101: Execute P3 for 20 units (remaining = 28).
7. At time 101-121: Execute P1 for 20 units (remaining = 13).
8. At time 121-134: Execute P1 for 13 units (completes).
9. At time 134-154: Execute P3 for 20 units (remaining = 8).
10. At time 154-162: Execute P3 for 8 units (completes).
Gantt Chart:
css
Copy code
|----P1----|----P2----|----P3----|----P4----|----P4----|----P3----|----P1----|----P1----|----P3----|----P3----|
0 20 37 57 77 81 101 121 134 154 162
Completion Times:
• P1: 134
• P2: 37
• P3: 162
• P4: 81
Turnaround Time (TAT) Calculation:
TAT=Completion Time−Arrival Time\text{TAT} = \text{Completion Time} - \text{Arrival Time}
TAT=Completion Time−Arrival Time
(Assuming arrival time for all processes is 0)
Process Completion Time Turnaround Time (TAT)
P1 134 134 - 0 = 134
P2 37 37 - 0 = 37

OS Page 7
P2 37 37 - 0 = 37
P3 162 162 - 0 = 162
P4 81 81 - 0 = 81

Waiting Time (WT) Calculation:


WT=TAT−Burst Time\text{WT} = \text{TAT} - \text{Burst Time}WT=TAT−Burst Time
Process Burst Time Waiting Time (WT)
P1 53 134 - 53 = 81
P2 17 37 - 17 = 20
P3 68 162 - 68 = 94
P4 24 81 - 24 = 57

Average Waiting Time Calculation:


Average WT=81+20+94+574=2524=63 units\text{Average WT} = \frac{81 + 20 + 94 + 57}{4} = \frac{252}{4} = 63
\text{ units}Average WT=481+20+94+57=4252=63 units
Summary:
• Average Waiting Time: 63 units

Let's solve the priority scheduling problem based on the given processes. In priority scheduling, the
process with the highest priority (lowest numerical value) is executed first.
Given Processes:
Process Burst Time Priority
P1 10 3
P2 1 1
P3 2 3
P4 1 4
P5 5 2

Step 1: Sort Processes by Priority


Let's arrange the processes based on their priority (lower number indicates higher priority):
1. P2: Priority 1
2. P5: Priority 2
3. P1: Priority 3
4. P3: Priority 3
5. P4: Priority 4
Execution Order
1. P2 (Burst Time = 1)
2. P5 (Burst Time = 5)
3. P1 (Burst Time = 10)
4. P3 (Burst Time = 2)
5. P4 (Burst Time = 1)
Gantt Chart
OS Page 8
Gantt Chart
css
Copy code
|--P2--|----P5----|----P1----|--P3--|--P4--|
0 1 6 16 18 19
Step 2: Completion Times
Process Completion Time
P2 1
P5 6
P1 16
P3 18
P4 19

Step 3: Turnaround Time (TAT) Calculation


TAT=Completion Time−Arrival Time\text{TAT} = \text{Completion Time} - \text{Arrival Time}
TAT=Completion Time−Arrival Time
(Assuming arrival time for all processes is 0)
Process Completion Time Turnaround Time (TAT)
P2 1 1-0=1
P5 6 6-0=6
P1 16 16 - 0 = 16
P3 18 18 - 0 = 18
P4 19 19 - 0 = 19

Step 4: Waiting Time (WT) Calculation


WT=TAT−Burst Time\text{WT} = \text{TAT} - \text{Burst Time}WT=TAT−Burst Time
Process Burst Time Waiting Time (WT)
P2 1 1-1=0
P5 5 6-5=1
P1 10 16 - 10 = 6
P3 2 18 - 2 = 16
P4 1 19 - 1 = 18

Step 5: Average Waiting Time Calculation


Average WT=0+1+6+16+185=415=8.2 units\text{Average WT} = \frac{0 + 1 + 6 + 16 + 18}{5} = \frac{41}
{5} = 8.2 \text{ units}Average WT=50+1+6+16+18=541=8.2 units
Summary:
• Average Waiting Time: 8.2 units

In the case of processes with the same priority, the scheduling policy typically uses one of two
approaches:
1. First-Come, First-Served (FCFS): Among the processes with the same priority, the one that arrives
first gets executed first. This is a common method to break ties in priority scheduling.
2. Round Robin (RR): Sometimes, a round-robin approach is used among the processes with the

OS Page 9
2. Round Robin (RR): Sometimes, a round-robin approach is used among the processes with the
same priority, allowing each to run for a time slice before moving to the next.
In Your Example:
For the processes with the same priority (P1 and P3 with priority 3), we applied the FCFS approach:
• Both P1 and P3 have the same priority of 3.
• Since P1 arrived before P3, P1 was executed first.
Summary of the Tie-Breaking:
• P1 (priority 3, arrived first) executed before P3 (priority 3, arrived later).

The concept of aging in priority scheduling is actually an important mechanism to prevent starvation of
lower-priority processes. Here's a clearer explanation:
Aging in Priority Scheduling
1. Definition: Aging is a technique used to gradually increase the priority of a process that waits in
the queue for a long time. This ensures that no process is starved indefinitely due to the
continuous execution of higher-priority processes.
2. Purpose: The main goal of aging is to ensure fairness in the scheduling of processes. If a high-
priority process continuously arrives and preempts others, lower-priority processes could wait
indefinitely, leading to starvation.
3. How It Works:
○ If a process with a lower priority has been waiting for a certain amount of time, its priority
can be increased.
○ This makes it more likely for the scheduler to select the waiting process for execution,
ensuring that it eventually gets CPU time.
Example:
• If P1 (high priority) has a long burst time and continues to preempt other processes, the lower-
priority processes (like P4) will get their priority increased over time, making them eligible for
execution.
Summary:
• Aging helps mitigate the risk of starvation by promoting long-waiting processes, ensuring that all
processes get a fair chance to execute, even if they initially have a lower priority.

OS Page 10

You might also like