简体   繁体   English

用于生成ftok()键的公式是什么?

[英]What is the formula used to produce a ftok() key?

What is the formula used to produce a key that ftok() produces? 用于生成ftok()生成的键的公式是什么? ftok is a Linux function for creating keys for SYSTEM V IPC. ftok是一个用于为SYSTEM V IPC创建密钥的Linux功能。

In ftok from glibc 2.29 : 来自glibc 2.29的ftok

key_t
ftok (const char *pathname, int proj_id)
{
  struct stat64 st;
  key_t key;

  if (__xstat64 (_STAT_VER, pathname, &st) < 0)
    return (key_t) -1;

  key = ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16)
     | ((proj_id & 0xff) << 24));

  return key;
}

Ie it's creating a 32-bit key_t by taking the upper 8 bits from the lower 8 bits of proj_id , the second upper 8 bits from the lower 8 bits of the device number of the provided pathname , and the lower 16 bits from the lower 16 bits of the inode number of the provided pathname . 也就是说,它通过从proj_id的低8位获取高8位来创建32位key_t ,从提供的pathname的设备编号的低8位获取第二高8位,从低16获取低16位提供的pathname的inode编号的位。

musl libc uses the same algorithm: musl libc使用相同的算法:

key_t ftok(const char *path, int id)
{
    struct stat st;
    if (stat(path, &st) < 0) return -1;

    return ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16) | ((id & 0xffu) << 24));
}

The ftok() source code in glibc library is : glibc库中的ftok()源代码是:

#include <sys/ipc.h>
#include <sys/stat.h>
key_t
ftok (const char *pathname, int proj_id)
{
  struct stat64 st;
  key_t key;
  if (__xstat64 (_STAT_VER, pathname, &st) < 0)
    return (key_t) -1;
  key = ((st.st_ino & 0xffff) | ((st.st_dev & 0xff) << 16)
         | ((proj_id & 0xff) << 24));
  return key;
}

Other functions are available here too. 其他功能也可在此处获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM