Erlang/OTP Forums

Author Message

<  Erlang  ~  Simultaneous processes

cgr
Posted: Fri Sep 21, 2007 4:23 pm Reply with quote
Joined: 15 Sep 2007 Posts: 5
I'm working through the Programming Erlang book and at the end of Chap. 8 there are a couple of exercises to write some concurrent programs. Ex. 1 says that the function start/2 must work "correctly in the case when two parallel processes simultaneously evaluate start/2". How can I test whether or not a solution I write fulfills this requirement?

Basically, I just want to know how I can evaluate the start/2 func simultaneously from 2 different processes to test if it fails or not.

_________________
-cgr
View user's profile Send private message
francesco
Posted: Wed Sep 26, 2007 6:30 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Concurrency is tricky to test because of the non-deterministic behaviour based on the order of evaluation. What Joe is trying to say is that if you write something like this in your code, you are not in safe hands:


Code:
start() ->
  case whereis(server) of
    undefined ->
        Pid = spawn(server, init, []),
        register(server, Pid),
        {ok, Pid};
    Pid when is_pid(Pid) ->
        {error, already_started}
  end.


If two processes execute the above and the first one is suspended right after the whereis or the spawn but before the register, you will get a race condition and two servers will be started (But only one registered). The error will manifest itself with the second register being called.

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
cgr
Posted: Fri Sep 28, 2007 7:44 pm Reply with quote
Joined: 15 Sep 2007 Posts: 5
Thanks for the reply.

I understand that particular trickiness you're referring to there and that code is actually very similar to what I quickly wrote after reading the problem as my first "naive" attempt just to get the algorithm's structure down. What I really want to know is if there is any way to actually take my start/2 function and run it simultaneously in 2 processes so that I can tell if it handles the error condition gracefully or not since the problem states that it should work correctly in that case.

_________________
-cgr
View user's profile Send private message
francesco
Posted: Mon Oct 15, 2007 4:15 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Sorry for the delay in getting back to you. We have been swamped with work, which is a nice problem to have. Try something in line with

Code:
F = fun(_) -> server:start( )end,
Seq = lists:seq(1,100),
lists:foreach(F, Seq).


Non determinism to counteract nondeterminism has been known to help Smile

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum