Answered step by step
Verified Expert Solution
Question
1 Approved Answer
i have this code in C language but i have to write a code for which algorithm preformed better between these cpu scheduling: #include #include
i have this code in C language but i have to write a code for which algorithm preformed better between these cpu scheduling:
#include
#include
#define NUM_PROCESSES 5
typedef struct process {
int arrivalTime;
int burstTime;
int priority;
int waitingTime;
} process;
int compareFCFS(const void *a, const void *b) {
process *p1 = (process *)a;
process *p2 = (process *)b;
return p1->arrivalTime - p2->arrivalTime;
}
int compareSJF(const void *a, const void *b) {
process *p1 = (process *)a;
process *p2 = (process *)b;
return p1->burstTime - p2->burstTime;
}
int compareSRTF(const void *a, const void *b) {
process *p1 = (process *)a;
process *p2 = (process *)b;
return p1->burstTime - p2->burstTime;
}
void calculateWaitingTime(process processes[], int n, char *algorithm) {
int completionTime[n];
completionTime[0] = processes[0].burstTime + processes[0].arrivalTime;
processes[0].waitingTime = 0;
for (int i = 1; i < n; i++) {
completionTime[i] = completionTime[i - 1] + processes[i].burstTime;
processes[i].waitingTime = completionTime[i] - processes[i].arrivalTime - processes[i].burstTime;
}
int totalWaitingTime = 0;
for (int i = 0; i < n; i++) {
totalWaitingTime += processes[i].waitingTime;
}
printf("Average waiting time using %s: %.2f ", algorithm, (float)totalWaitingTime / n);
}
int main() {
process processes[NUM_PROCESSES];
printf("Enter arrival time, burst time and priority for %d processes: ", NUM_PROCESSES);
for (int i = 0; i < NUM_PROCESSES; i++) {
scanf("%d%d%d", &processes[i].arrivalTime, &processes[i].burstTime, &processes[i].priority);
}
qsort(processes, NUM_PROCESSES, sizeof(process), compareFCFS);
calculateWaitingTime(processes, NUM_PROCESSES, "FCFS");
qsort(processes, NUM_PROCESSES, sizeof(process), compareSJF);
calculateWaitingTime(processes, NUM_PROCESSES, "SJF");
qsort(processes, NUM_PROCESSES, sizeof(process), compareSRTF);
calculateWaitingTime(processes, NUM_PROCESSES, "SRTF");
return 0;
}
OUTPUT:
Enter arrival time, burst time and priority for 5 processes:
2 3 3
4 6 7
2 8 5
9 6 4
1 0 5
Average waiting time using FCFS: 3.60
Average waiting time using SJF: 2.80
Average waiting time using SRTF: 2.80
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started