-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.c
399 lines (320 loc) · 13.2 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
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
391
392
393
394
395
396
397
398
399
#include <gtk/gtk.h>
#include <stdlib.h>
#include <time.h>
typedef struct {
int process_id;
int arrival_time;
int burst_time;
int priority;
int completion_time;
} Process;
Process processes[10];
int num_processes = 0;
GtkWidget *burst_entry;
GtkWidget *arrival_entry;
GtkWidget *priority_entry;
void clear_processes() {
num_processes = 0;
}
void reset_processes() {
memset(processes, 0, sizeof(processes));
num_processes = 0;
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "Processes have been reset.");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
void display_message_dialog(const char *message) {
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "%s", message);
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
void display_scheduling_result(const char *result) {
gchar result_message[512];
g_snprintf(result_message, sizeof(result_message), "%s\n\nScheduling Order:\n", result);
for (int i = 0; i < num_processes; i++) {
gchar process_info[128];
g_snprintf(process_info, sizeof(process_info), "Process ID: %d, Completion Time: %d\n", processes[i].process_id, processes[i].completion_time);
strcat(result_message, process_info);
}
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK_CANCEL, "%s", result_message);
gint response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
if (response == GTK_RESPONSE_OK) {
reset_processes();
}
}
void sort_by_burst_time() {
for (int i = 0; i < num_processes - 1; i++) {
for (int j = i + 1; j < num_processes; j++) {
if (processes[i].burst_time > processes[j].burst_time) {
Process temp = processes[i];
processes[i] = processes[j];
processes[j] = temp;
}
}
}
}
void lottery_schedule() {
int total_tickets = 0;
// Calculate the total number of tickets based on process priorities.
for (int i = 0; i < num_processes; i++) {
total_tickets += processes[i].priority;
}
// Generate a random ticket within the range of total_tickets.
srand(time(NULL));
int winning_ticket = rand() % total_tickets;
int selected_process = -1;
int current_tickets = 0;
// Find the process corresponding to the winning ticket.
for (int i = 0; i < num_processes; i++) {
current_tickets += processes[i].priority;
if (winning_ticket < current_tickets) {
selected_process = i;
break;
}
}
if (selected_process != -1) {
// Execute the selected process.
processes[selected_process].completion_time = processes[selected_process].burst_time;
display_scheduling_result("Lottery Scheduling Complete");
reset_processes();
} else {
display_message_dialog("No process selected.");
}
reset_processes();
}
void sjn_schedule() {
sort_by_burst_time();
int current_time = 0;
int completed = 0;
while (completed < num_processes) {
int next_process = -1;
int min_burst_time = INT_MAX;
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0) {
if (processes[i].burst_time < min_burst_time) {
min_burst_time = processes[i].burst_time;
next_process = i;
}
}
}
if (next_process != -1) {
processes[next_process].completion_time = current_time + processes[next_process].burst_time;
current_time = processes[next_process].completion_time;
completed++;
} else {
current_time++;
}
}
display_scheduling_result("SJN Scheduling Complete");
reset_processes();
}
void ljf_schedule() {
int current_time = 0;
int completed = 0;
while (completed < num_processes) {
int next_process = -1;
int max_burst_time = INT_MIN;
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0) {
if (processes[i].burst_time > max_burst_time) {
max_burst_time = processes[i].burst_time;
next_process = i;
}
}
}
if (next_process != -1) {
processes[next_process].completion_time = current_time + processes[next_process].burst_time;
current_time = processes[next_process].completion_time;
completed++;
} else {
current_time++;
}
}
display_scheduling_result("LJF Scheduling Complete");
reset_processes();
}
void fcfs_schedule() {
int current_time = 0;
int completed = 0;
while (completed < num_processes) {
int next_process = -1;
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0) {
next_process = i;
break;
}
}
if (next_process != -1) {
processes[next_process].completion_time = current_time + processes[next_process].burst_time;
current_time = processes[next_process].completion_time;
completed++;
} else {
current_time++;
}
}
display_scheduling_result("FCFS Scheduling Complete");
reset_processes();
}
void priority_schedule() {
int current_time = 0;
int completed = 0;
while (completed < num_processes) {
int next_process = -1;
int max_priority = INT_MIN;
for (int i = 0; i < num_processes; i++) {
if (processes[i].arrival_time <= current_time && processes[i].completion_time == 0) {
if (processes[i].priority > max_priority) {
max_priority = processes[i].priority;
next_process = i;
}
}
}
if (next_process != -1) {
processes[next_process].completion_time = current_time + processes[next_process].burst_time;
current_time = processes[next_process].completion_time;
completed++;
} else {
current_time++;
}
}
display_scheduling_result("Priority Scheduling Complete");
reset_processes();
}
void round_robin_schedule(int time_quantum) {
reset_processes();
int current_time = 0;
int remaining_burst[10];
for (int i = 0; i < num_processes; i++) {
remaining_burst[i] = processes[i].burst_time;
}
int done = 0;
while (!done) {
done = 1;
for (int i = 0; i < num_processes; i++) {
if (remaining_burst[i] > 0) {
done = 0;
if (remaining_burst[i] > time_quantum) {
current_time += time_quantum;
remaining_burst[i] -= time_quantum;
} else {
current_time += remaining_burst[i];
remaining_burst[i] = 0;
processes[i].completion_time = current_time;
}
}
}
}
display_scheduling_result("Round Robin Scheduling Complete");
reset_processes();
}
void on_run_sjn_clicked(GtkButton *button, gpointer user_data) {
sjn_schedule();
}
void on_run_fcfs_clicked(GtkButton *button, gpointer user_data) {
fcfs_schedule();
}
void on_run_priority_clicked(GtkButton *button, gpointer user_data) {
priority_schedule();
}
void on_run_round_robin_clicked(GtkButton *button, gpointer user_data) {
int time_quantum = 1;
round_robin_schedule(time_quantum);
}
void on_run_ljf_clicked(GtkButton *button, gpointer user_data) {
ljf_schedule();
}
void on_run_lottery_clicked(GtkButton *button, gpointer user_data) {
lottery_schedule();
}
void on_run_clicked(GtkButton *button, gpointer user_data) {
int burst_time = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(burst_entry));
int arrival_time = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(arrival_entry));
int priority = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(priority_entry));
Process new_process;
new_process.process_id = num_processes + 1;
new_process.burst_time = burst_time;
new_process.arrival_time = arrival_time;
new_process.priority = priority;
new_process.completion_time = 0;
processes[num_processes++] = new_process;
gchar process_details[128];
g_snprintf(process_details, sizeof(process_details), "Process ID: %d\nArrival Time: %d\nBurst Time: %d\nPriority: %d", new_process.process_id, new_process.arrival_time, new_process.burst_time, new_process.priority);
display_message_dialog(process_details);
}
void on_reset_clicked(GtkButton *button, gpointer user_data) {
GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK_CANCEL, "Reset processes and clear existing data?");
gint response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
if (response == GTK_RESPONSE_OK) {
clear_processes();
display_message_dialog("Processes cleared. You can now add new processes.");
}
}
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *button_box;
GtkWidget *run_sjn_button, *run_fcfs_button, *run_priority_button, *run_rr_button, *run_ljf_button, *run_lottery_button;
GtkWidget *add_process_button;
GtkWidget *reset_button;
GtkWidget *burst_label, *arrival_label, *priority_label;
GtkWidget *burst_spin, *arrival_spin, *priority_spin;
burst_entry = burst_spin;
arrival_entry = arrival_spin;
priority_entry = priority_spin;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
gtk_container_add(GTK_CONTAINER(window), vbox);
label = gtk_label_new("Enter Process Arrival Time, Burst Time, and Priority:");
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
burst_label = gtk_label_new("Burst Time:");
gtk_box_pack_start(GTK_BOX(vbox), burst_label, FALSE, FALSE, 0);
burst_spin = gtk_spin_button_new_with_range(0, 100, 1);
gtk_box_pack_start(GTK_BOX(vbox), burst_spin, FALSE, FALSE, 0);
burst_entry = burst_spin;
arrival_label = gtk_label_new("Arrival Time:");
gtk_box_pack_start(GTK_BOX(vbox), arrival_label, FALSE, FALSE, 0);
arrival_spin = gtk_spin_button_new_with_range(0, 100, 1);
gtk_box_pack_start(GTK_BOX(vbox), arrival_spin, FALSE, FALSE, 0);
arrival_entry = arrival_spin;
priority_label = gtk_label_new("Priority:");
gtk_box_pack_start(GTK_BOX(vbox), priority_label, FALSE, FALSE, 0);
priority_spin = gtk_spin_button_new_with_range(0, 10, 1);
gtk_box_pack_start(GTK_BOX(vbox), priority_spin, FALSE, FALSE, 0);
priority_entry = priority_spin;
add_process_button = gtk_button_new_with_label("Add Process");
g_signal_connect(add_process_button, "clicked", G_CALLBACK(on_run_clicked), NULL);
gtk_box_pack_start(GTK_BOX(vbox), add_process_button, FALSE, FALSE, 0);
reset_button = gtk_button_new_with_label("Reset Processes");
g_signal_connect(reset_button, "clicked", G_CALLBACK(on_reset_clicked), NULL);
gtk_box_pack_start(GTK_BOX(vbox), reset_button, FALSE, FALSE, 0);
button_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
gtk_box_pack_start(GTK_BOX(vbox), button_box, FALSE, FALSE, 0);
run_sjn_button = gtk_button_new_with_label("Run SJN");
g_signal_connect(run_sjn_button, "clicked", G_CALLBACK(on_run_sjn_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_sjn_button, FALSE, FALSE, 0);
run_ljf_button = gtk_button_new_with_label("Run LJF");
g_signal_connect(run_ljf_button, "clicked", G_CALLBACK(on_run_ljf_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_ljf_button, FALSE, FALSE, 0);
run_fcfs_button = gtk_button_new_with_label("Run FCFS");
g_signal_connect(run_fcfs_button, "clicked", G_CALLBACK(on_run_fcfs_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_fcfs_button, FALSE, FALSE, 0);
run_priority_button = gtk_button_new_with_label("Run Priority");
g_signal_connect(run_priority_button, "clicked", G_CALLBACK(on_run_priority_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_priority_button, FALSE, FALSE, 0);
run_rr_button = gtk_button_new_with_label("Run Round Robin");
g_signal_connect(run_rr_button, "clicked", G_CALLBACK(on_run_round_robin_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_rr_button, FALSE, FALSE, 0);
run_lottery_button = gtk_button_new_with_label("Run Lottery");
g_signal_connect(run_lottery_button, "clicked", G_CALLBACK(on_run_lottery_clicked), NULL);
gtk_box_pack_start(GTK_BOX(button_box), run_lottery_button, FALSE, FALSE, 0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
// Compile using:
// gcc `pkg-config --cflags gtk+-3.0` -o scheduling main.c `pkg-config --libs gtk+-3.0`