【发布时间】:2021-03-31 20:47:55
【问题描述】:
我想从多个函数中生成一个进程并创建一个指向该进程的链接。如何在系统进程中将接收到的信号转换为消息。我想创建“paralleljoin”系统进程,在它从其链接进程(taskTwo,taskThree,TaskFour)接收到('EXIT',pid,normal)消息后,它将产生另一个进程(taskFive),但这在我的代码,即“taskFive”没有执行。
-module(model).
-compile(export_all).
start() ->
io:format("codes of start event \n"),
%The spawn method calls the taskOne function with no parameters
spawn(model, taskOne, []),
Pid1 = spawn(model, paralleljoin, []),
register(join2,Pid1 ).
taskOne() ->
io:format(" code for business logic of task one \n"),
spawn(model, parallelsplit, []).
parallelsplit()->
Pid2 = spawn(model, taskTwo, []),
register(task2, Pid2),
Pid3 = spawn(model, taskThree, []),
register(task3, Pid3),
Pid4 = spawn(model, taskFour, []),
register(task4, Pid4).
taskTwo() ->
io:format("code for business logic of task two \n"),
link(whereis(join2)),
exit(whereis(join2), normal).
taskThree() ->
io:format(" code for business logic of task three \n"),
link(whereis(join2)),
exit(whereis(join2), normal).
taskFour() ->
io:format(" code for business logic of task four \n"),
link(whereis(join2)),
exit(whereis(join2), normal).
paralleljoin()->
process_flag(trap_exit, true),
Task2 = whereis(task2),
Task3 = whereis(task3),
Task4 = whereis(task4),
case get(messagesreceived) of
undefined -> put(messagesreceived, {nil, nil, nil});
{Task2, Task3, Task4} ->
spawn(model, taskFive, [])end,
receive
{'EXIT', Task2, normal} ->
put(messagesreceived, setelement(1, get(messagesreceived), Task2));
{'EXIT', Task3, normal}->
put(messagesreceived, setelement(2, get(messagesreceived), Task3));
{'EXIT', Task4, normal}->
put(messagesreceived, setelement(3, get(messagesreceived), Task4));
Other ->
ignore %Do something, or do nothing
end,
paralleljoin().
taskFive()->
io:format("code for business logic of task five \n").
【问题讨论】:
标签: erlang