I want to use a message queue to send from PHP to a C program under RedHat Linux. I do an ftok in C and PHP, and then msgget in C and msg_get_queue in PHP. If the PHP msg_get_queue occurs first, everything works - the C program finds the same queue. If the C program msgget occurs first, the PHP msg_get_queue fails, saying that the queue already exists. Both specify the same permissions. Apparently the PHP function cannot connect to an existing queue? That seems to be the behavior.
There is the PHP function msg_queue_exists, but this does not seem to help, because it just gives you a boolean result.
Problem using message queue with Linux C prog
Moderator: General Moderators
-
counterlink
- Forum Newbie
- Posts: 4
- Joined: Sun Nov 13, 2011 7:37 pm
Re: Problem using message queue with Linux C prog
How are you getting the KEY for msg_get_queue to connect to an existing queue? Perhaps that is failing. Someone posted in the manual:
The PHP source code (file sysvmsg.c) reveals that only if msgget(key,0) fails, msgget(key, IPC_CREAT | IPC_EXCL | perms) is used.
-
counterlink
- Forum Newbie
- Posts: 4
- Joined: Sun Nov 13, 2011 7:37 pm
Re: Problem using message queue with Linux C prog
I get the key by using the ftok in both places with a stable file name as parameter. I have verified that the same key is generated by both programs (C and PHP).
Perhaps I should look at the PHP source code. If PHP or's in IPC_EXCL, that would cause it to fail. But you say PHP first tries msgget(key,0) - that should not fail if the queue already exists with the right permissions (O666).
Perhaps I should look at the PHP source code. If PHP or's in IPC_EXCL, that would cause it to fail. But you say PHP first tries msgget(key,0) - that should not fail if the queue already exists with the right permissions (O666).
Re: Problem using message queue with Linux C prog
I'm just sort of guessing because I don't know your code. Have you tried looking at a few of the manual's examples? It seems other people have gotten this to work at least from the PHP side. I haven't ever tried doing exactly how you are trying to use it from C and using a shared key.