0% found this document useful (0 votes)
5 views8 pages

Expt 05 CPUScheduling

Uploaded by

heets4307
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)
5 views8 pages

Expt 05 CPUScheduling

Uploaded by

heets4307
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/ 8

BHARATIYA VIDYA BHAVAN’S

SARDAR PATEL INSTITUTE OF TECHNOLOGY


Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System

Name Heet Keyur Shah

UID no. 2023300208

Experiment 5

AIM : To develop efficient CPU scheduling algorithms that optimize process


execution, minimize waiting time, and enhance system performance.

THEORY: FCFS (First-Come-First-Served) is a simple scheduling algorithm where


processes are executed in the order they arrive, without considering their
execution time. This is like waiting in line at an amusement park, where the first
person rides first. SJF (Shortest Job First) prioritizes processes with the
shortest burst time, executing them first, similar to a bakery starting with
quicker-baking cupcakes before a longer-baking cake. Round Robin assigns a
fixed time slice (quantum) to each process, rotating between them until all are
completed, similar to friends taking turns in a game. Priority scheduling
executes processes based on priority, with higher-priority tasks going first,
much like a doctor treating critical patients before others. Completion Time is
when a process finishes, while Turnaround Time is the difference between
C.T. and Arrival Time, and Waiting Time is the difference between
Turnaround Time and Burst Time.

PROBLEM With the increasing number of devotees visiting the temple, the wait time for
STATEMENT : "Dev Darshan" has become longer, causing inconvenience to the devotees. This
has resulted in a need for a more efficient system that can manage the waiting
queue and reduce the wait time for "Dev Darshan".
Goal: To design and implement a CPU scheduling algorithm for the "Dev
Darshan" system in the temple that can effectively manage the waiting queue,
minimize the wait time for devotees, and improve the overall experience of
visiting the temple.
Scope: The CPU scheduling algorithm will be implemented in the existing
"Dev Darshan" system, and will consider the following factors:
1.​ The number of devotees waiting in the queue.
2.​ The priority of each devotee, such as senior citizens, children, and
differently-abled individuals.
3.​ The time taken by each devotee to complete "Dev Darshan".
4.​ The availability of resources, such as the number of "Dev Darshan"
counters and the capacity of the temple.
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
The solution will be tested and evaluated based on its ability to reduce the wait
time for devotees, improve the utilization of resources, and ensure fairness and
equality in the allocation of resources.

Give the solution with an example of Dev darshan CPU scheduling algorithm .
Write a program to simulate the following non-preemptive CPU scheduling
algorithms to find turnaround time and waiting time.

a) FCFS
b) SJF
c) Round Robin
d) Priority scheduling

CODE: #include <stdio.h>


#include <stdlib.h>

struct Devotee {
int id;
int arrival_time;
int burst_time;
int completion_time;
int waiting_time;
int turnaround_time;
int priority;
};

void calculateTimes(struct Devotee devotees[], int n) {


int completion_time[n];

completion_time[0] = devotees[0].arrival_time +
devotees[0].burst_time;
devotees[0].turnaround_time = completion_time[0] -
devotees[0].arrival_time;
devotees[0].waiting_time = devotees[0].turnaround_time -
devotees[0].burst_time;

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


if (completion_time[i - 1] < devotees[i].arrival_time)
completion_time[i] = devotees[i].arrival_time +
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
devotees[i].burst_time;
else
completion_time[i] = completion_time[i - 1] +
devotees[i].burst_time;

devotees[i].turnaround_time = completion_time[i] -
devotees[i].arrival_time;
devotees[i].waiting_time = devotees[i].turnaround_time
- devotees[i].burst_time;
}
}

void fcfs(struct Devotee devotees[], int n) {


printf("\nFirst-Come-First-Serve (FCFS) Scheduling\n");
calculateTimes(devotees, n);
for (int i = 0; i < n; i++) {
printf("Devotee %d: Waiting Time = %d, Turnaround Time
= %d\n", devotees[i].id, devotees[i].waiting_time,
devotees[i].turnaround_time);
}
}

void sjf(struct Devotee devotees[], int n) {


printf("\nShortest Job First (SJF) Scheduling\n");
struct Devotee temp;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (devotees[i].burst_time >
devotees[j].burst_time) {
temp = devotees[i];
devotees[i] = devotees[j];
devotees[j] = temp;
}
}
}
calculateTimes(devotees, n);

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


printf("Devotee %d: Waiting Time = %d, Turnaround Time
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
= %d\n",
devotees[i].id, devotees[i].waiting_time,
devotees[i].turnaround_time);
}
}

void roundRobin(struct Devotee devotees[], int n, int


quantum) {
printf("\nRound Robin Scheduling\n");

int remaining_burst_time[n];
for (int i = 0; i < n; i++) {
remaining_burst_time[i] = devotees[i].burst_time;
devotees[i].waiting_time = 0;
}

int time = 0;
int completed = 0;
while (completed < n) {
for (int i = 0; i < n; i++) {
if (remaining_burst_time[i] > 0) {
int time_slice = (remaining_burst_time[i] >
quantum) ? quantum : remaining_burst_time[i];
remaining_burst_time[i] -= time_slice;
time += time_slice;

if (remaining_burst_time[i] == 0) {
completed++;
devotees[i].completion_time = time;
devotees[i].turnaround_time =
devotees[i].completion_time - devotees[i].arrival_time;
devotees[i].waiting_time =
devotees[i].turnaround_time - devotees[i].burst_time;
}
}
}
}

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


BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
printf("Devotee %d: Waiting Time = %d, Turnaround Time
= %d\n",
devotees[i].id, devotees[i].waiting_time,
devotees[i].turnaround_time);
}
}

void priorityScheduling(struct Devotee devotees[], int n) {


printf("\nPriority Scheduling\n");

struct Devotee temp;


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (devotees[i].priority < devotees[j].priority) {
temp = devotees[i];
devotees[i] = devotees[j];
devotees[j] = temp;
}
}
}

calculateTimes(devotees, n);

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


printf("Devotee %d: Waiting Time = %d, Turnaround Time
= %d\n",
devotees[i].id, devotees[i].waiting_time,
devotees[i].turnaround_time);
}
}

int main() {
int n;
printf("Enter number of devotees: ");
scanf("%d", &n);
struct Devotee devotees[n];
printf("Enter devotee details (ID ArrivalTime BurstTime
Priority):\n");
for (int i = 0; i < n; i++) {
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
scanf("%d %d %d %d", &devotees[i].id,
&devotees[i].arrival_time, &devotees[i].burst_time,
&devotees[i].priority);
}

fcfs(devotees, n);
sjf(devotees, n);

int quantum;
printf("\nEnter time quantum for Round Robin: ");
scanf("%d", &quantum);
roundRobin(devotees, n, quantum);
priorityScheduling(devotees, n);

return 0;
}

OUTPUT:
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
BHARATIYA VIDYA BHAVAN’S
SARDAR PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India

DEPARTMENT OF COMPUTER ENGINEERING


SUBJECT: Operating System
CONCLUSION: In conclusion, the different CPU scheduling algorithms—FCFS, SJF,
Round Robin, and Priority Scheduling—offer varied approaches to
process management, each with its own advantages and disadvantages.
FCFS is simple but can lead to inefficiencies, especially with long
processes arriving first. SJF minimizes waiting time but requires
knowledge of the burst time, which is not always possible. Round Robin
ensures fair CPU time distribution but may lead to higher waiting times.
Priority scheduling allows for important tasks to be completed first but
can result in starvation for lower-priority processes. By understanding
these algorithms, we can choose the most suitable one based on the
system's requirements, balancing efficiency, fairness, and response times.

You might also like