[Erlang 0076] A strange problem with Erlang Shell

Source: Internet
Author: User

 

I have been busy recently, and occasionally spend some time reading books to supplement my energy. Recently I am studying programming languages-I am very touched by the road to practice. What I 've learned by mistake is a more system, the complete knowledge system is gradually absorbed. cities like Beijing, Shanghai, Guangzhou, and so on are quickly consuming our energy and knowledge, so we cannot slack ourselves, but we don't have to worry about it. If we are impatient, the effect will inevitably be compromised. Take the time to sort out recent notes, start with a weird problem!

 

In Erlang shell, I often create a process that is infinitely waiting to receive messages for testing. Code: spawn (fun ()-> receive after Infinity-> IO: Format ("done! ") End ). recurrence steps: 1. 2. send a message to the process. because there is no logic for message extraction, the messages received by the process will be accumulated in the message queue. You can view the messages through Erlang: process_info. First, use the shell process to give a try, we can see that the result is consistent with our expectation:

 

 

23> [self()!abc || _<-lists:seq(1,20)].[abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc]24> erlang:process_info(self()).      [{current_function,{erl_eval,do_apply,6}},{initial_call,{erlang,apply,2}},{status,running},{message_queue_len,20},{messages,[abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,abc,            abc,abc,abc,abc,abc,abc,abc,abc]},{links,[<0.26.0>]},{dictionary,[]},{trap_exit,false},{error_handler,error_handler},{priority,normal},{group_leader,<0.25.0>},{total_heap_size,5168},{heap_size,2584},{stack_size,24},{reductions,68319},{garbage_collection,[{min_bin_vheap_size,46368},                      {min_heap_size,233},                      {fullsweep_after,65535},                      {minor_gcs,16}]},{suspending,[]}] 
Let's try it using spawn (fun). Let's see the following results. After sending two messages, we can check the Process status and find that the message queue is empty !!!

 

Erlang R15B (erts-5.9) [source] [64-bit] [async-threads:0] [hipe] [kernel-poll:false]Eshell V5.9  (abort with ^G)1> P=spawn(fun() -> receive after infinity-> io:format("dONE!") end end).<0.34.0>2> P!abc.abc3> P!abcd.abcd4> erlang:process_info(P).[{current_function,{erl_eval,receive_clauses,8}},{initial_call,{erlang,apply,2}},{status,waiting},{message_queue_len,0},{messages,[]},{links,[]},{dictionary,[]},{trap_exit,false},{error_handler,error_handler},{priority,normal},{group_leader,<0.25.0>},{total_heap_size,233},{heap_size,233},{stack_size,10},{reductions,26},{garbage_collection,[{min_bin_vheap_size,46368},                      {min_heap_size,233},                      {fullsweep_after,65535},                      {minor_gcs,0}]},{suspending,[]}]

 

Is there a problem with Erlang shell? I will try this code in a test module:
-module(t).-compile(export_all).a() ->     receive          {Data}-> io:format("reveive Data :~p~n ",[Data]),         a()    after infinity ->          io:format("Done!")    end.b() -> spawn(fun() -> receive after infinity-> io:format("dONE!") end end). 
Run the above Code T: B (), send the message, and view the process status ~~~~~~, No. In this way, you can see the backlog of messages in the message queue !! So where is the message received in the previous writing?

 

5> t:b().<0.39.0>6> v(5)!abc.abc7> v(5)!abcd.abcd8> erlang:process_info(v(5)).[{current_function,{t,'-b/0-fun-0-',0}},{initial_call,{erlang,apply,2}},{status,waiting},{message_queue_len,2},{messages,[abc,abcd]},{links,[]},{dictionary,[]},{trap_exit,false},{error_handler,error_handler},{priority,normal},{group_leader,<0.25.0>},{total_heap_size,233},{heap_size,233},{stack_size,0},{reductions,2},{garbage_collection,[{min_bin_vheap_size,46368},                      {min_heap_size,233},                      {fullsweep_after,65535},                      {minor_gcs,0}]},{suspending,[]}]

 

Where is the message possible? There is another way to output stacktrace. From the following results, we can see that Y (2) [ABCD, ABC] is the message we sent before. Check the T: B () process and no similar information. Through comparison, the breakthrough point should be here, however, the detailed descriptions of process_dsiplay output are not found, so it is difficult to continue. record the information here and continue later.

9> bt(P).Program counter: 0x00007f738b7d76c0 (erl_eval:receive_clauses/8 + 168)CP: 0x0000000000000000 (invalid)arity = 00x00007f738d11c2d0 Return addr 0x000000000084e1b8 (<terminate process normally>)y(0)     []y(1)     valuey(2)     [abcd,abc]y(3)     {value,#Fun<shell.7.20862592>}y(4)     {eval,#Fun<shell.24.20862592>}y(5)     []y(6)     {[{call,1,{remote,1,{atom,1,io},{atom,1,format}},[{string,1,"dONE!"}]}],[]}y(7)     []y(8)     infinityok10> bt(v(5)).Program counter: 0x00007f73852f7ec0 (t:'-b/0-fun-0-'/0 + 8)CP: 0x000000000084e1b8 (<terminate process normally>)arity = 0ok11> 

 

Finally, I will post a picture of Galaxy impressions this year.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.