-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_job_instance.c
More file actions
224 lines (194 loc) · 6.48 KB
/
Copy pathexecute_job_instance.c
File metadata and controls
224 lines (194 loc) · 6.48 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "OSIIA1_threads.h"
#include "timer.h"
#include "OSIIA1_terminal.h"
#include "jobs.h"
#include <string.h>
static void record_execution(struct job_instance *ji)
{
if (!ji || RECORDS_COUNT >= MAXIMUM_RECORDS)
return;
// Check if we can consolidate with the last record
if (RECORDS_COUNT > 0)
{
struct job_instance_record *last = RECORDS[RECORDS_COUNT - 1];
if (last->job_id == ji->job_id)
{
last->duration++;
last->burst_time = ji->j->burst;
if (ji->j->burst == 0)
{
long sim_time = 0;
for(int i=0; i<RECORDS_COUNT-1; i++) sim_time += RECORDS[i]->duration;
last->finish_time = sim_time + last->duration;
}
return;
}
}
struct job_instance_record *rec = calloc(1, sizeof(struct job_instance_record));
rec->job_id = ji->job_id;
rec->burst_time = ji->j->burst;
rec->arrival_time = ji->j->arrival_time - STARTING_TIME;
rec->original_burst = ji->j->original_burst;
rec->exit_code = ji->j->e_code;
rec->duration = 1;
// We'll calculate finish time in the query command by summing durations
// or we can store the current simulation time here.
// Let's use the current RECORDS sum as a proxy for simulation time.
long sim_time = 0;
for(int i=0; i<RECORDS_COUNT; i++) sim_time += RECORDS[i]->duration;
if (ji->j->burst == 0)
rec->finish_time = sim_time + 1;
// String sharing logic
rec->message = NULL;
for (int i = 0; i < RECORDS_COUNT; i++)
{
if (RECORDS[i] && RECORDS[i]->job_id == ji->job_id)
{
rec->message = RECORDS[i]->message;
break;
}
}
if (!rec->message)
{
rec->message = strdup(ji->j->e_msg);
}
RECORDS[RECORDS_COUNT++] = rec;
}
static void free_record(int index)
{
if (index < 0 || index >= RECORDS_COUNT || !RECORDS[index])
return;
uint16_t id = RECORDS[index]->job_id;
int count = 0;
// We only free the message if this is the LAST record with this job_id in the array
for (int i = index + 1; i < RECORDS_COUNT; i++)
{
if (RECORDS[i] && RECORDS[i]->job_id == id)
{
count++;
break;
}
}
if (count == 0) // This is the last one
{
free(RECORDS[index]->message);
}
free(RECORDS[index]);
RECORDS[index] = NULL;
}
void free_records()
{
if (!RECORDS) return;
pthread_mutex_lock(&BUCKET_MUTEX);
for (int i = 0; i < RECORDS_COUNT; i++)
{
free_record(i);
}
free(RECORDS);
RECORDS = NULL;
RECORDS_COUNT = 0;
pthread_mutex_unlock(&BUCKET_MUTEX);
}
void *execute_job_instance(void *arg)
{
int line = 0;
int inner_height = getmaxy(CPU_EXEC_LOG_INNER_WIN);
while (PROGRAM_IS_RUNNING)
{
pthread_mutex_lock(&BUCKET_MUTEX);
struct job_instance *shortest_job = NULL;
int shortest_bucket = -1; // 0 for IN, 1 for SUS
int shortest_idx = -1;
uint8_t min_burst = 255;
if (CURRENT_JOB)
{
min_burst = CURRENT_JOB->j->burst;
}
// Check IN_BUCKET
for (int i = 0; i < IN_BUCKET->ji_accummulation; i++)
{
if (IN_BUCKET->ji[i]->j->burst < min_burst)
{
min_burst = IN_BUCKET->ji[i]->j->burst;
shortest_job = IN_BUCKET->ji[i];
shortest_bucket = 0;
shortest_idx = i;
}
}
// Check SUS_BUCKET
for (int i = 0; i < SUS_BUCKET->ji_accummulation; i++)
{
if (SUS_BUCKET->ji[i]->j->burst < min_burst)
{
min_burst = SUS_BUCKET->ji[i]->j->burst;
shortest_job = SUS_BUCKET->ji[i];
shortest_bucket = 1;
shortest_idx = i;
}
}
// Preemption logic
if (shortest_job)
{
if (CURRENT_JOB)
{
// Suspend current job
SUS_BUCKET->ji[SUS_BUCKET->ji_accummulation++] = CURRENT_JOB;
}
// Load new job
CURRENT_JOB = shortest_job;
// Remove from its bucket
struct Bucket *b = (shortest_bucket == 0) ? IN_BUCKET : SUS_BUCKET;
for (int i = shortest_idx; i < b->ji_accummulation - 1; i++)
{
b->ji[i] = b->ji[i + 1];
}
b->ji_accummulation--;
}
if (CURRENT_JOB)
{
// Execute for 1 cycle
pthread_mutex_unlock(&BUCKET_MUTEX);
OSIIA1_sleep(TIME_QUANTA);
pthread_mutex_lock(&BUCKET_MUTEX);
CURRENT_JOB->j->burst--;
// Record execution slice (after decrement)
record_execution(CURRENT_JOB);
if (line >= inner_height - 3)
{
pthread_mutex_lock(&TERMINAL_MUTEX);
werase(CPU_EXEC_LOG_INNER_WIN);
line = 0;
pthread_mutex_unlock(&TERMINAL_MUTEX);
}
pthread_mutex_lock(&TERMINAL_MUTEX);
mvwprintw(CPU_EXEC_LOG_INNER_WIN, ++line, 1, "CPU Executing [ ID: %hx ]", CURRENT_JOB->job_id);
mvwprintw(CPU_EXEC_LOG_INNER_WIN, ++line, 1, " Message: %s", CURRENT_JOB->j->e_msg);
mvwprintw(CPU_EXEC_LOG_INNER_WIN, ++line, 1, " Burst Remaining: %hhu s", CURRENT_JOB->j->burst);
wrefresh(CPU_EXEC_LOG_INNER_WIN);
pthread_mutex_unlock(&TERMINAL_MUTEX);
if (CURRENT_JOB->j->burst == 0)
{
pthread_mutex_lock(&TERMINAL_MUTEX);
mvwprintw(CPU_EXEC_LOG_INNER_WIN, ++line, 1, ">> Job %hx Finished (Code: %hhu)", CURRENT_JOB->job_id, CURRENT_JOB->j->e_code);
wrefresh(CPU_EXEC_LOG_INNER_WIN);
pthread_mutex_unlock(&TERMINAL_MUTEX);
free_job_instance(CURRENT_JOB);
CURRENT_JOB = NULL;
}
}
else
{
pthread_mutex_unlock(&BUCKET_MUTEX);
pthread_mutex_lock(&TERMINAL_MUTEX);
werase(CPU_EXEC_LOG_INNER_WIN);
mvwprintw(CPU_EXEC_LOG_INNER_WIN, 1, 1, "CPU STATUS: IDLE");
wrefresh(CPU_EXEC_LOG_INNER_WIN);
pthread_mutex_unlock(&TERMINAL_MUTEX);
line = 1;
OSIIA1_sleep(TIME_QUANTA); // Idle
continue;
}
pthread_mutex_unlock(&BUCKET_MUTEX);
}
return NULL;
}