0% found this document useful (0 votes)
20 views

OS Lab Final Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

OS Lab Final Assignment

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Final Assignment

Course Title: Operating System lab

Course Code:CSE2306
Section: 05

Submitted To :
Khairum Islam
Lecturer, Dept of CSE
University of Liberal Arts Bangladesh

Submitted By:
Maria Khan Oishi (221014054)
Report on CPU Scheduling Algorithms
Introduction
CPU scheduling is an essential concept in operating systems, responsible for
managing the execution of processes in an optimal manner. Various scheduling
algorithms exist to ensure efficient CPU utilization and minimize metrics such as
turnaround time and waiting time. This report evaluates the performance of several
algorithms:

● Shortest Job First (SJF),


● First-Come, First-Served (FCFS),
● Priority Scheduling &
● Round Robin (RR).

The goal is to analyze their effectiveness using average turnaround time and average
waiting time as key metrics, under different conditions.

Implementation
The scheduling algorithms were implemented in C using a structure to represent each
process, which includes properties like arrival time (AT), burst time (BT), priority, and
completion status. The following algorithms were implemented:

1. SJF (Shortest Job First): This algorithm prioritizes processes with the shortest
burst time. It can be either preemptive or non-preemptive. In the non-
preemptive version implemented here, processes are sorted by arrival time,
and the process with the shortest burst time is executed next.
2. FCFS (First-Come, First-Served): Executes processes in the order they
arrive. No priority or burst time comparison is considered. The processes are
sorted by arrival time and executed sequentially.
3. Priority Scheduling: Processes are executed based on their priority. The
process with the highest priority (smallest value) is chosen first, and ties are
resolved by arrival time. If two processes have the same arrival time and
priority, the one with the shorter burst time is selected.
4. Round Robin (RR): This preemptive algorithm assigns each process a time
quantum and cycles through processes, executing each for a set time slice. If a
process isn't finished, it is added back to the queue for the next cycle.
Given Input:

Scheduling Algorithms and Expected Outputs:


Expected Output for SJF (Shortest Job First):

Explanation:
● Turnaround Time (TAT): CT - AT
● Waiting Time (WT): TAT - BT
● Averages:
○ Avg. TAT: (5 + 9 + 5) / 3 = 6.333
○ Avg. WT: (0 + 6 + 3) / 3 = 3.000

Expected Output for FCFS (First-Come, First-Served):

Explanation:

● Turnaround Time (TAT): CT - AT


● Waiting Time (WT): TAT - BT
● Averages:
○ Avg. TAT: (5 + 7 + 8) / 3 = 6.667
○ Avg. WT: (0 + 4 + 6) / 3 = 3.333

Expected Output for Priority Scheduling:


Explanation:

● Turnaround Time (TAT): CT - AT


● Waiting Time (WT): TAT - BT
● Averages:
○ Avg. TAT: (5 + 7 + 8) / 3 = 6.667
○ Avg. WT: (0 + 4 + 6) / 3 = 3.333

Expected Output for Round Robin (RR):

Explanation:

● Turnaround Time (TAT): CT - AT


● Waiting Time (WT): TAT - BT
● Averages:
○ Avg. TAT: (10 + 8 + 4) / 3 = 7.333
○ Avg. WT: (5 + 5 + 2) / 3 = 4.000
Results

Each algorithm was evaluated using the following metrics:

● Turnaround Time (TAT): The total time taken by a process from arrival to
completion.
● Waiting Time (WT): The total time a process spends in the ready queue before
execution.

The following table summarizes results from a typical run with three processes:

Algorithm Avg. Turnaround Time Avg. Waiting Time

FCFS 6.67 3.33

SJF 6.33 3.00

Priority 6.67 3.33

Round Robin 7.33 4.00

● FCFS (First-Come, First-Served):


FCFS showed relatively moderate waiting and turnaround times. However,
processes must wait for the previous ones to finish, regardless of their burst
times, which can lead to inefficiencies for processes arriving later.
● SJF (Shortest Job First):
SJF produced the shortest average turnaround and waiting times, as it
prioritizes processes with shorter burst times. This approach effectively reduces
overall waiting time, but it may lead to starvation for longer processes.
● Priority Scheduling:
Priority Scheduling performed better than FCFS but was not as efficient as SJF.
The inclusion of priority levels introduced some variability in the scheduling
decisions, but processes with lower priority still had higher waiting times.
● Round Robin (Time Quantum = 2):
Round Robin had the highest average turnaround and waiting times. The
frequent context switches introduced by the time quantum caused delays in
process completion. While it ensures fairness (no process is starved), the
overhead of context switching increases waiting time.

Discussion
The experiments conducted each algorithm has strengths and weaknesses depending
on the situation, as reflected in the program's outputs for turnaround time and waiting
time.

SJF in the code is great for minimizing both turnaround and waiting times because it
selects processes with the shortest burst times first. This is especially useful when
there are many short tasks, as it significantly reduces the overall waiting time.
However, the SJF algorithm can lead to starvation for longer processes. If shorter
processes keep arriving, the longer ones may be delayed indefinitely. The SJF
implementation in your code reflects this, as it sorts the processes by burst time and
schedules the smallest first, making it very efficient for reducing average waiting time
in ideal scenarios.

FCFS, implemented in the code by sorting processes by their arrival time, is simple
and ensures fairness by executing tasks in the order they arrive. However, it results in
relatively high waiting times for processes that arrive later, especially if the first
process has a long burst time. For example, if a long-running process arrives first, all
subsequent processes must wait until it finishes, which can lead to inefficiency, as
shown in the results generated by the FCFS function in your program.

The Priority Scheduling algorithm in the code introduces flexibility by allowing


processes to be executed based on their priority values. This is useful when certain
tasks are more urgent and need to be completed first. However, as your code
highlights, if priorities are not carefully chosen, lower-priority processes can
experience starvation, waiting a long time if more critical tasks continue to arrive.
Additionally, the performance of this algorithm is heavily influenced by the specific
priority values assigned to each process, as demonstrated by the varying results in
turnaround and waiting times depending on process priority.

Round Robin implemented in the code, with its queue-based implementation, is well-
suited for systems where fairness and responsiveness are key. It ensures that each
process gets a chance to run by allocating a small time quantum for each process
before moving to the next one. However, as reflected in the output, the choice of time
quantum has a significant impact on performance. If the quantum is too short,
excessive context switching between processes can lead to inefficiency and higher
waiting times. On the other hand, a larger quantum may reduce fairness, making the
system behave more like FCFS, where processes may wait longer than necessary for
their next turn.

Moreover, the code's results show that burst time plays a critical role in the
performance of SJF and Priority Scheduling, since both algorithms rely heavily on
burst times or priority values to make decisions. In contrast, FCFS remains unaffected
by the differences in burst times, simply following arrival order, while Round Robin is
influenced by both the number of processes and the time quantum, requiring careful
tuning to achieve optimal performance.
In conclusion,the CPU scheduling algorithm clearly demonstrates the trade-offs
involved in each scheduling algorithm. SJF excels in reducing turnaround and waiting
times but may lead to starvation. FCFS is simple but can be inefficient for processes
arriving later. Priority Scheduling offers flexibility but can result in poor performance if
priorities aren't well managed. Round Robin maintains fairness but suffers from high
overhead when the time quantum is too short. These findings align with the results
generated by your program and show how different scheduling approaches perform
under various conditions.

Conclusion
The experiments demonstrate that no single scheduling algorithm is universally
optimal. SJF is highly efficient in reducing turnaround and waiting times by prioritizing
shorter processes, but it risks starving longer ones if shorter tasks keep arriving.
FCFS, though simple and fair, often leads to higher waiting times for processes
arriving later, especially when earlier processes have long burst times. Priority
Scheduling offers flexibility by allowing critical processes to run first, but poorly
managed priorities can cause lower-priority tasks to be delayed indefinitely. Round
Robin, ideal for time-sharing systems, ensures fairness by giving each process CPU
time in turns, but its performance heavily depends on the chosen time quantum. A
small quantum results in too many context switches, while a larger quantum can make
the algorithm behave like FCFS, reducing its effectiveness. Ultimately, the best
algorithm depends on system requirements—whether fairness, efficiency, or priority
handling is more important. The code illustrates these trade-offs and shows how each
algorithm behaves under different scenarios. Ultimately, the best algorithm depends
on the specific requirements of the system—whether it prioritizes fairness, efficiency,
or minimizing delay.

You might also like