Skip to content

Commit

Permalink
Added missing example file msg.erl
Browse files Browse the repository at this point in the history
  • Loading branch information
happi committed Apr 17, 2017
1 parent 07bc75a commit 3f97e8a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ adocs = book.asciidoc \
code/memory_chapter/src/lb.erl \
code/memory_chapter/src/send.erl \
code/memory_chapter/src/share.erl \
code/processes_chapter/src/msg.erl \
images/observer_applications.png \
images/observer_code_server.png \
images/observer_load.jpg \
Expand Down
6 changes: 6 additions & 0 deletions chapters/ap-code_listings.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ include::../code/compiler_chapter/src/json_parser.erl[]
include::../code/compiler_chapter/src/json_test.erl[]
----

[source,erlang]
----
include::../code/processes_chapter/src/msg.erl[]
----


[source,erlang]
----
include::../code/beam_chapter/src/stack_machine_compiler.erl[]
Expand Down
5 changes: 4 additions & 1 deletion chapters/processes.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -956,7 +956,10 @@ As mentioned we can force the message to end up in the in queue by
setting the flag _message_queue_data_. We can try this with the following
program:

<embed file= "code/msg.erl"/>
[source,erlang]
----
include::../code/processes_chapter/src/msg.erl[]
----

With this program we can try sending a message on heap and off heap and
look at the PCB after each send. With on heap we get the same result
Expand Down
36 changes: 36 additions & 0 deletions code/processes_chapter/src/msg.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-module(msg).

-export([send_on_heap/0
,send_off_heap/0]).

send_on_heap() -> send(on_heap).
send_off_heap() -> send(off_heap).

send(How) ->
%% Spawn a function that loops for a while
P2 = spawn(fun () -> receiver(How) end),
%% spawn a sending process
P1 = spawn(fun () -> sender(P2) end),
P1.

sender(P2) ->
%% Send a message that ends up on the heap
%% {_,S} = erlang:process_info(P2, heap_size),
M = loop(0),
P2 ! self(),
receive ready -> ok end,
P2 ! M,
%% Print the PCB of P2
hipe_bifs:show_pcb(P2),
ok.

receiver(How) ->
erlang:process_flag(message_queue_data,How),
receive P -> P ! ready end,
%% loop(100000),
receive x -> ok end,
P.


loop(0) -> [done];
loop(N) -> [loop(N-1)].

0 comments on commit 3f97e8a

Please sign in to comment.