Skip to content

Commit 9a3ba51

Browse files
committed
added SJF algo in CPU sched algos
1 parent f4cdf26 commit 9a3ba51

File tree

2 files changed

+214
-0
lines changed

2 files changed

+214
-0
lines changed

CPU Scheduling Algorithms/README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,135 @@ The **FCFS** scheduling algorithm follows a non-preemptive approach where proces
6767
To run this program, you need:
6868
- A C compiler like `gcc`.
6969
- Basic command-line skills for compiling and executing C programs.
70+
71+
72+
73+
74+
75+
76+
# Process Scheduling Simulation in C (Shortest Job First Algorithm)
77+
78+
## Table of Contents
79+
- [Algorithm Overview](#algorithm-overview)
80+
- [Features](#features)
81+
- [Shortest Job First (SJF) Algorithm](#shortest-job-first-sjf-algorithm)
82+
- [Detailed Explanation of Functions](#detailed-explanation-of-functions)
83+
- [Requirements](#requirements)
84+
- [Installation and Execution](#installation-and-execution)
85+
- [Example Usage](#example-usage)
86+
- [Output Explanation](#output-explanation)
87+
- [Memory Management](#memory-management)
88+
- [Code Structure and Files](#code-structure-and-files)
89+
- [Contributing](#contributing)
90+
- [License](#license)
91+
92+
## Algorithm Overview
93+
94+
This algorithm simulates a **Shortest Job First (SJF) scheduling** algorithm for processes in an operating system. In SJF scheduling, the CPU selects the process with the shortest burst time first, ensuring that shorter processes are completed before longer ones. This code calculates and displays essential metrics like **response time**, **turnaround time**, and **waiting time** for each process.
95+
96+
The SJF algorithm is a non-preemptive scheduling algorithm and is particularly efficient when short processes need to be prioritized for faster completion.
97+
98+
## Features
99+
100+
- **Sorts processes by burst time** to ensure the Shortest Job First order.
101+
- **Calculates Response Time** (time between arrival and first execution).
102+
- **Calculates Turnaround Time** (total time from arrival to completion).
103+
- **Calculates Waiting Time** (time spent waiting in the queue).
104+
- **Displays Results** in a well-formatted table, showing calculated metrics for each process.
105+
106+
## Shortest Job First (SJF) Algorithm
107+
108+
The **SJF** scheduling algorithm follows a non-preemptive approach where processes are executed based on the shortest burst time first. Here’s how it works in this program:
109+
110+
1. **Sorting by Burst Time**: The program first sorts all processes by their burst time. If two processes have the same burst time, they are further sorted by their arrival time.
111+
2. **Sequential Execution**: Each process begins execution as soon as the CPU is available, based on the completion of the previous process.
112+
3. **Metric Calculation**: For each process, the program calculates:
113+
- **Start Time**: When the process begins execution.
114+
- **Completion Time**: When the process finishes execution.
115+
- **Response Time**: Difference between start time and arrival time.
116+
- **Turnaround Time**: Difference between completion time and arrival time.
117+
- **Waiting Time**: Difference between turnaround time and burst time.
118+
119+
## Detailed Explanation of Functions
120+
121+
### 1. `find_tat(struct Process* input, int n)`
122+
- Calculates the **turnaround time** for each process.
123+
- Formula: **Turnaround Time** = `completion time - arrival time`
124+
125+
### 2. `find_wt(struct Process* input, int n)`
126+
- Calculates the **waiting time** for each process.
127+
- Formula: **Waiting Time** = `turnaround time - burst time`
128+
129+
### 3. `schedule(struct Process* input, int n)`
130+
- Sorts processes by burst time using the SJF approach.
131+
- Determines **start time** and **completion time** for each process based on its burst time and order of execution.
132+
133+
### 4. `comp(const void* a, const void* b)`
134+
- Comparison function used by `qsort` to sort processes by burst time, and by arrival time if burst times are equal.
135+
136+
## Requirements
137+
# Process Scheduling Simulation in C (Shortest Job First Algorithm)
138+
139+
## Table of Contents
140+
- [Algorithm Overview](#algorithm-overview)
141+
- [Features](#features)
142+
- [Shortest Job First (SJF) Algorithm](#shortest-job-first-sjf-algorithm)
143+
- [Detailed Explanation of Functions](#detailed-explanation-of-functions)
144+
- [Requirements](#requirements)
145+
- [Installation and Execution](#installation-and-execution)
146+
- [Example Usage](#example-usage)
147+
- [Output Explanation](#output-explanation)
148+
- [Memory Management](#memory-management)
149+
- [Code Structure and Files](#code-structure-and-files)
150+
- [Contributing](#contributing)
151+
- [License](#license)
152+
153+
## Algorithm Overview
154+
155+
This algorithm simulates a **Shortest Job First (SJF) scheduling** algorithm for processes in an operating system. In SJF scheduling, the CPU selects the process with the shortest burst time first, ensuring that shorter processes are completed before longer ones. This code calculates and displays essential metrics like **response time**, **turnaround time**, and **waiting time** for each process.
156+
157+
The SJF algorithm is a non-preemptive scheduling algorithm and is particularly efficient when short processes need to be prioritized for faster completion.
158+
159+
## Features
160+
161+
- **Sorts processes by burst time** to ensure the Shortest Job First order.
162+
- **Calculates Response Time** (time between arrival and first execution).
163+
- **Calculates Turnaround Time** (total time from arrival to completion).
164+
- **Calculates Waiting Time** (time spent waiting in the queue).
165+
- **Displays Results** in a well-formatted table, showing calculated metrics for each process.
166+
167+
## Shortest Job First (SJF) Algorithm
168+
169+
The **SJF** scheduling algorithm follows a non-preemptive approach where processes are executed based on the shortest burst time first. Here’s how it works in this program:
170+
171+
1. **Sorting by Burst Time**: The program first sorts all processes by their burst time. If two processes have the same burst time, they are further sorted by their arrival time.
172+
2. **Sequential Execution**: Each process begins execution as soon as the CPU is available, based on the completion of the previous process.
173+
3. **Metric Calculation**: For each process, the program calculates:
174+
- **Start Time**: When the process begins execution.
175+
- **Completion Time**: When the process finishes execution.
176+
- **Response Time**: Difference between start time and arrival time.
177+
- **Turnaround Time**: Difference between completion time and arrival time.
178+
- **Waiting Time**: Difference between turnaround time and burst time.
179+
180+
## Detailed Explanation of Functions
181+
182+
### 1. `find_tat(struct Process* input, int n)`
183+
- Calculates the **turnaround time** for each process.
184+
- Formula: **Turnaround Time** = `completion time - arrival time`
185+
186+
### 2. `find_wt(struct Process* input, int n)`
187+
- Calculates the **waiting time** for each process.
188+
- Formula: **Waiting Time** = `turnaround time - burst time`
189+
190+
### 3. `schedule(struct Process* input, int n)`
191+
- Sorts processes by burst time using the SJF approach.
192+
- Determines **start time** and **completion time** for each process based on its burst time and order of execution.
193+
194+
### 4. `comp(const void* a, const void* b)`
195+
- Comparison function used by `qsort` to sort processes by burst time, and by arrival time if burst times are equal.
196+
197+
## Requirements
198+
199+
To run this program, you need:
200+
- A C compiler like `gcc`.
201+
- Basic command-line skills for compiling and executing C programs.

CPU Scheduling Algorithms/SJF.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// Structure to hold each process's details
5+
struct Process {
6+
int id, bt, at, start, comp, wt, tat;
7+
};
8+
9+
// Function to calculate the turnaround time of each process
10+
void find_tat(struct Process* input, int n) {
11+
for (int i = 0; i < n; i++) {
12+
input[i].tat = input[i].comp - input[i].at; // Turnaround time = completion time - arrival time
13+
}
14+
}
15+
16+
// Function to calculate the waiting time of each process
17+
void find_wt(struct Process* input, int n) {
18+
for (int i = 0; i < n; i++) {
19+
input[i].wt = input[i].tat - input[i].bt; // Waiting time = turnaround time - burst time
20+
}
21+
}
22+
23+
// Comparison function for sorting processes by burst time and arrival time
24+
int comp(const void* a, const void* b) {
25+
struct Process* p1 = (struct Process*)a;
26+
struct Process* p2 = (struct Process*)b;
27+
28+
// If burst times are equal, sort by arrival time
29+
if (p1->bt == p2->bt)
30+
return p1->at - p2->at; // If burst times are the same, sort by arrival time
31+
32+
return p1->bt - p2->bt; // Sort by burst time otherwise
33+
}
34+
35+
// Function to schedule the processes and determine their start and completion times
36+
void schedule(struct Process* input, int n) {
37+
qsort(input, n, sizeof(struct Process), comp); // Sort processes by burst time
38+
39+
int last_comp = 0; // Tracks the completion time of the last scheduled process
40+
41+
for (int i = 0; i < n; i++) {
42+
if (input[i].at > last_comp) {
43+
input[i].start = input[i].at; // If the process arrives after the last one finishes
44+
} else {
45+
input[i].start = last_comp; // Otherwise, it starts right after the last process
46+
}
47+
48+
input[i].comp = input[i].start + input[i].bt; // Completion time = start time + burst time
49+
last_comp = input[i].comp; // Update last completion time
50+
}
51+
}
52+
53+
int main() {
54+
int n;
55+
printf("Enter the number of processes: \n");
56+
scanf("%d", &n);
57+
58+
struct Process* input = (struct Process*)malloc(n * sizeof(struct Process)); // Array to hold process data
59+
for (int i = 0; i < n; i++) {
60+
printf("Enter the id, burst time and arrival time of process %d:\n", i + 1);
61+
scanf("%d %d %d", &input[i].id, &input[i].bt, &input[i].at); // Input process details
62+
}
63+
64+
schedule(input, n); // Schedule processes based on burst time
65+
find_tat(input, n); // Calculate turnaround times
66+
find_wt(input, n); // Calculate waiting times
67+
68+
// Print table headers with appropriate column spacing
69+
printf("%8s %8s %8s %8s %12s %10s %12s %8s\n", "Process", "Burst", "Arrival", "Start", "Completion", "Turn Around", "Waiting");
70+
71+
// Print each process's details in the formatted table
72+
for (int i = 0; i < n; i++) {
73+
printf("%8s%d %8d %8d %8d %12d %10d %12d %8d\n",
74+
"P", input[i].id, input[i].bt, input[i].at,
75+
input[i].start, input[i].comp,
76+
input[i].tat, input[i].wt);
77+
}
78+
79+
// Free dynamically allocated memory
80+
free(input);
81+
return 0;
82+
}

0 commit comments

Comments
 (0)