Stratus3D / programming_erlang_exercises

Solutions to the exercises in Joe Armstrong's book "Programming Erlang" 2nd Edition. Solutions are my own. Better ones may exist.
MIT License
24 stars 7 forks source link

Spawning and monitoring multiple workers form one node #1

Open shaleengarg opened 8 years ago

shaleengarg commented 8 years ago

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.

Stratus3D commented 8 years ago

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.

shaleengarg commented 8 years ago

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

shaleengarg commented 8 years ago

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(wallclock), 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

Stratus3D commented 8 years ago

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.

7stud commented 7 years ago

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

Stratus3D commented 7 years ago

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