0% found this document useful (0 votes)
2 views

arjun ex4

The document outlines an experiment to implement various CPU scheduling algorithms including FCFS, SJF, Priority Scheduling, and Round-Robin. Each section contains C code for the respective algorithm, demonstrating how to calculate waiting time, turnaround time, and average metrics. The document also includes sample outputs for each algorithm implementation.

Uploaded by

953623205004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

arjun ex4

The document outlines an experiment to implement various CPU scheduling algorithms including FCFS, SJF, Priority Scheduling, and Round-Robin. Each section contains C code for the respective algorithm, demonstrating how to calculate waiting time, turnaround time, and average metrics. The document also includes sample outputs for each algorithm implementation.

Uploaded by

953623205004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 5

ARJUN G

953623205004

Experiment – 4
To Implement various CPU scheduling alogrithms.

1) FCFS :

#include <stdio.h>
int main() {
int n = 3, bt[] = {5, 8, 12}, wt[3] = {0}, tat[3], total_wt = 0, total_tat = 0;
for (int i = 1; i < n; i++) wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i];
printf("P BT WT TAT\n");
for (int i = 0; i < n; i++) {
printf("%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]);
total_wt += wt[i], total_tat += tat[i];
}
printf("Avg WT: %.2f, Avg TAT: %.2f\n", (float)total_wt / n, (float)total_tat / n);
}

Output:
ARJUN G
953623205004

2) SJF :

#include <stdio.h>
int main() {
int n = 3, p[] = {1, 2, 3}, bt[] = {1, 4, 7}, temp, wt[3] = {0}, tat[3], total_wt = 0,
total_tat = 0;
for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++)
if (bt[i] > bt[j]) temp = bt[i], bt[i] = bt[j], bt[j] = temp, temp = p[i], p[i] = p[j], p[j] =
temp;
for (int i = 1; i < n; i++) wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i];
printf("P BT WT TAT\n");
for (int i = 0; i < n; i++) {
printf("%d %d %d %d\n", p[i], bt[i], wt[i], tat[i]);
total_wt += wt[i], total_tat += tat[i];
}
printf("Avg WT: %.2f, Avg TAT: %.2f\n", (float)total_wt / n, (float)total_tat / n);
}

OUTPUT :

3) Priority Scheduling :

#include <stdio.h>
ARJUN G
953623205004
int main() {
int n = 3, t = 0, p[] = {2, 1, 3}, a[] = {0, 1, 2}, b[] = { 3 ,4 , 8}, c[3], tt[3], wt[3],
pid[] = {1, 2, 3};

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


for (int j = i + 1; j < n; j++) {
if (p[i] > p[j] || (p[i] == p[j] && a[i] > a[j])) {
int temp = p[i]; p[i] = p[j]; p[j] = temp;
temp = a[i]; a[i] = a[j]; a[j] = temp;
temp = b[i]; b[i] = b[j]; b[j] = temp;
temp = pid[i]; pid[i] = pid[j]; pid[j] = temp;
}
}
}

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


t = (t < a[i]) ? a[i] : t;
c[i] = t + b[i];
tt[i] = c[i] - a[i];
wt[i] = tt[i] - b[i];
t = c[i];
}

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


printf("Process %d: Arrival = %d, Burst = %d, Completion = %d, Turnaround =
%d, Waiting = %d\n",
pid[i], a[i], b[i], c[i], tt[i], wt[i]);
}
return 0;
}

OUTPUT :
ARJUN G
953623205004

4) ROUND-ROBIN :

#include <stdio.h>
int main() {
int n, tq, t = 0, c = 0;
scanf("%d%d", &n, &tq);
int p[n], a[n], b[n], r[n], ct[n], tt[n], wt[n];

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


scanf("%d%d%d", &p[i], &a[i], &b[i]);
r[i] = b[i];
ct[i] = -1;
}

while (c < n) {
for (int i = 0; i < n; i++) {
if (r[i] > 0) {
if (r[i] <= tq) {
t += r[i];
r[i] = 0;
ct[i] = t;
c++;
} else {
t += tq;
r[i] -= tq;
}
}
ARJUN G
953623205004
}
}

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


tt[i] = ct[i] - a[i];
wt[i] = tt[i] - b[i];
printf("%d %d %d %d %d %d\n", p[i], a[i], b[i], ct[i], tt[i], wt[i]);
}

return 0;
}

OUTPUT :

You might also like