Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

06 58 Os

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Name : Prabhakar Uparkar

Roll No : 58
Assignment 6
Q. Write a C Program to implement Round Robin Algorithm.

#include <stdio.h>
#include <stdlib.h>

struct process {
int pid;
int at;
int bt;
int ct;
int tat;
int wt;
int st; // Start time
int rt; // Response time
int rbt; // Remaining burst time
};

void sort(struct process* p, int n) {


for (int i = 0; i < n; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (p[j].at > p[j + 1].at) {
struct process temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}

int main() {
int n, tq;

printf("Enter the number of processes: ");


scanf("%d", &n);

struct process* p = (struct process*)malloc(n * sizeof(struct


process));
printf("Enter Time Quantum: ");
scanf("%d", &tq);

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


printf("\nEnter Process id for process %d: ", i + 1);
scanf("%d", &p[i].pid);
printf("Enter Arrival Time for process %d: ", i + 1);
scanf("%d", &p[i].at);
printf("Enter Burst Time for process %d: ", i + 1);
scanf("%d", &p[i].bt);
p[i].rbt = p[i].bt;
p[i].st = -1;
}

sort(p, n);

int current_time = 0, completed = 0, i = 0;

while (completed < n) {


if (p[i].rbt > 0 && p[i].at <= current_time) {
if (p[i].st == -1) {
p[i].st = current_time;
}
if (p[i].rbt > tq) {
current_time += tq;
p[i].rbt -= tq;
} else {
current_time += p[i].rbt;
p[i].rbt = 0;
p[i].ct = current_time;
p[i].tat = p[i].ct - p[i].at;
p[i].wt = p[i].tat - p[i].bt;
p[i].rt = p[i].st - p[i].at;
completed++;
}
}
i = (i + 1) % n;
}

printf("\nPid | AT | BT | CT | TAT | WT | RT\n");


printf("----------------------------------------\n");

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


printf(" %d | %d | %d | %d | %d | %d | %d \n",
p[k].pid, p[k].at, p[k].bt, p[k].ct, p[k].tat, p[k].wt, p[k].rt);
}

float avg_TAT = 0, avg_WT = 0, avg_RT = 0;


for (int i = 0; i < n; i++) {
avg_TAT += p[i].tat;
avg_WT += p[i].wt;
avg_RT += p[i].rt;
}

avg_TAT /= (float)n;
avg_WT /= (float)n;
avg_RT /= (float)n;

printf("\nAverage Turnaround time : %f", avg_TAT);


printf("\nAverage Waiting Time : %f", avg_WT);
printf("\nAverage Response Time : %f", avg_RT);
printf("\nThroughput : %.2f", (float)n / current_time);

free(p);
return 0;
}
Output:

You might also like