What is the formula used to produce a key that ftok() produces? ftok is a Linux function for creating keys for SYSTEM V IPC.
What is the formula used to produce a ftok() key?
942 Views Asked by test At
2
There are 2 best solutions below
2

The ftok() source code in glibc library is :
#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.
In ftok from glibc 2.29:
I.e. it's creating a 32-bit
key_t
by taking the upper 8 bits from the lower 8 bits ofproj_id
, the second upper 8 bits from the lower 8 bits of the device number of the providedpathname
, and the lower 16 bits from the lower 16 bits of the inode number of the providedpathname
.musl libc uses the same algorithm: