Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ERLANG wait() and blocking

Tags:

erlang

wait

Does the following function block on its running core?

wait(Sec) -> 
   receive
   after (1000 * Sec) -> ok
end.

A great answer will detail the internal working of Erlang and/or the CPU.

like image 609
BAR Avatar asked Jan 24 '12 04:01

BAR


People also ask

How do I stop Erlang process?

A process can terminate itself by calling one of the BIFs exit(Reason), erlang:error(Reason), erlang:error(Reason, Args), erlang:fault(Reason) or erlang:fault(Reason, Args). The process then terminates with reason Reason for exit/1 or {Reason,Stack} for the others.

What is spawn in Erlang?

spawn() creates a new process and returns the pid. The new process starts executing in Module:Name(Arg1,...,ArgN) where the arguments are the elements of the (possible empty) Args argument list.

What is process dictionary in Erlang?

Erlang has something called Process Dictionary which is a sort of Key/Value store that belongs to each Erlang process. We can use it to store values in a simple manner inside Erlang programs while avoiding the limitations of Pure Functional Programming.

Which module is used to spawn processes manually in Erlang?

The Erlang BIF spawn is used to create a new process: spawn(Module, Exported_Function, List of Arguments).


2 Answers

The process which executes that code will block, the scheduler which runs that process currently will not block. The code you posted is equal to a yield, but with a timeout.

The Erlang VM scheduler for that core will continue to execute other processes until that timeout fires and that process will be scheduled for execution again.

like image 183
Adam Lindberg Avatar answered Sep 25 '22 16:09

Adam Lindberg


Short answer: this will block only current (lightweight) process, and will not block all VM. For more details you must read about erlang scheduler. Nice description comes from book "Concurent Programming" by Francesco Cesarini and Simon Thompson.

...snip... When a process is dispatched, it is assigned a number of reductions† it is allowed to execute, a number which is reduced for every operation executed. As soon as the process enters a receive clause where none of the messages matches or its reduction count reaches zero, it is preempted. As long as BIFs are not being executed, this strategy results in a fair (but not equal) allocation of execution time among the processes. ...snip...

like image 45
danechkin Avatar answered Sep 25 '22 16:09

danechkin