-
Notifications
You must be signed in to change notification settings - Fork 105
/
odd_even_algo.c
321 lines (283 loc) · 6.58 KB
/
odd_even_algo.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
#include <stdio.h>
#include <time.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
typedef struct process
{
int nodeId;
int nodeValue;
} * PROCESS;
typedef struct event
{
int eventSource;
int eventTarget;
int eventType;
int eventFlag;
int eventValue;
struct event *eventNext;
} * EVENT;
EVENT initialization(EVENT evt)
{
evt = (EVENT)malloc(sizeof(struct event));
evt->eventNext = NULL;
return evt;
}
int isEmpty(EVENT evt)
{
if(evt->eventNext == NULL){
return 1;
}
return 0;
}
PROCESS generateValues(PROCESS S, int n, int iFlag)
{
S = (PROCESS)malloc(n*sizeof(struct process));
switch (iFlag)
{
case 0:
printf("\n");
printf("\t");
printf("Input Generation: SORTED SEQUENCE!");
printf("\n");
for (int i = 0; i < n; i++)
{
int val = i + 1;
S[i].nodeId = i;
S[i].nodeValue = val;
}
break;
case 1:
printf("\n");
printf("\t");
printf("Input Generation: RANDOM!");
printf("\n");
srand(time(0));
for (int i = 0; i < n; i++)
{
int val = rand() / 1000;
S[i].nodeId = i;
S[i].nodeValue = val;
}
break;
case 2:
printf("\n");
printf("\t");
printf("Input Generation: WORST CASE!");
printf("\n");
for (int i = 0; i < n; i++)
{
int val = n - i;
S[i].nodeId = i;
S[i].nodeValue = val;
}
break;
}
return S;
}
void printValues(PROCESS S, int n)
{
for (int i = 0; i < n; i++)
{
printf("%d",S[i].nodeValue);
printf("\t");
}
}
int swapValues(int *x, int *y)
{
int min, flg = 0;
if (*x <= *y)
{
min = *x;
}
else
{
min = *y;
*y = *x;
*x = min;
flg = 1;
}
return flg;
}
EVENT insert(EVENT evt, int eSource, int eTarget, int eType, int eFlag, int eValue)
{
EVENT new, prev = evt, curr;
new = (EVENT)malloc(sizeof(struct event));
new->eventSource = eSource;
new->eventTarget = eTarget;
new->eventType = eType;
new->eventFlag = eFlag;
new->eventValue = eValue;
new->eventNext = NULL;
if (prev->eventNext == NULL)
{
prev->eventNext = new;
return evt;
}
else
{
curr = prev;
while (curr->eventNext != NULL)
{
curr = curr->eventNext;
}
curr->eventNext = new;
}
return evt;
}
EVENT extractTopEvent(EVENT evt, EVENT tp)
{
EVENT del = evt, nxt, atop = tp;
atop = atop->eventNext;
if (del->eventNext != NULL)
{
del = del->eventNext;
if (del->eventNext != NULL)
{
nxt = del->eventNext;
evt->eventNext = nxt;
}
else
{
evt->eventNext = NULL;
}
atop->eventSource = del->eventSource;
atop->eventTarget = del->eventTarget;
atop->eventType = del->eventType;
atop->eventFlag = del->eventFlag;
atop->eventValue = del->eventValue;
atop->eventNext = NULL;
free(del);
}
else
{
printf("\n");
printf("\t");
printf("ERROR: No events found!!");
tp->eventNext = NULL;
atop->eventNext = NULL;
}
return tp;
}
EVENT destroyEvents(EVENT evt)
{
EVENT del = evt, nxt;
if (del->eventNext == NULL)
{
return evt;
}
else
{
del = del->eventNext;
while (del->eventNext != NULL)
{
nxt = del;
free(nxt);
del = del->eventNext;
}
free(del);
evt->eventNext == NULL;
return evt;
}
}
void handleSendEvents(EVENT evt, EVENT curr)
{
EVENT cuNext = curr->eventNext;
evt = insert(evt, cuNext->eventTarget, cuNext->eventSource, 0, cuNext->eventFlag, cuNext->eventValue);
}
void handleRecieveEvents(PROCESS S, EVENT evt, EVENT curr, int round)
{
EVENT cuNext = curr->eventNext;
int flag = cuNext->eventFlag, recievedValue = cuNext->eventValue;
if (flag == 1)
{
bool swap = swapValues(&recievedValue, &S[cuNext->eventSource].nodeValue);
evt = insert(evt, cuNext->eventSource, cuNext->eventTarget, 1, 0, recievedValue);
}
else
{
S[cuNext->eventSource].nodeValue = recievedValue;
}
}
int main(int argc, char *argv[])
{
int round = 1;
PROCESS S = NULL;
EVENT E, eTop, aTop;
srand(1000);
printf("No of Process: ");
printf("%d",5);
E = initialization(E);
eTop = initialization(eTop);
aTop = initialization(aTop);
eTop->eventNext = aTop;
S = generateValues(S, 5, 1);
printf("\n");
printf("\t");
printf("Initial : ");
printf("\t");
for (int i = 0; i < 5; i++)
{
printf("%d",S[i].nodeValue);
printf(" ");
}
while (round <= 5)
{
printf("\n");
printf("\t");
printf("STEP - ");
printf("%d",round);
printf("\t");
for (int i = 0; i < 4; i++)
{
if (i % 2 != round % 2)
{
E = insert(E, i, i + 1, 1, 1, S[i].nodeValue);
}
}
while (!isEmpty(E))
{
eTop = extractTopEvent(E, eTop);
if (eTop->eventNext->eventType == 1)
{
handleSendEvents(E, eTop);
}
else
{
if (eTop->eventNext->eventType == 0)
{
handleRecieveEvents(S, E, eTop, round);
}
}
}
for (int i = 0; i < 5; i++)
{
printf("%d",S[i].nodeValue);
printf(" ");
}
round++;
}
printf("\n");
printf("\t");
printf("STEP - ");
printf("%d",round);
printf("\t");
for (int i = 0; i < 5; i++)
{
printf("%d",S[i].nodeValue);
printf(" ");
}
printf("\n");
printf("\t");
printf("RESULT: ");
printf("\t");
printValues(S, 5);
E = destroyEvents(E);
eTop = destroyEvents(eTop);
free(S);
free(E);
free(eTop);
printf("\n");
return 0;
}