Question: I am trying to use a message queue for communication between two unrelated processes in Linux. I am aware that using a common key value will allow us to open the same message queue in both the unrelated processes. But the problem is that at times a key value corresponding to a message queue might already be used by some system utility. So I am using IPC\_CREAT | IPC\_EXEL flags to ensure that a new message queue is compulsorily created.

In case msgget() returns an error, I increment the key value and retry to create a new message queue, till it succeeds. Now, the problem is how do I communicate this successful key value to the other unrelated process which wants to operate on the same message queue.

Am I missing something? Has anyone faced similar issues before?

Answer: 
IIRC, this is exactly the problem that the [`ftok(3)`](http://www.opengroup.org/onlinepubs/009695399/functions/ftok.html) is used to solve. The programs that need to communicate simply use a common path and key value. We used to pass the name of a FIFO on the command line to our programs. They would pass this into `ftok()` to generate the key for our shared memory segments.
You could simply move into the 21st century and use a Posix message queue rather than a sysV one. Posix queues have names rather than obscure numbers. Under linux you can mount a filesystem to see them and remove them with "rm".