-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabb_filo.ex
More file actions
665 lines (572 loc) · 16.4 KB
/
labb_filo.ex
File metadata and controls
665 lines (572 loc) · 16.4 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
defmodule Chopstick do
def start() do
stick = spawn_link(fn -> available() end) # Start at available!!!
end
def available() do
#IO.puts("Available")
IO.puts("Stick unlocked #{inspect(self())}")
receive do
{:request, from} ->
send(from, {:granted, self()})
gone()
:quit -> :ok
end
end
def gone() do
IO.puts("Stick locked #{inspect(self())}")
receive do
:return -> available() #?
{:accept, _} ->
IO.puts("Error, delayed accept")
gone()
:quit -> :ok
end
end
# Stick is the PID
def request(stick) do
send(stick, {:request, self()}) # Stick eller self
receive do
{:granted, _} ->
:ok
end
end
def request_timeout(stick, timeout_ms) do
send(stick, {:request, self()}) # Stick eller self
receive do
{:granted, _} ->
:ok
after timeout_ms ->
:no
end
end
def request_async(stick, parent) do
send(stick, {:request, parent})
end
# def request_async(parent, stick, timeout_ms) do
# send(stick, {:request, self()}) # Stick eller self
# receive do
# :granted ->
# # This stick was granted
# send(parent, {:ok, stick})
# :ok
# after timeout_ms ->
# send(parent, :no)
# :no
# end
# end
# def request_timeout(stick, timeout_ms) do
# send(stick, {:request, self()}) # Stick eller self
# case request_responce(self, stick, timeout_ms) do
# :ok -> :ok
# :no -> :no
# end
# end
# def request_async(parent, stick, timeout_ms) do
# send(stick, {:request, self()}) # Stick eller self
# request_responce(parent, stick, timeout_ms)
# end
# def request_responce(parent, stick, timeout_ms) do
# receive do
# :granted ->
# #IO.puts("Granted")
# send(stick, {:accept, self()})
# request_responce(parent, stick, timeout_ms)
# :accepted ->
# #IO.puts("Accepted")
# send(parent, {:ok, stick})
# :ok
# after timeout_ms ->
# send(parent, :no)
# :no
# end
# end
def quit(stick) do
send(stick, :quit)
#flush()
end
# Antagligen helt fel. Förstår inte vad fan han vill ha
def return_stick(stick) do
send(stick, :return)
end
end
defmodule PhilosopherTimeout do
def start(hunger, right, left, name, ctrl, seed) do
IO.puts("#{name} joined the server")
spawn_link(fn -> process_start(hunger, right, left, name, ctrl, seed) end)
end
def process_start(hunger, right, left, name, ctrl, seed) do
:rand.seed(:exsplus, {seed, seed+1, seed+2})
dreaming(hunger, right, left, name, ctrl, 20, 1000) # ttd = 2
end
def dreaming(_, _, _, name, ctrl, 0, _) do
IO.puts("#{name} starved to death... big sad")
send(ctrl, :abort) # On starv, end dinner
end
def dreaming(0, _, _, name, ctrl, _, _) do
IO.puts("--------------- #{name} yeeted out ---------------")
send(ctrl, :done)
end
def dreaming(hunger, right, left, name, ctrl, ttd, dream_delay) do
IO.puts("#{name} is dreaming, has to eats #{hunger} more times")
sleep(dream_delay)
request_sticks_timeout(hunger, right, left, name, ctrl, ttd, dream_delay)
#request_sticks_async(hunger, right, left, name, ctrl, ttd, dream_delay)
receive do
:quit -> :ok
#{:granted, stick} ->
# return_stick(stick)
end
end
def request_sticks_timeout(hunger, right, left, name, ctrl, ttd, dream_delay) do
IO.puts("#{name} is reqesting chopsticks")
# Try to get chopsticks...
case Chopstick.request_timeout(right, 1000) do
:ok ->
#IO.puts("GOT FIRST!")
case Chopstick.request_timeout(left, 1000) do
:ok ->
# sleep(10000) # Makes it even worse
#IO.puts("GOT SECOND!")
eating(hunger, right, left, name, ctrl, ttd, dream_delay)
:no ->
# Return right stick and go back to dreaming
return_stick(right)
IO.puts("#{name} could not eat TTD-2")
dreaming(hunger, right, left, name, ctrl, ttd-1, dream_delay+300)
end
:no ->
IO.puts("#{name} could not eat TTD-1")
dreaming(hunger, right, left, name, ctrl, ttd-1, dream_delay+300)
end
receive do
:quit -> :ok
#{:granted, stick} ->
# return_stick(stick)
end
end
def request_sticks_async(hunger, right, left, name, ctrl, ttd, dream_delay) do
IO.puts("#{name} is reqesting chopsticks, async")
Chopstick.request_async(left, self()) # <-- ta bort delay
Chopstick.request_async(right, self())
case granted([], 2, 500) do
:granted ->
eating(hunger, right, left, name, ctrl, ttd, dream_delay)
:failed ->
IO.puts("#{name} did NOT eat")
# go back to dreaming?
unlock(ctrl)
dreaming(hunger, right, left, name, ctrl, ttd-1, dream_delay+300)
end
receive do
#{:granted, stick} ->
# return_stick(stick)
end
end
def granted(_, 0, timeout) do
:granted
end
def granted(granted_sticks, resp, timeout) do
receive do
{:granted, stick} ->
granted([stick|granted_sticks], resp-1, timeout)
after timeout ->
#Return sticks
Enum.each(granted_sticks, fn(stick) -> return_stick(stick) end)
:failed
end
end
# def granted(granted_sticks, resp) when resp<=0 do
# # Return sticks
# Enum.each(granted_sticks, fn(stick) ->
# return_stick(stick)
# end)
# :failed
# end
# def granted(granted_sticks, resp) do
# # Wait for 2 responces?
# receive do
# {:ok, stick} ->
# #IO.puts("GOT STICK")
# #IO.inspect(stick)
# granted_sticks = [stick|granted_sticks]
# [_|rest] = granted_sticks
# if(rest != []) do
# :granted
# else
# granted(granted_sticks, resp-1)
# end
# :no ->
# #IO.puts("DID NOT GET STICK")
# granted(granted_sticks, resp-1)
# after
# end
# end
def unlock(waiter) do
send(waiter, {:unlock, self()})
end
def lock(waiter) do
send(waiter, {:lock, self()})
end
def return_stick(stick) do
#IO.puts("Stick ... was returned")
#IO.inspect(stick)
Chopstick.return_stick(stick)
end
def return_sticks(name, right, left, ctrl) do
IO.puts("#{name} returned her sticks")
return_stick(right)
return_stick(left)
# Only if waiter is used!!!
send(ctrl, {:unlock, self()})
end
def eating(hunger, right, left, name, ctrl, ttd, dream_delay) do
IO.puts("#{name} is eating")
# Use chopsticks for some time
sleep(3000)
# Go back to dreaming
# Ändra algoritm här!!!
return_sticks(name, right, left, ctrl)
dreaming(hunger-1, right, left, name, ctrl, ttd, dream_delay)
receive do
:quit -> :ok
#{:granted, stick} ->
# return_stick(stick)
end
end
def quit(p) do
send(p, :quit)
end
# Random sleeep
def sleep(0) do :ok end
def sleep(t) do
:timer.sleep(:rand.uniform(t))
end
end
defmodule WaitedPhilosopher do
def start(hunger, right, left, name, ctrl, seed, ttd) do
IO.puts("#{name} joined the server")
spawn_link(fn -> process_start(hunger, right, left, name, ctrl, seed, ttd) end)
end
def process_start(hunger, right, left, name, ctrl, seed, ttd) do
:rand.seed(:exsplus, {seed, seed+1, seed+2})
dreaming(hunger, right, left, name, ctrl,ttd)
end
def dreaming(0, _, _, name, ctrl, _) do
IO.puts("--------------- #{name} yeeted out ---------------")
send(ctrl, :done)
end
def dreaming(hunger, right, left, name, ctrl, ttd) do
IO.puts("#{name} is dreaming, has to eats #{hunger} more times")
sleep(100)
request_sticks_async_waiter(hunger, right, left, name, ctrl, ttd)
receive do
:quit -> :ok
{:granted, stick} ->
return_stick(stick)
end
end
# def request_sticks_async(hunger, right, left, name, ctrl) do
# IO.puts("#{name} is reqesting chopsticks, async")
# Chopstick.request_async(self(), left, 100)
# Chopstick.request_async(self(), right, 100)
# case granted([], 2) do
# :granted ->
# eating(hunger, right, left, name, ctrl)
# :failed ->
# IO.puts("!!!!!!!!!!!!!!!!!!!!!! #{name} did NOT eat")
# # go back to dreaming?
# unlock(ctrl)
# dreaming(hunger, right, left, name, ctrl)
# end
# end
def request_sticks_async(hunger, right, left, name, ctrl, ttd) do
IO.puts("#{name} is reqesting chopsticks, async")
Chopstick.request_async(left, self()) # <-- ta bort delay
Chopstick.request_async(right, self())
case granted([], 2, 500) do
:granted ->
eating(hunger, right, left, name, ctrl, ttd)
:failed ->
IO.puts("#{name} did NOT eat")
# go back to dreaming?
unlock(ctrl)
dreaming(hunger, right, left, name, ctrl, ttd-1)
end
receive do
{:granted, stick} ->
return_stick(stick)
end
end
def granted(_, 0, timeout) do
:granted
end
def granted(granted_sticks, resp, timeout) do
receive do
{:granted, stick} ->
granted([stick|granted_sticks], resp-1, timeout)
after timeout ->
#Return sticks
Enum.each(granted_sticks, fn(stick) -> return_stick(stick) end)
:failed
end
end
def unlock(waiter) do
send(waiter, {:unlock, self()})
end
def lock(waiter) do
send(waiter, {:lock, self()})
end
def request_sticks_async_waiter(hunger, right, left, name, ctrl, ttd) do
IO.puts("#{name} is asking waiter if she can eat")
lock(ctrl)
case allowed_to_eat() do
:granted ->
# Use the basic request
request_sticks_async(hunger, right, left, name, ctrl, ttd)
:refused ->
IO.puts("#{name} was refused from eating")
# Go back to dreaming
dreaming(hunger, right, left, name, ctrl, ttd)
end
receive do
:quit -> :ok
{:granted, stick} ->
return_stick(stick)
end
end
def allowed_to_eat() do
receive do
:granted ->
:granted
:refused ->
:refused
other ->
IO.puts("ERROR!!! other was recived")
IO.inspect(other)
allowed_to_eat()
end
end
# def granted(granted_sticks, resp) when resp<=0 do
# # Return sticks
# Enum.each(granted_sticks, fn(stick) ->
# return_stick(stick)
# end)
# :failed
# end
# def granted(granted_sticks, resp) do
# # Wait for 2 responces?
# receive do
# {:ok, stick} ->
# #IO.puts("Recieved ok from stick: ")
# #IO.inspect(stick)
# granted_sticks = [stick|granted_sticks]
# [_|rest] = granted_sticks
# if(rest != []) do
# #IO.puts("IN GRANTED------------")
# #IO.inspect(granted_sticks)
# :granted
# else
# granted(granted_sticks, resp-1)
# end
# :no ->
# #IO.puts("Recived a NOT signal from one stick!")
# granted(granted_sticks, resp-1)
# end
# end
def return_stick(stick) do
Chopstick.return_stick(stick)
end
def return_sticks(name, right, left, ctrl, ttd) do
IO.puts("#{name} returned her sticks, sending unlock signal")
return_stick(right)
return_stick(left)
# Only if waiter is used!!!
unlock(ctrl)
end
def eating(hunger, right, left, name, ctrl, ttd) do
IO.puts("#{name} is eating")
# Use chopsticks for some time
sleep(3000)
# Go back to dreaming
# Ändra algoritm här!!!
return_sticks(name, right, left, ctrl, ttd)
dreaming(hunger-1, right, left, name, ctrl, ttd)
receive do
:quit -> :ok
{:granted, stick} ->
return_stick(stick)
end
end
# Random sleeep
def sleep(0) do :ok end
def sleep(t) do
:timer.sleep(:rand.uniform(t))
end
end
defmodule Dinner do
def start, do: spawn(fn -> init() end)
def init() do
c1 = Chopstick.start()
c2 = Chopstick.start()
c3 = Chopstick.start()
c4 = Chopstick.start()
c5 = Chopstick.start()
ctrl = self()
p1 = PhilosopherTimeout.start(5, c1, c2, "Arendt", ctrl, 100)
p2 = PhilosopherTimeout.start(5, c2, c3, "Hypatia", ctrl, 101)
p3 = PhilosopherTimeout.start(5, c3, c4, "Simone", ctrl, 102)
p4 = PhilosopherTimeout.start(5, c4, c5, "Elisabeth", ctrl, 103)
p5 = PhilosopherTimeout.start(5, c5, c1, "Ayn", ctrl, 104)
wait(5, [c1, c2, c3, c4, c5], [p1, p2, p3, p4, p5])
IO.puts(" WAINTG IS DONE!!! !!! !!! !!! !!! !!!")
end
def wait(0, chopsticks, _) do
#Enum.each(chopsticks, fn(c) -> Chopstick.quit(c) end)
kill_all(chopsticks)
end
def wait(n, chopsticks, philosophers) do
receive do
:done ->
wait(n-1, chopsticks, philosophers)
:abort ->
# Kill all other philosophers first
kill_all(philosophers)
IO.puts("Deadlock occured, terminating processes...")
Process.exit(self(), :kill)
end
end
def kill_all(processes) do
IO.puts("Killing all processes")
Enum.each(processes, fn(p) -> PhilosopherTimeout.quit(p) end)
end
end
defmodule DinnerWaiter do
def start, do: spawn(fn -> init() end)
def init() do
c1 = Chopstick.start()
c2 = Chopstick.start()
c3 = Chopstick.start()
c4 = Chopstick.start()
c5 = Chopstick.start()
waiter = Waiter.init(2, self())
ctrl = waiter #self()
p1 = WaitedPhilosopher.start(5, c1, c2, "Arendt", ctrl, 100, 20)
p2 = WaitedPhilosopher.start(5, c2, c3, "Hypatia", ctrl, 101, 20)
p3 = WaitedPhilosopher.start(5, c3, c4, "Simone", ctrl, 102, 20)
p4 = WaitedPhilosopher.start(5, c4, c5, "Elisabeth", ctrl, 103, 20)
p5 = WaitedPhilosopher.start(5, c5, c1, "Ayn", ctrl, 104, 20)
# Add to waiter
add_philosophers(waiter, [p1, p2, p3, p4, p5])
wait(5, [c1, c2, c3, c4, c5])
IO.puts(" WAINTG IS DONE!!! !!! !!! !!! !!! !!!")
end
def add_philosophers(waiter, pilo) do
Enum.each(pilo, fn(p) -> send(waiter, {:add, p}) end)
end
def wait(0, chopsticks) do
Enum.each(chopsticks, fn(c) -> Chopstick.quit(c) end)
end
def wait(n, chopsticks) do
receive do
:done ->
IO.puts("Recived :done signal!!!")
wait(n-1, chopsticks)
:abort ->
Process.exit(self(), :kill)
end
end
end
defmodule Waiter do
#{max, eating[], all[]}
def init(max_eaters, ctrl) do
spawn_link(fn -> run(max_eaters, [], [], ctrl) end)
end
def run(max, eating, all, ctrl) do
# p is the PID of the philosopher
receive do
{:add, p} ->
IO.puts("Added one")
run(max, eating, [p|all], ctrl)
{:lock, p} ->
# om inte brevid varandra
len = list_len(eating)
tot_len = list_len(all)
if(len < max && tot_len==5 && !next_to_any(p, eating, all)) do
IO.puts("lock granted")
send(p, :granted)
run(max, [p|eating], all, ctrl)
else
IO.puts("lock redjected")
send(p, :refused)
run(max, eating, all, ctrl)
end
{:unlock, p} ->
IO.puts("lock unlocked")
run(max, remove_from_list(eating, p), all, ctrl)
other ->
IO.puts("_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_Other was sent_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_")
send(ctrl, other)
run(max, eating, all, ctrl)
end
end
def get_last([]) do :error end
def get_last([h]) do h end
def get_last([h|t]) do
get_last(t)
end
def get_prev(p, [p|t]) do
get_last(t)
end
def get_prev(p, list) do
{found, prev} = Enum.reduce(list, {false, nil}, fn(x, {on, acc}) ->
on = on || (x==p)
if(on) do
{true, acc}
else
{on, x}
end
end)
prev
end
def contains(p, list) do
Enum.any?(list, fn(x) -> x==p end)
end
def get_next(p, list) do
{_, next} = Enum.reduce(list, {true, nil}, fn(x, {on, acc}) ->
n_on = (p==x)
if(on) do
{n_on, x}
else
{n_on, acc}
end
end)
next
end
def next_to_any(p, on, all) do
#IO.inspect(all)
#IO.inspect(on)
prev = get_prev(p, all)
next = get_next(p, all)
#IO.puts("P, prev, next = ")
#IO.inspect(p)
#IO.inspect(prev)
#IO.inspect(next)
if(contains(prev, on) || contains(next, on)) do
true
else
false
end
end
def list_len(list) do
Enum.reduce(list, 0, fn(x, acc) -> 1+acc end)
end
def remove_from_list(list, key) do
Enum.reduce(list, [], fn(x, l) ->
if(x==key) do
l
else
[x|l]
end
end)
end
end