Problem using message queue with Linux C prog

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
counterlink
Forum Newbie
Posts: 4
Joined: Sun Nov 13, 2011 7:37 pm

Problem using message queue with Linux C prog

Post 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.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem using message queue with Linux C prog

Post 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.
counterlink
Forum Newbie
Posts: 4
Joined: Sun Nov 13, 2011 7:37 pm

Re: Problem using message queue with Linux C prog

Post 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).
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: Problem using message queue with Linux C prog

Post 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.
Post Reply