monitor vs link

Ulf Wiger <>
Sat Apr 2 18:06:49 CEST 2005


(I believe this post didn't make it on the first try.)


Den 2005-04-02 16:36:24 skrev asergey <>:


> Could you please explain the difference between erlang:monitor(process,  
> Pid) and link(Pid) + process_flag(trap_exit, true)?

The most important differences are these:

- Links are two-way, while monitors are one-way. Thus,
    links work well for cascading exits, but you can't
    unobtrusively monitor another process using link
    (the other process will be affected if your process
    dies.)

- There is never more than one link between two processes,
    while there can be several nested monitors. Consider
    e.g. gen_server:call/2. It sets a monitor on the
    server process, and removes it when it gets a reply.
    In order to do that with link, the gen:call() function
    would have to call process_info(self(), links) first,
    and check the list of links to see whether there is
    already a link to the server; then perhaps set a link,
    and - if so - remove it afterwards (if there were a
    link already, it mustn't remove it). Still, the server
    must be able to handle EXIT messages from clients, which
    is normally not what one wants.

/Uffe



More information about the erlang-questions mailing list