-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfcfs.c
More file actions
57 lines (49 loc) · 1.55 KB
/
fcfs.c
File metadata and controls
57 lines (49 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include "scheduler.h"
#include "utils.h"
/*
* FCFS scheduling:
* - Sort by arrival time
* - Execute each process in arrival order; if CPU is idle, advance time to next arrival
*/
void fcfs_schedule(const Process proc_list[], size_t n) {
if (n == 0) {
printf("No processes to schedule.\n");
return;
}
Process *proc = malloc(sizeof(Process) * n);
if (!proc) {
perror("malloc");
return;
}
copy_processes(proc_list, proc, n);
sort_by_arrival(proc, n);
GanttEntry *gantt = malloc(sizeof(GanttEntry) * n);
size_t gcount = 0;
int time = 0;
for (size_t i = 0; i < n; ++i) {
if (time < proc[i].arrival_time) {
/* CPU idle until arrival */
time = proc[i].arrival_time;
}
proc[i].start_time = time;
proc[i].completion_time = time + proc[i].burst_time;
proc[i].turnaround_time = proc[i].completion_time - proc[i].arrival_time;
proc[i].waiting_time = proc[i].start_time - proc[i].arrival_time;
proc[i].finished = 1;
/* Gantt entry */
gantt[gcount].pid = proc[i].pid;
gantt[gcount].start = proc[i].start_time;
gantt[gcount].end = proc[i].completion_time;
++gcount;
time = proc[i].completion_time;
}
printf("\n--- First Come First Serve (FCFS) ---\n");
print_process_table(proc, n);
print_gantt_chart(gantt, gcount);
calculate_and_print_averages(proc, n);
free(proc);
free(gantt);
}