-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpmt1.exs
62 lines (53 loc) · 1.38 KB
/
mpmt1.exs
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
#!/usr/bin/elixir
#
# mpmt1.exs: Elixir version of mpmt1.py
#
# License:
# Apache License, Version 2.0
# History:
# * 2022/04/16 v0.1 Initial version
# Author:
# Masanori Itoh <[email protected]>
# TODO:
# * Show PID not reference at process termination time.
#
defmodule Mpmt1 do
def busy_worker(idx, time_left) do
IO.inspect("busy_worker: started. idx = #{idx} time_left = #{time_left}")
now = :os.system_time(:millisecond)
busy_loop(now, time_left)
end
def busy_loop(current, time_left) when time_left > 0 do
now = :os.system_time(:millisecond)
busy_loop(now, time_left - (now - current) / 1000.0)
end
def busy_loop(_current, time_left) do
IO.inspect("busy_worker: expired. time_left = #{time_left}")
end
end
#
# main routine
#
p = OptionParser.parse(System.argv,
switches: [num_context: :integer, duration: :float],
aliases: [n: :num_context, d: :duration]
)
num_context = Keyword.get(elem(p, 0), :num_context, 2)
duration = Keyword.get(elem(p, 0), :duration, 5.0)
IO.inspect("mpmt1.exs: num_context = #{num_context} duration = #{duration}")
pid_list = Enum.map(Enum.to_list(1..num_context),
fn x ->
spawn(Mpmt1, :busy_worker, [x, duration])
end
)
ref_list = Enum.map(pid_list,
fn x ->
Process.monitor(x)
end
)
for ref <- ref_list do
receive do
{:DOWN, ^ref, _, _, _} ->
IO.inspect("#{inspect(ref)} finished.")
end
end