Page 1 of 1
Problem using message queue with Linux C prog
Posted: Wed Dec 21, 2011 12:42 am
by counterlink
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.
Re: Problem using message queue with Linux C prog
Posted: Wed Dec 21, 2011 9:01 am
by Eric!
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.
Re: Problem using message queue with Linux C prog
Posted: Wed Dec 21, 2011 1:21 pm
by counterlink
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).
Re: Problem using message queue with Linux C prog
Posted: Wed Dec 21, 2011 2:33 pm
by Eric!
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.