-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisastrOS_test.c
82 lines (69 loc) · 2.06 KB
/
disastrOS_test.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
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <stdlib.h>
#include "disastrOS.h"
// we need this to handle the sleep state
void sleeperFunction(void* args){
printf("Hello, I am the sleeper, and I sleep %d\n",disastrOS_getpid());
while(1) {
getc(stdin);
disastrOS_printStatus();
}
}
void childFunction(void* args){
printf("Hello, I am the child function %d\n",disastrOS_getpid());
printf("I will iterate a bit, before terminating\n");
int type=0;
int mode=0;
int fd=disastrOS_openResource(disastrOS_getpid(),type,mode);
printf("fd=%d\n", fd);
printf("PID: %d, terminating\n", disastrOS_getpid());
for (int i=0; i<(disastrOS_getpid()+1); ++i){
printf("PID: %d, iterate %d\n", disastrOS_getpid(), i);
disastrOS_sleep((20-disastrOS_getpid())*5);
}
disastrOS_exit(disastrOS_getpid()+1);
}
void initFunction(void* args) {
disastrOS_printStatus();
printf("hello, I am init and I just started\n");
disastrOS_spawn(sleeperFunction, 0);
printf("I feel like to spawn 10 nice threads\n");
int alive_children=0;
for (int i=0; i<10; ++i) {
int type=0;
int mode=DSOS_CREATE;
printf("mode: %d\n", mode);
printf("opening resource (and creating if necessary)\n");
int fd=disastrOS_openResource(i,type,mode);
printf("fd=%d\n", fd);
disastrOS_spawn(childFunction, 0);
alive_children++;
}
disastrOS_printStatus();
int retval;
int pid;
while(alive_children>0 && (pid=disastrOS_wait(0, &retval))>=0){
disastrOS_printStatus();
printf("initFunction, child: %d terminated, retval:%d, alive: %d \n",
pid, retval, alive_children);
--alive_children;
}
printf("shutdown!");
disastrOS_shutdown();
}
int main(int argc, char** argv){
char* logfilename=0;
if (argc>1) {
logfilename=argv[1];
}
// we create the init process processes
// the first is in the running variable
// the others are in the ready queue
printf("the function pointer is: %p", childFunction);
// spawn an init process
printf("start\n");
disastrOS_start(initFunction, 0, logfilename);
return 0;
}