CSBB 203 : OPERATING SYSTEM
Lab Assignment 4
Name – Keshav
Roll No. – 231210058
Branch – CSE
Semester - 1
Group – 1B
Submitted To – Dr. Gunjan
DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEEARING
NATIONAL INSTITUTE OF TECHNILOGY DELHI
1.a Implement SJF Scheduling Algorithm.
1.b Implement SRTF Scheduling Algorithm.
Introduction to Shortest Job First (SJF) Algorithm:
The Shortest Job First (SJF) is a scheduling algorithm that selects the
process with the shortest execution time to run next. It is designed
to minimize the average waiting time by executing shorter tasks
before longer ones. This approach can either be non-pre-emptive
(once a job starts, it runs to completion) or pre-emptive (known as
Shortest Remaining Time First (SRTF), where the running process
can be interrupted if a shorter process arrives).
How SJF Works:
1. Non-Pre-emptive SJF:
o The process with the shortest burst time is selected from
the queue and runs until it finishes. No interruption
occurs even if a shorter process arrives during execution.
2. Pre-emptive SJF (SRTF):
o If a new process with a shorter burst time arrives while a
process is running, the currently running process is pre-
empted, and the shorter process is executed.
SJF focuses on reducing the average waiting time of processes,
which is its key advantage over First-Come, First-Served (FCFS).
Advantages of SJF:
1. Optimal Average Waiting Time:
o SJF provides the optimal solution for minimizing the
average waiting time for a set of processes, especially
when processes with shorter execution times are
frequent.
2. Efficient CPU Utilization:
o By prioritizing shorter processes, SJF ensures that quick
tasks are completed as soon as possible, improving overall
CPU utilization and throughput.
3. Low Turnaround Time:
o Since shorter tasks are completed first, the time between
the arrival and completion of processes (turnaround time)
is reduced.
4. Ideal for Batch Processing:
o SJF is well-suited for batch systems where processes are
known in advance and their burst times can be predicted
or estimated.
Disadvantages of SJF:
1. Starvation:
o Long processes can suffer from starvation if a steady
stream of shorter processes keeps arriving. The longer
tasks may never get executed, especially in a pre-emptive
version.
2. Difficult to Predict Burst Time:
o SJF requires knowledge of the exact or estimated burst
time for each process. In practical systems, it's often
difficult to predict how long a process will take to execute
accurately.
3. Not Suitable for Time-Critical Systems:
o SJF is not a good fit for real-time systems or interactive
environments where response times for critical processes
are essential.
4. Complexity in Implementation:
o Pre-emptive SJF (SRTF) requires constant monitoring of
the process queue to determine if a shorter job has
arrived, increasing the complexity of scheduling decisions
and system overhead.
5. Convoy Effect for Large Jobs:
o Similar to FCFS, if all shorter jobs are prioritized, longer
jobs may end up waiting indefinitely, especially in
environments where short jobs frequently arrive.
SJF Code:
SJF Output:
SRTF Code:
SRTF Output: