16.12. Sharing Variables in Different ProcessesProblemYou want to share variables across forks or between unrelated processes. SolutionUse SysV IPC, if your operating system supports it. Discussion
While SysV IPC (shared memory, semaphores, etc.) isn't as widely used as pipes, named pipes, and sockets for interprocess communication, it still has some interesting properties. Normally, however, you can't expect to use shared memory via
The CPAN module
IPC::Shareable takes care of that. Using a clever Example 16.11 is a simple demonstration of the module. Example 16.11: sharetest#!/usr/bin/perl # sharetest - test shared variables across forks use IPC::Shareable; $handle = tie $buffer, 'IPC::Shareable', undef, { destroy => 1 }; $SIG{INT} = sub { die "$$ dying\n" }; for (1 .. 10) { unless ($child = fork) { # i'm the child die "cannot fork: $!" unless defined $child; squabble(); exit; } push @kids, $child; # in case we care about their pids } while (1) { print "Buffer is $buffer\n"; sleep 1; } die "Not reached"; sub squabble { my $i = 0; while (1) { next if $buffer =~ /^$$\b/o; $handle->shlock(); $i++; $buffer = "$$ $i"; $handle->shunlock(); } } The starting process creates the shared variable, forks off 10 children, and then sits back and prints out the value of the buffer every second or so, forever, or until you hit Ctrl-C. Because the SIGINT handler was set before any forking, it got inherited by the squabbling children as well, so they'll also bite the dust when the process group is interrupted. Keyboard interrupts send signals to the whole process group, not just one process.
What do the kids squabble over? They're bickering over who gets to update that shared variable. Each one looks to see whether someone else was here or not. So long as the buffer starts with their own signature (their PID), they leave it alone. As soon as someone else has changed it, they lock the shared variable using a special method call on the handle returned from the
The program runs much faster by commenting out the line that starts with
The The IPC::Sharable module also supports sharing variables among unrelated processes on the same machine. See its documentation for details. Copyright © 2002 O'Reilly & Associates. All rights reserved. |
|