Skip to content

Commit

Permalink
added the SRTF algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
rdx40 committed Jan 6, 2025
1 parent 9a3ba51 commit fc43cee
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
76 changes: 76 additions & 0 deletions CPU Scheduling Algorithms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,79 @@ The **SJF** scheduling algorithm follows a non-preemptive approach where process
To run this program, you need:
- A C compiler like `gcc`.
- Basic command-line skills for compiling and executing C programs.




# Process Scheduling Simulation in C (Shortest Remaining Time First Algorithm)

## Table of Contents
- [Algorithm Overview](#algorithm-overview)
- [Features](#features)
- [Shortest Remaining Time First (SRTF) Algorithm](#shortest-remaining-time-first-srtf-algorithm)
- [Detailed Explanation of Functions](#detailed-explanation-of-functions)
- [Requirements](#requirements)
- [Installation and Execution](#installation-and-execution)
- [Example Usage](#example-usage)
- [Output Explanation](#output-explanation)
- [Memory Management](#memory-management)
- [Code Structure and Files](#code-structure-and-files)
- [Contributing](#contributing)
- [License](#license)

## Algorithm Overview

This algorithm simulates a **Shortest Remaining Time First (SRTF) scheduling** algorithm for processes in an operating system. In SRTF scheduling, the CPU selects the process with the shortest remaining burst time first, ensuring that processes with shorter remaining burst times are executed first. If a new process arrives with a shorter burst time than the currently running process, the CPU is preempted to execute the new process.

The SRTF algorithm is a **preemptive scheduling algorithm** that is particularly efficient in reducing waiting time, as it prioritizes processes with the least remaining time.

## Features

- **Preemptive Scheduling**: The process with the shortest remaining burst time is executed first. If a new process arrives with a shorter remaining burst time than the current one, it preempts the current process.
- **Calculates Response Time** (time between arrival and first execution).
- **Calculates Turnaround Time** (total time from arrival to completion).
- **Calculates Waiting Time** (time spent waiting in the queue).
- **Displays Results** in a well-formatted table, showing calculated metrics for each process.

## Shortest Remaining Time First (SRTF) Algorithm

The **SRTF** scheduling algorithm works as follows:

1. **Preemptive Execution**: The CPU always executes the process with the shortest remaining burst time. If a new process arrives with a shorter remaining burst time than the current one, the CPU is preempted, and the new process is executed.
2. **Sorting**: Initially, processes are sorted by their arrival time.
3. **Execution**: The system executes processes based on their remaining burst times. At each unit of time, it chooses the process with the shortest remaining burst time.
4. **Metric Calculation**: For each process, the program calculates:
- **Start Time**: The time when the process starts execution.
- **Completion Time**: The time when the process finishes execution.
- **Response Time**: The time difference between the start time and arrival time.
- **Turnaround Time**: The time difference between the completion time and arrival time.
- **Waiting Time**: The time difference between turnaround time and burst time.

## Detailed Explanation of Functions

### 1. `find_response(struct Process* input, int n)`
- Calculates the **response time** for each process.
- Formula: **Response Time** = `start time - arrival time`

### 2. `find_tat(struct Process* input, int n)`
- Calculates the **turnaround time** for each process.
- Formula: **Turnaround Time** = `completion time - arrival time`

### 3. `find_wt(struct Process* input, int* tat, int n)`
- Calculates the **waiting time** for each process.
- Formula: **Waiting Time** = `turnaround time - burst time`

### 4. `schedule(struct Process* input, int n)`
- Simulates the **Shortest Remaining Time First** scheduling.
- The processes are sorted initially by their arrival time.
- At each unit of time, the process with the shortest remaining burst time is executed.
- The start and completion times for each process are calculated dynamically as processes are executed.

### 5. `comp(const void* a, const void* b)`
- Comparison function used by `qsort` to sort processes by arrival time, and by ID if arrival times are equal.

## Requirements

To run this program, you need:
- A C compiler like `gcc`.
- Basic command-line skills for compiling and executing C programs.
117 changes: 117 additions & 0 deletions CPU Scheduling Algorithms/SRTF.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
#include <stdio.h>
#include <stdlib.h>

// Structure to hold each process's details
struct Process {
int id, bt, at, start, comp, remaining_bt;
};

// Comparison function for sorting processes by arrival time
int comp(const void* a, const void* b) {
struct Process* p1 = (struct Process*)a;
struct Process* p2 = (struct Process*)b;
if (p1->at == p2->at)
return p1->id - p2->id; // If arrival times are the same, sort by ID
return p1->at - p2->at; // Sort by arrival time otherwise
}

// Function to calculate the response time of each process
int* find_response(struct Process* input, int n) {
int* rt = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
rt[i] = input[i].start - input[i].at;
}
return rt;
}

// Function to calculate the turnaround time of each process
int* find_tat(struct Process* input, int n) {
int* tat = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
tat[i] = input[i].comp - input[i].at;
}
return tat;
}

// Function to calculate the waiting time of each process
int* find_wt(struct Process* input, int* tat, int n) {
int* wt = (int*)malloc(n * sizeof(int));
for (int i = 0; i < n; i++) {
wt[i] = tat[i] - input[i].bt;
}
return wt;
}

// Function to simulate the SRTF Scheduling Algorithm
void schedule(struct Process* input, int n) {
qsort(input, n, sizeof(struct Process), comp); // Sort processes by arrival time

int last_time = 0; // Keeps track of the current time
int remaining_processes = n; // Number of remaining processes
int completed = 0;

while (completed < n) {
int min_bt = __INT_MAX__;
int idx = -1;

// Find the process with the shortest remaining burst time
for (int i = 0; i < n; i++) {
if (input[i].at <= last_time && input[i].remaining_bt > 0 && input[i].remaining_bt < min_bt) {
min_bt = input[i].remaining_bt;
idx = i;
}
}

if (idx != -1) {
if (input[idx].remaining_bt == input[idx].bt) {
input[idx].start = last_time; // Set start time for the first execution
}

// Execute the process for 1 unit of time
input[idx].remaining_bt--;
last_time++;

// If the process is completed
if (input[idx].remaining_bt == 0) {
input[idx].comp = last_time; // Set completion time
completed++;
}
} else {
last_time++; // If no process is ready, increment time
}
}
}

// Main program to test the SRTF algorithm
int main() {
int n;
printf("Enter the number of processes: \n");
scanf("%d", &n);

struct Process* input = (struct Process*)malloc(n * sizeof(struct Process));
for (int i = 0; i < n; i++) {
printf("Enter the id, burst time and arrival time of process %d:\n", i + 1);
scanf("%d %d %d", &input[i].id, &input[i].bt, &input[i].at);
input[i].remaining_bt = input[i].bt; // Initialize remaining burst time
}

schedule(input, n);

int* response = find_response(input, n);
int* turntime = find_tat(input, n);
int* waittime = find_wt(input, turntime, n);

printf("%8s %8s %8s %8s %12s %10s %12s %8s\n", "Process", "Burst", "Arrival", "Start", "Completion", "Response", "Turn Around", "Waiting");
for (int i = 0; i < n; i++) {
printf("%8s%d %8d %8d %8d %12d %10d %12d %8d\n",
"P", input[i].id, input[i].bt, input[i].at,
input[i].start, input[i].comp, response[i],
turntime[i], waittime[i]);
}

free(input);
free(response);
free(turntime);
free(waittime);
return 0;
}

0 comments on commit fc43cee

Please sign in to comment.