Program-2-1
Program-2-1
Simulate the following CPU scheduling algorithms to find turnaround time and
waiting time
a) FCFS
b) SJF
c) Round Robin
d) Priority.*/
a) FCFS :
#include <stdio.h>
struct Process {
int pid; // Process ID
int arrival; // Arrival time
int burst; // Burst time
};
printf("%d\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid,
processes[i].arrival,
processes[i].burst, waitingTime, turnaroundTime);
currentTime += processes[i].burst;
}
}
int main() {
struct Process processes[] = {
{1, 0, 6},
{2, 2, 3},
{3, 3, 8}
};
int n = sizeof(processes) / sizeof(processes[0]);
fcfsScheduling(processes, n);
return 0;
}
b) SJF:
#include <stdio.h>
struct Process {
int pid; // Process ID
int burst; // Burst time
};
int main() {
int n;
printf("Enter the number of processes: ");
scanf("%d", &n);
sortProcesses(proc, n);
findAverageTime(proc, n);
return 0;
}
c) Round Robin:
#include <stdio.h>
struct Process {
int pid; // Process ID
int burst; // Burst time
};
while (1) {
int allDone = 1;
if (allDone)
break;
}
}
int main() {
struct Process processes[] = {
{1, 10},
{2, 5},
{3, 8},
{4, 12}
};
return 0;
}
d) Priority:
#include <stdio.h>
#include <stdlib.h>
#define MAX_PROCESSES 10
int total_time = 0;
double average_waiting_time = 0.0;
average_waiting_time /= num_processes;
printf("\nAverage Waiting Time: %.2lf seconds\n", average_waiting_time);
}
int main() {
int num_processes;
printf("Enter the number of processes: ");
scanf("%d", &num_processes);
Process processes[MAX_PROCESSES];
initializeProcesses(processes, num_processes);
priorityScheduling(processes, num_processes);
return 0;
}