《Erlang程序设计》第8章习题一解

习题1:

-module(ex1).
-compile(export_all).

main(_) ->
    start(abc, fun() -> io:format("Bing~n") end),
    start(abc, fun() -> io:format("Bang~n") end).

start(AnAtom, Fun) ->
    R = self(),
    spawn(
        fun() ->
            try register(AnAtom, self()) of
                true->
                    R ! true,
                    Fun()
            catch
                error:_ ->
                    R ! false
            end
        end
    ),

    receive
        true -> true;
        false -> io:format("false ~n")
    end.

习题2:

-module(ex2).
-compile(export_all).

main() ->
	start(1000, 10),
	start(1000, 20),
	start(2000, 10).

start(N, M) ->
	statistics(runtime),
	statistics(wall_clock),
	L = create(N, []),
	post(haha, L, M),
	{_, Time1} = statistics(runtime),
	{_, Time2} = statistics(wall_clock),
	io:format("Total time : ~p(~p) ~n", [Time1*1000, Time2*1000]).

create(0, L) ->
	L;

create(N, L) ->
	Pid = spawn(fun loop/0),
	create(N-1, [Pid|L]).

loop() ->
	receive
		Any ->
			io:format("Msg arrived: ~p~n", [Any]),
			loop()
	end.

post(_ , _, 0) ->
	void;

post(Msg, L, M) ->
	%io:format("M = ~p~n", [M]),
	[ H ! Msg || H <-L],
	post(Msg, L, M-1).

  • xpg

    楼主,你好,我觉得习题1的代码跟问题有出入,你注册的是start/2的pid而不是spawn(Fun)的