2007년 04월 23일
How receive works in Erlang
In Concurrent Progamming in Erlang(1996):
In Making reliable distributed systems in the presence of software errors(2003):
In Programming in Erlang(2007):
Each process has a mailbox and all messages which are sent to the process are stored in the mailbox in the same order as they arrive. In the above, Message1 and Message2 are patterns which are matched against messages that are in the process's mailbox. When a matching message is found and any corresponding guard succeeds the message is selected, removed from the mailbox and then the corresponding ActionsN are evaluated. receive returns the value of the last expression evaluated in the actions. As in other forms of pattern matching, any unbound variables in the message pattern become bound. Any messages which are in the mailbox and are not selected by receive will remain in the mailbox in the same order as they were stored and will be matched against in the next receive. The process evaluating receive will be suspended until a message is matched.
Erlang has a selective receive mechanism, thus no message arriving unexpectedly at a process can block other messages to that process. However, as any messages not matched by receive are left in the mailbox, it is the programmer's responsibility to make sure that the system does not fill up with such messages.
In Making reliable distributed systems in the presence of software errors(2003):
Msg1...MsgN are patterns. The patterns may be followed by optional guards. When a message arrives at a process it is put into a mailbox belonging to that process. The next time the process evaluates a receive statement the system will look in the mailbox and try to match the first item in the mailbox with the set of patterns contained in the current receive statement. If no message matches then the received message is moved to a temporary “save” queue and the process suspends and waits for the next message. If the message matches and if any guard test following the matching pattern also matches then the sequence of statements following the match are evaluated. At the same time, any saved messages are put back into the input mailbox of the process.
The receive statement can have an optional timeout. If no matching message is received within the timeout period then the commands associated with the timeout are evaluated.
In Programming in Erlang(2007):
receive works as follows:
When we enter a receive statement we start a timer (but only if an after section is present in the expression).
Take the first message in the mailbox and try and match it against Pattern1, Pattern2, and so on. If the match succeeds, the message is removed from the mailbox and the expressions following the pattern are evaluated.
If none of the patterns in the receive statement match the first message in the mailbox, then the first message is removed from the mailbox and put into a “save queue,” The second message in the mailbox is then tried. This procedure is repeated until a matching message is found or until all the messages in the mailbox have been examined.
If none of the messages in the mailbox match, then the process is suspended, and will be re-scheduled for execution the next time a new message is put in the mailbox.
As soon as a message has been matched, then all messages that have been put into the save queue are re-entered into the mailbox in the order in which they arrived at the process. If a timer was set it is cleared.
If the timer elapses when we are waiting for a message then evaluate the expressions ExpressionsTimeout and put any saved messages back into the mailbox in the order in which they arrived at the process.
# by | 2007/04/23 00:10 | Erlang | 트랙백 | 덧글(0)







☞ 내 이글루에 이 글과 관련된 글 쓰기 (트랙백 보내기) [도움말]