[erlang-questions] Subtle behaviour of Erlang scheduler

Thu May 24 12:07:40 CEST 2007

Dear Erlang Gurus,

we have tested the priority system of the processes and we are 
experiencing a very strange behaviour.

Take a look at the simple code attached: if you run "test:test()" from
the shell, the system hangs and "proc2" never ends blocking also
"proc1" and the shell. We think that this is due to the fact that
"proc2" hasn't any synchronization point, i.e. send or receive
messages, etc. In any case, this is quite strange since also "proc1"
has priority set to "high", so sooner or later, it should be selected
to be scheduled.

Now kill the shell (with CTRL+C, you don't have any other option!),
uncomment the "timer:sleep(1)" statement in proc2, recompile and
everything seems to work fine: "proc2" and "proc3" exit, and the value
reached by the counter proves that proc2 really runs with a higher
priority than proc3.

Well, now, don't exit from the shell, comment again the
"timer:sleep(1)" line, compile the code with "c(test)", and run it
again. Well, now it works!!! But if you (once again) exit the shell,
run "erl" again and launch "test:test()" once more.... it blocks!!!

What's behind this strange behaviour? An undocumented feature? A bug
in scheduler initialization? Too many beers before sitting down for
coding :-))) ???

All the best,
--Enzo and Corrado

[ Enzo Nicosia aka KatolaZ --- GLUG Catania -- Freaknet Medialab ]
[ me [at] katolaz.homeunix.net -- http://katolaz.homeunix.net -- ]
[ GNU/Linux User:#325780/ICQ UIN: #258332181/GPG key ID 0B5F062F ]
[ Fingerprint: 8E59 D6AA 445E FDB4 A153 3D5A 5F20 B3AE 0B5F 062F ]


-module (test).

-compile (export_all).

test () ->
  register (proc1, spawn (fun () -> proc1() end)),
  register (proc3, spawn (fun () -> proc3() end)),

proc1 () ->
  process_flag (priority, high),
  register (proc2, spawn (test, proc2, [])),
  timer:sleep (1000),
  proc2 ! x,
  proc3 ! x,

proc2 () ->
  process_flag (priority, high),
  %%timer:sleep (1),
  count (0).

proc3 () ->
  count (0).

count (X) ->
    _ -> io:format ("~p, ~p~n", [process_info (self(), registered_name), X])
  after 0 ->
      count (X + 1)

More information about the erlang-questions mailing list