0% found this document useful (0 votes)
3 views6 pages

SJN Scheduling Algorithm Overview

The document outlines the implementation of four CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest-Job-Next (SJN), Priority Scheduling, and Round Robin. Each algorithm is described with its purpose, methodology, and step-by-step procedures for calculating waiting and turnaround times. Additionally, it includes exercises to write programs implementing these algorithms.

Uploaded by

khalid123.19988
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)
3 views6 pages

SJN Scheduling Algorithm Overview

The document outlines the implementation of four CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest-Job-Next (SJN), Priority Scheduling, and Round Robin. Each algorithm is described with its purpose, methodology, and step-by-step procedures for calculating waiting and turnaround times. Additionally, it includes exercises to write programs implementing these algorithms.

Uploaded by

khalid123.19988
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

Operating System Lab

CPU Scheduling

Aims
1. Implement CPU Scheduling algorithms:
 First-Come, First-Served (FCFS) Scheduling
 Shortest-Job-Next (SJN) Scheduling
 Priority Scheduling
 Round Robin(RR) Scheduling

First Come First Serve


First Come First Serve is the full form of FCFS. It is the easiest and the simplest CPU
scheduling algorithm. In this type of algorithm, the process which requests the CPU gets
the CPU allocation first. This scheduling method can be managed with a FIFO queue.
Algorithm for FCFS
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time
Step 5: for each process in the Ready Q calculate
(a) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(b) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
Step 6: Calculate
(a) Average waiting time = Total waiting Time / Number of process
(b) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process.

1
Operating System Lab

2
Operating System Lab

Shortest-Job-Next (SJN) Scheduling


SJF is a full form of (Shortest job first) is a scheduling algorithm in which the process
with the shortest execution time should be selected for execution next. This scheduling
method can be preemptive or non-preemptive. It significantly reduces the average waiting
time for other processes awaiting execution.
Algorithm for SJN
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept CPU burst time
Step 4: Start the Ready Q according the shortest Burst time by sorting according to
lowest to highest burst time.
Step 5: Set the waiting time of the first process as ‘0’ and its turnaround time as its burst
time.
Step 6: For each process in the ready queue, calculate
(c) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(d) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)
Step 7: Calculate
(c) Average waiting time = Total waiting Time / Number of process
(d) Average Turnaround time = Total Turnaround Time / Number of process
Step 7: Stop the process

3
Operating System Lab

Priority Scheduling
Priority scheduling is a method of scheduling processes based on priority. In this method,
the scheduler selects the tasks to work as per the priority.
Priority scheduling also helps OS to involve priority assignments. The processes with
higher priority should be carried out first, whereas jobs with equal priorities are carried
out on a round-robin or FCFS basis. Priority can be decided based on memory
requirements, time requirements, etc.

Algorithm for Priority


Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Read the Priority for each process
Step 5: Sort the ready queue according to the priority number.
Step 6: Set the waiting of the first process as ‘0’ and its burst time as its turnaround time
Step 7: For each process in the Ready Q calculate
(e) Waiting time for process(n)= waiting time of process (n-1) + Burst time of
process(n-1)
(f) Turnaround time for Process(n)= waiting time of Process(n)+ Burst time for
process(n)

4
Operating System Lab

Step 8: Calculate
(e) Average waiting time = Total waiting Time / Number of process
(f) Average Turnaround time = Total Turnaround Time / Number of process
Step 9: Stop the process

Round Robin:
Round robin is the oldest, simplest scheduling algorithm. The name of this algorithm
comes from the round-robin principle, where each person gets an equal share of
something in turn. It is mostly used for scheduling algorithms in multitasking. This
algorithm method helps for starvation free execution of processes.
Algorithm for Round Robin:
Step 1: Start the process
Step 2: Accept the number of processes in the ready Queue and time quantum (or) time
slice
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst
time
Step 4: Calculate the no. of time slices for each process where
No. of time slice for process (n) = burst time process (n)/time slice
Step 5: If the burst time is less than the time slice then the no. of time slices =1.
Step 6: Consider the ready queue is a circular Q, calculate
(a) Waiting time for process(n) = waiting time of process(n-1)+ burst time of
process(n-1 ) + the time difference in getting the CPU from process(n-1)
(b) Turnaround time for process(n) = waiting time of process(n) + burst time of
process(n)+ the time difference in getting CPU from process(n).
Step 7: Calculate
(g) Average waiting time = Total waiting Time / Number of process
(h) Average Turnaround time = Total Turnaround Time / Number of process
Step 8: Stop the process

Exercises:

5
Operating System Lab

Write programs to implement the above algorithms.

Common questions

Powered by AI

In both FCFS and SJN scheduling, average waiting time is calculated as the total waiting time divided by the number of processes . However, the order of process execution affects the calculations. In FCFS, processes are executed in arrival order, with waiting time for each process being the sum of burst times of all preceding processes. In SJN, processes are executed based on the shortest execution time, minimizing waiting time by carefully selecting the order to minimize turnaround time . Consequently, SJN typically yields a lower average waiting time compared to FCFS due to its optimized scheduling order.

FCFS scheduling algorithm selects processes based on their arrival order without considering any other criteria, meaning that the first process to request the CPU gets executed first . In contrast, Priority Scheduling selects processes based on pre-assigned priority levels, where processes with higher priority are executed before those with lower priority. If processes have the same priority, they may be scheduled using either Round Robin or FCFS .

In Priority Scheduling, processes may be prioritized based on their memory requirements, granting higher priority to processes with specific memory needs. This can optimize CPU allocation for memory-intensive tasks . However, a possible drawback is that processes with less demanding memory needs could suffer from lower priority and may not be executed promptly, leading to potential delays or starvation, especially if higher-priority processes constantly occupy the CPU .

Round Robin scheduling is advantageous for multitasking environments because it allows each process to share CPU time equally within predefined time slices or quanta, preventing any single process from monopolizing the CPU. This approach ensures that all processes receive attention and helps avoid starvation, making it suitable for time-sharing systems . On the other hand, Shortest-Job-Next, while effective at reducing average waiting time, tends to favor short processes, which can lead to longer processes being starved for CPU time if new shorter processes continually arrive .

Setting an inappropriate time quantum in Round Robin scheduling can severely affect system performance. A very short time quantum increases context switching overhead, thus decreasing CPU efficiency due to frequent task switching. This overhead can outweigh the execution time, leading to a performance bottleneck . Conversely, a very long time quantum reduces the responsiveness akin to FCFS, potentially leading to the starvation of shorter tasks as longer processes consume excessive CPU time without interruption . An optimal time quantum strikes a balance allowing fair interaction and minimizing overhead.

In a multi-core system, using the FCFS algorithm might lead to inefficiencies due to poor load balancing among cores. Since FCFS does not consider the core-specific load or process characteristics beyond arrival order, some cores might be overburdened while others are underutilized, compromising the potential parallelism benefits of multi-core architecture. This could result in suboptimal system performance compared to more dynamic algorithms that actively balance workload across cores .

Round Robin scheduling prevents process starvation by assigning fixed time slots (time quanta) to each process in a cyclic order, ensuring that every process receives an equal opportunity to execute. The cyclic nature means that no process can indefinitely block others from execution as they return to the head of the queue in turns, ensuring fair CPU sharing and reducing the risk of starvation common in algorithms that can prioritize specific processes .

Priority Scheduling can be combined with Round Robin by addressing processes within the same priority level using Round Robin. This hybrid approach ensures that higher-priority processes are executed first based on priority, while processes with identical priority levels share the CPU equally in a time-slice manner provided by Round Robin. The combination seeks to harness the benefits of both strategies, ensuring priority handling while avoiding starvation within priority levels due to its cyclic nature .

The primary challenge in Priority Scheduling is accurately determining priority levels, which can depend on various factors like memory and time requirements inherently suggesting subjectivity . Misjudging priority can lead to unfair scheduling where critical processes are neglected in favor of those misassigned higher priorities, leading to potential starvation of lower-priority processes if higher-priority tasks dominate CPU time .

The primary disadvantage of using SJN in real-time systems stems from its requirement for advance knowledge of each process's execution time, which is often unavailable or inaccurate in real-time environments . Additionally, SJN may cause increased complexity and unpredictability because it can lead to starvation of longer processes if shorter jobs continually arrive. This unpredictability is unsuitable for real-time systems that demand predictable, consistent scheduling .

You might also like