1. Introduction to the example Project (bank)
Bank_server.erl is a process callback module for customer service
Bank_center.erl is the process callback module for the Banking Service Center
The customer (Bank_server) has to deduct annual fee every year
All annual fees are given to the Bank Service Center (Bank_center)
Examples of customer service use:
Create User: Bank_server:create_account (myname, 100).
Users save money: MyName! {deposit, 10}.
User withdraw money: MyName! {CASH1, 20}.
MyName! {CASH2, 30}.
Check balance: MyName! Check.
Example of using a Banking service center:
Check the total annual fee: Bank_center! Check.
2, start bank_center2.1, Bank_center has 3 launch API
Percent of here? Module is Bank_centerstart ()->%% gen_server:start/3%% no link, no registration {OK, Pid} = Gen_server:start (? MODULE, [], []), percent self-registered erlang:register (? MODULE, Pid). Start2 ()->%% gen_server:start/4%% no link, registered as a local process with the name? Modulegen_server:start ({local,? MODULE},? MODULE, [], []). Start_link ()->%% gen_server:start_link/4%% There is a link, there is a registered Gen_server:start_link ({local,? MODULE},? MODULE, [], []).
With start/0 or start2/0 is no link, start_link/0 is a link.
2.2. Start Bank_center in Bank_app
-module (Bank_app).-behaviour (Application). -export ([START/2, STOP/1]). -include ("Common.hrl"). Start (_type, _args). I ("Start ~p ...", [? MODULE]),% bank_center:start (),% bank_center:start2 (), Bank_center:start_link (), {OK, self ()}.stop (_state)? I ("Stop ~p!", [? MODULE]), OK.
After calling bank_center:start/0 or bank_center:start2/0 in the START/2 above,
Execute Appmon:start () to view the bank process as follows:
In the above START/2 call bank_center:start_link/0 startup,
Execute Appmon:start () to view the bank process as follows:
3, after the creation of the customer process, and Bank_center Linkbank_server.erl in the INIT/1
Init ([money])->%% is associated with the Banking Service Center link (Whereis (bank_center)), {OK, money}.
4, Link Experiment 4.1, test link the role of link mode start Bank_center and create a customer:
Bank_server:create_account (myname1, 100).
Whereis (bank_center) view Bank_center process ID, return not for undefined description bank_center process exists
Send an error to the client process:
Myname1! Error.
At this point myname1 exits abnormally, Whereis (Bank_center) and Whereis (myname1) return undefined, indicating that both processes have been terminated.
Eshell V5.10.4 (abort with ^g) 1> Bank_server:create_account (myname1, +) .true2> Whereis (bank_center) .< 0.37.0>3> myname1! ERROR.##[<0.38.0>BANK_SERVER:141] Terminate: {bad_return_value,{ok,80}}error4> =error REPORT==== 16- jan-2015::16:54:58 ===** Generic Server <0.38.0> terminating * * Last message in is error** when server state = = 80* * Reason for termination = = * {bad_return_value,{ok,80}}4> Whereis (bank_center) .undefined5> Whereis (myname1). Undefined
Summary: In the process group associated with link, as long as one of the processes terminates, the other terminates at the same time.
4.2, set Bank_center as the system process, and can monitor the client process exit settings Bank_center for the system process:
The INIT/1 in Bank_center.erl
Init (_)->%% set the system process to System process Process_flag (Trap_exit, true),? I ("Start ~p ...", [? MODULE]), {OK, 0}.
The demo process is as follows:
Eshell V5.10.4 (abort with ^g) 1> Bank_server:create_account (myname1, +) .true2> 2> myname1! ERROR.##[<0.38.0>BANK_SERVER:141] Terminate: {bad_return_value,{ok,100}}##[<0.37.0>bank_center:81] Handle_info: {' EXIT ',<0.38.0>,{bad_return_value,{ok,100}}}error3> =error report==== 16-Jan-2015::17:12:34 ===** Generic Server <0.38.0> terminating * * Last message in is error** when server state = = 100** Reason for Termi Nation = = * {bad_return_value,{ok,100}}3> Whereis (bank_center) .<0.37.0>4> Whereis (myname1). Undefined
Whereis (myname1). The return undefined description has been terminated.
Whereis (Bank_center) returned the PID, stating that it was not terminated and received an exit message for myname1:
{' EXIT ', <0.38.0>, {bad_return_value,{ok,100}}}
The exit PID and reason for exit are indicated in the message.
All of this is an unusual way to let the process exit, now test the normal exit situation:
Eshell V5.10.4 (abort with ^g) 1> Bank_server:create_account (myname1, +) .true2> myname1! STOP2.##[<0.38.0>BANK_SERVER:141] terminate:normal##[<0.37.0>bank_center:81] handle_info: {' EXIT ', <0.38.0>,normal}stop2
Normal exit will not error, received a normal exit message: {' exit ', <0.38.0>,normal}
Summary: By Process_flag (Trap_exit, True), the process is set to the system, not only exiting as other processes exit, but also monitoring the exit of other processes.
Question: What happens if an error occurs inside the system process and exits unexpectedly?
5. Full Demo code download
Address: http://download.csdn.net/detail/u011471961/8368973
Getting Started with Erlang: Building application Exercise 4 (The role of Process link)