Attempting to create a plethora of processes

Yves S. Garret <>
Sun Jul 12 05:14:03 CEST 2009


Hi all,

   I'm just trying to get a better unerstanding of IPC in Erlang and I
stumbled on this little problem.  The problem arises when I get to the
spawn method.  My present understanding is that the spawn method will
create a different process with the passed in method and then
immediately return.  I used the debugger in order to attempt to get to
the bottom of this and found out that when the program gets to the
simulate method (pardon the odd name, I borrowed the code from a
different file that I was working on) it hangs.  Understandably.  It's
waiting for a message (stop in this case) in order to proceed
further.  That's quite alright in my view, since I'm just testing
something out and I could care less if this fails or not.  However,
what throws me for a loop is the fact that the process that called the
spawn method seems to be unresponsive.  This was not my intention and
don't understand why this would happen.  What have I misunderstood?
spawn creates a new process, so in theory, it should just exit out of
that method and proceed with the parent...

Any help is appreciated.

This is my source code:

-module(superProcess).

-export([start/1,stop/0]).

-compile(export_all).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%
% CONTROLLING METHODS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%
start(Population) ->
  run(Population).

% the method that the user can call in order send the message to the
main
%   process that it needs to stop running the application.
stop() ->
  simAnnApp ! stop.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%
% WORKER METHODS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%

% begin running the application.
run(Population) ->
  registerApp(),
  ListOfProcs = spawnIndividuals(Population),
  loop(ListOfProcs).

% register the overall application, this way we can send messages to
the
%   general app.
registerApp() ->
  register(simAnnApp, self()).

% this is an interface method in order to simplify how sub-processes
are
%   spawned.
spawnIndividuals(Population) ->
  spawnIndividuals(Population, []).

% go through a loop and create a separate process for each member of
the
%   population.
spawnIndividuals(0, ListOfProcs) ->
  ListOfProcs;
spawnIndividuals(Population, ListOfProcs) ->
  Pid = spawn(simulate()),
  NewList = [Pid | ListOfProcs],
  spawnIndividuals(Population - 1, NewList).

loop(ListOfProcs) ->
  receive
    stop ->
      killAll(ListOfProcs),
      exit(normal)
  after 0 ->
    true
  end.

% will kill all sub-processes by sending it the die message.
killAll([]) ->
  io:format("All sub-processes killed.  Exiting application.~n~n");
killAll([Head | Tail]) ->
  die ! Head,
  killAll(Tail).

simulate() ->
  receive
    die ->
      exit(normal)
  %after 0 ->
  %  simulate()
  end.


More information about the erlang-questions mailing list