[erlang-questions] Letting 'every process run until it is blocked' on an Erlang node.

Stavros Aronis aronisstav@REDACTED
Mon Feb 25 11:31:32 CET 2013


(Also on Stack Overflow:
http://stackoverflow.com/questions/15064887/letting-every-process-run-until-it-is-blocked-on-an-erlang-node
)

Is it possible to write a function that waits for every process
running on an Erlang node to reach a point where it is blocked,
waiting for a message?

The function should return only when every process is waiting for a
message that has not yet been sent to it. Assume that no process is in
a time-related suspension (receive with an after clause, timer-related
operations etc). The process running this function is, of course,
excluded.

Obviously wrong answer:

`erlang:yield/0`: This gives a chance to every other process to run,
but not necessarily until it is blocked.

Not 100% correct approach:

    only_one_not_waiting() ->
      Running =
        [P || P <- processes(), process_info(P, status) =/= {status, waiting}],
      length(Running) == 1
    end.

    everyone_blocked() ->
      case only_one_not_waiting() of
        true -> ok;
        false -> everyone_blocked()
      end.

Ignoring timers, running `only_one_not_waiting/0` repeatedly until it
returns true (as `everyone_blocked/0` does) should indicate the
desired system state, if this state is eventually reached.

However I am not sure how much trust one should put on the return
value of `process_info(P, status)`.



More information about the erlang-questions mailing list