#include "fcfs.h"
#include "main.h"
#include <stdlib.h>

void fcfs_run(process processes[], int num_processes) {
  // Assign each to next free time
  cpu_time_t next_free = 0;
  for (int i = 0; i < num_processes; i++) {
    cpu_time_t arrival = processes[i].arrival;
    cpu_time_t burst = processes[i].burst;

    processes[i].num_schedules = 1;
    processes[i].starts = malloc(sizeof(cpu_time_t));
    processes[i].starts[0] = arrival >= next_free ? arrival : next_free;
    processes[i].durations = malloc(sizeof(cpu_time_t));
    processes[i].durations[0] = burst;
    next_free = processes[i].starts[0] + burst;
  }
}

scheduler fcfs = (scheduler){.name = "fcfs",
                             .description = "First Come First Serve",
                             .run = fcfs_run,
                             .uses_priority = 0};
