0% found this document useful (0 votes)
25 views4 pages

Lab 5

This document describes implementing the shortest job first (SJF) and shortest remaining time first (SRTF) algorithms to visualize how a CPU processes processes. SJF selects the process with the shortest execution time, while SRTF selects the process with the shortest remaining execution time and can preempt the current process. Both aim to minimize waiting times. The document provides pseudocode for the algorithms, showing taking process details as input, finding the shortest process, updating times, and outputting averages. It concludes that SJF and SRTF are more efficient than first-come, first-served scheduling.

Uploaded by

Prateek Jaiswal
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)
25 views4 pages

Lab 5

This document describes implementing the shortest job first (SJF) and shortest remaining time first (SRTF) algorithms to visualize how a CPU processes processes. SJF selects the process with the shortest execution time, while SRTF selects the process with the shortest remaining execution time and can preempt the current process. Both aim to minimize waiting times. The document provides pseudocode for the algorithms, showing taking process details as input, finding the shortest process, updating times, and outputting averages. It concludes that SJF and SRTF are more efficient than first-come, first-served scheduling.

Uploaded by

Prateek Jaiswal
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/ 4

LAB-5 IMPLEMENTING SJF AND SRTF ALGORITHM

Description: Implementing shortest job first and shortest remaining time first algorithm to
visualize how a cpu processes the process

Shortest Job First (SJF) is an algorithm in which the process having the smallest execution
time is chosen for the next execution. This scheduling method is preemptive .It significantly
reduces the average waiting time for other processes awaiting execution.

Algorithm:
1. Take the number of process , burst time and arrival time for each process as input.
2. We initialize the count and completion time to keep record of completed process and
completion time of process.
3. Scheduling (SJF):
While not all processes are completed (count < n):
● Find the process with the shortest burst time among those that have arrived.
● Update completiontime by adding the burst time of the selected process.
● Mark the process as completed and calculate its completion time (ct), turnaround time
(tat), and waiting time (wt).
● Set the burst time of the completed process to 0.
● Increment the count.
4. Calculate the total turnaround time and total waiting time.
5. Display the average turnaround time and waiting time by dividing it by total number of process.

.Code:
Output:

Shortest Remaining Time First(SRTF):


SRTF stands for Shortest Remaining Time First. It's a CPU scheduling algorithm where the
process with the shortest remaining execution time is given priority. Unlike SJF (Shortest Job
First), SRTF is preemptive, meaning it can interrupt the currently executing process if a new
process with a shorter remaining time becomes available. This scheduling algorithm aims to
minimize the waiting time for processes and provides faster response times for short tasks.
Algorithm:
1. Take number of process ,arrival time and burst time as input.
2. Create arrays for burst times (bt), a copy of original burst times (bt1), completion times (ct),
turnaround times (tat), waiting times (wt), and initialize s to -1.
3. Scheduling (SRTF):
a. Repeat until all processes are completed:
i. Find the process with the shortest remaining burst time (sbt) among eligible
processes.
ii. Execute it for one unit of time, updating completion time (ct), bt, and s.
iii. Mark completed processes.
4. Compute turnaround times (tat) and waiting times (wt) for each process.
5. Display the average waiting and turnaround time.

Code:
Output:

Conclusion:

In this lab we learned and implemented shortest job first and shortest time remaining first
algorithm for cpu scheduling to the process and we saw how it is much more efficient from
FCFS algorithm.

You might also like