Presentation is loading. Please wait.

Presentation is loading. Please wait.

Practical Erlang Programming Process Error Handling.

Similar presentations


Presentation on theme: "Practical Erlang Programming Process Error Handling."— Presentation transcript:

1 Practical Erlang Programming Process Error Handling

2 © 2001 - 2009 Erlang Training and Consulting Ltd 2Process Error Handling Erlang By Example

3 © 2001 - 2009 Erlang Training and Consulting Ltd 3Process Error Handling Overview: Process Error Handling I Process Error Handling I Links Exit Signals Definitions Propagation Semantics

4 © 2001 - 2009 Erlang Training and Consulting Ltd 4Process Error Handling Links link/1 will create a bi- directional link between the process calling the BIF and the process Pid spawn_link/3 will yield the same result as calling spawn/3 followed bylink/1, only that it will do it atomically link(PidA) PidAPidB

5 © 2001 - 2009 Erlang Training and Consulting Ltd 5Process Error Handling Exit Signals are Sent when processes terminate abnormally They are sent to all processes to which the failing process is currently linked to The process receiving the signal will exit, propagate a new signal to the processes it is linked to Exit Signals {EXIT, A, Reason}

6 © 2001 - 2009 Erlang Training and Consulting Ltd 6Process Error Handling When process A fails, the exit signals propagate to process B From B, it propagates to C Exit Signals {EXIT, A, Reason} {EXIT, B, Reason} C A B

7 © 2001 - 2009 Erlang Training and Consulting Ltd 7Process Error Handling Processes can trap exit signals by executing the BIF process_flag(trap_exit, true) Exit signals will be converted to messages of the format {EXIT, Pid, Reason} They are saved in the process mailbox If an exit signal is trapped, it does not propagate further Exit Signals {EXIT, A, Reason} A

8 © 2001 - 2009 Erlang Training and Consulting Ltd 8Process Error Handling Process B marked with a double ring is trapping EXITs If an error occurs in A, or C then they will terminate. Process B will receive the {EXIT,Pid,Reason} message The process that did not terminate will not be affected Exit Signals {EXIT, A, Reason} {EXIT, B, Reason} C B A

9 © 2001 - 2009 Erlang Training and Consulting Ltd 9Process Error Handling Definitions: Terminology Link A bi-directional propagation path for exit signals set up between processes Exit Signal A signal transmitted by a process upon exiting. It contains termination information Error trapping The ability of a process to handle exit signals as if they were messages

10 © 2001 - 2009 Erlang Training and Consulting Ltd 10Process Error Handling Definitions: Built In Functions link(Pid) Set a link between the calling process and Pid unlink(Pid) Removes a link to Pid spawn_link(Mod, Fun, Args) Atomically spawns and sets link between the calling and the spawned process process_flag(trap_exit, true | false) Set the current process to convert exit signals to exit messages

11 © 2001 - 2009 Erlang Training and Consulting Ltd 11Process Error Handling Definitions: Built In Functions The BIF exit/1 terminates the process who calls it Generates an exit signal sent to linked processes The BIF exit/1 can be caught in a catch {EXIT, Pid, Reason}

12 © 2001 - 2009 Erlang Training and Consulting Ltd 12Process Error Handling Definitions: Built In Functions exit(Pid, Reason) - Sends an exit signal containing Reason to the process Pid If trapping exits, the signal is converted to an exit message {EXIT, Pid, Reason} exit(Pid, Reason)

13 © 2001 - 2009 Erlang Training and Consulting Ltd 13Process Error Handling Propagation Semantics When a process terminates it sends an exit signal to the processes in its link set Exit signals can be normal or non-normal A process not trapping exit signals dies if it receives a non- normal exit signal. Normal signals are ignored A process which is trapping exit signals converts all incoming exit signals to conventional messages handled in a receive statement If Reason is kill, the process is terminated unconditionally

14 © 2001 - 2009 Erlang Training and Consulting Ltd 14Process Error Handling Trapping Exits Not Trapping Exits Normal receives {EXIT, Pid, normal} nothing happens Kill terminates with reason killed Other receives {EXIT, Pid, Other} terminates with reason Other Propagation Semantics Trap_exit Flag Exit Reason

15 © 2001 - 2009 Erlang Training and Consulting Ltd 15Process Error Handling Summary: Process Error Handling I Process Error Handling I Links Exit Signals Definitions Propagation Semantics


Download ppt "Practical Erlang Programming Process Error Handling."

Similar presentations


Ads by Google