Why would someone "kill the parent"? Do you have processes which are randomly choosing other processes to terminate? ;)<br><br>If your answer is "No" then I would suggest that you just kill the worker processes that is taking to long, kill it in cold blood, imo.<br>
<br>There is no reason to think about too many "if"-scenarios when the scenarios are too far fetched. Try the simple version first :)<br><br>otherwise you can either trap_exits or use monitors instead.<br><br>/M<br>
<br><div class="gmail_quote">On 25 May 2011 08:00, Igor Ribeiro Sucupira <span dir="ltr"><<a href="mailto:igorrs@gmail.com">igorrs@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Suppose there is a heavy operation Op that in some cases takes so long<br>
to finish that the caller loses interest in the result and gives up.<br>
<br>
I want to perform that operation in a way that allows me to:<br>
1) Interrupt its execution if it does not finish in N milliseconds.<br>
2) Interrupt its execution if the calling process exits (here I'm<br>
already supposing Op has to be run in another Erlang process, due to<br>
goal 1).<br>
<br>
To implement that, it seems unidirectional linking would be needed. Is<br>
there another safe and convenient way to do it?<br>
<br>
The first idea I had was something like this:<br>
<br>
Parent = self(),<br>
Child = spawn_link(fun() -> Parent ! (catch Op) end),<br>
receive Result -> Result<br>
after N -> unlink(Child), exit(Child, timeout), timeout<br>
end.<br>
<br>
But, if Parent is killed by another process right after calling<br>
unlink, Child would be left executing.<br>
Another problem is that I don't want Parent to die if Child exits for<br>
non-timeout reasons (although it seems very unlikely in the code<br>
above, with the catch).<br>
<br>
I was now thinking of substituting unlink(Child) with<br>
process_flag(trap_exit, true) and then kill Child, receive its exit<br>
message, set trap_exit to false again (I'm assuming it was false), and<br>
finally check if there were other exit messages (suiciding if it was<br>
the case).<br>
<br>
But then the code would become too ugly, so I got lazy and decided to<br>
post to this list. :-)<br>
<br>
What do you think?<br>
<br>
Thanks.<br>
Igor.<br>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br>