You are on page 1of 11

NAGARJUNA COLLEGE OF ENGINEERING AND TECHNOLOGY

(An Autonomous College under VTU, Belagavi)

A Report on CPU Scheduling Algorithms

Course Name: Operating System


Course Code:21ISI44

Name: Polathala Mounika


USN: 1NC21IS038
Sem:4th
Dept of ISE

Under the Guidance of


Ankita Shukla
Assistant Professor
Department of Information Science and Engineering

DEPARTMENT OF INFORMATION SCIENCE AND


ENGINEERINGNAGARJUNA COLLEGE OF ENGINEERING AND
TECHNOLOGY
(An Autonomous Institution under VTU, Accredited by NAAC with “A+”
Grade)BENGALURU– 562164
2022-2023
ABSTRACT

Efficient utilization of the central processing unit (CPU) is crucial for optimizing
system performance in modern computer operating systems. CPU scheduling algorithms
play a vital role in determining the order in which processes are executed and the allocation
of CPU time. This abstract provides a comprehensive overview and comparative analysis
of various CPU scheduling algorithms, including First-Come-First-Serve (FCFS), Shortest
Job First (SJF), Shortest Remaining Time First (SRTF), Round Robin (RR), Preemptive,
and Non-Preemptive.

The FCFS algorithm follows a simple and intuitive approach, executing processes in the
order they arrive. However, it may suffer from poor average waiting times, especially when
long processes arrive before shorter ones. SJF, on the other hand, prioritizes processes with
the shortest burst time, resulting in minimized average waiting times. However, it can lead
to starvation for longer processes.

SRTF is a preemptive variant of SJF where the CPU is allocated to the process with the
shortest remaining burst time. This algorithm provides even better performance in terms of
average waiting times but requires frequent context switching, which can incur overhead.

Round Robin scheduling is a popular algorithm that assigns fixed time slices to each process
in a circular manner. This ensures fairness among processes and prevents starvation.
However, it may lead to increased waiting times for long processes, known as the "convoy
effect."

Preemptive scheduling algorithms allow the interruption of executing processes to


allocate CPU time to higher-priority processes. This ensures better responsiveness and
prioritization of critical tasks. Non-preemptive algorithms, on the other hand, allow a
process to hold the CPU until it voluntarily releases it, ensuring simplicity but potentially
hindering system responsiveness.
This abstract presents a comparative analysis of these CPU scheduling algorithms,
highlighting their advantages, disadvantages, and scenarios where they are best suited.
Furthermore, we discuss metrics for evaluating their performance, such as average waiting
time, response time, throughput, and fairness. By understanding the strengths and
weaknesses of each algorithm, system designers and developers can make informed
decisions when selecting the most appropriate scheduling strategy based on specific system
requirements and workload characteristics.

Keywords: CPU scheduling, First-Come-First-Serve, Shortest Job First, Shortest


Remaining Time First, Round Robin, Preemptive, Non-Preemptive, Comparative Analysis.
Flow of chart(all cpu scheduling algorithms to be included)
+--------+ +---------+
| Start |-------->| Select |
+--------+ |Scheduling |
|Algorithm |
+-------+----+
|
+-------------+--------+
| |
+----+----+ +----+----+
| FCFS | | SJN |
+----+----+ +----+----+
| |
| |
v v
+---------+---------+ +---------+---------+
| Perform FCFS | | Perform SJN |
| Scheduling | | Scheduling |
+----+----+-------+ +----+----+---------+
| |
v v
+----+------+ +----+-------+
| Print FCFS | |Print SJN |
| Results | | Results |
+----+--------+ +-------+----+
| |
v v
+---------+---------+ +---------+---------+
| End | | End |
+---------+---------+ +---------+---------+
| |
| |
+----+-------+ +----+----+
| Priority | | Round |
| Scheduling| | Robin |
+----+-------+ +----+----+
| |
| |
v v
+----+----+---------+ +----+----+---------+
| Perform Priority | | Perform Round |
| Scheduling | | Robin Scheduling
|+----+----+---------+ +----+----+---------
+ | |
v v
+----+----------+ +---------+----+
| Print Priority | | Print Round |
| Scheduling | | Robin Results |
| Results | | |
+----+------------+ +-----------+----+
| |
v v
+---------+---------+ +---------+---------+
| End | | End |
+---------+---------+ +---------+---------+
Flow of Execution of CPU scheduling algorithms:

1. First-Come, First-Served (FCFS):

- The process that arrives first is selected first for execution.

- When a process arrives, it is added to the end of the ready queue.

- The currently running process continues execution until it finishes or is blocked.

- If the running process finishes or is blocked, the next process in the ready queue is
selected for execution.

2. Shortest Job First (SJF):

- The process with the shortest burst time is selected for execution.

- When a process arrives, its burst time is compared with the burst times of the processes
in the ready queue.

- If the arriving process has a shorter burst time, it is selected for execution.

- If the running process finishes or is blocked, the process with the shortest burst time in
the ready queue is selected.

3. Shortest Remaining Time First (SRTF):

- Similar to SJF, but the process with the shortest remaining burst time is selected for
execution.

- When a process arrives, its remaining burst time is compared with the remaining burst
times of the processes in the ready queue.

- If the arriving process has a shorter remaining burst time, it preempts the currently
running process and takes its place.

4. Round Robin (RR):

- Each process is assigned a fixed time slice or quantum.

- The processes are placed in a ready queue.


- The currently running process is allowed to execute for the time slice, and then it is
preempted.

- The next process in the ready queue is selected for execution, and the cycle continues.

- If a process does not finish within its time slice, it is moved to the end of the ready queue.

5. Preemptive Scheduling:

- The CPU can be preempted or taken away from a running process.

- Processes can be interrupted and replaced by higher-priority processes or processes with


shorter burst times.

- The scheduler determines when to preempt a running process based on predefined


criteria.

6. Non-Preemptive Scheduling:

- Once a process starts execution, it is not interrupted until it finishes or is blocked.

- The scheduler does not preempt a running process and allows it to run until completion.
Implementation

#include <stdio.h>

struct Process {
int process_id;
int burst_time;
int priority;
};
void fcfs(struct Process processes[], int n) {
int waiting_time[n], turnaround_time[n];
waiting_time[0] = 0;
turnaround_time[0] = processes[0].burst_time;
for (int i = 1; i < n; i++) {
waiting_time[i] = processes[i - 1].burst_time + waiting_time[i - 1];
turnaround_time[i] = processes[i].burst_time + waiting_time[i];
}
printf("FCFS:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: Waiting Time = %d, Turnaround Time = %d\n",
processes[i].process_id,
waiting_time[i], turnaround_time[i]);
}
}
void sjn(struct Process processes[], int n) {
int waiting_time[n], turnaround_time[n];
int burst_time_remaining[n];
for (int i = 0; i < n; i++) {
burst_time_remaining[i] = processes[i].burst_time;
}
int complete = 0;
int current_time = 0;
int min_burst_index = 0;
while (complete != n) {
for (int i = 0; i < n; i++) {
if (processes[i].burst_time < processes[min_burst_index].burst_time &&
burst_time_remaining[i] > 0) {
min_burst_index = i;
}
}
burst_time_remaining[min_burst_index] -= 1;
if (burst_time_remaining[min_burst_index] == 0) {
complete += 1;
int end_time = current_time + 1;
waiting_time[min_burst_index] = end_time - processes[min_burst_index].burst_time;
turnaround_time[min_burst_index] = end_time;
min_burst_index = (min_burst_index + 1) % n;
}

current_time += 1;
}
printf("\nSJN:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: Waiting Time = %d, Turnaround Time = %d\n", processes[i].process_id,
waiting_time[i], turnaround_time[i]);
}
}
void priority_scheduling(struct Process processes[], int n) {
int waiting_time[n], turnaround_time[n];
int burst_time_remaining[n];
for (int i = 0; i < n; i++) {
burst_time_remaining[i] = processes[i].burst_time;
}
int complete = 0;
int current_time = 0;
int min_priority_index = 0;

while (complete != n) {
for (int i = 0; i < n; i++) {
if (processes[i].priority < processes[min_priority_index].priority &&
burst_time_remaining[i] > 0) {
min_priority_index = i;
}
}
burst_time_remaining[min_priority_index] -= 1;
if (burst_time_remaining[min_priority_index] == 0) {
complete += 1;
int end_time = current_time + 1;
waiting_time[min_priority_index] = end_time - processes[min_priority_index].burst_time;
turnaround_time[min_priority_index] = end_time;
min_priority_index = (min_priority_index + 1) % n;
}
current_time += 1;
}
printf("\nPriority Scheduling:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: Waiting Time = %d, Turnaround Time = %d\n", processes[i].process_id,
waiting_time[i], turnaround_time[i]);
}
}
void round_robin(struct Process processes[], int n, int time_quantum) {
int remaining_burst_time[n];
for (int i = 0; i < n; i++) {
remaining_burst_time[i] = processes[i].burst_time;
}
int waiting_time[n], turnaround_time[n];
int current_time = 0;
int complete = 0;
while (complete != n) {
for (int i = 0; i < n; i++) {
if (remaining_burst_time[i] > time_quantum) {
remaining_burst_time[i] -= time_quantum;
current_time += time_quantum;
}
else if (remaining_burst_time[i] > 0) {
current_time += remaining_burst_time[i];
waiting_time[i] = current_time - processes[i].burst_time;
remaining_burst_time[i] = 0;
complete += 1;
}
}
}
for (int i = 0; i < n; i++) {
turnaround_time[i] = processes[i].burst_time + waiting_time[i];
}
printf("\nRound Robin:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: Waiting Time = %d, Turnaround Time = %d\n", processes[i].process_id,
waiting_time[i], turnaround_time[i]);
}
}

int main() {
struct Process processes[] = {
{1, 10, 2},
{2, 5, 0},
{3, 8, 1},
{4, 6, 3}
};
int n = sizeof(processes) / sizeof(processes[0]);
int time_quantum = 3;

fcfs(processes, n);
sjn(processes, n);
priority_scheduling(processes, n);
round_robin(processes, n, time_quantum);

return 0;
}
Output:

FCFS:
Process 1: Waiting Time = 0, Turnaround Time = 10
Process 2: Waiting Time = 10, Turnaround Time = 15
Process 3: Waiting Time = 15, Turnaround Time = 23
Process 4: Waiting Time = 23, Turnaround Time = 29

SJN:
Process 1: Waiting Time = 5, Turnaround Time = 15
Process 2: Waiting Time = 0, Turnaround Time = 5
Process 3: Waiting Time = 15, Turnaround Time = 23
Process 4: Waiting Time = 23, Turnaround Time = 29

Priority Scheduling:
Process 1: Waiting Time = 5, Turnaround Time = 15
Process 2: Waiting Time = 15, Turnaround Time = 20
Process 3: Waiting Time = 0, Turnaround Time = 8
Process 4: Waiting Time = 23, Turnaround Time = 29

Round Robin:
Process 1: Waiting Time = 6, Turnaround Time = 16
Process
Conculsion

1. First Come First Serve (FCFS):

- Processes are executed in the order they arrive.

- Simple and easy to implement.

- May lead to longer waiting times for processes with larger burst times.

2. Shortest Job First (SJF):

- The process with the smallest burst time is executed first.

- Provides the minimum average waiting time.

- Requires knowledge of the burst time in advance, which is often unrealistic.

3. Shortest Remaining Time First (SRTF):

- The process with the smallest remaining burst time is executed first.

- Dynamically adapts to new processes with shorter burst times.

- Requires preemption and frequent context switching, which can incur overhead.

4. Round Robin (RR):

- Each process is given a fixed time slice (quantum) to execute.

- Provides fair sharing of the CPU among processes.

- May result in higher waiting times for long-running processes.

5. Preemptive Scheduling:

- Allows a process to be interrupted and later resumed by a higher-priority process.

- Ensures that important tasks are executed promptly.

- Requires efficient process management and may lead to frequent context switching.

6. Non-preemptive Scheduling:

- Once a process starts executing, it cannot be interrupted until it completes.

- Simplifies process management and reduces context switching overhead.

- May result in delayed execution of high-priority processes.

You might also like