-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisastrOS_mytest.c
167 lines (134 loc) · 4.14 KB
/
disastrOS_mytest.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
#include <stdio.h>
#include <unistd.h>
#include <poll.h>
#include <assert.h>
#include <string.h>
#include "disastrOS.h"
#include "disastrOS_message_queue.h"
#define MQ 4
#define CHILDREN 8
// 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_writer(void* args){
printf("Hello, I am the child function %d\n",disastrOS_getpid());
printf("I will iterate a bit, before terminating\n");
int type=MESSAGE_QUEUE;
int mode=0;
int fd_passed = *(int*)args;
int fd=disastrOS_openResource(fd_passed,type,mode);
assert(fd>=0);
disastrOS_printStatus();
char message[MAX_MESSAGE_LENGTH] = "Hi I'm a writer.";
if(disastrOS_getpid()%5 == 0)
disastrOS_sleep(15);
if(disastrOS_getpid()%3 == 0)
disastrOS_sleep(24);
int res = DSOS_EMQAGAIN;
int written_messages = 0;
for (int i=0; i<MAX_MESSAGES_FOR_MQ; ++i){
res = DSOS_EMQAGAIN;
while(res == DSOS_EMQAGAIN){
res = disastrOS_writeMessageQueue(fd, message, MAX_MESSAGE_LENGTH);
}
assert(res >= 0);
written_messages++;
}
printf("child: written %d messages, I'm exiting..\n", written_messages);
disastrOS_closeResource(fd);
disastrOS_exit(written_messages);
}
void childFunction_reader(void* args){
printf("Hello, I am the child function %d\n",disastrOS_getpid());
printf("I will iterate a bit, before terminating\n");
int type=MESSAGE_QUEUE;
int mode=0;
int fd_passed = *(int*)args;
int fd=disastrOS_openResource(fd_passed,type,mode);
printf("Reading on MQ with FD = %d\n", fd_passed);
disastrOS_printStatus();
char message[MAX_MESSAGE_LENGTH];
int read_messages = 0;
int res = DSOS_EMQAGAIN;
if(disastrOS_getpid()%7 == 0)
disastrOS_sleep(15);
if(disastrOS_getpid()%6 == 0)
disastrOS_sleep(24);
for (int i=0; i<MAX_MESSAGES_FOR_MQ; ++i){
memset(message, 0, sizeof(message));
res = DSOS_EMQAGAIN;
while(res == DSOS_EMQAGAIN){
res = disastrOS_readMessageQueue(fd, message, MAX_MESSAGE_LENGTH);
}
assert(res >= 0);
read_messages++;
}
printf("child %d: read %d messages, I'm exiting..\n", disastrOS_getpid(), read_messages);
disastrOS_closeResource(fd);
disastrOS_exit(read_messages);
}
void initFunction(void* args) {
disastrOS_printStatus();
printf("hello, I am init and I just started\n");
printf("I feel like to spawn %d nice threads\n", CHILDREN);
int alive_children=0;
int id_resource[MQ];
int type=0;
int mode=MESSAGE_QUEUE;
printf("mode: %d\n", mode);
printf("opening resource (and creating if necessary)\n");
disastrOS_spawn(sleeperFunction, 0);
for (int i=0; i<MQ; ++i) {
type=MESSAGE_QUEUE;
mode=DSOS_CREATE;
printf("mode: %d\n", mode);
printf("opening resource (and creating if necessary)\n");
id_resource[i]= i;
assert(disastrOS_openResource(i,type,mode)>=0);
}
int counter = 0;
for(int i = 0; i < CHILDREN/2; i++){
if(counter == MQ) counter = 0;
disastrOS_spawn(childFunction_writer, id_resource+counter);
disastrOS_spawn(childFunction_reader, id_resource+counter);
alive_children+=2;
counter += 1;
}
disastrOS_printStatus();
int retval;
int pid;
int read_messages = 0;
int written_messages = 0;
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);
if(pid%2==0) written_messages += retval;
else read_messages += retval;
--alive_children;
}
printf("Written messages: %d\n", written_messages);
printf("Read messages: %d\n", read_messages);
printf("They must be equal, or there is something wrong!\n");
for(int i = 0; i < MQ; i++){
disastrOS_closeResource(id_resource[i]);
disastrOS_destroyResource(id_resource[i]);
}
printf("shutdown!\n");
disastrOS_shutdown();
}
int main(int argc, char** argv){
char* logfilename=0;
if (argc>1) {
logfilename=argv[1];
}
// spawn an init process
printf("start\n");
disastrOS_start(initFunction, 0, logfilename);
return 0;
}