programming_erlang_exercises icon indicating copy to clipboard operation
programming_erlang_exercises copied to clipboard

Spawning and monitoring multiple workers form one node

Open shaleengarg opened this issue 9 years ago • 6 comments

In chapter 13, there is a question which asks us to write a program which can monitor multiple processes(workers). I have written a program which can monitor one process, but am not sure how to scale that for multiple processes.

shaleengarg avatar May 11 '16 11:05 shaleengarg

Hi @shaleengarg, do you know what number that exercise is? It looks like there are at least 6 exercises for chapter 13, and I have only completed 4 in this repository. If you let me know what number that exercise was I'll try to add that it as well.

Stratus3D avatar May 11 '16 12:05 Stratus3D

Hi @Stratus3D . Its question 5 and 6 (Pg 209)

shaleengarg avatar May 11 '16 13:05 shaleengarg

Hi @Stratus3D Please take a look at this code

%%Coded By shaleengarg %%This module generates N processes on node and kills them -module(processes). -export([wait/0, master/1, sp/1]).

sp(N) -> Max = erlang:system_info(process_limit), io:format("Maximum allowed processes:~p~n",[Max]), statistics(runtime), statistics(wall_clock), L = for(1, N, fun() -> spawn(fun() -> wait() end) end), {, Time1} = statistics(runtime), {, Time2} = statistics(wall_clock), lists:foreach(fun(Pid) -> Pid ! die end, L), U1 = Time1 * 1000 / N, U2 = Time2 * 1000 / N, io:format("Process spawn time=~p (~p) microseconds from node: ~p ~n", [U1, U2, node()]).

%%list of the form [{Number of processes, Node}...] master([]) -> [];

master(List) -> [H|T] = List, {Number, Node} = H, subordinate(Number, Node), io:format("Spawned a subordinate~n"), master(T).

subordinate(Number, Node) -> spawn(Node, ?MODULE, sp, [Number]).

wait() -> receive die -> void end.

for(N, N, F) -> [F()]; for(I, N, F) -> [F()| for(I+1, N, F)].

I wrote this code myself. Please review this code. I will be happy to issue a push request

shaleengarg avatar May 21 '16 18:05 shaleengarg

Sorry it's taken me so long to respond. I'm in the process of updating everything for Chapter 13. I'm working on adding more detailed comments to all the exercises. Last night I pushed the solution for exercise 13.4. Hoping to push the solution for 13.5 sometime this week.

Stratus3D avatar Jun 03 '16 01:06 Stratus3D

In all your README.md files you need to leave a space after the ### marks for them to create headings:

###MyHeading

v.

### MyHeading

This is how it's rendered:

###MyHeading

v.

MyHeading

7stud avatar May 10 '17 02:05 7stud

@7stud thanks. I've fixed the formatting of all the README files.

Stratus3D avatar May 17 '17 17:05 Stratus3D