erlang 并发spawn部分学习
来源:互联网 发布:淘宝商城眼镜 编辑:程序博客网 时间:2024/06/06 02:16
本文的格式为:代码—>debug时查看到的各进程运行的状况及最终总结。
start(N) ->
Pid = my_spawn(spawn_fun,N),
io:format(“the pid is ~p~n”,[Pid]),
Pid.
my_spawn(Func,N) ->
Monitor = spawn(fun() ->
Fs = list_func(N,[],spawn_fun),
io:format(“Module name is ~p~n”,[?MODULE]),
Id = [spawn_link(?MODULE,F,[])||F <- Fs],%%这个spawn_link的第二个参数要为一个原子类型
receive
{‘DOWN’,_,process,Pid,Why} ->
io:format(“Pid:~p quit,Reason:~p~n”,[Pid,Why]),
io:format(“正在重启进程~n”),
spawn_link(Func),
io:format(“重启完毕~n”)
after 15000 ->
exit(“test test test”)
%% io:format(“is List ~p,the list is ~p~n”,[is_list(Id),Id]),
%% lists:min(Id) ! {ok,”exit test”}
end
end),
io:format(“Monitor:~p~n”,[Monitor]),
Monitor.
spawn_fun() ->
io:format(“hello world:~p~n”, [self()]),
receive
{ok,Message} ->
io:format(“about to exit!~p~n”,[self()]),
exit(Message)
after 30000 ->
io:format(“about to exit now!”),
exit(“test”)
end.
%%spawn_fun() ->
%% io:format(“hello world:~p~n”, [self()]),
%% receive
%% {ok,Message} ->
%% io:format(“about to exit!~p~n”,[self()]),
%% exit(Message);
%% other ->
%% spawn_fun()
%% end.
list_func(N,H,F) when N =/= 1 ->
list_func(N-1,[F|H],F);
list_func(N,H,F) when N =:= 1 ->
[F|H].
start(N) ->
Pid = my_spawn(spawn_fun,N),
io:format(“the pid is ~p~n”,[Pid]),
Pid.
my_spawn(Func,N) ->
Monitor = spawn(fun() ->
Fs = list_func(N,[],spawn_fun),
io:format(“Module name is ~p~n”,[?MODULE]),
Id = [spawn_link(?MODULE,F,[])||F <- Fs],%%这个spawn_link的第二个参数要为一个原子类型
receive
{‘DOWN’,_,process,Pid,Why} ->
io:format(“Pid:~p quit,Reason:~p~n”,[Pid,Why]),
io:format(“正在重启进程~n”),
spawn_link(Func),
io:format(“重启完毕~n”)
%% after 15000 ->
%% exit(“test test test”)
%% io:format(“is List ~p,the list is ~p~n”,[is_list(Id),Id]),
%% lists:min(Id) ! {ok,”exit test”}
end
end),
io:format(“Monitor:~p~n”,[Monitor]),
Monitor.
spawn_fun() ->
io:format(“hello world:~p~n”, [self()]),
receive
{ok,Message} ->
io:format(“about to exit!~p~n”,[self()]),
exit(Message)
after 5000 ->
io:format(“about to exit now!”),
exit(“test”)
end.
%%spawn_fun() ->
%% io:format(“hello world:~p~n”, [self()]),
%% receive
%% {ok,Message} ->
%% io:format(“about to exit!~p~n”,[self()]),
%% exit(Message);
%% other ->
%% spawn_fun()
%% end.
list_func(N,H,F) when N =/= 1 ->
list_func(N-1,[F|H],F);
list_func(N,H,F) when N =:= 1 ->
[F|H].
第一种情况是15s后monitor进程退出后,在其基础上裂分出来的子进程都挂了。
第二种情况是5s后子进程都退出后,父进程即monitor进程也退出了,且退出原因一样,monitor进程没能接收到子进程发来的{‘DOWN’,_,process,Pid,Why}消息。
下面是对普通spawn操作的一些测试,请参照代码和图片理解:
进程接收到一条消息后即退出了
other是一个原子,必须有给当前进程发送other时才能匹配
-module(testspawn).
-author(“Administrator”).
%% API
-export([testspawn/0]).
testspawn() ->
Pid = spawn(fun() ->
receive
{ok,Message} ->
io:format(“received message is:~p~n”,[Message]);
other ->
io:format(“received”)
end
end),
Pid.
这个是没有使用spawn_link时父子进程的无关联
- erlang 并发spawn部分学习
- erlang 并发编程,spawn 创建新的进程
- erlang中的spawn函数
- erlang:奇偶判断(spawn,register)
- Erlang 程序设计 学习笔记(六) 并发编程
- Erlang并发编程基础(学习总结)
- Erlang-并发
- programming erlang 分布式编程spawn(Node, fun)
- 学习 pexpect.spawn
- 《Erlang程序设计》学习笔记-第7章 并发
- 《Erlang程序设计》学习笔记-第8章 并发编程
- Erlang-- 学习笔记5,平行式、面向并发程序设计
- 分布式、并行计算语言Erlang 学习笔记(第一部分)
- 分布式、并行计算语言Erlang 学习笔记(第二部分)
- 分布式、并行计算语言Erlang 学习笔记(第三部分)
- Erlang 的 spawn() 使用时,值得注意的一点
- spawn
- Erlang—并发编程
- UML常用图--类图,用例图,序列图(时序图),协作图(通信图),状态图,活动图
- 变分算法
- 【Android】项目更改包名方法
- Session、Cookie 帮助类
- 每天坚持英语学习为什么还是学不好?
- erlang 并发spawn部分学习
- mysql命令行查看表结构,字段等信息 [mysql]
- Android Studio 的使用之添加权限
- SecureCRT 查看日志的简单命令
- 2016/11/8 反思
- linux 的 Socket IO 模型
- 【转】EditText中imeOptions的使用
- 今天该干嘛呢
- 基于OPENCV3与QT5.6实现摄像头拍照并保存图片