Shared memory with PHP and C

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vigniri
Forum Newbie
Posts: 3
Joined: Fri Jul 15, 2005 5:41 am

Shared memory with PHP and C

Post by vigniri »

hello,

I'm trying to use shared memory from two applications, one written in php and the other one in C.

Both applications can write and read their own data to the shared memory but I can't get the PHP app to read what the C app wrote.

I'm using the PHP function shm_get_var() (http://is.php.net/manual/en/function.shm-get-var.php) but I can't figure out what value I need for the variable_key parameter..

here is the code:

PHP:

Code: Select all

if ($id = shm_attach(0x0000162e, 1000, 0666))
	echo &quote;attahed to shared memory<br>&quote;;
else
	echo &quote;error attaching to shared memory<br>&quote;;

echo shm_get_var($id,0);
echo &quote;<br>&quote;;
echo shm_get_var($id,1);
C:

Code: Select all

#define SHMSZ	1000
int shmid, c, d;
key_t key;
char *shm, *s, *rcv;
key = 5678;
   
// create the segment.
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) 
{
	perror(&quote;shmget&quote;);
        exit(1);
}
else
	printf(&quote;Shared memory segment successfully created\n&quote;);
   
// attach to segment
if ((shm = shmat(shmid, NULL, 0)) == -1)
{
        perror(&quote;shmat&quote;);
        exit(1);
}
else
	printf(&quote;Attachment to shared memory: successful\n&quote;);
	shm&#1111;0] = 'a';
	shm&#1111;1] = 'b';
for (c = 0; c<2;c++) 
{
	printf(&quote;wrote: s&#1111;%d]=%c to memory\n&quote;, c, shm&#1111;c]);	
	usleep(1000000);
}

usleep(3000000);

// locate the segment
if ((shmid = shmget(key, SHMSZ, 0666)) < 0) 
{
	perror(&quote;shmget&quote;);
        exit(1);
}

// attach the segment to our data space.
if ((rcv = shmat(shmid, NULL, 0)) == (char *) -1)
{
       perror(&quote;shmat&quote;);
       exit(1);
}

// read what the server put in the memory
for (c = 0; c<2;c++) 
{
	printf(&quote;read: s&#1111;%d]=%c from memory\n&quote;, c, rcv&#1111;c]);
	usleep(1000000);
}

return(0);
any help would be appreciated
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Pardon me if I'm wrong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here. I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($ildn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.rong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once agarong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.uldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.iable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.ne some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.p]

...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.off base here.been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here. shm_get_var($id, 5678);


Once again, it's been some time so I apologize if I'm off base here.22]

...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.

Once again, it's been some time so I apologize if I'm off base here.rong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.ce I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once aga_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.ince I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.1fd5522]

...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here. if I'm off base here." variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.en a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.rong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.red memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here shm_get_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.rong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some tr($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.hp]
echo shm_get_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.cho shm_get_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.rong as it's been a while since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.e since I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.fd5522]
echo shm_get_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
[/php:1:f6 declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.ce I've done some shared memory stuff. Wouldn't be the "key" variable that you declared in the C code? So...

Code: Select all

echo shm_get_var($id,0);
...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.
echo shm_get_var($id,0);


...would be...

Code: Select all

echo shm_get_var($id, 5678);
Once again, it's been some time so I apologize if I'm off base here.
vigniri
Forum Newbie
Posts: 3
Joined: Fri Jul 15, 2005 5:41 am

Post by vigniri »

BDKR, thank you very much for taking the time to reply to my message.

the key variable in the C code gives the shared memory the key "0x0000162e" wich I use to attach to the memory in the PHP code.

I need a key to access specific variables in the memory, like the ones i declare in the following code..

Code: Select all

shm&#1111;0] = 'a';
shm&#1111;1] = 'b';
I thought the keys would simply be 0 and 1 but that doesn't seem to be the case.
vigniri
Forum Newbie
Posts: 3
Joined: Fri Jul 15, 2005 5:41 am

Post by vigniri »

anyone?
Post Reply