I have made a Linux CUI app that communicates between processes via Message-quene.
There is no problem with it as long as it is a single instance. However when there are multiple instances of the same app, the messages in the quene get sent to the wrong instance.
I understand this can be avoided by giving the msgget function a unique key. Using ftok() to create a key, but since the variables are the same they result in identical keys.
Can someone guide me how to have a unique key for each instance?
The only idea I have now is to randamize the variable given to ftok, and I know that cant be right.
Be careful with ftok!
This will only be unique for a given file system and only if then if the file system is not heavily used. fttok is driven by the file entry number in the file system.
This used to be a pretty good way of getting unique values but time and Moores law caught up with it a few years ago. It works on the lower 8 bits of the file number but the actual file number is now 32 bits and numbering starts again for each file system.
Process id is a pretty good choice, they do get re-cycled but not as long as the process is still alive.