From 293916c8f4dc109082ecd709e5e4c19a0847b0cd Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 9 Feb 2024 21:02:57 +0100 Subject: wip --- step-04/README.md | 0 step-04/tut17.erl | 31 +++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 step-04/README.md create mode 100644 step-04/tut17.erl (limited to 'step-04') diff --git a/step-04/README.md b/step-04/README.md new file mode 100644 index 0000000..e69de29 diff --git a/step-04/tut17.erl b/step-04/tut17.erl new file mode 100644 index 0000000..6808340 --- /dev/null +++ b/step-04/tut17.erl @@ -0,0 +1,31 @@ +-module(tut17). + +-export([start_ping/1, start_pong/0, ping/2, pong/0]). + +ping(0, Pong_Node) -> + {pong, Pong_Node} ! finished, + io:format("ping finished~n", []); + +ping(N, Pong_Node) -> + {pong, Pong_Node} ! {ping, self()}, + receive + pong -> + io:format("Ping received pong~n", []) + end, + ping(N - 1, Pong_Node). + +pong() -> + receive + finished -> + io:format("Pong finished~n", []); + {ping, Ping_PID} -> + io:format("Pong received ping~n", []), + Ping_PID ! pong, + pong() + end. + +start_pong() -> + register(pong, spawn(tut17, pong, [])). + +start_ping(Pong_Node) -> + spawn(tut17, ping, [3, Pong_Node]). -- cgit v1.2.3