CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
IPC::ShareLite

 



rockbird
New User

Feb 24, 2013, 3:34 PM

Post #1 of 3 (729 views)
IPC::ShareLite Can't Post

My main objective has been to share data between perl and C.
I have managed to pass one scalar at a time by using the c program to examine the data the IPC::ShareLite has put in the common memory.
I have two questions:
1) Where would I find an explanation of the byte usage by IPC::ShareLite? For example when perl writes one scalar, starting at the beginning of the common memory I see:
0x00
0xff
0xff
0xff
0x04 ( by deduction the number of bytes in the payload)
0x00
0x00
0x00
0x01
0x00
0x00
0x00
0x02
0x00
0x00
0x00
0x31 The beginning of the payload "1234"
0x32
0x33
0x34
0x00 and the rest of the shared memory all zeroes

My simple solution takes the string at byte 16 (zero relative) and ignores everything before it. if I understood the significance of the of the first 16 bytes it would help understanding and possibly a better solution.
I have attached a simplified version of my perl and C source. In this instance simply sending '9876' from perl to c and then c printing the memory contents and the parsed integer value. IPC1b.pl calls the C

2) Has anyone any suggestions for a more flexible solution? Ideally the contents of a struct could be passed in both directions.
Attachments: IPCb.c (2.10 KB)
  IPC1b.pl (0.56 KB)


lightspd
Novice

Feb 25, 2013, 3:24 PM

Post #2 of 3 (709 views)
Re: [rockbird] IPC::ShareLite [In reply to] Can't Post

Having never seen that issue before, my best guess would that it has to do with IPC::ShareLite.

Doing a simple test with IPC::SysV, simply because I've used it before.


Code
#!/usr/bin/perl -w 

use strict;
use IPC::SysV qw(IPC_CREAT IPC_RMID);

my $key = 12345;
my $size = 80;
my $message = "9876";

my $id = shmget($key, $size, &IPC_CREAT | 0666) or
die "Can't shmget: $!";

shmwrite( $id, $message, 0, $size ) or die "Can't shmwrite: $!";

sleep(1);

system("./cb $key $size ");

sleep(5);

shmctl( $id, &IPC_RMID, 0 ) or die "Can't shmctl: $! ";


To save space I'm just pasting a editing down main function. Will still work, just no checks.


Code
int main(int argc, char *argv[]) 
{

key_t key;
int msize;
char *shm;
int shmid, count=0;

key=atoi(argv[1]);
printf("key=%d\n",key);
msize=atoi(argv[2]);
printf("Size=%d\n",msize);

// connect to the shared memory

shmid = shmget(key,msize ,0666);
shm = shmat(shmid, (void *)0, 0);
printf("Trying to send data\n");
printf("%s\n", shm);

return(0);
}


output:
perl test.pl
key=12345
Size=80
Trying to send data
9876


rockbird
New User

Feb 26, 2013, 11:42 AM

Post #3 of 3 (693 views)
Re: [lightspd] IPC::ShareLite [In reply to] Can't Post

Thanks for that. I was very helpful and worked well.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives