一、概述

与大多数的进程相反,Erlang中的并发很廉价,派生出一个进程就跟面向对象的语言中分配一个对象的开销差不多。 在启动一个复杂的运算时,启动运算、派生进程以及返回结果后,所有进程神奇的烟消云散,它们的内存、邮箱、所持有的数据库句柄、它们打开的套接字,以及一些不乐意手工清理的东西,都一并消失。

Erlang进程不是操作系统进程,它们由erlang运行时系统实现,比线程要轻量的多,单个erlang系统可以轻易地派生出成百上千个进程。运行时系统中所有的进程都是隔离的,单个进程的内存不与其他进程共享,也不会被濒死或跑疯的进程破坏。

在操作系统中,典型的线程会在地址空间中为自己预留数兆的栈空间(也就是说32位的机器上并发线程最多也就几千个),栈空间溢出便会导致崩溃。erlang进程在启动时栈空间只有几百个字节,并会按需伸缩。

二、示例

 14> Pid = spawn(fun() -> timer:sleep(60000), primes:primelist(100000) end).    %% 派生一个进程,等待1分钟后做素数运算。 素数功能代码已提前编写好。
<0.55.0>
15> erlang:process_info(Pid).
[{current_function,{timer,sleep,1}},
{initial_call,{erlang,apply,2}},
{status,waiting},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.26.0>},
{total_heap_size,233},
{heap_size,233}, %%刚启动的erlang进程所占的堆的大小仅为233字节,栈的大小10个字节。
{stack_size,10},
{reductions,43}, %%创建erlang进程仅消耗了43个reductions, 可见erlang的轻量。
{garbage_collection,[{min_bin_vheap_size,46422},
{min_heap_size,233},
{fullsweep_after,65535},
{minor_gcs,0}]},
{suspending,[]}]
17> erlang:statistics(run_queue). %% 进程处于挂起状态,堆、栈、时间片消耗都不会变
0
22> erlang:statistics(run_queue). %% 进程进入运行队列,准备被调度。
1
23> erlang:process_info(Pid).
[{current_function,{primes,'-primelist/3-lc$^0/1-0-',2}},
{initial_call,{erlang,apply,2}},
{status,runnable},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.26.0>},
{total_heap_size,393300},
{heap_size,75113}, %%进程在做素数计算, 堆栈大小随着需要开始增加。 消耗的时间片也会随着计算量增加而增加。
{stack_size,13773},
{reductions,89874499},
{garbage_collection,[{min_bin_vheap_size,46422},
{min_heap_size,233},
{fullsweep_after,65535},
{minor_gcs,3085}]},
{suspending,[]}]
27> erlang:statistics(run_queue).
0
29> erlang:process_info(Pid).
[{current_function,{primes,'-primelist/3-lc$^0/1-0-',2}},
{initial_call,{erlang,apply,2}},
{status,runnable},
{message_queue_len,0},
{messages,[]},
{links,[]},
{dictionary,[]},
{trap_exit,false},
{error_handler,error_handler},
{priority,normal},
{group_leader,<0.26.0>},
{total_heap_size,393300},
{heap_size,75113},
{stack_size,87}, %% 随着计算的结束, 栈空间开始收缩, 这里可以看到堆空间没有变,堆的分配是由erlang GC来控制的, 进程结束时由GC来回收。
{reductions,958602600},
{garbage_collection,[{min_bin_vheap_size,46422},
{min_heap_size,233},
{fullsweep_after,65535},
{minor_gcs,33117}]},
{suspending,[]}]

三、进程的调度

由于Erlang虚拟机对SMP的支持,每个操作系统线程都可以运行在一个调度器上,每个调度器拥有一自己的运行队列,这样避免了多个调度器同时调度在运行队列中的任务产生的冲突,但是如何保证调度队列任务分配的公平性,Erlang引入了一个高效和公平的概念,迁移逻辑。迁移逻辑利用在系统中收集的统计数据,控制和平衡了队列。

Erlang启动模拟器的时候可以加上+S去指定最大调度器数和可用调度器数。可用调度器数可以在模拟器运行时更改。

 -> erl +S 16:8
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:16:8] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10.4 (abort with ^G)
1> erlang:system_info(schedulers_online).
8
2> erlang:system_info(schedulers).
16

下面我们尝试起多个erlang进程去做素数运算,然后看看调度队列情况。

 -> erl +S 8:8     %%启动8个调度器同时可用。
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10.4 (abort with ^G)
1> erlang:statistics(run_queue). %%之前没有任务进入调度队列
0
2> [spawn(fun() -> timer:sleep(5000), primes:primelist(10000) end) || _ <- lists:seq(1, 10)]. %% 同时启动10个进程做素数运算
[<0.36.0>,<0.37.0>,<0.38.0>,<0.39.0>,<0.40.0>,<0.41.0>,
<0.42.0>,<0.43.0>,<0.44.0>,<0.45.0>]
5> erlang:statistics(run_queue). %%同时有7个进程被调度运行,3个进程出去准备状态
3
7> erlang:statistics(run_queue). %%两个任务被换入。
1
8> erlang:statistics(run_queue). %%任务执行完毕,没有任务处于等待状态。
0

  那么问题来了,在同样的硬件环境下,是不是调度器越多,进程处理的速度越快呢?列出测试结果:

 -> erl +S 4:4     %%%启动4个可用调度器
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]
2> [spawn(fun() -> {P1, _P2} = timer:tc(primes, primelist, [100000]), io:format("timer:~p~n", [P1]) end) || _ <- lists:seq(1, 10)].
timer:185078651
timer:190735178
timer:192956743
timer:193186850
timer:220074562
timer:222929652
timer:234756209
timer:235304593
timer:235474721
timer:236500425 -> erl +S 8:8 %%%启动8个可用调度器
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10.4 (abort with ^G)
1>
1>
1>
1> [spawn(fun() -> {P1, _P2} = timer:tc(primes, primelist, [100000]), io:format("timer:~p~n", [P1]) end) || _ <- lists:seq(1, 10)].
[<0.35.0>,<0.36.0>,<0.37.0>,<0.38.0>,<0.39.0>,<0.40.0>,
<0.41.0>,<0.42.0>,<0.43.0>,<0.44.0>]
timer:187405676
timer:187568120
timer:188255698
timer:188577806
timer:190819642
timer:191208176
timer:235470698
timer:236842370
timer:237630863
timer:238206383 -> erl +S 16:11
Erlang R16B03-1 (erts-5.10.4) [source] [64-bit] [smp:16:11] [async-threads:10] [hipe] [kernel-poll:false] Eshell V5.10.4 (abort with ^G)
1> [spawn(fun() -> {P1, _P2} = timer:tc(primes, primelist, [100000]), io:format("timer:~p~n", [P1]) end) || _ <- lists:seq(1, 10)].
[<0.35.0>,<0.36.0>,<0.37.0>,<0.38.0>,<0.39.0>,<0.40.0>,
<0.41.0>,<0.42.0>,<0.43.0>,<0.44.0>] timer:243000833
timer:243636514
timer:244753411
timer:245005027
timer:245296405
timer:245356679
timer:245659526
timer:245662159
timer:245731926
timer:245779971

  从测试结果看,并不是调度器越多越好,也不是越少越好,合适实际应用场景才是最好的。

最新文章

  1. 带你实现开发者头条APP(五)--RecyclerView下拉刷新上拉加载
  2. jQuery选择器笔记
  3. UITabBarController的创建与自定义TarBar---学习笔记三
  4. Container容器控件的使用、Hbox与Vbox布局管理器的使用、以及AjaxAction前后台事件响应
  5. C# :XML和JSON互转
  6. 用Karma和Jasmine测试Angular应用
  7. Webstorm 激活破解
  8. 利用scrapy模拟登录知乎
  9. RabbitMQ和Kafka到底怎么选?
  10. python的安装和配置
  11. 日志信息log
  12. 分享一个Panda C-60 维修心得
  13. Flash Actionscript AS3 渐变透明 mask遮罩
  14. 重写jquery serialize 方法
  15. Linux文件类型(学习笔记六)
  16. Android -- Options Menu,Context Menu,Popup Menu
  17. CryptoZombies学习笔记——Lesson4
  18. ubuntu 14.04编译安装xen4.4总结
  19. Eclipse快速补全快捷键Ctrl+1修改为Android Studio的Alt+Enter
  20. 扒一扒spring,dom4j实现模拟实现读取xml

热门文章

  1. postman使用—chrome版
  2. Linux常用的网络命令
  3. Linux的mount命令简介
  4. 阿里云服务器实战(一) : 在Linux下Tomcat7下使用连接池
  5. netstat 的10个基本用法(转)
  6. LeetCode之“散列表”:Isomorphic Strings
  7. Mondrian Schema workbench工作界面 简介(实在懒得写,居然有人弄了,收藏了)
  8. HFile
  9. objective-c中所谓的僵尸对象
  10. How I Turned Down $300,000 from Microsoft to go Full-Time on GitHub