erlang 监控远程pid

问题1:

 

在node A上启动一个erlang节点并且等待接收消息,然后在node B上向node A发送一条消息:

 

 {java ,'testt@aliyun-18097n'}!ddddd.

 node A 上可以收到消息。

 

但是当使用BIF is_pid({java,'testt@aliyun-18097n'}). 返回的是false,说明系统不认为{java,'testt@aliyun-18097n'}这个是一个pid。

 

所以在调用下面的命令的时候都会报同样的badarg错误:

 

 

register(remotejava,{java,'testt@aliyun-18097n'}).

 

 

link({java,'testt@aliyun-18097n'}).

都会报这样的错误:

 

** exception error: bad argument
     in function  link/1
        called as link({java,'testt@aliyun-18097n'})。
 

问题2:

    执行以下命令可以正常执行

 

link( spawn(fun()-> void end))

 

    但是分开执行之后

 

Pid = spawn(fun()-> void end).
link(Pid).

 

   却报告错误,比较迷惑呀

   网上查了erlang的官方文档,http://www.erlang.org/doc/man/erlang.html

 里面对link函数的定义是这样的:

 

  link(Pid) -> true

Types:

Pid = pid() | port()
Creates a link between the calling process and another process (or port) Pid, if there is not such a link already. If a process attempts to create a link to itself, nothing is done. Returns true.

If Pid does not exist, the behavior of the BIF depends on if the calling process is trapping exits or not (see process_flag/2):

If the calling process is not trapping exits, and checking Pid is cheap -- that is, if Pid is local -- link/1 fails with reason noproc.
Otherwise, if the calling process is trapping exits, and/or Pid is remote, link/1 returns true, but an exit signal with reason noproc is sent to the calling process.

 

以上这两个问题,根源其实是相同的,那就是像以下是那个对象本质是不同的

 

  1. {java ,'testtiliyun-18097n'}元组
  2. spawn 函数的返回值 pid
  3. register注册别名之后的 进程原子替换符

 

虽然他们后面都能跟“!”这个符号向进程发送消息,但并不能说明在使用代码中可以相互替换使用。

 

比如:link/1 函数的参数必须是 pid 或者port,除此之外的其他类型的参数都会报错。所以这就能解释为什么以下的代码执行是有问题的:

 

register(pid1,spawn(fun()-> void end)).
link(pid1).

 

 和

 

link({java,'testt@aliyun-18097n'}).
 

 

另外,还需要讲一下link和monitor的区别:
    link 是将两个进程链接起来,只要其中任何一个进程挂了就是通知到另外一个进程。而monitor的工作机制是 monitor端进程如果挂了的话不会影响到 被监控端的运行。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值