#include struct process { int pid; int burst_time; int remaining_time; }; int find_shortest_process(struct process p[], int n, int current_time) { int i, shortest_index = -1, shortest_time = 9999; for(i=0; i 0 && p[i].burst_time < shortest_time && current_time >= p[i].arrival_time) { shortest_time = p[i].burst_time; shortest_index = i; } } return shortest_index; } void sjf_preemptive(struct process p[], int n) { int i, current_time = 0, total_time = 0, completed = 0; float avg_wait_time = 0, avg_turnaround_time = 0; printf("\nGantt Chart:\n"); while(completed != n) { int shortest_index = find_shortest_process(p, n, current_time); if(shortest_index == -1) { current_time++; continue; } p[shortest_index].remaining_time--; printf("%d P%d ", current_time, p[shortest_index].pid); if(p[shortest_index].remaining_time == 0) { completed++; total_time = current_time+1; printf("%d\n", total_time); avg_wait_time += total_time - p[shortest_index].burst_time - p[shortest_index].arrival_time; avg_turnaround_time += total_time - p[shortest_index].arrival_time; } current_time++; } avg_wait_time /= n; avg_turnaround_time /= n; printf("\nAverage Waiting Time: %.2f", avg_wait_time); printf("\nAverage Turnaround Time: %.2f", avg_turnaround_time); } int main() { int n, i; printf("Enter the number of processes: "); scanf("%d", &n); struct process p[n]; for(i=0; i