0% found this document useful (0 votes)
4 views10 pages

CPU Scheduling Algorithm

The document outlines four CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin (RR), and Priority Scheduling, including their implementation in C code. It compares their average waiting times, turnaround times, and execution times, highlighting strengths and weaknesses of each algorithm. The conclusion emphasizes that the choice of scheduling policy should align with specific workload requirements, balancing factors like efficiency, fairness, and responsiveness.

Uploaded by

2022-3-60-116
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)
4 views10 pages

CPU Scheduling Algorithm

The document outlines four CPU scheduling algorithms: First-Come, First-Served (FCFS), Shortest Job First (SJF), Round Robin (RR), and Priority Scheduling, including their implementation in C code. It compares their average waiting times, turnaround times, and execution times, highlighting strengths and weaknesses of each algorithm. The conclusion emphasizes that the choice of scheduling policy should align with specific workload requirements, balancing factors like efficiency, fairness, and responsiveness.

Uploaded by

2022-3-60-116
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/ 10

CPU Scheduling Algorithm

Lab Tasks: Code and Output.


1. First-Come, First-Served (FCFS) Scheduling:
#include <stdio.h>

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

int bt[n], wt[n], tat[n];


float avg_wt = 0, avg_tat = 0;

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


printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &bt[i]);
}

wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}

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


tat[i] = wt[i] + bt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}

printf("P\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}

printf("Average Waiting Time = %.2f\n", avg_wt / n);


printf("Average Turnaround Time = %.2f\n", avg_tat / n);
return 0;
}

2. Shortest Job First (SJF) Scheduling:

#include <stdio.h>

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

int p[n], bt[n], wt[n], tat[n];


float avg_wt = 0, avg_tat = 0;

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


p[i] = i + 1;
printf("Enter burst time for process %d: ", p[i]);
scanf("%d", &bt[i]);
}
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) {
if(bt[i] > bt[j]) {
int temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}

wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}

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


tat[i] = wt[i] + bt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}

printf("P\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\n", p[i], bt[i], wt[i], tat[i]);
}

printf("Average Waiting Time = %.2f\n", avg_wt / n);


printf("Average Turnaround Time = %.2f\n", avg_tat / n);
return 0;
}
3. Round Robin (RR) Scheduling:

#include <stdio.h>
int main() {
int n, i, time, remain, tq;
printf("Enter number of processes: ");
scanf("%d", &n);

int bt[n], rt[n], wt[n], tat[n];


remain = n;
for(i = 0; i < n; i++) {
printf("Enter burst time for process %d: ", i + 1);
scanf("%d", &bt[i]);
rt[i] = bt[i];
}
printf("Enter time quantum: ");
scanf("%d", &tq);
int t = 0;
while(remain != 0) {
for(i = 0; i < n; i++) {
if(rt[i] > 0) {
if(rt[i] > tq) {
t += tq;
rt[i] -= tq;
} else {
t += rt[i];
wt[i] = t - bt[i];
rt[i] = 0;
remain--;
}
}
}
}
float avg_wt = 0, avg_tat = 0;
printf("P\tBT\tWT\tTAT\n");
for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
avg_wt += wt[i];
avg_tat += tat[i];
printf("P%d\t%d\t%d\t%d\n", i + 1, bt[i], wt[i], tat[i]);
}
printf("Average Waiting Time = %.2f\n", avg_wt / n);
printf("Average Turnaround Time = %.2f\n", avg_tat / n);
return 0;}
4. Priority Scheduling:

#include <stdio.h>

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

int p[n], bt[n], pr[n], wt[n], tat[n];


float avg_wt = 0, avg_tat = 0;
for(i = 0; i < n; i++) {
p[i] = i + 1;
printf("Enter burst time and priority for process %d: ", p[i]);
scanf("%d%d", &bt[i], &pr[i]);
}
for(i = 0; i < n - 1; i++) {
for(j = i + 1; j < n; j++) {
if(pr[i] > pr[j]) {
int temp = pr[i]; pr[i] = pr[j]; pr[j] = temp;
temp = bt[i]; bt[i] = bt[j]; bt[j] = temp;
temp = p[i]; p[i] = p[j]; p[j] = temp;
}
}
}
wt[0] = 0;
for(i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
}
for(i = 0; i < n; i++) {
tat[i] = wt[i] + bt[i];
avg_wt += wt[i];
avg_tat += tat[i];
}
printf("P\tBT\tPriority\tWT\tTAT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t\t%d\t%d\n", p[i], bt[i], pr[i], wt[i], tat[i]);
}
printf("Average Waiting Time = %.2f\n", avg_wt / n);
printf("Average Turnaround Time = %.2f\n", avg_tat / n);
return 0;
}
Comparison of CPU Scheduling
Algorithms
1. Average Waiting Time Comparison
Scheduling Average Waiting Average Execution Time (s)
Algorithm Time Turnaround Time

FCFS 2.33 5.33 20.548

SJF 2.33 5.33 31.376

Round Robin 3.00 6.00 26.667

Priority 2.33 5.33 16.597


Figure: Comparison of scheduling algorithms based on waiting time, turnaround time, and
execution time.

2. Analysis of Algorithms
First Come First Serve (FCFS)
 Strengths:

 Simple and easy to implement.


 Fair in terms of order of arrival.

 Weaknesses:

 Can cause high waiting time for short processes (Convoy effect).
 No prioritization; not suitable for real-time systems.

Best Use Case: Suitable for batch systems where simplicity is more important than efficiency.

Shortest Job First (SJF)


 Strengths:

 Minimizes average waiting time for non-preemptive cases.

 Weaknesses:

 Requires prior knowledge of burst times.


 Can lead to starvation of longer processes.
 Higher execution time due to sorting overhead.

Best Use Case: Systems where job lengths can be accurately predicted, such as batch
processing.

Round Robin (RR)


 Strengths:

 Better response time for interactive systems.


 Fair time-sharing among all processes.

 Weaknesses:

 Higher average waiting and turnaround time if time quantum is not optimized.
 More context switches, increasing overhead.

Best Use Case: Ideal for time-sharing and multitasking environments.


Priority Scheduling
 Strengths:

 Allows prioritization of important tasks.


 Efficient for systems needing different levels of service.

 Weaknesses:

 May suffer from starvation of low-priority processes.


 Requires a mechanism for priority assignment.

Best Use Case: Real-time systems or systems where task importance varies significantly.

3. Conclusion
All four scheduling algorithms offer distinct advantages and disadvantages depending on the
system requirements. FCFS, SJF, and Priority Scheduling yielded the same average waiting and
turnaround times for the test data, but varied in execution time, with Priority being the most
efficient. Round Robin, while slightly less efficient in waiting times, offers superior
responsiveness for interactive systems. In practice, the optimal scheduling policy depends on
the specific needs of the workload, such as fairness, predictability, or responsiveness.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy