《Erlang程序设计》第8章习题第一…
来源:互联网 发布:淘宝高达模型 编辑:程序博客网 时间:2024/06/07 00:38
(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 processesexecute start/2 in the same time.
true->
R ! true,
Fun()
catch
error:_ -> R ! false
end
end),
receive
true -> io:format("True~n");
false -> io:format("False ~n")
end.
实际执行中会在末行多输出一个ok.目前不知道是什么原因,留待回头解决.实际上程序主体是别人的.我之前用的whereis()来控制,但没用.
-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 processesexecute start/2 in the same time.
实际执行中会在末行多输出一个ok.目前不知道是什么原因,留待回头解决.实际上程序主体是别人的.我之前用的whereis()来控制,但没用.
0 0
- 《Erlang程序设计》第8章习题第一…
- 《Erlang程序设计》学习笔记-第8章 并发编程
- 第1章 程序设计入门 习题
- 第2章 循环结构程序设计 习题
- 《Erlang程序设计(第2版)》
- 《Erlang程序设计》学习笔记-第2章 入门
- 《Erlang程序设计》学习笔记-第3章 顺序型编程
- 《Erlang程序设计》学习笔记-第4章 异常
- 《Erlang程序设计》学习笔记-第7章 并发
- 《erlang程序设计》学习笔记-第10章 分布式编程
- 《Erlang程序设计》学习笔记-第11章 IRC Lite
- 《Erlang程序设计》学习笔记-第12章 接口技术
- 第8章习题
- 《C程序设计教程》第5章习题9
- 《面向对象程序设计(C++)》第4章习题2
- 《C程序设计教程》第5章习题6
- 《c++primer》第15章面对对象程序设计习题解答
- 开始啃 Erlang 程序设计,第一口
- P279_1013 以矩形逼近法求定积分的通用函数,分别求有关sin(x),cos(x),e^x,学习使用函数指针
- P279_1014 按照输入时逆序,只要用指针做交换即可。若大数据不便于移动,输入时建标识表排序之
- P279_1015 学习数组与指针的交换使用
- p279_1016在字符串有数字与非字符,找出连续的数字作为整数存放并统计个数。方法简洁,不同于答案书
- p279_10162 答案书版:在字符串有数字与非字符,找出连续的数字作为整数存放并统计个数
- 《Erlang程序设计》第8章习题第一…
- P279_1017 写一函数,实现两个字符串的比较,即自己写一个strcmp函数
- P279_1018编写程序,打入月份,输出该月英文月名,用指针数组处理
- P279_1019模拟alloc和free
- P279_1020用指向指针的指针对字符…
- Test11_6 指针用p->X和(*p).X等价
- 建立三个学生的单链表试试感觉
- P305_1111链表的综合操作,建表、…
- P314_1113 求五取三不重复的颜色排列