0% found this document useful (0 votes)
11 views14 pages

Os Exp6

The document outlines various CPU scheduling algorithms applied in a pizza delivery system, including First Come First Serve, Shortest Job First, Round Robin, and Priority Scheduling. Each algorithm is explained in terms of how it processes pizza orders and ensures fair delivery based on different criteria. Additionally, it includes C programming implementations for each scheduling method, demonstrating their functionality in a practical scenario.

Uploaded by

reyanshmehta2005
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)
11 views14 pages

Os Exp6

The document outlines various CPU scheduling algorithms applied in a pizza delivery system, including First Come First Serve, Shortest Job First, Round Robin, and Priority Scheduling. Each algorithm is explained in terms of how it processes pizza orders and ensures fair delivery based on different criteria. Additionally, it includes C programming implementations for each scheduling method, demonstrating their functionality in a practical scenario.

Uploaded by

reyanshmehta2005
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/ 14

BHARATIYA VIDYA BHAVAN’S SARDAR

PATEL INSTITUTE OF TECHNOLOGY


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

Name Vriddhi Mahesh Vashi

UID no. 2023300250

Experiment No. 6

AIM: Scheduling Part 2


THEORY: How CPU Scheduling algorithms were applied in a Pizza Delivery
System: -

First Come First Serve:


In this method, processes orders in the exact order they are received. This means
the first pizza order placed will be the first to be prepared and delivered.
No preemption: Once a pizza's preparation starts, it must be completed before
moving to the next order.

Shortest Job First:


In this method, remaining preparation time is tracked for each pizza order and
interrupts the current order if a new order arrives with a shorter preparation time
(preemptive). It checks for the shortest remaining time at every step, and
updates waiting and turnaround times dynamically.

Round Robin Scheduling:


In this method, to avoid any unfair practice, each order gets a fixed time
quantum for its preparation, no single order can monopolize the system and it
prevents starvation as well. If no pizza is available time moves forward to the
next order arrival.

Priority Scheduling:
In thus method, each pizza order is assigned a priority. The highest-priority
orders are delivered first, regardless of when they were placed. While ensuring
fair scheduling by implementing aging (to prevent starvation).
At each point the system finds the highest priority order available (dynamic
processing).

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

PROBLEM Rominus pizza wants to implement a pizza scheduling system. Create


STATEMENT : individual systems implementing each of the above CPU scheduling
algorithms as follows:
● FCFS: In a pizza delivery system, how does the First-Come-First-
Served (FCFS) scheduling algorithm determine the order in which pizzas
are delivered to customers?
● SJF: In a pizza delivery system, how does the Shortest Job First (SJF)
scheduling algorithm prioritize which pizzas are delivered first based on
their preparation time?
● Round Robin: In a pizza delivery system, how does the Round Robin
scheduling algorithm ensure that each delivery driver gets a fair and
equal chance to deliver pizzas?
● Priority: In a pizza delivery system, how does the Priority scheduling
algorithm prioritize which pizzas are delivered first based on the urgency
of the delivery (e.g. VIP customers, emergency deliveries)? Also, explain
which algorithm will be best suited for the following scenario. Is it
possible to combine the above algorithms to create an even more efficient
algorithm? If yes, then explain how.
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

PROGRAM: Implementation in C: -
FCFS

#include <stdio.h>

struct Pizza {
int id, arrivalTime, burstTime, completionTime, waitingTime,
turnaroundTime;
};

//sorting function based on arrival time


void sortByArrival(struct Pizza customers[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (customers[j].arrivalTime > customers[j +
1].arrivalTime) {
struct Pizza temp = customers[j];
customers[j] = customers[j + 1];
customers[j + 1] = temp;
}
}
}
}

// First Come First Serve Scheduling


void fcfsScheduling(struct Pizza customers[], int n) {
sortByArrival(customers, n);

customers[0].completionTime = customers[0].arrivalTime +
customers[0].burstTime;
customers[0].turnaroundTime = customers[0].completionTime -
customers[0].arrivalTime;
customers[0].waitingTime = 0;

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


if (customers[i].arrivalTime > customers[i -
1].completionTime) {
customers[i].completionTime =
customers[i].arrivalTime + customers[i].burstTime;
} else {
customers[i].completionTime = customers[i -
1].completionTime + customers[i].burstTime;
}

customers[i].turnaroundTime = customers[i].completionTime
- customers[i].arrivalTime;
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

customers[i].waitingTime = customers[i].turnaroundTime -
customers[i].burstTime;
}
}

// display results
void displayResults(struct Pizza customers[], int n) {
float totalWT = 0, totalTAT = 0;
printf("\nOptimized Pizza Order:\n");
printf("ID Arrival Burst Completion Turnaround Waiting\n");

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


printf("%d \t %d \t %d \t %d \t %d \t %d\n",
customers[i].id, customers[i].arrivalTime,
customers[i].burstTime,
customers[i].completionTime, customers[i].turnaroundTime,
customers[i].waitingTime);
totalWT += customers[i].waitingTime;
totalTAT += customers[i].turnaroundTime;
}

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


printf("Average Turnaround Time: %.2f\n", totalTAT / n);
}

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

struct Pizza customers[n];


for (int i = 0; i < n; i++) {
customers[i].id = i + 1;
printf("Enter arrival time of customer/order %d: ", i +
1);
scanf("%d", &customers[i].arrivalTime);
printf("Enter preparation time (burst time) of that order
%d: ", i + 1);
scanf("%d", &customers[i].burstTime);
}

fcfsScheduling(customers, n);
displayResults(customers, n);

return 0;
}
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

SJF (SRTF- SJF WITH PREEMPTIVE)


#include <stdio.h>
#include <limits.h>

struct Pizza {
int id, arrivalTime, burstTime, remainingTime,
completionTime, waitingTime, turnaroundTime;
};

// Preemptive Shortest Job First (SRTF) Scheduling


void srtfScheduling(struct Pizza customers[], int n) {
int completed = 0, currentTime = 0, minIndex = -1;
int minBurst;

// Initialize remaining times


for (int i = 0; i < n; i++) {
customers[i].remainingTime = customers[i].burstTime;
}

while (completed < n) {


minIndex = -1;
minBurst = INT_MAX;

// Find the process with the shortest remaining time at


current time
for (int i = 0; i < n; i++) {
if (customers[i].arrivalTime <= currentTime &&
customers[i].remainingTime > 0) {
if (customers[i].remainingTime < minBurst) {
minBurst = customers[i].remainingTime;
minIndex = i;
}
}
}

if (minIndex == -1) {
currentTime++; //if no pizza is available increment
time
} else {
customers[minIndex].remainingTime--;
currentTime++;

if (customers[minIndex].remainingTime == 0) { // If
pizza preparation is completed
customers[minIndex].completionTime = currentTime;
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

customers[minIndex].turnaroundTime =
customers[minIndex].completionTime -
customers[minIndex].arrivalTime;
customers[minIndex].waitingTime =
customers[minIndex].turnaroundTime -
customers[minIndex].burstTime;
completed++;
}
}
}
}

// display table
void displayResults(struct Pizza customers[], int n) {
float totalWT = 0, totalTAT = 0;
printf("\nOptimized Pizza Order:\n");
printf("ID Arrival Burst Completion Turnaround Waiting\n");

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


printf("%d %d\t%d\t%d\t%d\t%d\n", customers[i].id,
customers[i].arrivalTime, customers[i].burstTime,
customers[i].completionTime, customers[i].turnaroundTime,
customers[i].waitingTime);
totalWT += customers[i].waitingTime;
totalTAT += customers[i].turnaroundTime;
}

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


printf("Average Turnaround Time: %.2f\n", totalTAT / n);
}

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

struct Pizza customers[n];


for (int i = 0; i < n; i++) {
customers[i].id = i + 1;
printf("Enter arrival time of Pizza %d: ", i + 1);
scanf("%d", &customers[i].arrivalTime);
printf("Enter preparation time (burst time) of Pizza %d:
", i + 1);
scanf("%d", &customers[i].burstTime);
}

srtfScheduling(customers, n);
displayResults(customers, n);
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

return 0;
}

ROUND ROBIN
//Round Robin Scheduling

#include <stdio.h>
struct Pizza {
int id, arrivalTime, burstTime, completionTime, waitingTime,
turnaroundTime, remainingTime;
};

int main() {
int n, tq, t = 0, remain, flag;
float totalWT = 0, totalTAT = 0;
printf("Enter number of customers: ");
scanf("%d", &n);
struct Pizza customers[n];

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


customers[i].id = i + 1;
printf("Enter arrival time of customer/order %d: ", i + 1);
scanf("%d", &customers[i].arrivalTime);
printf("Enter preparation time (burst time) of Pizza %d: ", i +
1);
scanf("%d", &customers[i].burstTime);
customers[i].remainingTime = customers[i].burstTime;
}

printf("Enter Time Quantum: ");


scanf("%d", &tq);
remain = n;
int queue[n], front = 0, rear = 0;
int visited[n];

for (int i = 0; i < n; i++) visited[i] = 0;


// Push first process that arrives
queue[rear++] = 0;
visited[0] = 1;
while (remain > 0) {
int i = queue[front++];
flag = 0;
if (customers[i].remainingTime > 0) {
if (customers[i].remainingTime <= tq) {
t += customers[i].remainingTime;
customers[i].remainingTime = 0;
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

customers[i].completionTime = t;
customers[i].turnaroundTime = customers[i].completionTime -
customers[i].arrivalTime;
customers[i].waitingTime = customers[i].turnaroundTime -
customers[i].burstTime;
totalWT += customers[i].waitingTime;
totalTAT += customers[i].turnaroundTime;
remain--;
} else {
customers[i].remainingTime -= tq;
t += tq;
}
flag = 1;
}
// Add newly arrived processes to the queue
for (int j = 0; j < n; j++) {
if (!visited[j] && customers[j].arrivalTime <= t &&
customers[j].remainingTime > 0) {
queue[rear++] = j;
visited[j] = 1;
}
}
// If the current process still has time left, push it back into
the queue
if (flag && customers[i].remainingTime > 0) {
queue[rear++] = i;
}
// If queue is empty, move time forward

if (front == rear) {
for (int j = 0; j < n; j++) {
if (customers[j].remainingTime > 0) {
queue[rear++] = j;
visited[j] = 1;
break;
}
}
}
}

// Print results
printf("\nOptimized Pizza Order (Round Robin Scheduling):\n");
printf("ID Arrival Burst Completion Turnaround Waiting\n");
for (int i = 0; i < n; i++) {
printf("%d %d %d %d %d %d\n",
customers[i].id, customers[i].arrivalTime,
customers[i].burstTime,
customers[i].completionTime, customers[i].turnaroundTime,
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

customers[i].waitingTime);
}
printf("\nAverage Waiting Time: %.2f\n", totalWT / n);
printf("Average Turnaround Time: %.2f\n", totalTAT / n);
return 0;
}
PRIORITY WITH AGING
#include <stdio.h>
#include <limits.h>

struct Pizza {
int id, arrivalTime, burstTime, remainingTime, priority,
completionTime, waitingTime, turnaroundTime;
};

// preemptive priority Scheduling with aging to avoid starvation


void priorityScheduling(struct Pizza customers[], int n) {
int completed = 0, currentTime = 0, minIndex;

// Initialize remaining times


for (int i = 0; i < n; i++) {
customers[i].remainingTime = customers[i].burstTime;
}

while (completed < n) {


minIndex = -1;
int highestPriority = INT_MAX;

// Find the highest-priority order available


for (int i = 0; i < n; i++) {
if (customers[i].arrivalTime <= currentTime &&
customers[i].remainingTime > 0) {
//aging part
int effectivePriority = customers[i].priority -
(currentTime - customers[i].arrivalTime) / 5;
if (effectivePriority < 1) effectivePriority =
1; // Priority should not be less than 1

if (effectivePriority < highestPriority) {


highestPriority = effectivePriority;
minIndex = i;
}
}
}

if (minIndex == -1) {
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

currentTime++; // No available pizza orders increase


time
} else {
customers[minIndex].remainingTime--;
currentTime++;

if (customers[minIndex].remainingTime == 0) { // If
completed
customers[minIndex].completionTime = currentTime;
customers[minIndex].turnaroundTime =
customers[minIndex].completionTime -
customers[minIndex].arrivalTime;
customers[minIndex].waitingTime =
customers[minIndex].turnaroundTime -
customers[minIndex].burstTime;
completed++;
}
}
}
}

void displayResults(struct Pizza customers[], int n) {


float totalWT = 0, totalTAT = 0;
printf("\nOptimized Pizza Order (Based on Priority with
Aging):\n");
printf("ID Arrival Burst Priority Completion Turnaround
Waiting\n");

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


printf("%d %d %d %d %d %d
%d\n",
customers[i].id, customers[i].arrivalTime,
customers[i].burstTime, customers[i].priority,
customers[i].completionTime,
customers[i].turnaroundTime, customers[i].waitingTime);
totalWT += customers[i].waitingTime;
totalTAT += customers[i].turnaroundTime;
}

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


printf("Average Turnaround Time: %.2f\n", totalTAT / n);
}

int main() {
int n;
printf("Enter number of customers: ");
scanf("%d", &n);
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

struct Pizza customers[n];


for (int i = 0; i < n; i++) {
customers[i].id = i + 1;
printf("Enter arrival time of Pizza %d: ", i + 1);
scanf("%d", &customers[i].arrivalTime);
printf("Enter preparation time (burst time) of Pizza %d:
", i + 1);
scanf("%d", &customers[i].burstTime);
printf("Enter priority of order %d (1 = VIP, 2 =
Emergency, 3 = Regular): ", i + 1);
scanf("%d", &customers[i].priority);
}

priorityScheduling(customers, n);
displayResults(customers, n);

return 0;
}
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

RESULT:
FCFS Scheduling:

SJF Scheduling:
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

Round Robin Scheduling:

Priority Scheduling:
BHARATIYA VIDYA BHAVAN’S SARDAR
PATEL INSTITUTE OF TECHNOLOGY
Bhavan’s Campus, Munshi Nagar, Andheri (West), Mumbai – 400058-India
Department of Computer Engineering

CONCLUSION: In this experiment, I implemented the algorithms used for CPU Scheduling,
like FCFS, Round Robin, SJF, Priority based etc to design a Pizza delivery
system.
In a pizza delivery system, the choice of scheduling algorithm depends on
the business goals. Round Robin (RR) is ideal for ensuring fairness, as it
gives each order a fixed time slice, preventing any single order from being
delayed too long. Shortest Job First (SJF) focuses on minimizing average
waiting time by delivering orders with shorter preparation times first.
However, First Come, First Serve (FCFS), while simple, can lead to long
delays if a large order arrives early. Priority Scheduling is useful for
handling urgent orders, such as VIP or emergency deliveries, ensuring they
are fulfilled faster than less urgent ones. For a balanced approach, Priority
Scheduling with preemption allows urgent orders to be processed first,
while still ensuring fairness for regular orders.

A hybrid scheduling approach can combine the benefits of multiple


algorithms. For example, Priority Scheduling could be used for urgent
orders, followed by SJF to minimize waiting times for remaining orders.
Within each priority level, Round Robin would ensure fairness,
preventing starvation. If no urgent orders exist, FCFS can be applied to
maintain order consistency.
This hybrid system ensures that urgent orders are prioritized, waiting times
are minimized, fairness is maintained among similar priority orders, and
the overall delivery process is optimized for both efficiency and customer
satisfaction.

You might also like