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

Os Exp-5b

The document outlines a project aimed at improving the queue management system for 'Dev Darshan' at a temple, addressing the increasing wait times for devotees. It discusses various CPU scheduling algorithms such as FCFS, SJF (both preemptive and non-preemptive), Priority Scheduling, and Round Robin, evaluating their effectiveness in reducing wait times and ensuring fairness. The implementation includes a program that allows users to choose a scheduling method and input devotee details to manage the queue efficiently.

Uploaded by

ghumareaditya9
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)
4 views21 pages

Os Exp-5b

The document outlines a project aimed at improving the queue management system for 'Dev Darshan' at a temple, addressing the increasing wait times for devotees. It discusses various CPU scheduling algorithms such as FCFS, SJF (both preemptive and non-preemptive), Priority Scheduling, and Round Robin, evaluating their effectiveness in reducing wait times and ensuring fairness. The implementation includes a program that allows users to choose a scheduling method and input devotee details to manage the queue efficiently.

Uploaded by

ghumareaditya9
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/ 21

Name Aditya Valmik Ghumare

UID no. 2023300066

Experiment No. 5

AIM: With the increasing number of devotees visiting the temple, the wait time
for 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 .
Implement this using FCFS, SJF (Preemptive, non preemptive) priority and
Round robin scheduling algorithm.

PROBLEM To design and implement a CPU scheduling algorithm for the "Dev
STATEMENT : 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. The CPU scheduling algorithm will be
implemented in the existing "Dev Darshan" system, and will consider the
following factors:
The number of devotees waiting in the queue. The priority of each devotee,
such as senior citizens, children, and differently-
abled individuals.
The time taken by each devotee to complete "Dev Darshan".
The availability of resources, such as the number of "Dev Darshan" counters
and the capacity of the temple. 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.

Theory: Efficient Queue Management for "Dev Darshan" Using Scheduling


Algorithms

As the number of devotees visiting the temple increases, the waiting time
for "Dev Darshan" has also gone up. Long waiting times cause
inconvenience and discomfort for devotees, making it necessary to find a
better way to manage the queue. To improve this system, different
scheduling methods can be used to organize the queue and reduce waiting
time.
Some of the most effective scheduling algorithms are:
• First-Come-First-Serve (FCFS)
• Shortest Job First (SJF)
• Priority Scheduling
• Round Robin (RR)

Each of these methods works differently and has its own advantages and
disadvantages. This document explains these algorithms in a simple way
and compares their effectiveness in managing the temple queue.

Understanding the Scheduling Methods

1.First-Come-First-Serve (FCFS)

This is the simplest method, where devotees are served in the order they
arrive. The first person in the queue gets the Darshan first, and others
follow in sequence.

How it works:

• The queue follows a strict order based on arrival time.


• No one gets priority over others.
• Waiting time is calculated based on when a devotee joins the queue
and when their turn comes.

✅ Advantages:

• Simple and easy to understand.


• Fair to everyone, as no one gets special treatment.

❌ Disadvantages:

• Devotees who arrive late may have to wait for a long time.
• If one devotee takes too long, others behind them also get delayed.

2. Shortest Job First (SJF)

In this method, devotees with shorter Darshan times are served first. This
helps in reducing the average waiting time.
Types of SJF:

• Non-Preemptive SJF: Once a devotee starts Darshan, they


complete it before the next devotee gets a chance.
• Preemptive SJF (Shortest Remaining Time First - SRTF): If a
new devotee with a shorter Darshan time arrives, they are served
before those with longer Darshan times.

✅ Advantages:

• Minimizes the average waiting time.


• Makes the system more efficient when Darshan times vary.

❌ Disadvantages:

• Requires an estimate of Darshan time in advance.


• Devotees with long Darshan times may have to wait for a very long
time (Starvation problem).

3. Priority Scheduling
In this method, devotees are assigned priority based on certain factors like
age, health condition, or VIP status. Devotees with higher priority are
served first.

Types of Priority Scheduling:

• Non-Preemptive Priority Scheduling: Once a devotee starts


Darshan, they complete it before the next devotee is served.
• Preemptive Priority Scheduling: If a higher-priority devotee
arrives, they are served immediately, even if another devotee was
already having Darshan.

✅ Advantages:

• Important devotees (elderly, sick, VIPs) get Darshan first.


• Can reduce waiting time for special cases.

❌ Disadvantages:
• Lower-priority devotees may have to wait for a long time.
• If not managed properly, it can be unfair to some devotees.

4. Round Robin (RR)

In this method, each devotee gets a fixed time slot (called a time
quantum) for Darshan. If they complete their Darshan within this time, they
leave the queue. If not, they go back to the end of the queue and wait for
another turn.

How it works:
• A fixed time quantum is set (for example, 5 minutes per devotee).
• Each devotee gets their turn for the given time quantum.
• If a devotee finishes within this time, they leave.
• If not, they rejoin the queue and wait for another round.
• The process repeats until all devotees complete their Darshan.

✅ Advantages:

• Ensures fairness as everyone gets a chance.


• Prevents very long waiting times for any single devotee.

❌ Disadvantages:
• If the time quantum is too short, it may lead to frequent switching,
making the process inefficient.
• If the time quantum is too long, it may become similar to FCFS.
Which Method is Best?

• FCFS is simple but can result in long waiting times.


• SJF is the most efficient in terms of waiting time but can cause
starvation for devotees with long Darshan times.
• Priority Scheduling helps important devotees but can be unfair to
others.
• Round Robin is fair to everyone but may not be the most efficient if
the time quantum is not chosen properly.

Considering both fairness and efficiency, Round Robin with an


appropriate time quantum is the best choice for managing the temple
queue. It ensures that no devotee waits too long while keeping the process
organized and smooth.

PROGRAM: #include <stdio.h>


#include <stdlib.h>
#include <limits.h>

#define MAX_PROCESSES 100

struct Process {
int id;
int arrivalTime;
int burstTime;
int priority;
int remainingTime;
int waitTime;
int turnaroundTime;
int completed;
};

void FCFS(struct Process processes[], int n);


void SJF_NonPreemptive(struct Process processes[], int n);
void SJF_Preemptive(struct Process processes[], int n);
void Priority_NonPreemptive(struct Process processes[], int n);
void Priority_Preemptive(struct Process processes[], int n);
void RoundRobin(struct Process processes[], int n, int quantum);
void resetProcesses(struct Process processes[], int n);
void inputProcesses(struct Process processes[], int *n);
void printGanttChart(int ganttChart[], int ganttTime[], int ganttSize);

int main() {
int choice, n, quantum;
struct Process processes[MAX_PROCESSES];

do {
printf("\n==== Dev Darshan Queue Scheduling System ====\n");
printf("1. First Come First Serve (FCFS)\n");
printf("2. Shortest Job First (SJF) - Non-Preemptive\n");
printf("3. Shortest Job First (SJF) - Preemptive\n");
printf("4. Priority Scheduling - Non-Preemptive\n");
printf("5. Priority Scheduling - Preemptive\n");
printf("6. Round Robin\n");
printf("7. Exit\n");
printf("Choose an option: ");
scanf("%d", &choice);

if (choice == 7) {
printf("Exiting program. Thank you!\n");
break;
}

inputProcesses(processes, &n);

if (choice == 6) {
printf("Enter Time Quantum for Round Robin: ");
scanf("%d", &quantum);
}

switch (choice) {
case 1: FCFS(processes, n); break;
case 2: SJF_NonPreemptive(processes, n); break;
case 3: SJF_Preemptive(processes, n); break;
case 4: Priority_NonPreemptive(processes, n); break;
case 5: Priority_Preemptive(processes, n); break;
case 6: RoundRobin(processes, n, quantum); break;
default: printf("Invalid choice! Try again.\n");
}

resetProcesses(processes, n);
} while (1);
return 0;
}

void inputProcesses(struct Process processes[], int *n) {


printf("Enter number of devotees: ");
scanf("%d", n);

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


processes[i].id = i + 1;
printf("\nEnter Arrival Time for Devotee %d: ", processes[i].id);
scanf("%d", &processes[i].arrivalTime);
printf("Enter Dev Darshan Time for Devotee %d: ", processes[i].id);
scanf("%d", &processes[i].burstTime);
printf("Enter Priority for Devotee %d (lower value = higher priority): ",
processes[i].id);
scanf("%d", &processes[i].priority);
processes[i].remainingTime = processes[i].burstTime;
processes[i].completed = 0;
}
}

void resetProcesses(struct Process processes[], int n) {


for (int i = 0; i < n; i++) {
processes[i].remainingTime = processes[i].burstTime;
processes[i].completed = 0;
processes[i].waitTime = 0;
processes[i].turnaroundTime = 0;
}
}

void printGanttChart(int ganttChart[], int ganttTime[], int ganttSize) {


printf("\nGantt Chart:\n");
for (int i = 0; i < ganttSize; i++) {
printf("| P%d ", ganttChart[i]);
}
printf("|\n");

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


printf("%d ", ganttTime[i]);
}
printf("%d\n", ganttTime[ganttSize]);
}
void FCFS(struct Process processes[], int n) {
printf("\n--- FCFS Scheduling ---\n");
int time = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES], ganttTime[MAX_PROCESSES + 1],
ganttSize = 0;

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


if (time < processes[i].arrivalTime)
time = processes[i].arrivalTime;

processes[i].waitTime = time - processes[i].arrivalTime;


processes[i].turnaroundTime = processes[i].waitTime +
processes[i].burstTime;
totalWait += processes[i].waitTime;
totalTurnaround += processes[i].turnaroundTime;

ganttChart[ganttSize] = processes[i].id;
ganttTime[ganttSize] = time;
ganttSize++;

time += processes[i].burstTime;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[i].id, processes[i].waitTime,
processes[i].turnaroundTime);
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);

printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",


(float)totalWait / n, (float)totalTurnaround / n);
}

void SJF_NonPreemptive(struct Process processes[], int n) {


printf("\n--- SJF Scheduling (Non-Preemptive) ---\n");
int completed = 0, time = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES], ganttTime[MAX_PROCESSES + 1],
ganttSize = 0;

while (completed < n) {


int minIndex = -1, minBurst = INT_MAX;

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


if (processes[i].arrivalTime <= time && !processes[i].completed &&
processes[i].burstTime < minBurst) {
minBurst = processes[i].burstTime;
minIndex = i;
}
}

if (minIndex == -1) {
time++;
continue;
}

processes[minIndex].waitTime = time -
processes[minIndex].arrivalTime;
processes[minIndex].turnaroundTime = processes[minIndex].waitTime
+ processes[minIndex].burstTime;
processes[minIndex].completed = 1;
totalWait += processes[minIndex].waitTime;
totalTurnaround += processes[minIndex].turnaroundTime;

ganttChart[ganttSize] = processes[minIndex].id;
ganttTime[ganttSize] = time;
ganttSize++;

time += processes[minIndex].burstTime;
completed++;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[minIndex].id, processes[minIndex].waitTime,
processes[minIndex].turnaroundTime);
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);

printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",


(float)totalWait / n, (float)totalTurnaround / n);
}
void SJF_Preemptive(struct Process processes[], int n) {
printf("\n--- SJF Scheduling (Preemptive) ---\n");
int time = 0, completed = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES * 10], ganttTime[MAX_PROCESSES
* 10 + 1], ganttSize = 0;

while (completed < n) {


int minIndex = -1, minBurst = INT_MAX;

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


if (processes[i].arrivalTime <= time && processes[i].remainingTime
> 0 &&
processes[i].remainingTime < minBurst) {
minBurst = processes[i].remainingTime;
minIndex = i;
}
}

if (minIndex == -1) {
time++;
continue;
}

ganttChart[ganttSize] = processes[minIndex].id;
ganttTime[ganttSize] = time;
ganttSize++;

processes[minIndex].remainingTime--;

if (processes[minIndex].remainingTime == 0) {
processes[minIndex].turnaroundTime = time + 1 -
processes[minIndex].arrivalTime;
processes[minIndex].waitTime =
processes[minIndex].turnaroundTime - processes[minIndex].burstTime;
totalWait += processes[minIndex].waitTime;
totalTurnaround += processes[minIndex].turnaroundTime;
completed++;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[minIndex].id, processes[minIndex].waitTime,
processes[minIndex].turnaroundTime);
}
time++;
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);

printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",


(float)totalWait / n, (float)totalTurnaround / n);
}

void Priority_NonPreemptive(struct Process processes[], int n) {


printf("\n--- Priority Scheduling (Non-Preemptive) ---\n");
int completed = 0, time = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES], ganttTime[MAX_PROCESSES + 1],
ganttSize = 0;

while (completed < n) {


int minIndex = -1, minPriority = INT_MAX;

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


if (processes[i].arrivalTime <= time && !processes[i].completed &&
processes[i].priority < minPriority) {
minPriority = processes[i].priority;
minIndex = i;
}
}

if (minIndex == -1) {
time++;
continue;
}

processes[minIndex].waitTime = time -
processes[minIndex].arrivalTime;
processes[minIndex].turnaroundTime = processes[minIndex].waitTime
+ processes[minIndex].burstTime;
processes[minIndex].completed = 1;
totalWait += processes[minIndex].waitTime;
totalTurnaround += processes[minIndex].turnaroundTime;

ganttChart[ganttSize] = processes[minIndex].id;
ganttTime[ganttSize] = time;
ganttSize++;

time += processes[minIndex].burstTime;
completed++;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[minIndex].id, processes[minIndex].waitTime,
processes[minIndex].turnaroundTime);
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);

printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",


(float)totalWait / n, (float)totalTurnaround / n);
}

void Priority_Preemptive(struct Process processes[], int n) {


printf("\n--- Priority Scheduling (Preemptive) ---\n");
int time = 0, completed = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES * 10], ganttTime[MAX_PROCESSES
* 10 + 1], ganttSize = 0;

while (completed < n) {


int minIndex = -1, minPriority = INT_MAX;

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


if (processes[i].arrivalTime <= time && processes[i].remainingTime
> 0 &&
processes[i].priority < minPriority) {
minPriority = processes[i].priority;
minIndex = i;
}
}

if (minIndex == -1) {
time++;
continue;
}

ganttChart[ganttSize] = processes[minIndex].id;
ganttTime[ganttSize] = time;
ganttSize++;

processes[minIndex].remainingTime--;

if (processes[minIndex].remainingTime == 0) {
processes[minIndex].turnaroundTime = time + 1 -
processes[minIndex].arrivalTime;
processes[minIndex].waitTime =
processes[minIndex].turnaroundTime - processes[minIndex].burstTime;
totalWait += processes[minIndex].waitTime;
totalTurnaround += processes[minIndex].turnaroundTime;
completed++;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[minIndex].id, processes[minIndex].waitTime,
processes[minIndex].turnaroundTime);
}

time++;
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);

printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",


(float)totalWait / n, (float)totalTurnaround / n);
}

void RoundRobin(struct Process processes[], int n, int quantum) {


printf("\n--- Round Robin Scheduling ---\n");
int time = 0, completed = 0, totalWait = 0, totalTurnaround = 0;
int ganttChart[MAX_PROCESSES * 10], ganttTime[MAX_PROCESSES
* 10 + 1], ganttSize = 0;
int queue[MAX_PROCESSES], front = 0, rear = 0;

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


queue[rear++] = i;
}

while (completed < n) {


if (front == rear) {
time++;
continue;
}

int index = queue[front];


front = (front + 1) % MAX_PROCESSES;

if (processes[index].arrivalTime > time) {


queue[rear] = index;
rear = (rear + 1) % MAX_PROCESSES;
continue;
}

ganttChart[ganttSize] = processes[index].id;
ganttTime[ganttSize] = time;
ganttSize++;

if (processes[index].remainingTime > quantum) {


processes[index].remainingTime -= quantum;
time += quantum;
queue[rear] = index;
rear = (rear + 1) % MAX_PROCESSES;
} else {
time += processes[index].remainingTime;
processes[index].turnaroundTime = time -
processes[index].arrivalTime;
processes[index].waitTime = processes[index].turnaroundTime -
processes[index].burstTime;
processes[index].remainingTime = 0;
processes[index].completed = 1;
completed++;
totalWait += processes[index].waitTime;
totalTurnaround += processes[index].turnaroundTime;

printf("Devotee %d - Wait Time: %d, Turnaround Time: %d\n",


processes[index].id, processes[index].waitTime,
processes[index].turnaroundTime);
}
}

ganttTime[ganttSize] = time;

printGanttChart(ganttChart, ganttTime, ganttSize);


printf("Avg Wait Time: %.2f, Avg Turnaround Time: %.2f\n",
(float)totalWait / n, (float)totalTurnaround / n);
}

Output:
CONCLUSION: In conclusion, this experiment successfully implemented and analyzed
various CPU scheduling algorithms, including FCFS, SJF (both preemptive
and non- preemptive), and priority scheduling (both preemptive and non-
preemptive), to optimize the "Dev Darshan" system in a temple. Each
algorithm was evaluated based on its ability to minimize wait time, improve
resource utilization, and ensure fairness in queue management. The results
demonstrated that preemptive algorithms, such as SJF Preemptive and
Priority Preemptive, provided better responsiveness and reduced wait times
compared to non-preemptive approaches. However, the choice of algorithm
depends on the specific requirements of the system, such as the need for
fairness, priority handling, and computational efficiency. Overall, the
experiment highlighted the importance of selecting the right scheduling
algorithm.

You might also like