24 Octwork ARIN
24 Octwork ARIN
h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int pid;
int burst_time;
int arrival_time;
int priority;
int waiting_time;
int turnaround_time;
int response_time;
} process_t;
int current_time = 0;
int total_waiting_time = 0, total_turnaround_time = 0, total_response_time =
0;
printf("\nFCFS Scheduling:\n");
printf("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\
tResponse Time\n");
total_waiting_time += processes[i].waiting_time;
total_turnaround_time += processes[i].turnaround_time;
total_response_time += processes[i].response_time;
current_time += processes[i].burst_time;
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[i].pid,
processes[i].burst_time, processes[i].arrival_time, processes[i].waiting_time,
processes[i].turnaround_time, processes[i].response_time);
}
// Find the process with the smallest burst time among the arrived
processes
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && is_completed[i] ==
0) {
if (processes[i].burst_time < min_burst_time) {
min_burst_time = processes[i].burst_time;
min_index = i;
}
// In case two processes have the same burst time, choose the
one that arrived first
if (processes[i].burst_time == min_burst_time &&
processes[i].arrival_time < processes[min_index].arrival_time) {
min_index = i;
}
}
}
if (min_index == -1) {
current_time++; // If no process has arrived yet, move the current
time
continue;
}
total_waiting_time += processes[min_index].waiting_time;
total_turnaround_time += processes[min_index].turnaround_time;
total_response_time += processes[min_index].response_time;
// Find the process with the smallest remaining burst time that has
arrived
for (int i = 0; i < n; i++) {
if (processes[i].arrival_time <= current_time && !is_completed[i] &&
remaining_burst_time[i] < min_burst_time) {
min_burst_time = remaining_burst_time[i];
min_index = i;
}
}
if (min_index == -1) {
current_time++; // If no process has arrived yet, move the current
time
continue;
}
// If the process is being executed for the first time, calculate its
response time
if (remaining_burst_time[min_index] == processes[min_index].burst_time)
{
processes[min_index].response_time = current_time -
processes[min_index].arrival_time;
total_response_time += processes[min_index].response_time;
}
total_turnaround_time += processes[min_index].turnaround_time;
total_waiting_time += processes[min_index].waiting_time;
// If process is completed
if (remaining_burst_time[index] == 0) {
completed++;
processes[index].turnaround_time = current_time -
processes[index].arrival_time;
processes[index].waiting_time = processes[index].turnaround_time -
processes[index].burst_time;
total_turnaround_time += processes[index].turnaround_time;
total_waiting_time += processes[index].waiting_time;
printf("%d\t%d\t\t%d\t\t%d\t\t%d\t\t%d\n", processes[index].pid,
processes[index].burst_time, processes[index].arrival_time,
processes[index].waiting_time, processes[index].turnaround_time,
response_time[index]);
} else {
// Re-enqueue the process if it has not finished
queue[rear] = index;
rear = (rear + 1) % MAX_PROCESSES;
}
generate_processes(processes, n);
return 0;
}