/
external.cpp
276 lines (248 loc) · 5.84 KB
/
external.cpp
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
#include "QStream.h"
#include <QList>
#include <QVariant>
#include <mqueue.h>
#include <stdio.h>
#define P0 "/70"
#define P1 "/71"
#define P2 "/72"
#define P3 "/73"
#define P4 "/74"
#define P5 "/75"
#define P6 "/76"
QList<QString> parseArguments(int argc, char *argv[]);
bool getArgumentValues(QList<QString> arguments,int &pid, float &initialTemperature);
void sendFloat(mqd_t name, float f, float pid);
float receiveFloat(mqd_t mailbox, float &pid);
mqd_t openMailbox(const char *name, mq_attr &ma);
bool hasMessages(mqd_t mqueue);
int numMessages(mqd_t mqueue);
int main(int argc, char *argv[])
{
int pid = -1; //PID
float temp = 0; //inital temperature from command line
float downTemp = 0;
float parentTemp = 0;
float upTemp = 0;
//parses command line arguments and updates pid and initial temperature/exits on fail
if(!getArgumentValues(parseArguments(argc, argv), pid, temp)) exit(0);
mqd_t mqueue; /*message queue*/
mqd_t parent;
mqd_t child_1;
mqd_t child_2;
struct mq_attr ma;
ma.mq_flags = 0;
ma.mq_maxmsg = 10;
ma.mq_msgsize = 33;
ma.mq_curmsgs = 0;
float child1Value;
float child2Value;
//sendFloat(mqueue, temp);
//qout << receiveFloat(mqueue);
//mq_close(mqueue);
//mq_unlink(P0);
switch(pid){
case 0:
{
mqueue = openMailbox(P0, ma);
child_1 = openMailbox(P1, ma);
child_2 = openMailbox(P2, ma);
sendFloat(child_1, temp, 0.0); //false flags as sent down
sendFloat(child_2, temp, 0.0);
break;
}
case 1:
parent = openMailbox(P0, ma);
mqueue = openMailbox(P1, ma);
child_1 = openMailbox(P3, ma);
child_2 = openMailbox(P4, ma);
break;
case 2:
parent = openMailbox(P0, ma);
mqueue = openMailbox(P2, ma);
child_1 = openMailbox(P5, ma);
child_2 = openMailbox(P6, ma);
break;
case 3:
parent = openMailbox(P1, ma);
mqueue = openMailbox(P3, ma);
break;
case 4:
parent = openMailbox(P1, ma);
mqueue = openMailbox(P4, ma);
break;
case 5:
parent = openMailbox(P2, ma);
mqueue = openMailbox(P5, ma);
break;
case 6:
parent = openMailbox(P2, ma);
mqueue = openMailbox(P6, ma);
break;
}
bool isStable = false;
while(!isStable)
{
if(pid == 0)
{
if(numMessages(mqueue) > 1)
{
float cPid = 10;
float c2Pid = 10;
float child1Value = receiveFloat(mqueue, cPid);
float child2Value = receiveFloat(mqueue, c2Pid);
upTemp = (temp + child1Value + child2Value)/3.0;
qout << "Process " << pid << " current temperature " << upTemp << endl;
if(qAbs(upTemp - temp) <= .01)
{
temp = upTemp;
isStable = true;
sendFloat(child_1, temp, 20);
sendFloat(child_2, temp, 20);
}
else
{
temp = upTemp;
sendFloat(child_1, temp, pid);
sendFloat(child_2, temp, pid);
}
}
}
if(pid == 1 || pid == 2)
{
if(hasMessages(mqueue))
{
float rPid = 10;
float value = receiveFloat(mqueue, rPid);
if(rPid == 20)
{
isStable = true;
sendFloat(child_1, temp, 20);
sendFloat(child_1, temp, 20);
}
else if(rPid == 0)
{
float downTemp = (temp + value)/2.0;
temp = downTemp;
sendFloat(child_1, downTemp, pid);
sendFloat(child_2, downTemp, pid);
}
else if(rPid == 3 || rPid == 4 || rPid == 5 || rPid == 6)
{
float child1_value = value;
float sPid = 10;
float child2_value = receiveFloat(mqueue, sPid);
if(sPid == 3 || sPid == 4 || sPid == 5 || sPid == 6)
{
upTemp = (temp + child1_value + child2_value)/3.0;
temp = upTemp;
qout << "Process " << pid << " current temperature " << upTemp << endl;
sendFloat(parent, upTemp, pid);
}
}
}
}
if(pid == 3 || pid == 4 || pid == 5 || pid ==6)
{
if(hasMessages(mqueue))
{
float rPid;
parentTemp = receiveFloat(mqueue, rPid);
if(rPid == 20)
{
isStable = true;
}
else
{
downTemp = (temp + parentTemp)/2.0;
temp = downTemp;
sendFloat(parent, downTemp, pid); //true flags it as sent up
qout << "Process " << pid << " current temperature " << downTemp << endl;
}
}
}
}
qout << "Process " << pid << " final temperature " << temp << endl;
mq_close(parent);
mq_close(child_1);
mq_close(child_2);
mq_close(mqueue);
mq_unlink(P0);
mq_unlink(P1);
mq_unlink(P2);
mq_unlink(P3);
mq_unlink(P4);
mq_unlink(P5);
mq_unlink(P6);
return 0;
} /*end main()*/
bool getArgumentValues(QList<QString> arguments,int &pid, float &initialTemperature)
{
bool isSet = true;
bool isValidFloat = false;
if(arguments[0] == "0" || (arguments[0].toInt() >=1 && arguments[0].toInt() <= 6))
pid = arguments[0].toInt();
else
{
qout << "You did not enter a valid PID." << endl;
isSet = false;
}
arguments[1].toFloat(&isValidFloat);
if(isValidFloat) initialTemperature = arguments[1].toFloat();
else
{
qout << "You did not enter a valid temperature." << endl;
isSet = false;
}
return isSet;
}
QList<QString> parseArguments(int argc, char *argv[])
{
QList<QString> arguments;
for(int i = 0; i < argc; i++)
{
arguments.append(argv[i]);
}
arguments.takeFirst();
return arguments;
}
void sendFloat(mqd_t name, float f, float pid)
{
char send[40];
float *f_send = (float*)send;
*f_send = f;
f_send[1] = pid;
mq_send(name, send, sizeof(f_send), 1); //original sizeof(float)+1
}
float receiveFloat(mqd_t mailbox, float &pid)
{
char receive[40];
float *f_receive = (float*)receive;
mq_receive(mailbox, receive, 50, NULL);
pid = f_receive[1];
return f_receive[0];
}
mqd_t openMailbox(const char *name, mq_attr &ma){
return mq_open(name, O_CREAT|O_RDWR, 0644, &ma);
}
bool hasMessages(mqd_t mqueue)
{
mq_attr mqstat;
bool hasMessages = false;
if(!mq_getattr(mqueue, &mqstat))
{
if(mqstat.mq_curmsgs > 0)
hasMessages = true;
}
return hasMessages;
}
int numMessages(mqd_t mqueue)
{
mq_attr mqstat;
if(!mq_getattr(mqueue, &mqstat))
{
return mqstat.mq_curmsgs;
}
else
return 0;
}