Note that there are some explanatory texts on larger screens.

plurals
  1. POsafe to access shared data structure from signal handler
    text
    copied!<p>I'm trying to decide wether it's safe to access a common (read: shared between handler-code and rest of the programm) data structure from a signal handler in perl (v5.14.2) built for <code>x86_64-linux-thread-multi</code>, but target platform is solaris11).</p> <p><a href="http://perldoc.perl.org/perlipc.html" rel="nofollow">perlipc</a> has the following sample code:</p> <pre><code>use POSIX ":sys_wait_h"; # for nonblocking read my %children; $SIG{CHLD} = sub { # don't change $! and $? outside handler local ($!, $?); my $pid = waitpid(-1, WNOHANG); return if $pid == -1; return unless defined $children{$pid}; delete $children{$pid}; cleanup_child($pid, $?); }; while (1) { my $pid = fork(); die "cannot fork" unless defined $pid; if ($pid == 0) { # ... exit 0; } else { $children{$pid}=1; # ... system($command); # ... } } </code></pre> <p>So, <code>%children</code> is accessed from the while-loop and the handler. This seems to be no problem as:</p> <ol> <li>There won't be two processes having the same <code>pid</code></li> <li>Access is keyed by <code>pid</code> (I am not sure if <code>$childer{pid}=1</code> is atomic and interruptible without causing corruption, though.)</li> </ol> <p>Now, i'm trying to do even more in my handler:</p> <pre><code>my %categoryForPid; my %childrenPerCategory; $SIG{CHLD} = sub { # ... acquire pid like above my $category = $categoryForPid{$pid}; $childrenPerCategory{$category}--; delete $categoryForPid{$pid}; } while (1) { # ... same as above } else { $children{$pid}=1; my $category = # ... chose some how $childrenPerCategory{$category}++; $categoryForPid{$pid} = $category; # ... } } </code></pre> <p>The idea here is: every child belongs to a certain category (N to 1). I want to keep track of how many children per category exist. That information could be derived from <code>$categoryForPid</code>, but i think that might be problematic also (e.g., when the subroutine doing the computation gets interrupted while summing up).</p> <p>So my question is:</p> <ul> <li>Do I need to synchronize here somehow?</li> </ul> <p>And on a side note:</p> <ul> <li>Are nested invocations of the signal handler possible in perl 5.12, or are they linearized by the interpreter?</li> </ul> <h2>Update</h2> <p>In addition to the problem spotted by @goldilocks and his proposed solution I block signals now while updating the data structures to ensure "atomicity":</p> <pre><code>my $sigset = POSIX::SigSet-&gt;new(SIGCHLD); sub ublk { unless (defined sigprocmask(SIG_UNBLOCK, $sigset)) { die "Could not unblock SIGCHLD\n"; } } sub blk { unless (defined sigprocmask(SIG_BLOCK, $sigset)) { die "Could not block SIGCHLD\n"; } } while (1) { # ... same as above } else { blk; $children{$pid}=1; my $category = # ... chose some how $childrenPerCategory{$category}++; $categoryForPid{$pid} = $category; ublk; # ... } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload