0% found this document useful (0 votes)
7 views

Sjf Scheduling Algorithm

The document provides C code implementations for both Non-Preemptive and Preemptive Shortest Job First (SJF) scheduling algorithms. It includes structures for process management and functions to calculate waiting and turnaround times, along with sorting mechanisms. The main function demonstrates the execution of both scheduling algorithms with sample process data.

Uploaded by

level1pratik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Sjf Scheduling Algorithm

The document provides C code implementations for both Non-Preemptive and Preemptive Shortest Job First (SJF) scheduling algorithms. It includes structures for process management and functions to calculate waiting and turnaround times, along with sorting mechanisms. The main function demonstrates the execution of both scheduling algorithms with sample process data.

Uploaded by

level1pratik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SJF Scheduling Algorithm in C (Non-Preemptive and Preemptive)

Non-Preemptive SJF Scheduling Algorithm (C Code):

#include <stdio.h>

#include <stdlib.h>

typedef struct {

int processID;

int arrivalTime;

int burstTime;

int completionTime;

int waitingTime;

int turnaroundTime;

} Process;

void sortByBurstTime(Process processes[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (processes[i].burstTime > processes[j].burstTime) {

Process temp = processes[i];

processes[i] = processes[j];

processes[j] = temp;

}
}

void nonPreemptiveSJF(Process processes[], int n) {

int totalWaitingTime = 0, totalTurnaroundTime = 0;

sortByBurstTime(processes, n);

int completionTime = 0;

for (int i = 0; i < n; i++) {

if (processes[i].arrivalTime > completionTime) {

completionTime = processes[i].arrivalTime;

processes[i].completionTime = completionTime + processes[i].burstTime;

processes[i].turnaroundTime = processes[i].completionTime - processes[i].arrivalTime;

processes[i].waitingTime = processes[i].turnaroundTime - processes[i].burstTime;

totalWaitingTime += processes[i].waitingTime;

totalTurnaroundTime += processes[i].turnaroundTime;

completionTime = processes[i].completionTime;

printf("\nNon-Preemptive SJF Scheduling:\n");

printf("ProcessID\tArrivalTime\tBurstTime\tWaitingTime\tTurnaroundTime\n");

for (int i = 0; i < n; i++) {

printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].processID, processes[i].arrivalTime,


processes[i].burstTime, processes[i].waitingTime, processes[i].turnaroundTime);

printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime / n);

printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);

int main() {

Process processes[] = {

{1, 0, 6, 0, 0, 0},

{2, 1, 8, 0, 0, 0},

{3, 2, 7, 0, 0, 0},

{4, 3, 3, 0, 0, 0}

};

int n = sizeof(processes) / sizeof(processes[0]);

nonPreemptiveSJF(processes, n);

return 0;

Preemptive SJF (SRTF) Scheduling Algorithm (C Code):

#include <stdio.h>

#include <stdlib.h>
typedef struct {

int processID;

int arrivalTime;

int burstTime;

int remainingTime;

int completionTime;

int waitingTime;

int turnaroundTime;

int startTime;

} Process;

void sortByArrivalTime(Process processes[], int n) {

for (int i = 0; i < n - 1; i++) {

for (int j = i + 1; j < n; j++) {

if (processes[i].arrivalTime > processes[j].arrivalTime) {

Process temp = processes[i];

processes[i] = processes[j];

processes[j] = temp;

void preemptiveSJF(Process processes[], int n) {

int totalWaitingTime = 0, totalTurnaroundTime = 0;

int completed = 0;
int currentTime = 0;

int shortest = -1;

sortByArrivalTime(processes, n);

while (completed < n) {

shortest = -1;

int minRemainingTime = 99999;

for (int i = 0; i < n; i++) {

if (processes[i].arrivalTime <= currentTime && processes[i].remainingTime > 0 &&

processes[i].remainingTime < minRemainingTime) {

minRemainingTime = processes[i].remainingTime;

shortest = i;

if (shortest == -1) {

currentTime++;

continue;

processes[shortest].remainingTime--;

if (processes[shortest].remainingTime == 0) {

completed++;

processes[shortest].completionTime = currentTime + 1;
processes[shortest].turnaroundTime = processes[shortest].completionTime -

processes[shortest].arrivalTime;

processes[shortest].waitingTime = processes[shortest].turnaroundTime -

processes[shortest].burstTime;

totalWaitingTime += processes[shortest].waitingTime;

totalTurnaroundTime += processes[shortest].turnaroundTime;

currentTime++;

printf("\nPreemptive SJF (SRTF) Scheduling:\n");

printf("ProcessID\tArrivalTime\tBurstTime\tWaitingTime\tTurnaroundTime\n");

for (int i = 0; i < n; i++) {

printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].processID, processes[i].arrivalTime,

processes[i].burstTime, processes[i].waitingTime, processes[i].turnaroundTime);

printf("\nAverage Waiting Time: %.2f\n", (float)totalWaitingTime / n);

printf("Average Turnaround Time: %.2f\n", (float)totalTurnaroundTime / n);

int main() {

Process processes[] = {

{1, 0, 6, 6, 0, 0, 0, 0},

{2, 1, 8, 8, 0, 0, 0, 0},
{3, 2, 7, 7, 0, 0, 0, 0},

{4, 3, 3, 3, 0, 0, 0, 0}

};

int n = sizeof(processes) / sizeof(processes[0]);

preemptiveSJF(processes, n);

return 0;

You might also like