Hi, after being edified in Mmap question, I used Inline::C to actually share memory segments on linux. This is just an "educational proof of concept, but could be useful. The AdvancedLinuxProgramming Guide says that shared_memory_segments are the fastest ipc available, since the 2 programs share actual memory. The code below consists of a sender and reader. The c libs seem to want to use char strings to write to shared_mem, so that is why I have an "atoi" scheme in the reader. You can also make the segment size, HUGE if desired, just change the 0x800 to 0x64000 ( or whatever). When you first run them, run the init script first, and feed the shmid output of it as $ARGV[0] to the listener. Otherwise the listener will compile , but fail due to having the wrong shmid to work with. Then it must be restarted with a valid shmid.

It seems like this could be made into a module with XS, maybe this will be where I learn it. :-)

# shmem-init.pl # run first and feed shmid output # as $ARGV[0] to listener below ###################################################### #!/usr/bin/perl -w use Inline C; use strict; my $go = 1; $SIG{INT} = sub{ $go = 0; &detach_m(); #detach the shared mem &close_m() #close up the shared mem }; my $segment_id = init_m(); print "shmid-> $segment_id\n"; my $i = 0; while($go){ send_m($i); $i++; select(undef,undef,undef,.01); last if ! $go; } __END__ __C__ /* code adapted from */ /* http://www.advancedlinuxprogramming.com/alp-folder */ #include <stdio.h> #include <sys/shm.h> #include <sys/stat.h> int segment_id; char* shared_memory; struct shmid_ds shmbuffer; int segment_size; const int shared_segment_size = 0x800; int init_m() { /* Allocate a shared memory segment. */ segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); printf ("shared memory attached at address %p\n id->%d\n", shared_memory,segment_id); /* Determine the segment's size. */ shmctl (segment_id, IPC_STAT, &shmbuffer); segment_size = shmbuffer.shm_segsz; printf ("segment size: %d\n", segment_size); return(segment_id); } int send_m (int i) { /* Write a string to the shared memory segment. */ sprintf (shared_memory, "%d",i); return 0; } int detach_m(){ /* Detach the shared memory segment. */ shmdt (shared_memory); return 0; } int close_m(){ /* Deallocate the shared memory segment. */ shmctl (segment_id, IPC_RMID, 0); return 0; } ##################################################### ##################################################### ###################################################### ##################################################### # shmem-listen needs a valid shmid as $argv[0] #!/usr/bin/perl -w use Inline C; use strict; my $go = 1; my $segment_id = $ARGV[0] + 0; # make sure it's integer $SIG{INT} = sub{ $go = 0; &detach_m(); exit; }; init_m($segment_id); while($go){ my $readm = read_m(); print "$readm\n"; last if ! $go; select(undef,undef,undef,.01); } __END__ __C__ /* code adapted from */ /* http://www.advancedlinuxprogramming.com/alp-folder */ #include <stdio.h> #include <stdlib.h> #include <sys/shm.h> #include <sys/stat.h> char* shared_memory; struct shmid_ds shmbuffer; int segment_size; const int shared_segment_size = 0x800; int init_m( int segment_id ) { /* get shared memory segment. */ printf("Your shmid is .... %d\n", segment_id ); /* Attach the shared memory segment. */ shared_memory = (char*) shmat (segment_id, 0, 0); printf ("shared memory attached at address %p\n", shared_memory); /* Determine the segment's size. */ shmctl (segment_id, IPC_STAT, &shmbuffer); segment_size = shmbuffer.shm_segsz; printf ("segment size: %d\n", segment_size); printf ("shared memory attached at address %p\n", shared_memory); printf ("%d ->id Check ipcs now\n",segment_id); return 0; } int read_m(){ int r = atoi(shared_memory); return(r); } int detach_m(){ /* Detach the shared memory segment. */ shmdt (shared_memory); return 0; }

In reply to shared mem segments with Inline::C by zentara

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.