Os Unit 2 Project
Os Unit 2 Project
PROJECT REPORT
Roll No.: 23891A67H6 Name: Pranav Jella Branch: Data Science
Project Title:
Problem Statement:
In modern operating systems, efficient CPU scheduling is crucial for optimizing the performance of
processes.
The Shortest Remaining Time Next (SRTN) scheduling policy is a preemptive scheduling algorithm
that selects the process with the smallest remaining time to execute next.
This algorithm can lead to improved turnaround times and reduced waiting times for processes.
The challenge is to implement the SRTN scheduling policy while considering the arrival times of
processes, ensuring that the CPU always executes the process with the least remaining time.
Introduction:
CPU scheduling is a fundamental aspect of operating systems that determines how processes are
assigned to the CPU for execution.
The SRTN scheduling policy is an extension of the Shortest Job Next (SJN) algorithm, which is non-
preemptive. SRTN, however, allows for preemption, meaning that if a new process arrives with a
shorter remaining time than the currently running process, the CPU will switch to the new
process.
This approach can significantly enhance system responsiveness and efficiency, particularly in
environments with varying process lengths and arrival times
Concepts used :
1. Process Structure: A data structure is defined to hold essential information about each process,
including its ID, arrival time, burst time, remaining time, completion time, waiting time, and
turnaround time.
2. Preemptive Scheduling: The SRTN algorithm is implemented in a preemptive manner, allowing the
CPU to interrupt a currently running process if a new process arrives with a shorter remaining
time.
3. Time Management: The algorithm simulates the passage of time, checking at each time unit which
process should be executed based on the remaining time and arrival times.
4. Completion and Turnaround Calculations: Once a process completes execution, the algorithm
calculates its completion time, turnaround time (the total time taken from arrival to completion),
and waiting time (the total time spent waiting in the ready queue).
5. Input Handling: The program takes user input for the number of processes and their
respective arrival and burst times, allowing for dynamic scheduling based on user-defined
Page 1 of 4
VIGNAN INSTITUTE OF TECHNOLOGY AND SCIENCE
(AN AUTONOMOUS INSTITUTION)
Deshmukhi (V), Pochampally (M), Yadadri Bhuvanagiri Dist., TS-508284
scenarios.
Code:
#include <stdio.h>
#define MAX_PROCESSES 100
typedef struct {
int id; // Process ID
int arrival; // Arrival time
int burst; // Burst time
int remaining; // Remaining time
int completion; // Completion time
int waiting; // Waiting time
int turnaround; // Turnaround time
} Process;
void calculateTimes(Process processes[], int n) {
int total_time = 0;
int completed = 0;
int min_remaining_time = 9999;
int shortest_index = -1;
int finish_time;
int is_completed[MAX_PROCESSES] = {0};
while (completed < n) {
// Find the process with the shortest remaining time
for (int i = 0; i < n; i++) {
if (processes[i].arrival <= total_time && !is_completed[i] && processes[i].remaining <
min_remaining_time) {
min_remaining_time = processes[i].remaining;
shortest_index = i;
}
}
if (shortest_index != -1) {
processes[shortest_index].remaining--;
min_remaining_time--;
if (processes[shortest_index].remaining == 0) {
finish_time = total_time + 1;
processes[shortest_index].completion = finish_time;
processes[shortest_index].turnaround = finish_time - processes[shortest_index].arrival;
processes[shortest_index].waiting = processes[shortest_index].turnaround -
processes[shortest_index].burst;
is_completed[shortest_index] = 1;
completed++;
}
Page 2 of 4
VIGNAN INSTITUTE OF TECHNOLOGY AND SCIENCE
(AN AUTONOMOUS INSTITUTION)
Deshmukhi (V), Pochampally (M), Yadadri Bhuvanagiri Dist., TS-508284
total_time++;
min_remaining_time = 9999; // Reset for the next iteration
shortest_index = -1; // Reset for the next iteration
}
}
void printProcessDetails(Process processes[], int n) {
printf("Process ID\tArrival Time\tBurst Time\tCompletion Time\tTurnaround Time\tWaiting Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n",
processes[i].id,
processes[i].arrival,
processes[i].burst,
processes[i].completion,
processes[i].turnaround,
processes[i].waiting);
}
}
int main() {
Process processes[MAX_PROCESSES];
int n;
printf("Enter number of processes: ");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
processes[i].id = i + 1; // Assigning process ID
printf("Enter arrival time and burst time for process %d: ", i + 1);
scanf("%d %d", &processes[i].arrival, &processes[i].burst);
processes[i].remaining = processes[i].burst; // Initialize remaining time
}
calculateTimes(processes, n);
printProcessDetails(processes, n);
return 0;
}
Expected output:
Enter number of processes: 3
Enter arrival time and burst time for process 1: 0 8
Enter arrival time and burst time for process 2: 1 4
Enter arrival time and burst time for process 3: 2 9
Page 3 of 4
VIGNAN INSTITUTE OF TECHNOLOGY AND SCIENCE
(AN AUTONOMOUS INSTITUTION)
Deshmukhi (V), Pochampally (M), Yadadri Bhuvanagiri Dist., TS-508284
Process ID Arrival Time Burst Time Completion Time Turnaround Time Waiting Time
1 0 8 12 12 4
2 1 4 5 4 0
3 2 9 21 19 10
Conclusion:
The implementation of the Shortest Remaining Time Next (SRTN) CPU scheduling policy
demonstrates the effectiveness of preemptive scheduling in optimizing process management in
operating systems.
By prioritizing processes with the shortest remaining time, SRTN can lead to reduced waiting
times and improved turnaround times, enhancing overall system performance.
This implementation serves as a foundational example of how scheduling algorithms can be
applied in real-world scenarios, providing insights into the complexities of process management in
multitasking environments.
Future work could involve extending this implementation to handle more complex scenarios, such
as varying priorities, I/O-bound processes, and multi-core scheduling.
Page 4 of 4