在 linux 中使用 ftok() 最多可以创建多少个共享内存密钥?

How many maximum shared memory keys can be created using ftok() in linux?

我正在使用 ftok() 创建共享内存密钥。我想为 ftok() 的每个手册页创建 500 个共享内存 keys.As 提到低位 8 位很重要。

是不是意味着我们最多只能生成256个share memory key? 如果是,那么如何生成超过256个密钥?

因为参数 ftok() 被定义为 proj_id 接受 int 参数:-

key_t ftok(const char *pathname, int proj_id);

所以它应该生成 2^02^31 个具有相同路径名的唯一键。

ftok 可以创建的最大键数不受ftok 的整数参数限制。它也取决于整数参数和路径参数。即使只有int自变量的低8位有意义,理论上路径的数量也是不可胜数的。

ftok 返回的键是 key_t 类型,即 __S32_TYPE,这是一个 int。所以这可以从 2^0 to 2^31int 有 4 个字节的实现上进行。

根据 ftok 的手册页:

The ftok() function shall return the same key value for all paths that name the same file, when called with the same id value, and return different key values when called with different id values or with paths that name different files existing on the same file system at the same time. It is unspecified whether ftok() shall return the same key value when called again after the file named by path is removed and recreated with the same name.

但是正如其他人在评论中指出的那样,创建这么多进程是不可取的。