Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

IPC::Sharable ... curious ipcs -a results

by andyok (Initiate)
on May 27, 2021 at 16:05 UTC ( #11133147=perlquestion: print w/replies, xml ) Need Help??

andyok has asked for the wisdom of the Perl Monks concerning the following question:

Consider the following:
use IPC::Shareable; use IPC::SysV qw(ftok); $ftok = ftok($0); printf("ftok=$ftok (0x%08X)\n", $ftok); $tied = tie %Sess, 'IPC::Shareable', {key => $ftok, mode => 0666, crea +te=>"yes"};

Before executing the above, I do an ipcs -a

It tells me that there are no Semaphore Arrays and only a handful of shared-memory segments.

I invoke the perl-debugger and execute the above code (including the tie).

ftok() gives me 0x01003A83

I now do another ipcs -a ... and now I see:

 Shared-memory-segment with key 0x01003A83 and size 65536 bytes

 Semaphore-array with the same key and 3 semaphores

Question-1: is it normal that a semaphore is created when a shared-memory-segment is set up?

In the debugger, I now do $Sess{abc} = 1

There is no change to what is reported by ipcs -a

However, if I do now $Sess{a}{b}{c}{d}{e} = 1

I now find 4 more 65536 shared-memory-segments created and a load of corresponding semaphores.

Question-2:Does that seem reasonable?

It just seems very curious that the originally created shared-memory segment doesn't simply grow as the data-structure %Sess gets populated

And the %Sess isn't heavily populated, yet I now have five 65536-byte segments, each with it's own semaphore ... very curious?

Replies are listed 'Best First'.
Re: IPC::Sharable ... curious ipcs -a results
by stevieb (Canon) on May 27, 2021 at 18:18 UTC

    1) Yes. The semaphore is set up at the same time as the shared memory segment.

    2) Yes. What happens with IPC::Shareable is that in nested data structures, the nested data isn't stored in the initially created shared memory segment. Only the initial variable is. Each new element is contained within its own new segment.

    Disclaimer: I'm a co-maintainer of the distribution in question.

Re: IPC::Sharable ... curious ipcs -a results
by andyok (Initiate) on May 27, 2021 at 18:49 UTC
    ...and the point of your completely irrelevant post is, what?

    This is my first foray into IPC::Shareable ... and I wanted to be sure that my script was using system-resources (shared-memory) in a reasonable way.

    The %Sess hash could grow to have perhaps 100 entries .... and if each entries causes another 64Kbyte shared-memory segment to be created, that would probably be not a good idea.

    I couldn't understand why semaphores were being created and why, even though the hash was quite small, so many 64Kbyte segments were being created.

    It is now clear that I need to be a bit more sophisticated in what I store in the shared-memory segment ...

    ... especially if %Sess could have many many entries

    For example, perhaps it might be better to convert the hash to a string (using Data::Dumper) and it is the string that is stored in the shared-memory segment.

    Thank you for the explanation.

      Mind the threading in the replies.

      ...and the point of your completely irrelevant post is, what?

      was not a reply to your original post, but to some anonymous noise.

      Greetings,
      -jo

      $gryYup$d0ylprbpriprrYpkJl2xyl~rzg??P~5lp2hyl0p$
        was not a reply to your original post, but to some anonymous noise.

        Absolutely correct! I was definitely not responding to you with that statement, andyok!

      For example, perhaps it might be better to convert the hash to a string (using Data::Dumper) and it is the string that is stored in the shared-memory segment.

      Personally, I use JSON to store my data. I like the ease of it, and its especially good because it's cross platform and stores well in files if you want to dump the data for future program runs. Here's a non-complete example:

      use warnings; use strict; use JSON; my $shared_memory_variable = ...; # Create shared mem with IPC::Sharea +ble my $data = { a => 1, b => [ 1, 2, 3 ], c => { z => 99, y => 98, x => { m => 'hello, world!' }, }, }; # Create JSON string and store it in the shared memory allocation $shared_memory_variable = encode_json $data; # Restore the shared memory JSON to a Perl data structure my $perl = decode_json $shared_memory_variable;
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2021-09-22 03:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?