You are on page 1of 2

Step-by-step

1st step All steps Answer only

Step 1/4

Shortest Job Next (SJN) Algorithm Flowchart


Assumptions:

All the arrived jobs are at "Ready State" and ready to be processed by CPU.
Only ONE CPU available for processing the jobs.
Each context switching incurs ONE CPU CYCLE.
Memory is always sufficient to hold any jobs.

Algorithm:

1. Sort all the jobs in the ready queue in ascending order of their CPU burst time.
2. Schedule the job with the shortest CPU burst time next.
3. Execute the job until it completes or is interrupted by a higher priority job.
4. If the job is interrupted, add it back to the ready queue, maintaining the ascending order of CPU burst times.
5. Repeat steps 2-4 until all the jobs in the ready queue are completed.

Calculation of Performance Metrics:

Turnaround time: The time it takes for a job to complete from the moment it arrives in the ready queue to the moment it finishes executing.
Waiting time: The amount of time a job spends in the ready queue waiting to be executed.
Number of interrupts incurred: The number of times a job is interrupted while executing.

Step 2/4

Example:
Consider the following set of jobs:

Job CPU Burst Time


A 5
B 10
C 3

Step 3/4

The SJN algorithm would schedule job A first, followed by job C, and then job B. The performance metrics for each job would be as follows:

Job Turnaround Time Waiting Time Number of Interrupts


A 5 0 0
B 15 10 1
C 8 3 1

Shortest Job Next (SJN) Flowchart


Start

1. Sort jobs in ready queue by CPU burst time in ascending order.


2. While ready queue is not empty:
Schedule job with shortest CPU burst time.
Execute job until completion or interruption.
If job is interrupted, add it back to ready queue, maintaining ascending order of CPU burst times.

End

Step 4/4

Here is the code for the Shortest Job Next (SJN) algorithm:

1 class Job:
2 def __init__(self, job_id, cpu_burst_time):
3 self.job_id = job_id
4 self.cpu_burst_time = cpu_burst_time
5 self.arrival_time = 0
6 self.start_time = 0
7 self.completion_time = 0
8 self.turnaround_time = 0
9 self.waiting_time = 0
10
11 def sjf_algorithm(jobs):
12 # Sort jobs by CPU burst time in ascending order
13 jobs.sort(key=lambda job: job.cpu_burst_time)
14
15 # Initialize variables
16 current_time = 0
17 completed_jobs = []
18 running_job = None
19
20 # Simulate SJN algorithm
21 while jobs or running_job:
22 # If there are jobs in the ready queue and no job is running, schedule the next job
23 if jobs and not running_job:
24 running_job = jobs.pop(0)
25 running_job.start_time = current_time
26
27 # Execute the running job for one CPU cycle
28 running_job.cpu_burst_time -= 1
29 current_time += 1
30
31 # If the job is completed, update its turnaround and waiting times, and add it to the completed jobs list
32 if running_job.cpu_burst_time == 0:
33 running_job.completion_time = current_time
34 running_job.turnaround_time = running_job.completion_time - running_job.arrival_time
35 running_job.waiting_time = running_job.start_time - running_job.arrival_time
36 completed_jobs.append(running_job)
37 running_job = None
38
39 # Calculate average turnaround and waiting times
40 total_turnaround_time = sum(job.turnaround_time for job in completed_jobs)
41 total_waiting_time = sum(job.waiting_time for job in completed_jobs)
42 average_turnaround_time = total_turnaround_time / len(completed_jobs)
43 average_waiting_time = total_waiting_time / len(completed_jobs)
44
45 # Print results
46 print("SJF Algorithm Results:")
47 print("Job ID\tArrival Time\tStart Time\tCompletion Time\tTurnaround Time\tWaiting Time")
48 for job in completed_jobs:
49 print(f"{job.job_id}\t{job.arrival_time}\t{job.start_time}\t{job.completion_time}\t{job.turnaround_time}\t{job.waiting_time}")
50 print(f"Average Turnaround Time: {average_turnaround_time}")
51 print(f"Average Waiting Time: {average_waiting_time}")
52
53 # Example usage
54 jobs = [Job(1, 5), Job(2, 10), Job(3, 3)]
55 sjf_algorithm(jobs)

Final answer

Output of the code:

1 SJF Algorithm Results:


2 Job ID Arrival Time Start Time Completion Time Turnaround Time Waiting Time
3 3 0 0 3 3 0
4 1 0 3 8 8 3
5 2 0 8 18 18 8
6 Average Turnaround Time: 9.666666666666666
7 Average Waiting Time: 3.6666666666666665

You might also like