缘起坚强哥分享霸爷对于公平调度的解答。
![](http://images0.cnblogs.com/blog/419564/201301/08111305-5219ef0af8ff4d28a1d279995da044b3.jpg)
===========================================================================
如果函数A会调用函数B, 函数B每次调用都会递归3次。那么每调用一次函数A,是否就是4个reduction?
到底是怎样自己动手测一下吧:
先是不嵌套的
Eshell V5.9 (abort with ^G)
1> A = spawn(fun() -> one:start() end).
<0.33.0>
2> process_info(A, reductions).
{reductions,46}
3> process_info(A, reductions).
{reductions,46}
4> process_info(A, reductions).
{reductions,46}
5> process_info(A, reductions).
{reductions,46}
6> A!go.
go
7> process_info(A, reductions).
{reductions,49}
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
aaa(3)
end,
receive
stop ->
ok
end,
ok.
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
========================================================
改为嵌套调用:
1> A = spawn(fun() -> one:start() end).
<0.33.0>
2> process_info(A, reductions).
{reductions,46}
3> A!go.
go
4> process_info(A, reductions).
{reductions,50}
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
bbb()
end,
receive
stop ->
ok
end,
ok.
bbb() ->
aaa(3).
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
===================================================
receive会不会有影响呢?
![](http://images0.cnblogs.com/blog/419564/201301/08110830-f0723169d4f14d169a7e148f05ba3310.png)
不会的。。。
代码:
-module(one).
-compile(export_all).
start() ->
receive
go ->
bbb()
end,
receive
rec ->
ok
end,
receive
stop ->
stop
end,
ok.
bbb() ->
aaa(3).
aaa(1) ->
ok;
aaa(N) ->
aaa(N-1).
综上,reduction是函数调用,如果函数A会调用函数B, 函数B每次调用都会递归3次。那么每调用一次函数A,就是4个reduction。