-
Notifications
You must be signed in to change notification settings - Fork 0
/
disastrOS.c
371 lines (292 loc) · 10.2 KB
/
disastrOS.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
#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include "disastrOS.h"
#include "disastrOS_syscalls.h"
#include "disastrOS_timer.h"
#include "disastrOS_resource.h"
#include "disastrOS_descriptor.h"
#include "disastrOS_mq.h"
FILE* log_file=NULL;
PCB* init_pcb;
PCB* running;
int last_pid;
ListHead ready_list;
ListHead waiting_list;
ListHead zombie_list;
ListHead timer_list;
/**
int reader_messi_in_pausa = 0;
int reader_fatti_ripartire = 0;
int writer_messi_in_pausa = 0;
int writer_fatti_ripartire = 0;
**/
// a resource can be a device, a file or an ipc thing
ListHead resources_list;
ListHead mq_list;
SyscallFunctionType syscall_vector[DSOS_MAX_SYSCALLS];
int syscall_numarg[DSOS_MAX_SYSCALLS];
ucontext_t interrupt_context;
ucontext_t trap_context;
ucontext_t main_context;
ucontext_t idle_context;
int shutdown_now=0; // used for termination
char system_stack[STACK_SIZE];
sigset_t signal_set; // process wide signal mask
char signal_stack[STACK_SIZE];
volatile int disastrOS_time=0;
void timerHandler(int j, siginfo_t *si, void *old_context) {
if(DEBUG==1) printf("Sono entrato nella funzione timerHandler, sto per fare uno swap\n");
swapcontext(&running->cpu_state, &interrupt_context);
if(DEBUG==1) printf("Ho fatto uno swap in TimerHandler\n");
}
void timerInterrupt(){
if (log_file)
fprintf(log_file, "TIME: %d\tPID: %d\tACTION: %s\n", disastrOS_time, running->pid, "TIMER_OUT");
++disastrOS_time;
printf("time: %d\n", disastrOS_time);
internal_schedule();
if (log_file)
fprintf(log_file, "TIME: %d\tPID: %d\tACTION: %s\n", disastrOS_time, running->pid, "TIMER_IN");
printf("running pid: %d\n", running->pid);
//disastrOS_printStatus();
setcontext(&running->cpu_state);
}
//set up the signal action
void setupSignals(void) {
struct sigaction act;
act.sa_sigaction = timerHandler;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART | SA_SIGINFO;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGALRM);
if(sigaction(SIGALRM, &act, NULL) != 0) {
perror("Signal handler");
}
// start the timer
struct itimerval it;
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = INTERVAL * 1000;
it.it_value = it.it_interval;
if (setitimer(ITIMER_REAL, &it, NULL) ) perror("setitiimer");
}
int disastrOS_syscall(int syscall_num, ...) {
assert(running);
va_list ap;
if (syscall_num<0||syscall_num>DSOS_MAX_SYSCALLS)
return DSOS_ESYSCALL_OUT_OF_RANGE;
int nargs=syscall_numarg[syscall_num];
va_start(ap,syscall_num);
for (int i=0; i<nargs; ++i){
running->syscall_args[i] = va_arg(ap,long int);
}
va_end(ap);
running->syscall_num=syscall_num;
swapcontext(&running->cpu_state, &trap_context);
return running->syscall_retvalue;
}
void disastrOS_trap(){
int syscall_num=running->syscall_num;
if (log_file)
fprintf(log_file, "TIME: %d\tPID: %d\tACTION: %s %d\n",
disastrOS_time,
running->pid,
"SYSCALL_IN",
syscall_num);
if (syscall_num<0||syscall_num>DSOS_MAX_SYSCALLS) {
running->syscall_retvalue = DSOS_ESYSCALL_OUT_OF_RANGE;
goto return_to_process;
}
SyscallFunctionType my_syscall=syscall_vector[syscall_num];
if (! my_syscall) {
running->syscall_retvalue = DSOS_ESYSCALL_NOT_IMPLEMENTED;
goto return_to_process;
}
disastrOS_debug("syscall: %d, pid: %d\n", syscall_num, running->pid);
(*my_syscall)();
//internal_schedule();
return_to_process:
if (log_file)
fprintf(log_file, "TIME: %d\tPID: %d\tACTION: %s %d\n",
disastrOS_time,
running->pid,
"SYSCALL_OUT",
syscall_num);
if (running)
setcontext(&running->cpu_state);
else {
printf("no active processes\n");
//disastrOS_printStatus();
}
}
void disastrOS_start(void (*f)(void*), void* f_args, char* logfile){
/* INITIALIZATION OF SYSTEM STRUCTURES*/
disastrOS_debug("initializing system structures\n");
PCB_init();
Timer_init();
Resource_init();
Descriptor_init();
MessageQueue_init();
Message_init();
init_pcb=0;
// populate the vector of syscalls and number of arguments for each syscall
for (int i=0; i<DSOS_MAX_SYSCALLS; ++i){
syscall_vector[i]=0;
}
syscall_vector[DSOS_CALL_PREEMPT] = internal_preempt;
syscall_numarg[DSOS_CALL_PREEMPT] = 0;
syscall_vector[DSOS_CALL_FORK] = internal_fork;
syscall_numarg[DSOS_CALL_FORK] = 0;
syscall_vector[DSOS_CALL_SPAWN] = internal_spawn;
syscall_numarg[DSOS_CALL_SPAWN] = 2;
syscall_vector[DSOS_CALL_WAIT] = internal_wait;
syscall_numarg[DSOS_CALL_WAIT] = 2;
syscall_vector[DSOS_CALL_EXIT] = internal_exit;
syscall_numarg[DSOS_CALL_EXIT] = 1;
syscall_vector[DSOS_CALL_SLEEP] = internal_sleep;
syscall_numarg[DSOS_CALL_SLEEP] = 1;
syscall_vector[DSOS_CALL_OPEN_RESOURCE] = internal_openResource;
syscall_numarg[DSOS_CALL_OPEN_RESOURCE] = 3;
syscall_vector[DSOS_CALL_CLOSE_RESOURCE] = internal_closeResource;
syscall_numarg[DSOS_CALL_CLOSE_RESOURCE] = 1;
syscall_vector[DSOS_CALL_DESTROY_RESOURCE] = internal_destroyResource;
syscall_numarg[DSOS_CALL_DESTROY_RESOURCE] = 1;
syscall_vector[DSOS_CALL_SHUTDOWN] = internal_shutdown;
syscall_numarg[DSOS_CALL_SHUTDOWN] = 0;
//Nuove system call
syscall_vector[DSOS_CALL_OPEN_MQ] = internal_openMessageQueue;
syscall_numarg[DSOS_CALL_OPEN_MQ] = 2;
syscall_vector[DSOS_CALL_CLOSE_MQ] = internal_closeMessageQueue;
syscall_numarg[DSOS_CALL_CLOSE_MQ] = 1;
syscall_vector[DSOS_CALL_WRITE_MQ] = internal_MessageQueue_write;
syscall_numarg[DSOS_CALL_WRITE_MQ] = 3;
syscall_vector[DSOS_CALL_READ_MQ] = internal_MessageQueue_read;
syscall_numarg[DSOS_CALL_READ_MQ] = 3;
syscall_vector[DSOS_CALL_DESTROY_MQ] = internal_destroyMessageQueue;
syscall_numarg[DSOS_CALL_DESTROY_MQ] = 1;
// setup the scheduling lists
running=0;
List_init(&ready_list);
List_init(&waiting_list);
List_init(&zombie_list);
List_init(&resources_list);
List_init(&timer_list);
List_init(&mq_list);
/* INITIALIZATION OF SYSCALL AND INTERRUPT INFRASTRUCTIRE*/
disastrOS_debug("setting entry point for system shudtown... ");
getcontext(&main_context); //<< we will come back here on shutdown
if (shutdown_now)
exit(0);
// setting system trap
disastrOS_debug("setting entry point for system trap... ");
getcontext(&trap_context);
trap_context.uc_stack.ss_sp = system_stack;
trap_context.uc_stack.ss_size = STACK_SIZE;
sigemptyset(&trap_context.uc_sigmask);
sigaddset(&trap_context.uc_sigmask, SIGALRM); //< this disables alarm during system trap
trap_context.uc_stack.ss_flags = 0;
trap_context.uc_link = &main_context;
makecontext(&trap_context, disastrOS_trap, 0); //<< this extablishes a context for the system
disastrOS_debug("setting entry point for timer interrupt... ");
interrupt_context=trap_context; // the interrupt and the system live on the same stack
interrupt_context.uc_link = &main_context;
sigemptyset(&interrupt_context.uc_sigmask);
makecontext(&interrupt_context, timerInterrupt, 0); //< this is a context for the interrupt
/* STARTING FIRST PROCESS AND IDLING*/
running=PCB_alloc();
running->status=Running;
init_pcb=running;
// create a trampoline for the first process (see spawn)
disastrOS_debug("preparing trampoline for first process ... ");
getcontext(&running->cpu_state);
running->cpu_state.uc_stack.ss_sp = running->stack;
running->cpu_state.uc_stack.ss_size = STACK_SIZE;
running->cpu_state.uc_stack.ss_flags = 0;
running->cpu_state.uc_link = &main_context;
makecontext(&running->cpu_state, (void(*)()) f, 1, f_args);
// initialize timers and signals
setupSignals();
// we start the first process
disastrOS_debug("starting\n");
if (logfile){
log_file=fopen(logfile, "w");
fprintf(log_file, "TIME: %d\tPID: -1\tACTION: START\n", disastrOS_time);
}
setcontext(&running->cpu_state);
}
int disastrOS_fork(){
return disastrOS_syscall(DSOS_CALL_FORK);
}
int disastrOS_wait(int pid, int *retval){
return disastrOS_syscall(DSOS_CALL_WAIT, pid, retval);
}
void disastrOS_exit(int exitval) {
disastrOS_syscall(DSOS_CALL_EXIT, exitval);
}
void disastrOS_preempt() {
disastrOS_syscall(DSOS_CALL_PREEMPT);
}
void disastrOS_spawn(void (*f)(void*), void* args ) {
disastrOS_syscall(DSOS_CALL_SPAWN, f, args);
}
void disastrOS_shutdown() {
disastrOS_syscall(DSOS_CALL_SHUTDOWN);
}
void disastrOS_sleep(int sleep_time) {
disastrOS_syscall(DSOS_CALL_SLEEP, sleep_time);
}
int disastrOS_getpid(){
if (! running)
return -1;
return running->pid;
}
int disastrOS_openResource(int resource_id, int type, int mode) {
return disastrOS_syscall(DSOS_CALL_OPEN_RESOURCE, resource_id, type, mode);
}
int disastrOS_closeResource(int fd) {
return disastrOS_syscall(DSOS_CALL_CLOSE_RESOURCE, fd);
}
int disastrOS_destroyResource(int resource_id) {
return disastrOS_syscall(DSOS_CALL_DESTROY_RESOURCE, resource_id);
}
// Nuove syscalls
int disastrOS_openMessageQueue(int id, int mode) {
return disastrOS_syscall(DSOS_CALL_OPEN_MQ, id, mode);
}
int disastrOS_closeMessageQueue(int fd) {
return disastrOS_syscall(DSOS_CALL_CLOSE_MQ, fd);
}
int disastrOS_MessageQueue_write(int id, char* write_buffer, int len) {
return disastrOS_syscall(DSOS_CALL_WRITE_MQ, id, write_buffer, len);
}
int disastrOS_MessageQueue_read(int id, char* buf, int len) {
return disastrOS_syscall(DSOS_CALL_READ_MQ, id, buf, len);
}
int disastrOS_MessageQueue_destroy(int id) {
return disastrOS_syscall(DSOS_CALL_DESTROY_MQ, id);
}
void disastrOS_printStatus(){
printf("\n****************** DisastrOS ******************\n");
printf("Running: ");
if (running)
PCB_print(running);
printf("\n");
printf("Timers: ");
TimerList_print(&timer_list);
printf("\nResources: ");
ResourceList_print(&resources_list);
printf("\nMessage Queues: ");
MessageQueueList_print(&mq_list);
printf("\nReady: ");
PCBList_print(&ready_list);
printf("\nWaiting: ");
PCBList_print(&waiting_list);
printf("\nZombie: ");
PCBList_print(&zombie_list);
printf("\n***********************************************\n\n");
};