views:

99

answers:

4

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.

+2  A: 

You could try using the process id. My google foo got this

Joe Soul-bringer
+2  A: 

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.

James Anderson
thank you for the advice! will keep in mind every time I use ftok
Saifis
A: 

Looking globally unique ids usually called Guid or Uuid. There must be a library you can use to generate them. They are unique strings made from your nic address, the current time, and a random number.

BeWarned
A: 

How about the clock? WikiPedia say's it's better than RDTSC (and SMP safe).

"Under Linux, similar functionality is provided by reading the value of CLOCK_MONOTONIC clock using POSIX clock_gettime function."

RandomNickName42