```
1.77 The design and purpose of the trap mechanism of Erlang ERTS Published: 2009-02-18 Keyword: The trap mechanism of bif_traperlang is used a lot in the implementation, and can basically be seen in the time-consuming BIF operation. Its realization requires the cooperation of erl vm. It basically has three functions: 1. Time-consuming operations are done in stages. Since erlang is a soft real-time system, a process or bif cannot occupy the cpu time unlimitedly. Therefore, when each process of erlang executes, it can only execute a certain number of instructions at most. This is a design goal. It must be matched to the realization. So, for example, md5, list_member search, this potentially time-consuming operation is implemented by the trap mechanism, which means that when the process is scheduled, a certain number of calculations are performed and the context is trapped to abandon the execution and wait for the next scheduling to continue. Calculation. 2. Delay execution to realize upper-level decision-making. The obvious example is the send operation. When sending, the nodes may not be connected, so the send operation cannot continue, trap first, and then execute the node connection operation during the next scheduling. Once the send operation is successful, continue to execute. This operation is transparent to customers. He doesn't know these things behind you.
3. Take the initiative to abandon the CPU yield. The design of erlang is quite detailed! PS: BIFs involved in time-consuming operations include:do_bif_utf8_to_listets_delete_1spawn_3monitor_2spawn_link_3spawn_opt_1send_2crc32_1adler32_1md5_1send_3build_utf8_returnbuild_list_returnfinalize_list_to_listdo_bif_utf8_to_listets_select_reverseets_match_spec_run_r_3 re_run_3re_exec_trapkeyfindmonitor_node_3