Experiment 1:
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;
printf("\nEnter the number of processes: ");
scanf("%d", &n);
// Input burst times for each process
for(i = 0; i < n; i++)
{
printf("\nEnter Burst Time for Process %d: ", i);
scanf("%d", &bt[i]);
}
// Initial values for first process
wt[0] = 0;
tat[0] = bt[0];
// Calculate waiting times and turnaround times
for(i = 1; i < n; i++)
{
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = tat[i - 1] + bt[i];
wtavg += wt[i];
tatavg += tat[i];
}
// Output the results
printf("\n\tPROCESS\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");
for(i = 0; i < n; i++)
printf("\n\tP%d\t\t%d\t\t%d\t\t%d", i, bt[i], wt[i], tat[i]);
// Calculate and print averages
wtavg /= n;
tatavg /= n;
printf("\n\nAverage Waiting Time: %f", wtavg);
printf("\nAverage Turnaround Time: %f", tatavg);
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
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P0 24 0 24
P1 3 24 27
P2 3 27 30
Average Waiting Time: 17.000000
Average Turnaround Time: 19.000000
***********************************************************************************
*****
B) SHORTEST JOB FIRST (SJF)
#include<stdio.h>
#include<conio.h>
int main()
{
int p[20], bt[20], wt[20], tat[20], i, k, n, temp; float wtavg,
tatavg;
printf("\nEnter the number of processes -- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
p[i]=i;
printf("Enter Burst Time for Process %d -- ", i);
scanf("%d", &bt[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(bt[i]>bt[k])
{
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=p[i];
p[i]=p[k];
p[k]=temp;
}
wt[0] = wtavg = 0;
tat[0] = tatavg = bt[0]; for(i=1;i<n;i++)
{
wt[i] = wt[i-1] +bt[i-1];
tat[i] = tat[i-1] +bt[i];
wtavg = wtavg + wt[i];
tatavg = tatavg + tat[i];
}
printf("\n\t PROCESS \tBURST TIME \t WAITING TIME\t TURNAROUND TIME\n");
for(i=0;i<n;i++)
printf("\n\t P%d \t\t %d \t\t %d \t\t %d", p[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time -- %f", wtavg/n);
printf("\nAverage Turnaround Time -- %f", tatavg/n);
}
Out put:
Enter the number of processes: 4
Enter Burst Time for Process P0: 6
Enter Burst Time for Process P1: 8
Enter Burst Time for Process P2: 7
Enter Burst Time for Process P3: 3
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
P3 3 0 3
P0 6 3 9
P2. 7 9 16
P1 8 16 24
Average Waiting Time: 7.00
Average Turnaround Time: 12.25
*********************************************************************************
C)ROUND ROBIN
#include <stdio.h>
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
PROCESS BURST TIME WAITING TIME TURNAROUND TIME
1 24 6 30
2 3 3 6
3 3 6 9
The Average Turnaround time is -- 15.000000
The Average Waiting time is -- 5.000000
***********************************************************************************
D) PRIORITY
#include<stdio.h>
int main()
{
int p[20], bt[20], pri[20], wt[20], tat[20], i, k, n, temp;
float wtavg = 0, tatavg = 0;
printf("Enter the number of processes --- ");
scanf("%d", &n);
if (n <= 0) {
printf("Invalid number of processes.\n");
return 1; // Exit if no processes are entered.
}
// Input Burst Time and Priority for each process
for(i = 0; i < n; i++) {
p[i] = i;
printf("Enter the Burst Time & Priority of Process %d --- ", i);
scanf("%d %d", &bt[i], &pri[i]);
}
// Sorting based on Priority (lower priority number = higher priority)
for(i = 0; i < n; i++)
for(k = i + 1; k < n; k++)
if(pri[i] > pri[k]) {
temp = p[i];
p[i] = p[k];
p[k] = temp;
temp = bt[i];
bt[i] = bt[k];
bt[k] = temp;
temp = pri[i];
pri[i] = pri[k];
pri[k] = temp;
}
// Initialize the waiting time and turnaround time
wt[0] = 0;
tat[0] = bt[0];
// Calculate waiting time and turnaround time for each process
for(i = 1; i < n; i++) {
wt[i] = wt[i - 1] + bt[i - 1];
tat[i] = tat[i - 1] + bt[i];
wtavg += wt[i];
tatavg += tat[i];
}
// Display the results
printf("\nPROCESS\t\tPRIORITY\tBURST TIME\tWAITING TIME\tTURNAROUND TIME\n");
for(i = 0; i < n; i++)
printf("%d \t\t %d \t\t %d \t\t %d \t\t %d\n", p[i], pri[i], bt[i], wt[i], tat[i]);
printf("\nAverage Waiting Time is --- %.2f", wtavg / n);
printf("\nAverage Turnaround Time is --- %.2f", tatavg / n);
return 0;
}
Out put:
Enter the number of processes --- 5
Enter the Burst Time & Priority of Process 0 --- 10 3
Enter the Burst Time & Priority of Process 1 --- 1 1
Enter the Burst Time & Priority of Process 2 --- 2 4
Enter the Burst Time & Priority of Process 3 --- 1 5
Enter the Burst Time & Priority of Process 4 --- 5 2
PROCESS PRIORITY BURST TIME WAITING TIME TURNAROUND TIME
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
Average Waiting Time is --- 8.20
Average Turnaround Time is --- 11.80