multithreading - Erlang and processes -
I'm very new to Arlong and I'm currently reading Armstrong's book, chapter 'Concurrent Programming'. I am trying to run a list of processes, if any number is prime (inexperienced method). But my code runs as if there was no process, both methods have the same duration. Where am I?
shell.erl:
c (a list of% primes)% NUMS = [102950143,102950143,102950143,102950143,102950143]. % Time start now =) (now) when: fwrite ("monothread ~ n"). If it is a major list to test each number: foreach (fun) (n) -> gt; RESULT = prime: is_prime (N), io: fwrite ("result n = ~ p ~ n", [RESULT] ) End, NUMS). Display% duration io: fwrite ("duration n = ~ p ~ n", [timer: now_diff (now (), now1) /1.0e6]). % Time starts now = (2) when: fwrite ("multithread ~ n"). For every number, a new process is created and if it is a main list then: foreach (fun) -> gt; ProcId = prime: start (), io: fwrite ("count: procId = ~ pn = P ~ N ", [ProcId, N]), RESULT = Prime: is_prime (ProcId, N), io: fwrite (" result n = ~ p ~ n ", [RESULT] end, NUMS). Display% duration io: fwrite ("duration n = ~ p ~ n", [timer: now_diff (now (, now 2) /1.0e6]). Halt ()
file prime.erl:
- Module (prime) export ([/ 0, is_prime / 1 start, is_prime / 2]). Run for forever - Start the function () -> & gt; Spawn (funny fashion function / 0)% hold the query forever - function () - & gt; {From, number} - & gt; Get From! Is_prime (self (), 2, number), forever () end% monohedreded function is_prime (number) - & gt; Is_prime (self (), 2, number). % Multithreaded function is_prime (process ID, number) - & gt; Process id! {Self (), number}, RESULT-> The result is obtained by the% recursive function, scanning all numbers from number 2 to is_prime (process ID, div, number) - & gt; If div =: = number - & gt; {{Number, number} {prime minister, true}}; Number Riyadh div =: = 0 - & gt; {{Number, number} {prime minister, false}}; True-> Is_prime (ProcessID, Div + 1, Number) End
In your lists: Foreign calls in the second block, you are calling RESULT = prime: is_prime (ProcId, N)
, which Therefore, you are creating a process and are waiting till you create the next process. This is the reason that it takes a similar time as a single-threaded approach: On both sides, you are only doing one at a time.
Instead, you first need to generate all the procedures lists: map
to keep track of all the pids) and then wait for results in a different step. Note that this means that you have to break the ProcessID! In the first phase, otherwise you are just creating a bunch of waste procedures. part received
from the part, P> then, something like this:
Pids = list: map (Funnier) -> ProcId = prime: start (n), io: fwrite ("calculation: procId = ~ pn = ~ p ~ n", [procId, n]), ProcId end, NUMS). List: foreach (ProcId) - & gt; {N, RESULT} = Prime: is_prime (ProcId), io: fwrite ("result procId = ~ pn = ~ p result = ~ p ~ n", [ProcId, N , RESULT]) end, Pids). Start (n) - & gt; Spawn (? Modules, forever, work, [n]). Forever_function (number) - & gt; Results = is_prime (self (), 2, number), {to, get_result} - & gt; Get From! {Number, result},% is unnecessary because we never call it a plague, but do it anyway :) Forever (%) number even set number% to reach here Can even do one-shot functions like before. % Multithreaded function is_prime (ProcessID) - & gt; Process id! {Self (), get_result}, RESULT-> get end result
Note: This is unwanted, so it may require some tweaking.
Comments
Post a Comment