0% found this document useful (0 votes)
14 views4 pages

RR and Priority

The document contains code implementing two different CPU scheduling algorithms: round robin and priority scheduling. For round robin scheduling, it uses a time quantum to allow time slicing between processes. For priority scheduling, it schedules the process with the highest priority level at each time step. Both algorithms track waiting times and turnaround times for processes to calculate average metrics.
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)
14 views4 pages

RR and Priority

The document contains code implementing two different CPU scheduling algorithms: round robin and priority scheduling. For round robin scheduling, it uses a time quantum to allow time slicing between processes. For priority scheduling, it schedules the process with the highest priority level at each time step. Both algorithms track waiting times and turnaround times for processes to calculate average metrics.
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/ 4

Exercise -2

iii) Round Robin:


#include <stdio.h>
#define MAX_PROCESS 10
int main() {
int burst_time[MAX_PROCESS];
int remaining_time[MAX_PROCESS];
int waiting_time[MAX_PROCESS];
int turnaround_time[MAX_PROCESS];
int arrival_time[MAX_PROCESS];
int n, time_quantum;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter burst time for each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &burst_time[i]);
remaining_time[i] = burst_time[i];
}
printf("Enter arrival time for each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d: ", i + 1);
scanf("%d", &arrival_time[i]);
}
printf("Enter time quantum: ");
scanf("%d", &time_quantum);
int time = 0;
int completed = 0;
while (completed < n) {
for (int i = 0; i < n; i++) {
if (arrival_time[i] <= time && remaining_time[i] > 0) {
int execute_time = (remaining_time[i] < time_quantum) ? remaining_time[i] :
time_quantum;
remaining_time[i] -= execute_time;
time += execute_time;
waiting_time[i] = time - burst_time[i] - arrival_time[i];
if (remaining_time[i] == 0) {
turnaround_time[i] = time - arrival_time[i];
completed++;
}
}
}
}
// Display results
printf("\nProcess\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], arrival_time[i], waiting_time[i],
turnaround_time[i]);
}
float average_waiting_time = 0;
float average_turnaround_time = 0;
for (int i = 0; i < n; i++) {
average_waiting_time += waiting_time[i];
average_turnaround_time += turnaround_time[i];
}
average_waiting_time /= n;
average_turnaround_time /= n;
printf("\nAverage Waiting Time: %.2f", average_waiting_time);
printf("\nAverage Turnaround Time: %.2f\n", average_turnaround_time);
return 0;}

Priority Scheduling:
#include <stdio.h>
#define MAX_PROCESS 10
int main() {
int burst_time[MAX_PROCESS];
int priority[MAX_PROCESS];
int waiting_time[MAX_PROCESS];
int turnaround_time[MAX_PROCESS];
int arrival_time[MAX_PROCESS];
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
printf("Enter burst time, priority, and arrival time for each process:\n");
for (int i = 0; i < n; i++) {
printf("Process %d:\n", i + 1);
printf("Burst Time: ");
scanf("%d", &burst_time[i]);
printf("Priority: ");
scanf("%d", &priority[i]);
printf("Arrival Time: ");
scanf("%d", &arrival_time[i]);
}
// Implementing Priority Scheduling
int time = 0;
int completed = 0;
int process_executing;
while (completed < n) {
int highest_priority = -1;

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


if (arrival_time[i] <= time && priority[i] != -1) {
if (highest_priority == -1 || priority[i] < priority[highest_priority]) {
highest_priority = i;
}
}
}
if (highest_priority != -1) {
process_executing = highest_priority;
time += burst_time[process_executing];
turnaround_time[process_executing] = time - arrival_time[process_executing];
waiting_time[process_executing] = turnaround_time[process_executing] -
burst_time[process_executing];
priority[process_executing] = -1; // Mark the process as completed
completed++;
} else {
time++; // Idle time when no process is ready
}
}
// Display results
printf("\nProcess\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\n");
for (int i = 0; i < n; i++) {
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], arrival_time[i], waiting_time[i],
turnaround_time[i]);
}
float average_waiting_time = 0;
float average_turnaround_time = 0;
for (int i = 0; i < n; i++) {
average_waiting_time += waiting_time[i];
average_turnaround_time += turnaround_time[i];
}

average_waiting_time /= n;
average_turnaround_time /= n;
printf("\nAverage Waiting Time: %.2f", average_waiting_time);
printf("\nAverage Turnaround Time: %.2f\n", average_turnaround_time);
return 0;
}

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