Solved

Hash of Hashes Shared Across Parallel Forks Orphans Memory

Posted on 2003-11-04
6
1,622 Views
Last Modified: 2012-06-27
I have a simplified example of a problem I'm trying to solve (below).  Up
until recently, I have had much success with the IPC::Shareable module for
sharing variables across parallel forks.

Unfortunately, when I use a hash of hashes with IPC::Shareable, I get
orphaned shared memory segments.  If I remove the line that says
$hash{'test'}{'test'} = 'test', then the script runs without orphaning
memory...  The maintainer of the module no longer lives at the listed email
address, and if I could get a hash of hashes to share across parallel
forks, it would reduce my run-times dramatically...

Any thoughts for another way short of Open2, udp datagrams, or any other
such ugliness?

Broken code below:
---
!/usr/bin/perl -w

use lib 'site_perl';
use IPC::Shareable(':all');
use Parallel::ForkManager;

my $forkman = new Parallel::ForkManager(1);

my $key;
my %hash;
my %options = (
                 create => 'yes',
                 mode => 0644,
                 exclusive => 0,
                 destroy => 'yes'
               );
my $index;

my $HANDLE = tie %hash, 'IPC::Shareable', 'data', { %options };

for($index=1; $index <= 7; $index++) {

    print "Spawning process: $index\n";

    $forkman->start and next;
    do_this($index);
    $forkman->finish;

}

$forkman->wait_all_children;

print "All children are done\n";

foreach $key (sort keys %hash) {

    print "PID of process $key = $hash{$key}\n";

}


sub do_this {
    my $index = $_[0];

    $HANDLE->shlock();
    $hash{$index} = "PID: $$";
    $hash{'test'}{'test'} = 'test';
    $HANDLE->shunlock();

    return 0;

}
0
Comment
Question by:ventolin
6 Comments
 
LVL 2

Author Comment

by:ventolin
Comment Utility
See the man pages for 'ipcs' and 'ipcrm' for more info.
0
 
LVL 2

Expert Comment

by:icrf
Comment Utility
Did you ever get this one figured out? I found it and started playing today and didn't make much headway.

If I manually "tie %{$hash{'test'}}" before forking, it doesn't leak, but according to the docs, it's supposed to automagically tie references. If I let it automagically create it and then I manually destroy it "(tied %{$hash{test}})->remove" when it's all over, it doesn't leak. It sounds like the destory option isn't recursing through the tree as it should, but even calling clean_up or clean_up_all doesn't help, and they're supposed to destroy everything the current process created or encoutered.

Unless I'm implementing wrong, there's some serious issues with that module cleaning up after itself. Granted, it's designed for interprocess communication and it leaves the variables available for future processes to use, but they should go when implicitly told.
0
 
LVL 20

Expert Comment

by:jmcg
Comment Utility
Nothing has happened on this question in more than 6 weeks. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by icrf [grade B] (advice but not a solution).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 2

Author Comment

by:ventolin
Comment Utility
You guys can go ahead an clean this up, but it still hasn't been answered to my satisfaction.
0
 
LVL 20

Expert Comment

by:jmcg
Comment Utility
Ventolin,

You have the option of requesting the question be closed (I'd recommend as a PAQ) and your points refunded. Just post a 0-point question in Community Support and a moderator will take a look.

I agree that you haven't been given a fully satisfactory answer, but after looking at the documentation myself and reading about icrm's experiments, I judged that he had done some work worth rewarding. I generally recommend a B grade on "answers" that are only steps along the road to an apparent solution. You, as the asker, can certainly disagree and the moderators are likely to give your desires more weight than my recommendation.

You might also like to try asking again, perhaps with more details. There has been a recent influx of new experts who might be interested in your problem.
0
 

Accepted Solution

by:
PashaMod earned 0 total points
Comment Utility
PAQed, with points refunded (500)

PashaMod
Community Support Moderator
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This video discusses moving either the default database or any database to a new volume.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now