IPC::Sharable

P

Peteris Krumins

To keep statistics which are collected by
parent and child processes I decided to
take the advantage of shared memory (shm) and
share some variables (scalars and hashes).

The parent forks lots of child processes (depending
on load 100 to 1000 an hour), which
increase some counters and create hash data structures.

The problem is that after couple of hours looking
at `ipcs' output I see no shm is released, new memory
is allocated and the list grows larger and larger.
I am afraid to run out of memory.
Once an hour the program received USR1 signal
upon which it dumps the statistics to a file and
(imo) frees memory.

the program structure is like following:

my %stats;
my $stats_handle;
my $stats_handle_chkd;
my $stats_handle_rej;
my ($stats_rcvd, $stats_chkd, $stats_rej);

# ...
# make variables shared
#

$stats_handle_chkd = tie $stats_chkd, 'IPC::Shareable',
undef, { destroy => 1 };
$stats_handle_rej = tie $stats_rej, 'IPC::Shareable',
undef, { destroy => 1 };
$stats_handle = tie %stats, 'IPC::Shareable',
undef, { destroy => 1 };
($stats_rcvd, $stats_chkd, $stats_rej) = (0,0,0);

# ...
# loop until <something> {
# ...

$stats_rcvd++;

# ...

fork

here increase $stats_chkd, $stats_rej,
and do smth with hash, for example
$stats{'in'}{'user_one'}++;

exit child

# }

# and the signal USR1 handler just:
#
sub usr1_handler {
# dump data to file
# ...

($stats_rcvd, $stats_chkd, $stats_rej) = (0,0,0);
%stats = ();
}


P.Krumins
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,797
Messages
2,569,647
Members
45,378
Latest member
danzeev

Latest Threads

Top