为可以有多个实例的应用程序创建惟一的消息quene键

时间:2021-01-05 08:44:03

I have made a Linux CUI app that communicates between processes via Message-quene.

我制作了一个Linux CUI应用,通过消息-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.

只要是一个实例,就没有问题。然而,当同一应用程序有多个实例时,quene中的消息会被发送到错误的实例。

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.

我知道可以通过给msgget函数一个唯一的键来避免这种情况。使用ftok()创建一个密钥,但是由于变量是相同的,它们会产生相同的密钥。

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.

我现在唯一的想法是将给ftok的变量随机化,我知道这是不对的。

4 个解决方案

#1


2  

Be careful with ftok!

小心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.

这只对给定的文件系统是唯一的,并且只有在文件系统没有被大量使用的情况下。fttok由文件系统中的文件条目号驱动。

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.

这曾经是获得独特价值的一种很好的方式,但是时间和摩尔定律在几年前赶上了它。它在文件号的下8位上工作,但是实际的文件号现在是32位,每个文件系统的编号都重新开始。

Process id is a pretty good choice, they do get re-cycled but not as long as the process is still alive.

进程id是一个不错的选择,它们确实会被重新循环,但只要进程还活着,就不会被循环使用。

#2


2  

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

你可以用进程id,我的谷歌foo得到这个

#3


0  

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.

查找全局惟一的id,通常称为Guid或uid。必须有一个可以用来生成它们的库。它们是由nic地址、当前时间和随机数组成的唯一字符串。

#4


0  

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

钟怎么样?*说它比RDTSC(和SMP保险箱)要好。

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

“在Linux下,通过使用POSIX clock_gettime函数读取clock_单调时钟的值,可以提供类似的功能。”

#1


2  

Be careful with ftok!

小心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.

这只对给定的文件系统是唯一的,并且只有在文件系统没有被大量使用的情况下。fttok由文件系统中的文件条目号驱动。

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.

这曾经是获得独特价值的一种很好的方式,但是时间和摩尔定律在几年前赶上了它。它在文件号的下8位上工作,但是实际的文件号现在是32位,每个文件系统的编号都重新开始。

Process id is a pretty good choice, they do get re-cycled but not as long as the process is still alive.

进程id是一个不错的选择,它们确实会被重新循环,但只要进程还活着,就不会被循环使用。

#2


2  

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

你可以用进程id,我的谷歌foo得到这个

#3


0  

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.

查找全局惟一的id,通常称为Guid或uid。必须有一个可以用来生成它们的库。它们是由nic地址、当前时间和随机数组成的唯一字符串。

#4


0  

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

钟怎么样?*说它比RDTSC(和SMP保险箱)要好。

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

“在Linux下,通过使用POSIX clock_gettime函数读取clock_单调时钟的值,可以提供类似的功能。”