0% found this document useful (0 votes)
42 views11 pages

Experiment 5 - Scheduling Algorithms

The document outlines an experiment on CPU scheduling algorithms, including implementations for First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each algorithm is accompanied by C code that calculates waiting time, turnaround time, and averages for multiple processes based on user input. The document provides detailed code snippets and expected outputs for each scheduling method.

Uploaded by

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

Experiment 5 - Scheduling Algorithms

The document outlines an experiment on CPU scheduling algorithms, including implementations for First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin, and Priority Scheduling. Each algorithm is accompanied by C code that calculates waiting time, turnaround time, and averages for multiple processes based on user input. The document provides detailed code snippets and expected outputs for each scheduling method.

Uploaded by

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

Experiment 5: Scheduling Algorithms

Objective: Explore CPU scheduling algorithms.

Name: Gunjan Vijay Choube


Batch : B1
Roll no.: 02

Tasks:

1] Implement FCFS (First-Come, First-Served) scheduling


algorithm.

Code:-

#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of processes: ");
scanf("%d", &n);
int burst_time[n], waiting_time[n], turnaround_time[n];
// Taking burst time input
printf("Enter burst time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
// Waiting time for the first process is 0
waiting_time[0] = 0;
// Calculate waiting time for each process
for (i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}
// Calculate turnaround time for each process
for (i = 0; i < n; i++) {
turnaround_time[i] = waiting_time[i] + burst_time[i];
}
// Calculate total turnaround time and total waiting time for averaging
int total_tat = 0;
int total_wt = 0;
for (i = 0; i < n; i++) {
total_tat += turnaround_time[i];
total_wt += waiting_time[i];
}
// Calculate average turnaround time and average waiting time
float avg_tat = (float)total_tat / n;
float avg_wt = (float)total_wt / n;
// Print results
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], waiting_time[i],
turnaround_time[i]);
}
// Print average turnaround time and average waiting time
printf("\nAverage Turnaround Time: %.2f\n", avg_tat);
printf("Average Waiting Time: %.2f\n", avg_wt);
return 0;
}

Output :-
2] Implement SJF (Shortest Job First) scheduling algorithm.

Code :

#include <stdio.h>

// Function to swap values


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int n, i, j;
printf("Enter the number of processes: ");
scanf("%d", &n);

int process[n], burst_time[n], waiting_time[n], turnaround_time[n];

// Initialize process numbers


for (i = 0; i < n; i++) {
process[i] = i + 1; // Process ID
}

// Taking burst time input


printf("Enter burst time for each process:\n");
for (i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
}
// Sort processes based on burst time (using simple bubble sort for
demonstration)
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (burst_time[j] > burst_time[j + 1]) {
// Swap burst times
swap(&burst_time[j], &burst_time[j + 1]);
// Swap process numbers as well to keep track
swap(&process[j], &process[j + 1]);
}
}
}

// Waiting time for the first process is 0


waiting_time[0] = 0;

// Calculate waiting time for each process


for (i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}

// Calculate turnaround time for each process


for (i = 0; i < n; i++) {
turnaround_time[i] = waiting_time[i] + burst_time[i];
}

// Calculate total turnaround time and total waiting time for averaging
int total_tat = 0;
int total_wt = 0;
for (i = 0; i < n; i++) {
total_tat += turnaround_time[i];
total_wt += waiting_time[i];
}

// Calculate average turnaround time and average waiting time


float avg_tat = (float)total_tat / n;
float avg_wt = (float)total_wt / n;
// Print results
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", process[i], burst_time[i], waiting_time[i],
turnaround_time[i]);
}

// Print average turnaround time and average waiting time


printf("\nAverage Turnaround Time: %.2f\n", avg_tat);
printf("Average Waiting Time: %.2f\n", avg_wt);

return 0;
}

Output :-

3] Implement Round Robin scheduling algorithm.

Code :-
#include <stdio.h>

int main() {
int n, tq, time = 0;
printf("Enter the number of processes: ");
scanf("%d", &n);

int process[n], burst_time[n], remaining_bt[n], waiting_time[n],


turnaround_time[n];

// Input burst times and initialize remaining burst times and waiting times
for (int i = 0; i < n; i++) {
process[i] = i + 1;
printf("Enter burst time for Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
remaining_bt[i] = burst_time[i];
waiting_time[i] = 0;
}

printf("Enter the time quantum: ");


scanf("%d", &tq);

// Round Robin execution


int done;
while (1) {
done = 1;
for (int i = 0; i < n; i++) {
if (remaining_bt[i] > 0) {
done = 0;

// Process execution for time quantum


if (remaining_bt[i] > tq) {
time += tq;
remaining_bt[i] -= tq;
} else {
time += remaining_bt[i];
waiting_time[i] = time - burst_time[i];
remaining_bt[i] = 0;
}
}
}
if (done) break; // Break when all processes are done
}

// Calculate turnaround times


int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; i++) {
turnaround_time[i] = burst_time[i] + waiting_time[i];
total_wt += waiting_time[i];
total_tat += turnaround_time[i];
}

// Print results
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\n", process[i], burst_time[i], waiting_time[i],
turnaround_time[i]);
}

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


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

return 0;
}

Output :-
4] Implement Priority Scheduling algorithm.

Code:-

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int n, i, j;
printf("Enter the number of processes: ");
scanf("%d", &n);

int process[n], burst_time[n], priority[n], waiting_time[n],


turnaround_time[n];

// Initialize process numbers and take burst time and priority input
for (i = 0; i < n; i++) {
process[i] = i + 1; // Process ID
printf("Enter burst time for Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
printf("Enter priority for Process %d: ", i + 1);
scanf("%d", &priority[i]);
}

// Sort processes based on priority (lower priority value means higher


priority)
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (priority[j] > priority[j + 1]) {
// Swap priority
swap(&priority[j], &priority[j + 1]);
// Swap burst times to match the new order
swap(&burst_time[j], &burst_time[j + 1]);
// Swap process numbers to maintain correct mapping
swap(&process[j], &process[j + 1]);
}
}
}

// Waiting time for the first process is 0


waiting_time[0] = 0;

// Calculate waiting time for each process


for (i = 1; i < n; i++) {
waiting_time[i] = waiting_time[i - 1] + burst_time[i - 1];
}

// Calculate turnaround time for each process


for (i = 0; i < n; i++) {
turnaround_time[i] = waiting_time[i] + burst_time[i];
}

// Calculate total turnaround time and total waiting time for averaging
int total_tat = 0;
int total_wt = 0;
for (i = 0; i < n; i++) {
total_tat += turnaround_time[i];
total_wt += waiting_time[i];
}

// Calculate average turnaround time and average waiting time


float avg_tat = (float)total_tat / n;
float avg_wt = (float)total_wt / n;

// Print results
printf("\nProcess\tPriority\tBurst Time\tWaiting Time\tTurnaround Time\n");
for (i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", process[i], priority[i], burst_time[i],
waiting_time[i], turnaround_time[i]);
}

// Print average turnaround time and average waiting time


printf("\nAverage Turnaround Time: %.2f\n", avg_tat);
printf("Average Waiting Time: %.2f\n", avg_wt);

return 0;
}
Output :-

You might also like