Erlang can not receive message from where it begins
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Erlang is a functional programming language used primarily for building scalable and fault-tolerant systems. One of the key features in Erlang is its concurrency model which relies heavily on message passing between processes. In Erlang, each process has a mailbox and communicates with other processes by sending and receiving messages. However, a process cannot receive messages before it officially starts, as these messages are routed to its unique mailbox which isn't active until the process itself is running.
Understanding Message Passing in Erlang
In Erlang, processes communicate asynchronously via message passing. When you send a message in Erlang, it gets queued in the recipient process's mailbox. A process checks its mailbox in a first-in, first-out (FIFO) order with the receive expression, which matches incoming messages against specified patterns.
The Start of an Erlang Process
When an Erlang process is created using the spawn function, it begins its life cycle. However, its ability to check or receive messages starts only after the process is fully initialized and enters its receive loop. If any message is sent to a process before it reaches its receive loop, those messages simply get queued in the mailbox.
Examples
Consider a scenario where process A spawns process B and immediately sends a message to B:
In this example, the message {self(), hello} is sent to process B right after it is spawned. Even though it is sent before process B enters the receive block, the Erlang runtime ensures that the message is queued in process B’s mailbox. It will be waiting when B starts receiving.
Situations where Messages are not Received as Expected
1. Wrong Process Identifier (PID)
If you send a message to a non-existing or wrong PID, the message is lost because there are no checks on the existence of a PID when sending a message.
2. Crashed Before Receiving
If a process crashes or terminates before it starts receiving messages or during its processing, any messages in its mailbox are lost.
Summary Table
| Aspect | Characteristic |
| Concurrency Model | Based on Asynchronous Message Passing |
| Communication | Via Mailbox, messages are queued until processed |
| Message Receipt Start | After process enters its receive loop |
| Message Loss Conditions | Wrong PID, Process crashes before/during message processing |
| Integrity of Messaging | Reliable as long as PID is correct and recipient process can handle incoming messages |
Additional Considerations
- Mailbox Overflow: If a process does not process its messages quickly enough, the mailbox can grow indefinitely, which may lead to memory issues. Hence, occasionally, it's essential to design back-pressure systems or set limits on mailboxes.
- Monitoring: Processes can monitor each other to detect crashes and handle errors gracefully.
- Timed Receive: Processes can use timed
receiveto avoid being blocked indefinitely, enhancing the responsiveness of the system.
In conclusion, understanding the nuances of message passing and the lifecycle of Erlang processes is crucial in leveraging the full potential of its concurrency model. By grasping these concepts, developers can build robust and efficient systems that handle various real-time, distributed computing challenges.
Related reading
- Erlang's let-it-crash philosophy - applicable elsewhere?
- Error-Handling in Swift-Language
- error -215 ssize.width 0 ssize.height 0 in function resize
- Error - is not marked as serializable
- Error - The transaction associated with the current connection has completed but has not been disposed
- Error - trustAnchors parameter must be non-empty
- Error - Unable to access the IIS metabase
- Error - Unable to access the IIS metabase
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.