Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Re: Shared variables under win32?

by Roger (Parson)
on Feb 06, 2004 at 16:51 UTC ( #327140=note: print w/replies, xml ) Need Help??

in reply to Shared variables under win32?

I have written a proper Win32:MMF module for shared memory support under Windows. I have tested it under .Net and VC 6. It works under Windows NT, 2000 and XP. I haven't tested it under 98, but who cares about 98's anyway?

The following is a snip on using the module mentioned -
use strict; use warnings; use Win32::MMF; use Data::Dumper; use CGI; # fork a process defined(my $pid = fork()) or die "Can not fork a child process!"; if ($pid) { my $ns1 = Win32::MMF->new ( -namespace => "My.data1" ); my $ns2 = Win32::MMF->new ( -namespace => "My.data2" ); my $cgi = new CGI; my $data = {a=>[1,2,3], b=>4, c=>"A\0B\0C\0"}; $ns1->write($data); # automatic locking by default $ns2->write($cgi); print "--- Sent ---\n"; print Dumper($data), "\n"; print Dumper($cgi), "\n"; sleep(1); } else { # in child sleep(1); my $ns1 = Win32::MMF->new ( -namespace => "My.data1", -reuse => 1 ) or die "Namespace does not exist!"; my $ns2 = Win32::MMF->new ( -namespace => "My.data2", -reuse => 1 ) or die "Namespace does not exist!"; my $data = $ns1->read(); my $cgi = $ns2->read(); print "--- Received ---\n"; print Dumper($data), "\n"; print Dumper($cgi), "\n"; print "--- Use Received Object ---\n"; # use the object from another process :-) print $cgi->header(), $cgi->start_html(), "\n", $cgi->end_html(), "\n"; }

The above is just a simple demo on the module. To impliment something like what you have suggested -
# turn off built-in auto-locking my $ns = Win32::MMF->new ( -namespace => "My.data1", -size => 2*1024*1024, -autolock => 0 ); - use Win32::Semaphore module - Create a read and a write semaphore. - In server: wait($read_semaphore); $xml = $ns->read(); release($write_semaphore); - In client: wait($write_semaphore); $ns->write($xml); release($read_semaphore);

You can also pass blessed objects from one process into another and pick up execution in another process.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://327140]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2022-08-10 11:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found