-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.c
More file actions
390 lines (337 loc) · 10.2 KB
/
Copy pathserver.c
File metadata and controls
390 lines (337 loc) · 10.2 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <math.h>
#define MAX_THREADS 10
#define MAX_TASKS 50 // Maximum number of tasks from tasks.config
pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
/*
Struct to store each task with:
- name
- cpu requirement C
- period T
- deadline D
*/
typedef struct
{
char task_name[20];
double execution_time;
double period;
double deadline;
} TaskConfig;
/*
Struct for activaded tasks with their
thread and data
*/
typedef struct
{
char task_name[20];
int active;
pthread_t thread_id;
TaskConfig config;
} Task;
TaskConfig predefined_tasks[MAX_TASKS]; // Stores tasks from tasks.config
int num_tasks = 0; // Actual number of tasks loaded
Task task_list[MAX_THREADS] = {0}; // Activated tasks
/* Function to get current time in milliseconds */
double get_time_ms()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000.0 + ts.tv_nsec / 1e6;
}
/* Load tasks from tasks.config */
void load_task_configurations()
{
FILE *file = fopen("tasks.config", "r");
if (!file)
{
perror("[SERVER]: Failed to open tasks.config");
exit(1);
}
num_tasks = 0;
while (num_tasks < MAX_TASKS &&
fscanf(file, "%s %lf %lf %lf",
predefined_tasks[num_tasks].task_name,
&predefined_tasks[num_tasks].execution_time,
&predefined_tasks[num_tasks].period,
&predefined_tasks[num_tasks].deadline) == 4)
{
num_tasks++;
}
fclose(file);
printf("[SERVER]: Loaded %d tasks from tasks.config\n", num_tasks);
}
/* Simulated periodic task execution with deadline monitoring */
void *task_runner(void *arg)
{
Task *task = (Task *)arg;
printf("[TASK MANAGER]: Running %s (C=%.1fms, T=%.1fms, D=%.1fms) on thread %lu...\n",
task->task_name, task->config.execution_time, task->config.period, task->config.deadline, task->thread_id);
double next_release_time = get_time_ms();
while (task->active)
{
double start_time = get_time_ms();
// Simulate task execution
struct timespec exec_time = {
.tv_sec = (int)(task->config.execution_time / 1000),
.tv_nsec = (long)((fmod(task->config.execution_time, 1000)) * 1e6)};
nanosleep(&exec_time, NULL);
double end_time = get_time_ms();
double response_time = end_time - start_time;
// Check if the task met its deadline
if (response_time > task->config.deadline)
{
printf("[TASK MANAGER]: Deadline Missed for %s! (Response Time: %.2fms, Deadline: %.2fms)\n",
task->task_name, response_time, task->config.deadline);
}
else
{
printf("[TASK MANAGER]: %s finished execution in %.2fms at thread %lu\n",
task->task_name, response_time, task->thread_id);
}
// Wait until the next release time
next_release_time += task->config.period;
double sleep_time = next_release_time - get_time_ms();
if (sleep_time > 0)
{
struct timespec sleep_ts = {
.tv_sec = (int)(sleep_time / 1000),
.tv_nsec = (long)((sleep_time - ((int)sleep_time)) * 1e6)};
nanosleep(&sleep_ts, NULL);
}
}
printf("[TASK MANAGER]: %s stopped.\n", task->task_name);
pthread_exit(0);
}
/* Function to determine which task has higher priority based on DM principle */
int compare_tasks(const void *a, const void *b)
{
TaskConfig *taskA = (TaskConfig *)a;
TaskConfig *taskB = (TaskConfig *)b;
return (taskA->deadline > taskB->deadline) - (taskA->deadline < taskB->deadline);
}
/* Response Time Analysis */
int response_time_analysis(TaskConfig *new_task)
{
TaskConfig active_tasks[MAX_THREADS];
int active_count = 0;
pthread_mutex_lock(&task_mutex);
for (int i = 0; i < MAX_THREADS; i++)
{
if (task_list[i].active)
{
active_tasks[active_count++] = task_list[i].config;
}
}
pthread_mutex_unlock(&task_mutex);
active_tasks[active_count++] = *new_task;
qsort(active_tasks, active_count, sizeof(TaskConfig), compare_tasks);
for (int i = 0; i < active_count; i++)
{
TaskConfig *task = &active_tasks[i];
double Ri = task->execution_time;
double prev_Ri = 0;
while (Ri != prev_Ri)
{
prev_Ri = Ri;
Ri = task->execution_time;
for (int j = 0; j < i; j++)
{
Ri += ceil(prev_Ri / active_tasks[j].period) * active_tasks[j].execution_time;
}
if (Ri > task->deadline)
{
printf("[RTA] Task %s CANNOT be scheduled (Ri=%.2f, Di=%.2f)\n",
new_task->task_name, Ri, task->deadline);
return 0;
}
}
}
return 1;
}
/* Activate Task if possible */
int activate_task(const char *task_name)
{
TaskConfig *config = NULL;
for (int i = 0; i < num_tasks; i++)
{
if (strcmp(predefined_tasks[i].task_name, task_name) == 0)
{
config = &predefined_tasks[i];
break;
}
}
if (!config)
return -2;
int active_tasks = 0;
pthread_mutex_lock(&task_mutex);
for (int i = 0; i < MAX_THREADS; i++)
{
if (task_list[i].active == 1)
{
active_tasks++;
}
}
pthread_mutex_unlock(&task_mutex);
if (active_tasks == MAX_THREADS)
return -3;
if (!response_time_analysis(config))
return -1;
pthread_mutex_lock(&task_mutex);
for (int i = 0; i < MAX_THREADS; i++)
{
if (task_list[i].active == 0)
{
strcpy(task_list[i].task_name, task_name);
task_list[i].active = 1;
task_list[i].config = *config;
pthread_create(&task_list[i].thread_id, NULL, task_runner, &task_list[i]);
pthread_mutex_unlock(&task_mutex);
return 1;
}
}
pthread_mutex_unlock(&task_mutex);
}
/* Deactivate Task on all threads related to it */
void deactivate_task(const char *task_name)
{
for (int i = 0; i < MAX_THREADS; i++)
{
if (task_list[i].active == 1 && strcmp(task_list[i].task_name, task_name) == 0)
{
pthread_mutex_lock(&task_mutex);
task_list[i].active = 0;
pthread_t thread_to_join = task_list[i].thread_id;
pthread_mutex_unlock(&task_mutex);
pthread_join(thread_to_join, NULL);
}
}
}
static int receive(int sd, char *retBuf, int size)
{
int totSize = 0, currSize;
while (totSize < size)
{
currSize = recv(sd, &retBuf[totSize], size - totSize, 0);
if (currSize <= 0)
return -1;
totSize += currSize;
}
return 0;
}
static void handleConnection(int currSd)
{
unsigned int netLen;
int len;
char command[50];
char response[256];
for (;;)
{
if (receive(currSd, (char *)&netLen, sizeof(netLen)))
break;
len = ntohl(netLen);
if (receive(currSd, command, len))
break;
command[len] = '\0';
int action;
char task_name[20];
if (sscanf(command, "%d %s", &action, task_name) == 2)
{
if (action == 1)
{
int status = activate_task(task_name);
if (status == 1)
snprintf(response, sizeof(response), "[SERVER]: Task %s activated", task_name);
else if (status == -1)
snprintf(response, sizeof(response), "[SERVER]: Task %s cannot be scheduled (System overloaded)", task_name);
else if (status == -2)
snprintf(response, sizeof(response), "[SERVER]: Task %s not found", task_name);
else if (status == -3)
snprintf(response, sizeof(response), "[SERVER]: Maximum tasks reached, cannot activate %s", task_name);
}
else if (action == 0)
{
deactivate_task(task_name);
snprintf(response, sizeof(response), "[SERVER]: Task %s deactivated", task_name);
}
else
{
snprintf(response, sizeof(response), "[SERVER]: Invalid action");
}
}
else
{
snprintf(response, sizeof(response), "[SERVER]: Invalid command format");
}
len = strlen(response);
netLen = htonl(len);
if (send(currSd, &netLen, sizeof(netLen), 0) == -1)
break;
if (send(currSd, response, len, 0) == -1)
break;
}
close(currSd);
}
static void *connectionHandler(void *arg)
{
int currSock = *(int *)arg;
free(arg);
handleConnection(currSock);
pthread_exit(0);
}
int main(int argc, char *argv[])
{
int sd, *currSd;
struct sockaddr_in sAddr, cAddr;
socklen_t sAddrLen;
int port;
if (argc < 2)
{
printf("Usage: %s <port>\n", argv[0]);
exit(1);
}
sscanf(argv[1], "%d", &port);
// Load tasks from tasks.config
load_task_configurations();
if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
memset(&sAddr, 0, sizeof(sAddr));
sAddr.sin_family = AF_INET;
sAddr.sin_addr.s_addr = INADDR_ANY;
sAddr.sin_port = htons(port);
if (bind(sd, (struct sockaddr *)&sAddr, sizeof(sAddr)) == -1)
{
perror("bind");
exit(1);
}
listen(sd, 5);
printf("[SERVER]: Listening on port %d...\n", port);
while (1)
{
int clientSock = accept(sd, NULL, NULL);
if (clientSock == -1)
{
perror("[SERVER]: Failed to accept connection");
continue;
}
currSd = malloc(sizeof(int));
*currSd = clientSock;
pthread_t thread_id;
pthread_create(&thread_id, NULL, connectionHandler, currSd);
pthread_detach(thread_id);
}
close(sd);
return 0;
}