diff --git a/Makefile b/Makefile index f11821f..d0a77b6 100755 --- a/Makefile +++ b/Makefile @@ -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 \ diff --git a/chapters/ap-code_listings.asciidoc b/chapters/ap-code_listings.asciidoc index 7db9328..0867f39 100644 --- a/chapters/ap-code_listings.asciidoc +++ b/chapters/ap-code_listings.asciidoc @@ -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[] diff --git a/chapters/processes.asciidoc b/chapters/processes.asciidoc index a600e2b..474cfd9 100644 --- a/chapters/processes.asciidoc +++ b/chapters/processes.asciidoc @@ -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: - +[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 diff --git a/code/processes_chapter/src/msg.erl b/code/processes_chapter/src/msg.erl new file mode 100644 index 0000000..68e8187 --- /dev/null +++ b/code/processes_chapter/src/msg.erl @@ -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)].