Numerical Concepts - Mid Sem (Scheduling, Etc)
Numerical Concepts - Mid Sem (Scheduling, Etc)
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.
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
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.
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
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
//////////////////////
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
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
OS Page 7
P2 37 37 - 0 = 37
P3 162 162 - 0 = 162
P4 81 81 - 0 = 81
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
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