Experiment 1-Os Record
Experiment 1-Os Record
Aim: Write a C program to simulate the following non-preemptive CPU scheduling algorithms to find
turnaround time and waiting time for the above problem.
a) FCFS b) SJF c) Round Robin d) Priority
A) FIRST COME FIRST SERVE(FCFS)
#include<stdio.h>
int main()
{
int bt[20], wt[20], tat[20], i, n;
float wtavg = 0, tatavg = 0;
return 0;
}
Out put:
Enter the number of processes: 3
Enter Burst Time for Process 0: 24
Enter Burst Time for Process 1: 3
Enter Burst Time for Process 2: 3
P1 3 24 27
P2 3 27 30
***********************************************************************************
*****
P3 3 0 3
P0 6 3 9
P2. 7 9 16
P1 8 16 24
int main() {
int i, j, n, bu[10], wa[10], tat[10], t, ct[10], max;
float awt = 0, att = 0, temp = 0;
printf("Enter the no of processes -- ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter Burst Time for process %d -- ", i + 1);
scanf("%d", &bu[i]);
ct[i] = bu[i]; // Copy the burst time to ct (completion time array).
}
printf("\nEnter the size of time slice -- ");
scanf("%d", &t);
max = bu[0];
for (i = 1; i < n; i++)
if (max < bu[i])
max = bu[i];
// Round Robin scheduling simulation
for (j = 0; j < (max / t) + 1; j++) {
for (i = 0; i < n; i++) {
if (bu[i] != 0) {
if (bu[i] <= t) {
tat[i] = temp + bu[i]; // Turnaround time calculation
temp = temp + bu[i];
bu[i] = 0; // Process completed
} else {
bu[i] = bu[i] - t; // Reduce the burst time
temp = temp + t; // Add time slice
}
}
}
}
// Calculate waiting time and total turnaround time
for (i = 0; i < n; i++) {
wa[i] = tat[i] - ct[i]; // Waiting time = Turnaround time - Burst time
att += tat[i]; // Total turnaround time
awt += wa[i]; // Total waiting time
}
printf("\nThe Average Turnaround time is -- %f", att / n);
printf("\nThe Average Waiting time is -- %f ", awt / n);
printf("\n\tPROCESS\t BURST TIME \t WAITING TIME\tTURNAROUND TIME\n");
for (i = 0; i < n; i++) {
printf("\t%d \t %d \t\t %d \t\t %d \n", i + 1, ct[i], wa[i], tat[i]);
}
return 0;
}
Out put:
Enter the no of processes -- 3
Enter Burst Time for process 1 -- 24
Enter Burst Time for process 2 -- 3
Enter Burst Time for process 3 -- 3
Enter the size of time slice -- 3
1 24 6 30
2 3 3 6
3 3 6 9
1 1 1 0 1
4 2 5 1 6
0 3 10 6 16
2 4 2 16 18
3 5 1 18 19