Cpu Scheduling
Cpu Scheduling
Performance metrics in an operating system (OS) are quantitative measures used to evaluate the
efficiency, effectiveness, and overall performance of the system. These metrics help system
administrators, developers, and users understand how well the OS is managing resources and
executing processes. Below are some key performance metrics commonly used in operating
systems:
1. CPU Utilization
Definition: The percentage of time the CPU is actively executing processes versus being
idle.
Importance: High CPU utilization indicates that the CPU is being effectively used, while
very low utilization might suggest underutilization. However, consistently near-100%
utilization could indicate overloading.
Target: A balanced CPU utilization, typically between 50% and 90%, depending on the
system's design and workload.
2. Throughput
Definition: The number of processes completed by the system in a given amount of time.
Importance: High throughput indicates that the system can handle a large volume of work
efficiently. It is a key metric for systems where processing speed is critical.
Target: Maximizing throughput while maintaining other metrics within acceptable limits.
3. Turnaround Time
Definition: The total time taken to execute a process, from its submission to its completion.
Importance: Shorter turnaround times are preferable, especially in batch processing systems
where the objective is to process jobs quickly.
Target: Minimize turnaround time to improve the responsiveness of the system.
4. Waiting Time
Definition: The total time a process spends waiting in the ready queue before it gets
executed.
Importance: Lower waiting times indicate that processes are being scheduled efficiently,
reducing delays and improving system responsiveness.
Target: Minimize waiting time, especially for time-sensitive processes.
5. Response Time
Definition: The time taken from when a request is submitted until the first response is
produced (not the completion time).
Importance: Critical in interactive and real-time systems, where quick feedback is
necessary. Lower response times improve user experience.
Target: Minimize response time for better user satisfaction.
Scenario
Assume we have a simple operating system managing four processes: P1, P2, P3, and P4. The
following information is given:
First-Come, First-Served (FCFS) is one of the simplest scheduling algorithms used in operating
systems. It operates on a straightforward principle: the process that arrives first in the ready queue is
the one that gets executed first. Here’s a detailed explanation:
Average Turnaround Time (TAT): 11.25 units
Average Waiting Time (WT): 5.75 units
CPU Utilization: 100%
Throughput: 0.18 processes/unit time
Response Times: P1=0, P2=5, P3=8, P4=16
EXAmple :Scenario
Assume we have three processes: P1, P2, and P3. The CPU time for each process is as follows:
P1: 4 units
P2: 3 units
P3: 2 units
Also, assume the total time during which the CPU is monitored is 10 units. During this time, the
CPU spends:
Total Burst Time (the time the CPU is executing processes): 9 units (P1 + P2 + P3).
Idle Time: 1 unit (time during which the CPU was not executing any process).
Calculation of CPU Utilization
1. Total CPU Time (Active + Idle): 10 units
2. Active Time (Total Burst Time): 9 units
CPU Utilization is calculated as:
CPU Utilization
How FCFS Scheduling Works
1. ) is one of the simplest scheduling algorithms used in operating systems to manage the order
in which processes are executed by the CPU. It works on the principle that the process
which arrives first is served first. This method is non-preemptive, meaning that once a
process starts execution, it runs to completion without being interrupted by other processes.
Execution Order
1. P1 arrives first at time 0 ms, so it starts executing immediately.
2. P2 arrives at time 1 ms but must wait for P1 to finish.
3. P3 arrives at time 2 ms and also waits for P1 and P2 to finish.
Here’s how the processes would be executed:
P1 starts at 0 ms and finishes at 4 ms.
P2 starts at 4 ms and finishes at 7 ms.
P3 starts at 7 ms and finishes at 12 ms.
Gantt Chart:
Copy code
| P1 | P2 | P3 |
0 4 7 12
Limitations of FCFS
1. Convoy Effect: A long process can delay all the processes behind it, leading to longer
waiting times, especially for shorter processes.
2. High Average Waiting Time: Processes that arrive later may have to wait a long time if
earlier processes take a lot of CPU time.
3. Poor Performance for Time-Sharing Systems: Since it is non-preemptive, it’s not suitable
for interactive systems where response time is critical.
Shortest Job First (SJF) is a scheduling algorithm used in operating systems to determine
the order in which processes are executed by the CPU. It is designed to minimize the
average waiting time by prioritizing processes with the shortest burst time (the time required
to complete the process).
Execution Order
1. P4 has the shortest burst time (3 ms), so it is executed first.
2. P1 is next with a burst time of 6 ms.
3. P3 follows with a burst time of 7 ms.
4. P2 is executed last with a burst time of 8 ms.
Gantt Chart:
Copy code
| P4 | P1 | P3 | P2 |
0 3 9 16 24
Execution Order
1. P1 starts at 0 ms.
2. P2 arrives at 1 ms with a burst time of 4 ms, so it preempts P1 and is executed next.
3. P4 arrives at 3 ms with a burst time of 5 ms but is longer than P2’s remaining time, so P2
continues execution.
4. P4 starts at 5 ms after P2 finishes.
5. P1 resumes at 10 ms after P4 finishes.
6. P3 starts at 17 ms and finishes at 26 ms.
Gantt Chart:
Copy code
| P1 | P2 | P4 | P1 | P3 |
0 1 5 10 17 26
Limitations of SJF
1. Starvation: Longer processes may suffer from starvation, especially in preemptive SJF.
2. Difficult to Implement: SJF requires knowledge of the burst time of processes, which is not
always known in advance.
3. Not Suitable for Real-Time Systems: SJF is not ideal for systems where time predictability
and immediate responsiveness are critical.
Tasks:
Draw the Gantt chart for the processes using the FCFS scheduling algorithm.
Calculate the waiting time and turnaround time for each process.
Compute the average waiting time and average turnaround time.
Example 2:
Tasks:
Draw the Gantt chart for the processes using the non-preemptive SJF scheduling algorithm.
Calculate the waiting time and turnaround time for each process.
Compute the average waiting time and average turnaround time.
Draw the Gantt chart for the processes using the preemptive
SJF (SRTF) scheduling algorithm.
Calculate the waiting time and turnaround time for each process.
Compute the average waiting time and average turnaround time.
Round Robin Scheduling
Round Robin (RR) scheduling is one of the simplest and fairest CPU scheduling algorithms
designed to ensure that every process gets an equal share of the CPU. It is particularly effective in
time-sharing and interactive systems where responsiveness is critical.
Key Characteristics:
Preemptive: RR is inherently a preemptive scheduling algorithm. It allows the operating
system to interrupt a running process after a fixed time quantum.
Time Quantum: This is the fixed time interval during which a process is allowed to run. If
the process does not finish within this time, it is preempted and placed at the end of the
ready queue.
Fairness: Every process gets an equal opportunity to execute. However, if the time quantum
is too small, it could lead to excessive context switching, resulting in inefficiency.
Conversely, if the time quantum is too large, RR behaves like a First-Come, First-Served
(FCFS) scheduler.
Example:
Consider four processes with the following burst times:
Process A: 5 units
Process B: 9 units
Process C: 6 units
Process D: 3 units
Assume the time quantum is 4 units.
Execution:
1. Time 0-4: Process A runs for 4 units (Remaining burst time = 1 unit).
2. Time 4-8: Process B runs for 4 units (Remaining burst time = 5 units).
3. Time 8-12: Process C runs for 4 units (Remaining burst time = 2 units).
4. Time 12-15: Process D runs for 3 units (Process D finishes).
Second Round: 5. Time 15-16: Process A completes its remaining 1 unit (Process A finishes). 6.
Time 16-20: Process B runs for 4 units (Remaining burst time = 1 unit). 7. Time 20-22: Process C
completes its remaining 2 units (Process C finishes).
Third Round: 8. Time 22-23: Process B completes its remaining 1 unit (Process B finishes).
Key Points:
Turnaround Time: The total time taken from process submission to completion. In RR,
turnaround time can be higher because processes are frequently switched out and may wait
multiple times before completing.
Waiting Time: The time a process spends in the ready queue. This time can vary widely in
RR based on the number of processes and the time quantum.
Advantages:
Ensures a high degree of fairness since each process receives an equal share of the CPU.
Ideal for systems where short response times are essential.
Disadvantages:
The performance is highly dependent on the choice of time quantum. Too small a quantum
leads to high context switching overhead; too large, and it risks reducing RR to an FCFS
approach.
Can result in longer turnaround times and waiting times compared to other algorithms,
especially for processes with short burst times.
Key Characteristics:
Preemptive or Non-Preemptive: Priority scheduling can be either preemptive or non-
preemptive.
Preemptive: If a new process arrives with a higher priority than the currently
running process, the CPU is preempted and allocated to the new process.
Non-Preemptive: The CPU continues executing the current process until it
completes, even if a higher-priority process arrives in the meantime.
Starvation: A significant downside of priority scheduling is the potential for starvation,
where low-priority processes may never execute if higher-priority processes continue to
arrive. This issue can be mitigated by implementing aging, where a process's priority
increases the longer it waits in the system.
Example:
Consider four processes with the following burst times and priorities:
Process P1: Burst time = 10 units, Priority = 3
Process P2: Burst time = 1 unit, Priority = 1
Process P3: Burst time = 2 units, Priority = 4
Process P4: Burst time = 1 unit, Priority = 2
Here, a lower number indicates a higher priority.
Non-Preemptive Priority Scheduling Execution:
1. Process P2 (highest priority) runs first and completes in 1 unit.
2. Process P4 (next highest priority) runs next and completes in 1 unit.
3. Process P1 (next highest priority) runs next and completes in 10 units.
4. Process P3 (lowest priority) runs last and completes in 2 units.
Execution Order: P2 → P4 → P1 → P3
Preemptive Priority Scheduling Execution:
Suppose Process P1 is running. If Process P2 arrives, it preempts P1 because P2 has a higher
priority. After P2 completes, P1 resumes unless another higher-priority process arrives.
Key Points:
Turnaround Time: The total time from when a process is submitted to when it completes
can vary widely depending on the priority of the process. High-priority processes typically
have lower turnaround times.
Waiting Time: Similar to RR, waiting time is the time a process spends waiting in the
queue. In priority scheduling, low-priority processes can have a much higher waiting time,
particularly if many high-priority processes are present.
Advantages:
Provides a mechanism for prioritizing more important or time-sensitive tasks, leading to
more efficient use of CPU resources in certain contexts.
Can result in lower average waiting and turnaround times for high-priority processes.
Disadvantages:
Starvation: Low-priority processes may starve if high-priority processes continuously arrive.
More complex to implement and manage compared to simpler scheduling algorithms like
FCFS or RR.
Multilevel Queue Scheduling Algorithm
The Multilevel Queue Scheduling algorithm divides the ready queue into several separate queues,
where each queue has a different priority level or type of processes assigned to it. For example,
system processes, interactive processes, batch processes, etc., are assigned to different queues. Each
queue can have its own scheduling algorithm, and processes are permanently assigned to one queue.
Scheduling decisions happen in two ways:
1. Fixed priority scheduling: The queues are assigned priorities. A higher priority queue is
executed first before lower priority ones.
2. Time slicing: Each queue gets a time slice for execution.
Characteristics:
Separation of process types: Processes are categorized into distinct groups and placed in
specific queues.
Priority: Each queue is assigned a priority, with higher priority queues being executed first.
Scheduling within queues: Each queue can use a different scheduling algorithm (e.g.,
Round Robin, First Come First Serve).
No movement between queues: A process stays in its assigned queue throughout its
lifecycle.
Advantages:
Efficient process grouping based on process type.
Ensures system-critical processes are executed with high priority.
Provides flexibility by allowing different scheduling algorithms within each queue.
Disadvantages:
Can cause starvation for lower-priority queues if higher-priority queues are always full.
No flexibility to move processes between queues, even if their priority or type changes over
time.
Multilevel Feedback Queue Scheduling Algorithm
The Multilevel Feedback Queue (MLFQ) scheduling algorithm is an extension of the multilevel
queue, where processes can move between queues based on their behavior and the time they have
spent in execution. This allows more flexibility in dynamically adjusting the priority of processes.
MLFQ adjusts the priority of processes based on their execution history, penalizing long-running
processes (CPU-bound) and promoting short, interactive processes (I/O-bound).
Characteristics:
Multiple queues with different priorities: Each queue has its own priority level, with
higher-priority queues being executed first.
Dynamic process movement: Processes can move between queues based on their runtime
characteristics (e.g., if a process uses too much CPU time, it moves to a lower-priority
queue).
Aging: To avoid starvation, processes can be moved back to higher-priority queues after
waiting too long in a lower-priority queue.
Time quantums: The higher-priority queues often use shorter time quantums, while lower-
priority queues may use longer ones.
Advantages:
Prevents starvation by allowing processes to age and eventually move up in priority.
More efficient for real-world scenarios where process behavior can change (e.g., a process
might start as interactive but later become CPU-bound).
Adaptable: It adjusts based on the needs of the process and system load.
Disadvantages:
Complex implementation: It requires more overhead due to the movement of processes
between queues.
Tuning required: Proper configuration of the time quantum and queue behavior is essential
for optimal performance.