Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Re: Re: Win32::MMF - Memory Mapped File Support for Perl

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


in reply to Re: Re: Re: Win32::MMF - Memory Mapped File Support for Perl
in thread (Updated Again) Win32::MMF - Memory Mapped File Support for Perl

Thanks xiper++ once again for the excellent feedback! :-D

I have made several improvements after reading your comments...

1) no explicit Init statement required
The default MMF namespace (shared memory) will be created if the user has not explicitly provided namespace parameters. Once initialized, all subsequent tie() will use the default namespace, unless otherwise specified in the parameter list to tie().
use strict; use Win32::MMF::Shareable; use Data::Dumper; my $ns = tie my $ref, 'Win32::MMF::Shareable', '$ref'; tie my $alias, 'Win32::MMF::Shareable', '$ref'; $ref = { a => 1, b => 2, c => 3 }; $alias->{d} = 4; print Dumper($ref); $ref = [ qw( a b c d e f g h ) ]; print Dumper($alias); # $ns->debug();
__OUTPUT__ $VAR1 = { 'c' => '3', 'a' => '1', 'b' => '2', 'd' => 4 }; $VAR1 = [ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' ];

2) Namespace consideration
Win32::MMF has a slightly different design to IPC::Shareable in shared memory management. The following diagram is a view of a MMF namespace:
+-----------------+ | Var1 definition | +-----------------+ | Var2 definition | +-----------------+<- Heap Top | | | | | | | | | | | | | | | | |-----------------|<- Watermark | | | | | | | | +-----------------+<- Heap Bottom | MMF Descriptor | +-----------------+
Every MMF created carries a MMF descriptor table that contains information about the size of the MMF, number of variables declared inside the namespace and heap information. Variable definition starts from the top of MMF and grows downward. The variable definition carries a pointer if there is associated data for the variable. Memory is allocated from the heap bottom and grows upward to hold the actual data. When a variable is deleted, the MMF manager deletes the variable definition and frees the memory allocated to the variable data.

A MMF contains everything a process need to know about, because all the memory management information is carried inside the MMF itself. So that when one process adds variable, another process immediately sees it, and can use it without any additional initialization steps.

The tie() wrapper does not directly control how a variable is stored inside the MMF. It doesn't care how much memory is required to store a particular tied variable, the MMF manager automatically shrinks or grows the memory allocated to the variable, as long as there is still enough memory left in the heap.

# key ĘC same as your 'varid', suggest you use this, or at least accept it as an alias

The Win32::MMF::Shareable module is designed as a drop-in replacement to IPC::Shareable under Windows, you can use it the same way as IPC::Shareable. Options like mode, destroy, are ignored. I have created the copied the following demo from then IPC::Shareable demo in the POD, replaced use IPC::Shareable to automatically switch between Win32::MMF::Shareable and IPC::Shareable on different platform, leaving the rest of the code untouched.
use strict; # ---------------------------------------------------------------- # code to switch between Win32::MMF::Shareable and IPC::Shareable # automatically on different platforms # ---------------------------------------------------------------- use vars qw/ $Shareable /; BEGIN { $Shareable = ($^O eq 'MSWin32') ? 'Win32::MMF::Shareable' : 'IPC::Shareable'; eval "require $Shareable"; $Shareable->import(); } # ---------------------------------------------------------------- my $glue = 'data'; my %options = ( create => 0, exclusive => 0, mode => 0644, destroy => 0, ); my %colours; tie %colours, $Shareable, $glue, { %options } or die "client: tie failed\n"; foreach my $c (keys %colours) { print "client: these are $c: ", join(', ', @{$colours{$c}}), "\n"; } delete $colours{'red'}; exit;

# The two modules Win32::MMF and Win32::MMF::Shareable, while implemented in a similar way, behave quiet differently to the user. It may be a good idea to document each separately to avoid confusion.

Will do.

# In the pod: "Because memory and variable management are managed internally by the Win32::MMF module, you do not need to specify how much memory is required by the variable." Is this correct? You don't have to specify a size for a tied variable? Will it automatically expand if the data grows? If so, why not have this for non-tied variables as well?

That's correct, the MMF manager decides the correct amount of memory required inside the MMF to hold the object, so it can be correctly recreated from another process. This applies to untied variables too.

# How does your module handle the issue of when to clear the data in shared memory? I assume that it's when the last 'connected' process exits or tied variable falls out of scope, correct? What about if the process is killed (via a SIGTERM)? Any possible memory leaks? (I assume you've already covered these issues, just interested to know).

Currently it's up to Windows own memory management system to handle a killed process. Windows will destroy the MMF if all processes accessing it are killed. (A least I hope so.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2023-04-01 08:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?