(1)编写一个函数start(AnAtom,Fun)来把spawn(Fun)的结果注册为AnAtom。当两个并行的进行同时执行到start/2函数时,要确保代码能够正常工作。也就是说,这两个进程其中一个成功执行,而另一个必须执行失败。
-module(p150_1_process_conflict).
-export([main/0]).
%Not to concern about what the function Fun is.
main() -> %% ctreate two process.
start(wrong111,(fun() -> io:format("Wrong~n") end)),
%�d something to make sure there are not system output
start(ok111,(fun() -> io:format("Ok~n") end)).
start(AnAtom, Fun) ->
R = self(),
spawn(fun() -> try register(AnAtom, R) of
%%start should be registered in order to avoid two processes execute start/2 in the same time.
-module(p150_1_process_conflict).
-export([main/0]).
%Not to concern about what the function Fun is.
main() -> %% ctreate two process.
start(wrong111,(fun() -> io:format("Wrong~n") end)),
%�d something to make sure there are not system output
start(ok111,(fun() -> io:format("Ok~n") end)).
start(AnAtom, Fun) ->
%%start should be registered in order to avoid two processes execute start/2 in the same time.