#include <stdio.
h>
#include <limits.h>
int main() {
int numProcesses;
printf("Enter number of processes: ");
scanf("%d", &numProcesses);
int arrivalTime[numProcesses], burstTime[numProcesses],
remainingTime[numProcesses];
int waitingTime[numProcesses], turnaroundTime[numProcesses],
completionTime[numProcesses];
int completed[numProcesses];
// Input arrival and burst times
for (int i = 0; i < numProcesses; i++) {
printf("Enter arrival time of PID %d: ", i + 1);
scanf("%d", &arrivalTime[i]);
printf("Enter burst time of PID %d: ", i + 1);
scanf("%d", &burstTime[i]);
remainingTime[i] = burstTime[i]; // Initialize remaining time
completed[i] = 0; // Initialize completed status
}
int time = 0, completedProcesses = 0;
while (completedProcesses < numProcesses) {
int minIndex = -1;
int minRemainingTime = INT_MAX;
// Find the process with the smallest remaining time that has arrived
for (int i = 0; i < numProcesses; i++) {
if (arrivalTime[i] <= time && !completed[i] && remainingTime[i] <
minRemainingTime) {
minRemainingTime = remainingTime[i];
minIndex = i;
}
}
if (minIndex != -1) {
// Execute the selected process for one unit of time
time++;
remainingTime[minIndex]--;
// If the process is completed
if (remainingTime[minIndex] == 0) {
completed[minIndex] = 1; // Mark process as completed
completedProcesses++; // Increment completed process count
completionTime[minIndex] = time; // Set completion time
// Calculate turnaround and waiting times
turnaroundTime[minIndex] = completionTime[minIndex] -
arrivalTime[minIndex];
waitingTime[minIndex] = turnaroundTime[minIndex] -
burstTime[minIndex];
}
} else {
// If no process is ready, increment time
time++;
}
}
// Calculate averages
float totalWaitingTime = 0, totalTurnaroundTime = 0;
printf("PID\tArrival\tBurst\tCompletion\tTAT\tWait\n");
for (int i = 0; i < numProcesses; i++) {
printf("%d\t%d\t%d\t%d\t\t%d\t%d\n",
i + 1,
arrivalTime[i],
burstTime[i],
completionTime[i],
turnaroundTime[i],
waitingTime[i]);
totalWaitingTime += waitingTime[i];
totalTurnaroundTime += turnaroundTime[i];
}
printf("\nAverage Waiting Time: %.2f\n", totalWaitingTime / numProcesses);
printf("Average Turnaround Time: %.2f\n", totalTurnaroundTime / numProcesses);
return 0;
}