-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
271 lines (253 loc) · 8.31 KB
/
main.c
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
#include <dirent.h>
#include <dlfcn.h>
#include "misc.h"
#include <gtk/gtk.h>
#include <gtk/gtkx.h>
GtkWidget *window;
GtkBuilder *builder;
GtkStack *stack1;
GtkBox *algorithm_box;
GtkWidget *draw1;
GtkButton *configpcb;
GtkWidget *labelcheck;
GtkWidget *maxNumber;
GtkWidget *maxArrival;
GtkWidget *maxPrio;
GtkWidget *maxExec;
typedef struct
{
gint value1;
gint value2;
gint value3;
gint value4;
} SpinButtonValues;
typedef void (*SchedulingAlgorithm)(processus *);
void extractFunctionName(const char *algorithmName, char *functionName)
{
const char *lastDot = strrchr(algorithmName, '.');
if (lastDot)
{
size_t length = lastDot - algorithmName;
strncpy(functionName, algorithmName, length);
functionName[length] = '\0';
}
else
{
strcpy(functionName, algorithmName);
}
}
SchedulingAlgorithm loadSchedulingAlgorithm(const char *algorithmName)
{
char fullPath[256];
printf("loading scheduling algo from lib");
snprintf(fullPath, sizeof(fullPath), "/usr/local/lib/algos/%s", algorithmName);
void *handle = dlopen(fullPath, RTLD_LAZY);
if (!handle)
{
fprintf(stderr, "Error loading %s: %s\n", algorithmName, dlerror());
exit(EXIT_FAILURE);
}
char functionName[256];
extractFunctionName(algorithmName, functionName);
SchedulingAlgorithm algorithm = (SchedulingAlgorithm)dlsym(handle, functionName);
if (!algorithm)
{
fprintf(stderr, "Error loading function from %s: %s\n", algorithmName, dlerror());
exit(EXIT_FAILURE);
}
return algorithm;
}
void getSOFiles(const char *directory, char ***soFiles, int *numFiles)
{
DIR *dir = opendir(directory);
if (!dir)
{
fprintf(stderr, "Error opening directory!!!: %s\n", directory);
exit(EXIT_FAILURE);
}
int count = 0;
struct dirent *entry;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_REG)
{
const char *dot = strrchr(entry->d_name, '.');
if (dot != NULL && strcmp(dot, ".so") == 0)
{
count++;
}
}
}
rewinddir(dir);
*soFiles = (char **)malloc(count * sizeof(char *));
if (!(*soFiles))
{
fprintf(stderr, "Memory allocation error\n");
closedir(dir);
exit(EXIT_FAILURE);
}
int currentIndex = 0;
while ((entry = readdir(dir)) != NULL)
{
if (entry->d_type == DT_REG)
{
const char *dot = strrchr(entry->d_name, '.');
if (dot != NULL && strcmp(dot, ".so") == 0)
{
int len = strlen(entry->d_name);
(*soFiles)[currentIndex] = (char *)malloc((len + 1) * sizeof(char));
if (!(*soFiles)[currentIndex])
{
fprintf(stderr, "Memory allocation error\n");
closedir(dir);
exit(EXIT_FAILURE);
}
strcpy((*soFiles)[currentIndex], entry->d_name);
currentIndex++;
}
}
}
closedir(dir);
*numFiles = count;
}
void title(void)
{
printf(" --------------------------------------------------------\n");
printf(" | ~~ (❁´◡`❁) ~~ |\n");
printf(" | PROCESS SCHEDULER APPLICATION |\n");
printf(" --------------------------------------------------------\n");
}
void on_switchbutton_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page2");
}
void on_switchbutton1_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page1");
}
void on_configpcb_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page4");
gtk_label_set_text(GTK_LABEL(labelcheck), "");
}
void on_feelingLucky_clicked(GtkButton *b)
{
generateFile();
FILE *file = fopen("pcb.txt", "rt");
processus *p = enreg_pcb(file);
sortProcesses(p);
gtk_stack_set_visible_child_name(stack1, "page1");
fclose(file);
}
void on_generateFile_clicked(GtkButton *b, gpointer user_data)
{
SpinButtonValues *values = (SpinButtonValues *)user_data;
values->value1 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "maxNumber")));
values->value2 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "maxArrival")));
values->value3 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "maxExec")));
values->value4 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(gtk_builder_get_object(builder, "maxPrio")));
generateFileParam(values->value1, values->value2, values->value3, values->value4);
FILE *file = fopen("pcb.txt", "rt");
processus *p = enreg_pcb(file);
fclose(file);
displayTab(p);
gtk_stack_set_visible_child_name(stack1, "page1");
}
void on_configRandom_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page3");
}
void on_goback1_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page4");
gtk_label_set_text(GTK_LABEL(labelcheck), " ");
}
void on_goback_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page4");
gtk_label_set_text(GTK_LABEL(labelcheck), " ");
}
void on_inputPCBfile_clicked(GtkButton *b)
{
gtk_stack_set_visible_child_name(stack1, "page2");
}
void on_choosePCB_file_set(GtkFileChooserButton *fileChooserButton, gpointer user_data)
{
gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(fileChooserButton));
g_print("Selected file: %s\n", filename);
FILE *selectedFile = fopen(filename, "r");
if (!selectedFile)
{
g_print("Error opening selected file: %s\n", filename);
g_free(filename);
return;
}
FILE *destinationFile = fopen("pcb.txt", "w");
if (!destinationFile)
{
g_print("Error opening destination file: pcb.txt\n");
fclose(selectedFile);
g_free(filename);
return;
}
char buffer[1024];
size_t bytesRead;
while ((bytesRead = fread(buffer, 1, sizeof(buffer), selectedFile)) > 0)
{
fwrite(buffer, 1, bytesRead, destinationFile);
}
fclose(selectedFile);
fclose(destinationFile);
g_free(filename);
if (!verifFile())
{
gtk_label_set_text(GTK_LABEL(labelcheck), "File is empty ! Try again");
}
else
{
g_print("File is valid ! ");
gtk_stack_set_visible_child_name(stack1, "page1");
}
}
void on_algorithm_button_clicked(GtkButton *button, gpointer user_data)
{
const gchar *algorithm_name = gtk_button_get_label(button);
gtk_window_set_title(GTK_WINDOW(window), g_strdup(algorithm_name));
SchedulingAlgorithm algo = loadSchedulingAlgorithm(g_strdup(algorithm_name));
g_print("\n Algorithm selected: %s\n", algorithm_name);
FILE *file = fopen("pcb.txt", "rt");
processus *p = enreg_pcb(file);
fclose(file);
algo(p);
}
int main(int argc, char *argv[])
{
title();
const char *directory = "/usr/local/lib/algos";
char **soFiles;
int numFiles;
SchedulingAlgorithm algo;
getSOFiles(directory, &soFiles, &numFiles);
gtk_init(&argc, &argv);
builder = gtk_builder_new_from_file("/usr/local/lib/prototype.glade");
window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_builder_connect_signals(builder, NULL);
stack1 = GTK_STACK(gtk_builder_get_object(builder, "stack1"));
algorithm_box = GTK_BOX(gtk_builder_get_object(builder, "algorithm_box"));
configpcb = GTK_BUTTON(gtk_builder_get_object(builder, "generateFile"));
labelcheck = GTK_WIDGET(gtk_builder_get_object(builder, "vibecheck"));
SpinButtonValues spin_button_values = {0, 0, 0, 0};
g_signal_connect(configpcb, "clicked", G_CALLBACK(on_generateFile_clicked), &spin_button_values);
for (int i = 0; i < numFiles; i++)
{
gchar *function_name = soFiles[i];
GtkWidget *button = gtk_button_new_with_label(function_name);
g_signal_connect(button, "clicked", G_CALLBACK(on_algorithm_button_clicked), NULL);
gtk_box_pack_start(algorithm_box, button, FALSE, FALSE, 0);
g_free(function_name);
}
gtk_widget_show_all(window);
gtk_main();
return EXIT_SUCCESS;
}