Saifis
Saifis

Reputation: 2237

Creating unique keys for a message quene for an app that can have multiple instances

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.

Upvotes: 2

Views: 420

Answers (4)

RandomNickName42
RandomNickName42

Reputation: 5955

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."

Upvotes: 0

BeWarned
BeWarned

Reputation: 2338

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.

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

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.

Upvotes: 2

Joe Soul-bringer
Joe Soul-bringer

Reputation: 3304

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

Upvotes: 2

Related Questions