<br><br><div class="gmail_quote">On Thu, Jan 14, 2010 at 10:39 AM, Madison Kelly <span dir="ltr"><<a href="mailto:linux@alteeve.com">linux@alteeve.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi all,<br>
<br>
I want to put a shutdown timer on a spawnable daemon (specifically, a script spawned by dbus). To do this, I created a bless'ed "self" hash reference in my module's constructor, set a few time values in it and the call a timer method. In the timer method, I 'fork' and the child starts a loop and the main script returns and waits for method calls over dbus.<br>
<br>
The problem is that, in the child process, changes to the values in 'self' made by the parent do not appear in the child. Specifically, each method call changes a '$self->{SHUTDOWN_TIME}' value. I can understand this behavior to an extent, but when I print out the hash reference itself both in the parent and the child, they have the same reference string. Obviously though, they aren't actually using the same memory space.<br>
<br>
So two questions;<br>
<br>
1. Why do the self references match when they don't (apparently) use the same memory space<br></blockquote><div><br>Because a forked process contains a 'copy' of the parent process. Since its an exact copy, it<br>
effectively has exactly the same memory addresses. (but they are in a separate instance of memory space)<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
2. What is the best way to "share" variables between children and parents?<br></blockquote><div><br>Pipes, shared memory, files, sockets, etc. There is no 'best' way.<br>Originally forked process (parent/child) were intended to be 'very independent'<br>
and hence don't 'share memory', and are protected from the other's 'bugs',<br>and your only choices are the ones above.<br><br>Then the world invented threads, where everything runs in the same process space<br>
and effectively shares everything, were one thread can accidentally (bug) walk over<br>everything.<br><br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Currently I am working around the problem by writing out my timing info to a tmp file,</blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
but this seems to be unnecessarily expensive.<br></blockquote></div><br>