Erlang OTP: Distributed & Fault-Tolerant Systems Programming · บทเรียน

โพรเซสและการรับส่งข้อความใน Erlang

ทำความเข้าใจกลไกพื้นฐานของการทำงานพร้อมกันใน Erlang นั่นคือโพรเซส เรียนรู้การสื่อสารระหว่างโพรเซสผ่านการส่งข้อความแบบอะซิงโครนัส และสร้างหน่วยการทำงานพร้อมกันที่แยกอิสระและใช้ทรัพยากรน้อย

บทเรียน 2 จาก 412 ขั้นตอน

โพรเซสและการรับส่งข้อความใน Erlang เป็นบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Erlang's Core: The Process

The fundamental unit of Erlang concurrency is the process: a tiny, independent program running alongside others, far lighter than an OS thread.

Why Erlang Processes?

Erlang processes are lightweight (millions per machine), isolated (no shared memory), concurrent, and talk only via async messages.

Spawning a New Process

You create a process with spawn, passing a module, function, and args. It returns a unique PID identifying the new process.

-module(process_spawn).
-export([start/0, hello_world_process/0]).

hello_world_process() ->
    io:format("Hello from a new Erlang process!~n").

start() ->
    % Spawns a new process running hello_world_process/0
    Pid = spawn(process_spawn, hello_world_process, []),
    io:format("Spawned process with PID: ~p~n", [Pid]).

Understanding Process IDs (PIDs)

Every process gets a unique PID — its postal address. You need that PID to send it any message; it looks like <0.80.0>.

Sending Messages with '!'

Processes talk by messages, never shared memory. Send one with the ! operator: Pid ! Message, where the message is any Erlang term.

-module(message_sender).
-export([start/0, receiver_process/0, sender_process/1]).

receiver_process() ->
    io:format("Receiver process ~p started.~n", [self()]),
    % This process will just print its PID for now.
    % It doesn't receive messages in this version.
    timer:sleep(1000). % Keep process alive for a moment

sender_process(ReceiverPid) ->
    io:format("Sender process ~p sending message to ~p...~n", [self(), ReceiverPid]),
    ReceiverPid ! {hello, "from sender"},
    io:format("Message sent!~n").

start() ->
    ReceiverPid = spawn(message_sender, receiver_process, []),
    io:format("Receiver spawned with PID: ~p~n", [ReceiverPid]),
    sender_process(ReceiverPid).

Receiving Messages: 'receive'

A process pulls messages from its mailbox with receive, which pattern-matches each one and waits (or times out) until a match arrives.

-module(message_receiver).
-export([start/0, my_receiver/0, my_sender/1]).

my_receiver() ->
    io:format("Receiver ~p waiting for messages...~n", [self()]),
    receive
        {hello, Msg} ->
            io:format("Receiver ~p received: ~p~n", [self(), Msg]);
        _AnyOtherMessage ->
            io:format("Receiver ~p received an unexpected message: ~p~n", [self(), _AnyOtherMessage])
    end,
    io:format("Receiver ~p finished.~n", [self()]).

my_sender(ReceiverPid) ->
    io:format("Sender ~p sending message to ~p.~n", [self(), ReceiverPid]),
    ReceiverPid ! {hello, "World"},
    io:format("Sender ~p sent message.~n", [self()]).

start() ->
    ReceiverPid = spawn(message_receiver, my_receiver, []),
    timer:sleep(100), % Give receiver time to start
    my_sender(ReceiverPid).

A Full Messaging Example

Here is the full flow in one example: spawn a server, the client sends a request, and the server receives and replies.

-module(full_message_example).
-export([start/0, server_loop/0, client_action/1]).

server_loop() ->
    io:format("Server ~p started, waiting for requests...~n", [self()]),
    receive
        {request, ClientPid, Message} ->
            io:format("Server ~p received request from ~p: ~p~n", [self(), ClientPid, Message]),
            ClientPid ! {response, self(), "Got your message!"};
        _Other ->
            io:format("Server ~p received unexpected: ~p~n", [self(), _Other])
    end.

client_action(ServerPid) ->
    io:format("Client ~p sending request to server ~p...~n", [self(), ServerPid]),
    ServerPid ! {request, self(), "Can you hear me?"},
    receive
        {response, ServerPid, Reply} ->
            io:format("Client ~p received reply from ~p: ~p~n", [self(), ServerPid, Reply])
    end.

start() ->
    ServerPid = spawn(full_message_example, server_loop, []),
    timer:sleep(100), % Give server time to start
    client_action(ServerPid).

Asynchronous Communication

Erlang messaging is asynchronous: the sender drops the message in the mailbox and keeps going, never blocking on the receiver.

The Process Mailbox

Each process has a private mailbox where incoming messages queue. A receive scans it for a pattern match, usually in arrival order.

Knowing Your Own PID: `self()`

The built-in self() returns the current process's PID — handy for messaging yourself or embedding a reply address in a message.

-module(self_example).
-export([start/0, print_self/0]).

print_self() ->
    io:format("Hello, my PID is: ~p~n", [self()]).

start() ->
    io:format("Parent process PID: ~p~n", [self()]),
    spawn(self_example, print_self, []).

Quick Check: Message Flow

Consider the following sequence of events in Erlang:

  1. Process A spawns Process B.
  2. Process A sends a message to Process B.
  3. Process B receives the message.

Which of the following statements about this interaction are true?

Recap: Processes & Messaging

Recap: processes communicate by async messages — spawn returns a PID, ! sends, receive matches, each has a mailbox, and self() gives your own PID.

เริ่มต้นได้ฟรี

เรียนรู้ Erlang ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “โพรเซสและการรับส่งข้อความใน Erlang” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “โพรเซสและการรับส่งข้อความใน Erlang” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Erlang OTP: Distributed & Fault-Tolerant Systems Programming มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “โพรเซสและการรับส่งข้อความใน Erlang”

ทำความเข้าใจกลไกพื้นฐานของการทำงานพร้อมกันใน Erlang นั่นคือโพรเซส เรียนรู้การสื่อสารระหว่างโพรเซสผ่านการส่งข้อความแบบอะซิงโครนัส และสร้างหน่วยการทำงานพร้อมกันที่แยกอิสระและใช้ทรัพยากรน้อย คุณปฏิบัติ Erlang OTP: Distributed & Fault-Tolerant Systems Programming ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Erlang OTP: Distributed & Fault-Tolerant Systems Programming บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “โพรเซสและการรับส่งข้อความใน Erlang” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming นี้ได้ไหม

ได้ บทเรียน Erlang OTP: Distributed & Fault-Tolerant Systems Programming ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนะนำ Erlang และ VM
  2. โพรเซสและการรับส่งข้อความใน Erlang
  3. รูปแบบพื้นฐานของการทำงานพร้อมกัน
  4. การจับคู่รูปแบบและเงื่อนไขกำกับ
← กลับไปที่ Erlang OTP: Distributed & Fault-Tolerant Systems Programming