Skip to content

Instantly share code, notes, and snippets.

@nishidy
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nishidy/fa28495cb1e1d341518e to your computer and use it in GitHub Desktop.
Save nishidy/fa28495cb1e1d341518e to your computer and use it in GitHub Desktop.
Demo for process_flag
-module(demo).
-compile(export_all).
start(Bool, M) ->
A = spawn(fun()->a()end),
B = spawn(fun()->b(A,Bool)end),
C = spawn(fun()->c(B,M)end),
sleep(1000),
status(b,B),
status(c,C).
a() ->
process_flag(trap_exit,true),
wait(a).
b(A,Bool) ->
process_flag(trap_exit,Bool),
link(A),
wait(b).
c(B,M) ->
link(B),
case M of
{die,Reason}->
exit(Reason);
{divide,N}->
_=1/N,
wait(c);
normal->
true
end.
wait(Prog) ->
receive
Any ->
io:format("Process ~p received ~p~n",[Prog,Any]),
wait(Prog)
end.
sleep(T) ->
receive
after T->true
end.
status(Name,Pid) ->
case erlang:is_process_alive(Pid) of
true ->
io:format("Process ~p (~p) is alive~n",[Name,Pid]);
false ->
io:format("Process ~p (~p) is dead~n",[Name,Pid])
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment